gpt4 book ai didi

sequelize.js - 将模型附加到 Sequelize 对象

转载 作者:行者123 更新时间:2023-12-03 22:32:59 25 4
gpt4 key购买 nike

自从我使用 sequelize 已经有一段时间了,我最近开始使用 sequelize-cli 工具进行新设置,该工具似乎具有非常模板化的模型生成方法,我正在尝试确定如何访问我的模型以进行查询。在下面的尝试中,我起初以为我错误地访问了模型对象,但是当我控制台记录 sequelize 对象时,我注意到 Model 属性带有一个空对象。从技术上讲,这是模型引用的来源吗?除了从 cli 初始化并以相同的方式生成模型之外,还有什么我应该调整的吗?
在我的 API 调用中记录 sequelize 对象:

import db from '../../models/index';
console.log(db)正如你在下面看到的 models: {} 是空的,据我所知,应该用 users 中定义的 users.js 模型填充
{
sequelize: Sequelize {
options: {
...
},
config: {
...
},
dialect: PostgresDialect {
...
},
queryInterface: PostgresQueryInterface {
sequelize: [Circular],
queryGenerator: [PostgresQueryGenerator]
},
models: {},
modelManager: ModelManager { models: [], sequelize: [Circular] },
connectionManager: ConnectionManager {
...
}
}, ...
这是由 cli 生成的 index.js(很想解释一下它是如何自动识别模型的):
/models/index.js
'use strict';

const fs = require('fs');
const path = require('path');
const { Sequelize } = require('sequelize');
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../config/sequelize.js')[env];
const db = {};

let sequelize;
if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
sequelize = new Sequelize(config.database, config.username, config.password, config);
}

fs
.readdirSync(__dirname)
.filter(file => {
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
})
.forEach(file => {
const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes);
console.log(db[model.name])
db[model.name] = model;
});

Object.keys(db).forEach(modelName => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;
/models/users.js :
'use strict';
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class users extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
// define association here
}
};
users.init({
id: {
type: DataTypes.UUID,
defaultValue: Sequelize.UUIDV4
},
first_name: DataTypes.STRING,
last_name: DataTypes.STRING,
password: DataTypes.STRING,
email: DataTypes.STRING,
active: DataTypes.BOOLEAN,
deleted: DataTypes.BOOLEAN
}, {
sequelize,
modelName: 'users',
createdAt: 'created_at',
updatedAt: 'updated_at',
deletedAt: 'deleted_at',
paranoid: true,
underscored: true,
define: {
freezeTableName: true,
tableName: 'users'
}
});
return users;
};

最佳答案

sequelize-cliinitmodel 生成器似乎并不总是像预期的那样适应应用程序环境。创建的样板文件存在三个问题。

  • fs.readdirSync(__dirname) 正在同步我计算机上的父目录,而不是我的应用程序目录
  • const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes); 遇到了同样的问题,应该是加载模型目录
  • 中的 js 文件
  • const Sequelize = require('sequelize'); 从模型生成器中丢失, users.jsconst model = require(...)(sequelize, Sequelize.DataTypes); 中引用。没有它,它会抛出一个错误,说明 Sequelize 缺少

  • 这是正确的代码:
    我使用了 process.cwd() 并附加了 '/models/' 来纠正文件夹位置问题。
    index.js(代码的更新部分):
    fs
    .readdirSync(process.cwd() + '/models/') // Updated
    .filter(file => {
    console.log(file)
    return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
    })
    .forEach(file => {
    console.log('file + Sequelize')
    console.log(Sequelize);
    const model = require('.' + path.join(__dirname, file))(sequelize, Sequelize.DataTypes); // Updated
    console.log(db[model.name])
    db[model.name] = model;
    });
    users.js(代码的更新部分):
    'use strict';
    const Sequelize = require('sequelize');

    关于sequelize.js - 将模型附加到 Sequelize 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65061718/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com