Node Sequelize 迁移/模型是否可以共享相同的代码?

编程入门 行业动态 更新时间:2024-10-28 22:24:02
本文介绍了Node Sequelize 迁移/模型是否可以共享相同的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是 Sequelize 的新手,所以请耐心等待.

I'm new at Sequelize so be patient.

我使用 Sequelize 启动了一个新项目和迁移,所以我有这样的:

I started up a new project using Sequelize and migrations so I've got like this:

"use strict"; module.exports = { up: function(migration, DataTypes, done) { migration.createTable("MyUsers", { id: { allowNull: false, autoIncrement: true, primaryKey: true, type: DataTypes.INTEGER }, first_name: { type: DataTypes.STRING }, last_name: { type: DataTypes.STRING }, bio: { type: DataTypes.TEXT }, createdAt: { allowNull: false, type: DataTypes.DATE }, updatedAt: { allowNull: false, type: DataTypes.DATE } }).done(done); }, down: function(migration, DataTypes, done) { migration.dropTable("MyUsers").done(done); } };

模型/myuser.js:

"use strict"; module.exports = function(sequelize, DataTypes) { var MyUser = sequelize.define("MyUser", { first_name: DataTypes.STRING, last_name: DataTypes.STRING, bio: DataTypes.TEXT }, { classMethods: { associate: function(models) { // associations can be defined here } } }); return MyUser; };

如你所见表定义是关于迁移和模型文件.

as you can see the table definition is both on the migration and the model file.

我想知道有没有分享的方法代码?

I'm wondering if there is a way to share the code ?

我的意思是我不喜欢在两个文件中有逻辑如果字段更改,我必须更新两次.

I mean I don't like to have logic in two files if a field change I've to update twice.

按照下面的Yan Foto示例不同的方式可能更干净.

following the Yan Foto example below a different way may be cleaner.

'use strict'; module.exports = { name: 'users', definition : function(DataTypes) { return { id: { type: DataTypes.INTEGER, autoIncrement: true, primaryKey: true }, firstname: { type:DataTypes.STRING }, lastname: { type:DataTypes.STRING }, email: { type: DataTypes.STRING, unique: true }, username: { type:DataTypes.STRING, unique: true } }; } };

模型/用户

'use strict'; var Schema = require('../schemas/users'); module.exports = function(sequelize, DataTypes) { return sequelize.define( Schema.name, Schema.definition(DataTypes), { freezeTableName: true , instanceMethods: { countTasks: function() { // how to implement this method ? } } } ); };

迁移/20150720184716-users.js

'use strict'; var Schema = require('../schemas/users'); module.exports = { up: function (queryInterface, Sequelize) { return queryInterface.createTable( Schema.name, Schema.definition(Sequelize) ); }, down: function (queryInterface, Sequelize) { return queryInterface.dropTable(Schema.name); } };

推荐答案

当我开始使用 sequelize 时,我想知道同样的事情,这里是我的解决方案.我将我的模型定义如下:

I wondered the same thing as I started using sequelize and here is my solution. I define my models as bellow:

module.exports = { def: function(DataTypes) { return { id: { type: DataTypes.INTEGER, autoIncrement: true, primaryKey: true }, username: DataTypes.STRING, password: DataTypes.STRING, createdAt: DataTypes.DATE, updatedAt: DataTypes.DATE, } }, config: {} };

其中 def 定义属性,config 是 options 对象/en/latest/api/sequelize/#definemodelname-attributes-options-model" rel="nofollow">define 或 迁移方法.我使用以下代码导入它们:

Where def defines the attributes and config is the optional options object accepted by define or migration methods. And I import them using the following code:

fs.readdirSync(__dirname + '/PATH/TO/models') .filter(function(file) { return (file.indexOf('.') !== 0) && (file !== basename); }) .forEach(function(file) { var name = file.substring(0, file.lastIndexOf(".")), definition = require(path.join(__dirname + '/models', file)); sequelize['import'](name, function(sequelize, DataTypes) { return sequelize.define( name, definition.def(DataTypes), definition.config ); }); });

对于迁移,我有类似的方法:

For the migrations I have a similar approach:

const path = require('path'); module.exports = { up: function (queryInterface, Sequelize) { return queryInterface.createTable( 'users', require(path.join(__dirname + '/PATH/TO/models', 'user.js')).def(Sequelize) ); }, down: function (queryInterface, Sequelize) { return queryInterface.dropTable('users'); } };

更多推荐

Node Sequelize 迁移/模型是否可以共享相同的代码?

本文发布于:2023-10-20 03:12:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1509670.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模型   代码   Node   Sequelize

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!