sequize .import()"从另一个文件导入模型
How can "sequelize.import()" import models from another file?
当我以以下方式创建新模型时:
//user.js file
module.exports = function (sequelize, DateTypes) {
return sequelize.define("user", {
email: {
type: DateTypes.STRING,
allowNull: false,
unique: true,
validate: {
isEmail: true
}
},
password: {
type: DateTypes.STRING,
allowNull: false,
validate: {
len: [7, 100]
}
}
});
};
和db.js文件,我建立了一个新的数据库:
var Sequelize = require('sequelize');
var env = process.env.NODE_ENV || "development"; // established if you work in production or in development mode
var sequelize;
if (env == "production") {
sequelize = new Sequelize(process.env.DATABASE_URL, {
"dialect": "postgres",
});
} else {
var sequelize = new Sequelize(undefined, undefined, undefined, {
'dialect': 'sqlite',
'storage': __dirname + '/data/dev-todo-api.sqlite' // location where you create a new sqlite database
});
}
var db = {};
db.todo = sequelize.import(__dirname + "/models/todo.js");
db.user = sequelize.import(__dirname + "/models/user.js");
db.sequelize = sequelize; //contain a settings of database
db.Sequelize = Sequelize;
module.exports = db;
我不明白user.js如何知道sequelize
(我插入作为参数到module.exports
)是一个序列化包的实例,如果它位于另一个文件?也许是因为sequelize.import('/user.js')
会导入整个续集包?
参见sequelize.import
的定义:
Sequelize.prototype.import = function(path) {
// is it a relative path?
if(Path.normalize(path) !== Path.resolve(path)){
// make path relative to the caller
var callerFilename = Utils.stack()[1].getFileName()
, callerPath = Path.dirname(callerFilename);
path = Path.resolve(callerPath, path);
}
if (!this.importCache[path]) {
var defineCall = (arguments.length > 1 ? arguments[1] : require(path));
if (typeof defineCall === 'object' && defineCall.__esModule) {
// Babel/ES6 module compatability
defineCall = defineCall['default'];
}
this.importCache[path] = defineCall(this, DataTypes);
}
return this.importCache[path];
};
实际上,它在路径上调用require
,然后调用结果,将sequelize实例作为其第一个参数。这使得模块可以有对导入它的序列化实例的引用。
可能有帮助。这是我编译后的代码:
/**
* Imports a model defined in another file
*
* Imported models are cached, so multiple calls to import with the same path will not load the file multiple times
*
* See https://github.com/sequelize/express-example for a short example of how to define your models in separate files so that they can be imported by sequelize.import
* @param {String} path The path to the file that holds the model you want to import. If the part is relative, it will be resolved relatively to the calling file
* @return {Model}
*/
import(path) {
// is it a relative path?
if (Path.normalize(path) !== Path.resolve(path)) {
// make path relative to the caller
const callerFilename = Utils.stack()[1].getFileName();
const callerPath = Path.dirname(callerFilename);
path = Path.resolve(callerPath, path);
}
if (!this.importCache[path]) {
let defineCall = arguments.length > 1 ? arguments[1] : require(path);
if (typeof defineCall === 'object') {
// ES6 module compatibility
defineCall = defineCall.default;
}
this.importCache[path] = defineCall(this, DataTypes);
}
return this.importCache[path];
}
相关文章:
- 如何在android中使用phonegap将文件从一个文件夹移动/复制到另一个文件夹
- 正在传递JSONP标头's数据参数到另一个文件中的AJAX请求
- 要求定义另一个文件中的对象
- 从AngularJS中的另一个文件中的控制器访问服务
- 文件的节点自动化移动到另一个文件夹中
- 用于文件移动到另一个文件夹的Javascript自动化
- jQuery Mobile样式从另一个文件加载内容
- 为什么当我上传文件并点击更多上传另一个文件的第一个删除
- 将另一个文件中的对象添加到单独文件中的阵列中
- 从另一个文件访问函数的返回值
- 在jquery的另一个文件上浏览jquery插件
- 从HTML中的另一个文件访问javascript方法
- 如何将从另一个文件接收的数组分配给另一个数组
- 在另一个文件夹中显示React组件
- 如何在来自另一个文件的控制器中定义变量
- 从导出的模块返回变量,并在另一个文件(NodeJS)中使用它
- 如何多次将PHP变量从一个文件传递到另一个文件
- Gulp.js - 无法将生成的文件从一个文件夹复制到另一个文件夹
- 我可以使用gulp将一个文件中的内容插入另一个文件吗
- 用javascript将一个文件的变量传递给另一个文件