最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

使用节点+ Knex + Postgres的模型和模型处理方法

IT培训 admin 3浏览 0评论

使用节点+ Knex + Postgres的模型和模型处理方法

我希望能得到一些帮助。我刚开始使用的是Postgres我节点应用和好奇地找出如何去处理模型和模型方法。在关于模式和方法与节点和Postgres工作时,什么是最好的做法是什么?我环顾四周,所有我能找到一些所谓的反对,但绝对有必要我走这条路?

理想情况下,我想对每个组件model.js文件,但我还没有看到在Postgres +节点打交道时,他们使用。

任何帮助是极大的赞赏。谢谢你们,希望你们有一个伟大的感恩节!

回答如下:

这是我model.js文件

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
    }
  }

这是我controller.js文件

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 }

而最后一个的connection.js文件

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;

使用节点+ Knex + Postgres的模型和模型处理方法

我希望能得到一些帮助。我刚开始使用的是Postgres我节点应用和好奇地找出如何去处理模型和模型方法。在关于模式和方法与节点和Postgres工作时,什么是最好的做法是什么?我环顾四周,所有我能找到一些所谓的反对,但绝对有必要我走这条路?

理想情况下,我想对每个组件model.js文件,但我还没有看到在Postgres +节点打交道时,他们使用。

任何帮助是极大的赞赏。谢谢你们,希望你们有一个伟大的感恩节!

回答如下:

这是我model.js文件

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
    }
  }

这是我controller.js文件

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 }

而最后一个的connection.js文件

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;
发布评论

评论列表 (0)

  1. 暂无评论