使用Sequelizer处理不同文件中的模型和关系的最佳方法是什么?

时间:2019-03-27 23:31:28

标签: node.js sequelize.js

我是Sequelizer的新手,我已经阅读了文档。我在PostgreSQL中有3个表:

1)用户。 2)UserHasPermissions。 3)许可。

在这些表中,我在User-UserHasPermissions和UserHasPermissions-Permission之间具有关系

我写了连接文件(db.js)

display_index

我写了模型...

User.js

display_index

Permission.js

const Sequelize = require('sequelize')
const sequelize = new Sequelize('example', 'postgres', 'none', {
    host: 'localhost',
    dialect: 'postgres',
    operatorsAliases: false,
    pool: {
        max: 5,
        min: 0,
        acquire: 30000,
        idle: 10000
    }
})

const db = {}
db.sequelize = sequelize
db.Sequelize = Sequelize

module.exports = db

还有UserHasPermissions.js

const Sequelize = require('sequelize')
const db = require('./database/db.js')
var Permission = require('./Permission')
var UserHasPermissions = require('./UserHasPermissions')

const User = db.sequelize.define('users', {
    id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    email: {
    type: Sequelize.STRING,
        unique: true,
        validate: {
            isEmail: true,
            notEmpty: true
        }
  },
    password: {
        type: Sequelize.STRING,
        validate: {
            notEmpty: true
        }
    },
    fullname: {
        type: Sequelize.STRING({length: 100}),
        validate: {
            notEmpty: true
        }
    },
    phone: {
        type: Sequelize.STRING({length: 10}),
        validate: {
            isNumeric: true,
            notEmpty: true,
            len: [9,10]
        }
    },
    birthdate: {
        type: Sequelize.DATEONLY
    },
    address: {
        type: Sequelize.STRING({length: 200})
    },
    status: {
        type: Sequelize.STRING({length: 5})
    }
},
{
    schema: 'configuration',
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    deletedAt: 'deleted_at',
    paranoid: true,
    underscored: true
})

UserHasPermissions.belongsTo(User)
User.hasMany(UserHasPermissions)

db.sequelize.sync()
  .then(() => {
    console.log('Tables created.')
   })

好的。数据库中三个表的三个模型。 现在的关系...

如您所见,我在User.js和Permission.js中编写了关系,并在User.js中同步了所有模型。用关系创建表。

它可以在数据库中工作,但是我想测试所有模型。

Sequelize的文档说,如果我使用belongsTo()或hasMany(),sequelize会自动创建访问器(getter和setter),但是在这种情况下,当我想在控制器文件中使用getter方法时,我得到“方法不是函数“

const Sequelize = require('sequelize')
const db = require('./database/db.js')

const Permission = db.sequelize.define('permission', {
    id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
  },
  name: {
        type: Sequelize.STRING({length: 100}),
        validate: {
            notEmpty: true
        }
  },
  description: {
        type: Sequelize.STRING({length: 100}),
        validate: {
            notEmpty: true
        }
  }
},
{
    schema: 'configuration',
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    deletedAt: 'deleted_at',
    paranoid: true,
    underscored: true
})

UserHasPermissions.belongsTo(Permission)
Permission.hasMany(UserHasPermissions)

module.exports = Permission

我无法访问user.getUserHasPermissions(),但是当我使用“ belongsTo”或“ hasMany”时,续集必须创建该方法

我该怎么办?我被困在这里。请帮帮我!

0 个答案:

没有答案