NODE.JS ORM之sequelize 框架

1、安装:

npm install sequelize   //ORM 主框架

npm install -g sequelize-auto // 已有数据库表生成modle工具



2、已有的数据库生成modle

npm install -g tedious   //安装MSSQL数据库驱动。

sequelize-auto -o "./models" -d zmcs -h localhost -u sa_ -p 1433 -x 422426362227001 -e mssql -c zmcy.json

zmcy.json 文件内容
{
    "dialect": "mssql",
    "dialectOptions": {
        "tdsVersion": "7_1" //默认是7_4,对应的是sql2012以上版本, 7_1对应的是sql2000 ,需要定义驱动程序版本。
    }
}
 
Options:
  -h, --host        IP/Hostname for the database.   [required]
  -d, --database    Database name.                  [required]
  -u, --user        Username for database.
  -x, --pass        Password for database.
  -p, --port        Port number for database.
  -c, --config      JSON file for Sequelize's constructor "options" flag object as defined here: https://sequelize.readthedocs.org/en/latest/api/sequelize/
  -o, --output      What directory to place the models.
  -e, --dialect     The dialect/engine that you're using: postgres, mysql, sqlite
  -a, --additional  Path to a json file containing model definitions (for all tables) which are to be defined within a model's configuration parameter. For more info: https://sequelize.readthedocs.org/en/latest/docs/models-definition/#configuration
  -t, --tables      Comma-separated names of tables to import
  -C, --camel       Use camel case to name models and fields
  -n, --no-write    Prevent writing the models to disk.
3、使用生成的modle
var fsl = sequelize.import(__dirname + "/models/frontlog")
sequelize.query('SELECT top 3 int_flowid,vch_logtype,vch_operID,dt_operdate from front_sys_log where vch_operID = ? and int_flowid > ? ', {
    replacements: ['1001',1],
    type: sequelize.QueryTypes.SELECT
}).then((data) => {
    console.log(data);
});
fsl.create({
    id:3,
    vch_logtype: '系统管理日志',
    vch_operID: '1002',
    dt_operdate: '2017-05-02 17:42:42.950',
    vch_computer: 'ZM-HEHM',
    vch_function: '登录',
    vch_functype: '系统管理',
    vch_description: '登录后台管理系统!'
}, {
    returning: false  
}).then(function (task) {
    console.log(task);
})



你可能感兴趣的:(NODE.JS)