在单独的文件中使用 Sequelize 创建模型并在您的项目中使用它们 [英] Creating Models using Sequelize in seperate files and use them in your project

查看:63
本文介绍了在单独的文件中使用 Sequelize 创建模型并在您的项目中使用它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始在我的应用程序中使用 Node.js ORM Sequelize.到目前为止,我已经在同一个文件中定义了数据库模型,并在我的控制器文件中使用它们来执行基本操作.这是我定义模型的方式:

I've just started using Node.js ORM Sequelize in my application. So far I've defined database models in the same file and used them in my controller files to do basic operations. Here is how I defined Models:

var sqlize = require("sequelize");
var sq = new sqlize('test', 'root', 'root', {
host: 'localhost',
dialect: 'mysql',

pool: {
    max: 5,
    min: 0,
    idle: 10000
}
});







function services(){

var ser = sq.define('services',{

    idservices: {
        type: sqlize.INTEGER,
        autoIncrement: true,
        primaryKey: true

    },
    title:      sqlize.STRING,
    des:        sqlize.TEXT,
    vendor:     sqlize.STRING,
    rating:     sqlize.STRING,
    pricing_hr: sqlize.STRING,
    pricing_mn: sqlize.STRING,
    size:       sqlize.STRING,
    cpu:        sqlize.STRING,
    ram:        sqlize.STRING,
    os:         sqlize.STRING,
    img_path:   sqlize.STRING
});

sq.sync();



return ser;

}



function category(){

var category = sq.define('category',{
    id: {
        type: sqlize.INTEGER,
        autoIncrement: true,
        primaryKey: true

    },
    category: sqlize.STRING,
    sid: sqlize.INTEGER

},{

    freezeTableName: true,
    timestamps: false
});

sq.sync();



return category;

}


function cat(){

var cat = sq.define('cat',{
    idcat: {
        type: sqlize.INTEGER,
        autoIncrement: true,
        primaryKey: true

    },
    cat: sqlize.STRING

},{

    freezeTableName: true,
    timestamps: false
});

sq.sync();



return cat;

}



exports.services=services;
exports.category=category;
exports.cat=cat;

但是还有另一种方法,您可以在单独的文件中定义模型并创建一个索引文件,您可以在其中加载模型.我发现 Sequelize 的官方文档不足以让我了解其用法.我知道如何创建模型和 index.js 文件,但是很难找到如何在我的代码中使用这些模型.任何人都可以通过一个非常简单的示例(没有关系)帮助我,其中包括模型在单独文件中的定义及其在其他代码文件中的用法.

But there is another way where you define models in seperate files and create an index file where you load your models. I find the official documentaion of Sequelize not enough for me to undersatnd the usage. I know how to create models and index.js file but how to use those models in my code is difficult to find. Can anyone please help me with a very simple example (without relations), that includes the defination of models in seperate files and their usage in other code files.

推荐答案

EDIT

我已经解决了我的问题.现在我的模型在单独的文件中,我在我的代码中使用它们.下面是我如何定义我的模型文件 services.js:

module.exports = function(sequelize, DataTypes) {

return sequelize.define('services',{

    idservices: {
        type: DataTypes.INTEGER,
        autoIncrement: true,
        primaryKey: true

    },
    title:      DataTypes.STRING,
    des:        DataTypes.TEXT,
    vendor:     DataTypes.STRING,
    rating:     DataTypes.STRING,
    pricing_hr: DataTypes.STRING,
    pricing_mn: DataTypes.STRING,
    size:       DataTypes.STRING,
    cpu:        DataTypes.STRING,
    ram:        DataTypes.STRING,
    os:         DataTypes.STRING,
    img_path:   DataTypes.STRING
});


};

然后通过在 index.js 中使用 sequelize.import 我已经导入了我的所有模型:

Then by using sequelize.import in the index.js I've imported all my models:

var Sequelize = require('sequelize');


// initialize database connection
var sequelize = new Sequelize('test', 'root', 'root', {
host: 'localhost',
dialect: 'mysql',

pool: {
    max: 5,
    min: 0,
    idle: 10000
}
});

// load models
var models = [
'services',
'serviceCategory',
'category'
];
models.forEach(function(model) {
module.exports[model] = sequelize.import(__dirname + '/' + model);
});



// export connection
module.exports.sequelize = sequelize;

现在我可以通过像这样要求 index.js 在我的项目中的任何文件中使用这些模型:

Now I can use these models in any file in my project by requiring index.js like this:

//*********************************************
//Sequlize models to handle database commands;
var models  =  require('../models/index.js');
var s       =  models.services;             //Services table handler
var sc      =  models.serviceCategory;      //Service Category table handler
var ca      =  models.category;             //Category table handler

现在如果你想使用服务模型选项,你可以使用服务模型对象:

Now if you want to use services model options, you can use the services model object:

s.create({title: "anything"}).then(function(task){

    task.save;
});

这篇关于在单独的文件中使用 Sequelize 创建模型并在您的项目中使用它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆