Dealing with models and model methods using Node + Knex + Postgres












0















I was hoping to get some help. I just started using Postgres with my Node applications and am curious to find out how to go about dealing with models and model methods. What is the best practice when working with Node and Postgres in regards to models and methods? I was looking around and all I could find is something called Objection, but is it absolutely necessary I take that route?



Ideally I would like to have a model.js file for each component but I have not seen them used when dealing with Postgres + Node.



Any help is greatly appreciated. Thanks guys, hope you all had a great Thanksgiving!










share|improve this question



























    0















    I was hoping to get some help. I just started using Postgres with my Node applications and am curious to find out how to go about dealing with models and model methods. What is the best practice when working with Node and Postgres in regards to models and methods? I was looking around and all I could find is something called Objection, but is it absolutely necessary I take that route?



    Ideally I would like to have a model.js file for each component but I have not seen them used when dealing with Postgres + Node.



    Any help is greatly appreciated. Thanks guys, hope you all had a great Thanksgiving!










    share|improve this question

























      0












      0








      0








      I was hoping to get some help. I just started using Postgres with my Node applications and am curious to find out how to go about dealing with models and model methods. What is the best practice when working with Node and Postgres in regards to models and methods? I was looking around and all I could find is something called Objection, but is it absolutely necessary I take that route?



      Ideally I would like to have a model.js file for each component but I have not seen them used when dealing with Postgres + Node.



      Any help is greatly appreciated. Thanks guys, hope you all had a great Thanksgiving!










      share|improve this question














      I was hoping to get some help. I just started using Postgres with my Node applications and am curious to find out how to go about dealing with models and model methods. What is the best practice when working with Node and Postgres in regards to models and methods? I was looking around and all I could find is something called Objection, but is it absolutely necessary I take that route?



      Ideally I would like to have a model.js file for each component but I have not seen them used when dealing with Postgres + Node.



      Any help is greatly appreciated. Thanks guys, hope you all had a great Thanksgiving!







      node.js postgresql






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 24 '18 at 18:30









      ColorCutClarityColorCutClarity

      113




      113
























          1 Answer
          1






          active

          oldest

          votes


















          0














          This is my model.js file



          module.exports = ({
          knex = require('./connection'),
          name = '',
          tableName = '',
          selectableProps = ,
          timeout = 1000
          }) => {
          const query = knex.from(tableName)

          const create = props => {
          delete props.id
          return knex.insert(props)
          .returning(selectableProps)
          .into(tableName)
          .timeout(timeout)
          }
          const findAll = () => {
          return knex.select(selectableProps)
          .from(tableName)
          .timeout(timeout)
          }
          const find = filters => {
          return knex.select(selectableProps)
          .from(tableName)
          .where(filters)
          .timeout(timeout)
          }

          const update = (id, props) => {
          delete props.id

          return knex.update(props)
          .from(tableName)
          .where({
          id
          })
          .returning(selectableProps)
          .timeout(timeout)
          }

          const destroy = id => {
          return knex.del()
          .from(tableName)
          .where({
          id
          })
          .timeout(timeout)
          }

          return {
          query,
          name,
          tableName,
          selectableProps,
          timeout,
          create,
          findAll,
          find,
          update,
          destroy
          }
          }


          This is my controller.js file



          const model = require('./model');

          const user = model({
          name: "users",
          tableName: "tbl_users",
          });

          const getAllUsers = async (req, res, next)=>{
          let result = await user.findAll();
          res.send(result);
          }

          module.exports = { getAllUsers }


          And Lastly a the connection.js file



          const knex = require('knex')({
          client: 'pg',
          connection: {
          host: 'YOUR_HOST_ADDR',
          user: 'YOUR_USERNAME',
          password: 'YOUR_PASS',
          database: 'YOUR_DB_NAME'
          },
          pool: {
          min: 0,
          max: 7
          }
          });

          module.exports = knex;





          share|improve this answer

























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53461188%2fdealing-with-models-and-model-methods-using-node-knex-postgres%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            This is my model.js file



            module.exports = ({
            knex = require('./connection'),
            name = '',
            tableName = '',
            selectableProps = ,
            timeout = 1000
            }) => {
            const query = knex.from(tableName)

            const create = props => {
            delete props.id
            return knex.insert(props)
            .returning(selectableProps)
            .into(tableName)
            .timeout(timeout)
            }
            const findAll = () => {
            return knex.select(selectableProps)
            .from(tableName)
            .timeout(timeout)
            }
            const find = filters => {
            return knex.select(selectableProps)
            .from(tableName)
            .where(filters)
            .timeout(timeout)
            }

            const update = (id, props) => {
            delete props.id

            return knex.update(props)
            .from(tableName)
            .where({
            id
            })
            .returning(selectableProps)
            .timeout(timeout)
            }

            const destroy = id => {
            return knex.del()
            .from(tableName)
            .where({
            id
            })
            .timeout(timeout)
            }

            return {
            query,
            name,
            tableName,
            selectableProps,
            timeout,
            create,
            findAll,
            find,
            update,
            destroy
            }
            }


            This is my controller.js file



            const model = require('./model');

            const user = model({
            name: "users",
            tableName: "tbl_users",
            });

            const getAllUsers = async (req, res, next)=>{
            let result = await user.findAll();
            res.send(result);
            }

            module.exports = { getAllUsers }


            And Lastly a the connection.js file



            const knex = require('knex')({
            client: 'pg',
            connection: {
            host: 'YOUR_HOST_ADDR',
            user: 'YOUR_USERNAME',
            password: 'YOUR_PASS',
            database: 'YOUR_DB_NAME'
            },
            pool: {
            min: 0,
            max: 7
            }
            });

            module.exports = knex;





            share|improve this answer






























              0














              This is my model.js file



              module.exports = ({
              knex = require('./connection'),
              name = '',
              tableName = '',
              selectableProps = ,
              timeout = 1000
              }) => {
              const query = knex.from(tableName)

              const create = props => {
              delete props.id
              return knex.insert(props)
              .returning(selectableProps)
              .into(tableName)
              .timeout(timeout)
              }
              const findAll = () => {
              return knex.select(selectableProps)
              .from(tableName)
              .timeout(timeout)
              }
              const find = filters => {
              return knex.select(selectableProps)
              .from(tableName)
              .where(filters)
              .timeout(timeout)
              }

              const update = (id, props) => {
              delete props.id

              return knex.update(props)
              .from(tableName)
              .where({
              id
              })
              .returning(selectableProps)
              .timeout(timeout)
              }

              const destroy = id => {
              return knex.del()
              .from(tableName)
              .where({
              id
              })
              .timeout(timeout)
              }

              return {
              query,
              name,
              tableName,
              selectableProps,
              timeout,
              create,
              findAll,
              find,
              update,
              destroy
              }
              }


              This is my controller.js file



              const model = require('./model');

              const user = model({
              name: "users",
              tableName: "tbl_users",
              });

              const getAllUsers = async (req, res, next)=>{
              let result = await user.findAll();
              res.send(result);
              }

              module.exports = { getAllUsers }


              And Lastly a the connection.js file



              const knex = require('knex')({
              client: 'pg',
              connection: {
              host: 'YOUR_HOST_ADDR',
              user: 'YOUR_USERNAME',
              password: 'YOUR_PASS',
              database: 'YOUR_DB_NAME'
              },
              pool: {
              min: 0,
              max: 7
              }
              });

              module.exports = knex;





              share|improve this answer




























                0












                0








                0







                This is my model.js file



                module.exports = ({
                knex = require('./connection'),
                name = '',
                tableName = '',
                selectableProps = ,
                timeout = 1000
                }) => {
                const query = knex.from(tableName)

                const create = props => {
                delete props.id
                return knex.insert(props)
                .returning(selectableProps)
                .into(tableName)
                .timeout(timeout)
                }
                const findAll = () => {
                return knex.select(selectableProps)
                .from(tableName)
                .timeout(timeout)
                }
                const find = filters => {
                return knex.select(selectableProps)
                .from(tableName)
                .where(filters)
                .timeout(timeout)
                }

                const update = (id, props) => {
                delete props.id

                return knex.update(props)
                .from(tableName)
                .where({
                id
                })
                .returning(selectableProps)
                .timeout(timeout)
                }

                const destroy = id => {
                return knex.del()
                .from(tableName)
                .where({
                id
                })
                .timeout(timeout)
                }

                return {
                query,
                name,
                tableName,
                selectableProps,
                timeout,
                create,
                findAll,
                find,
                update,
                destroy
                }
                }


                This is my controller.js file



                const model = require('./model');

                const user = model({
                name: "users",
                tableName: "tbl_users",
                });

                const getAllUsers = async (req, res, next)=>{
                let result = await user.findAll();
                res.send(result);
                }

                module.exports = { getAllUsers }


                And Lastly a the connection.js file



                const knex = require('knex')({
                client: 'pg',
                connection: {
                host: 'YOUR_HOST_ADDR',
                user: 'YOUR_USERNAME',
                password: 'YOUR_PASS',
                database: 'YOUR_DB_NAME'
                },
                pool: {
                min: 0,
                max: 7
                }
                });

                module.exports = knex;





                share|improve this answer















                This is my model.js file



                module.exports = ({
                knex = require('./connection'),
                name = '',
                tableName = '',
                selectableProps = ,
                timeout = 1000
                }) => {
                const query = knex.from(tableName)

                const create = props => {
                delete props.id
                return knex.insert(props)
                .returning(selectableProps)
                .into(tableName)
                .timeout(timeout)
                }
                const findAll = () => {
                return knex.select(selectableProps)
                .from(tableName)
                .timeout(timeout)
                }
                const find = filters => {
                return knex.select(selectableProps)
                .from(tableName)
                .where(filters)
                .timeout(timeout)
                }

                const update = (id, props) => {
                delete props.id

                return knex.update(props)
                .from(tableName)
                .where({
                id
                })
                .returning(selectableProps)
                .timeout(timeout)
                }

                const destroy = id => {
                return knex.del()
                .from(tableName)
                .where({
                id
                })
                .timeout(timeout)
                }

                return {
                query,
                name,
                tableName,
                selectableProps,
                timeout,
                create,
                findAll,
                find,
                update,
                destroy
                }
                }


                This is my controller.js file



                const model = require('./model');

                const user = model({
                name: "users",
                tableName: "tbl_users",
                });

                const getAllUsers = async (req, res, next)=>{
                let result = await user.findAll();
                res.send(result);
                }

                module.exports = { getAllUsers }


                And Lastly a the connection.js file



                const knex = require('knex')({
                client: 'pg',
                connection: {
                host: 'YOUR_HOST_ADDR',
                user: 'YOUR_USERNAME',
                password: 'YOUR_PASS',
                database: 'YOUR_DB_NAME'
                },
                pool: {
                min: 0,
                max: 7
                }
                });

                module.exports = knex;






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Feb 3 at 16:21

























                answered Feb 3 at 16:08









                Atish ShakyaAtish Shakya

                975




                975
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53461188%2fdealing-with-models-and-model-methods-using-node-knex-postgres%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    404 Error Contact Form 7 ajax form submitting

                    How to know if a Active Directory user can login interactively

                    TypeError: fit_transform() missing 1 required positional argument: 'X'