node.js—sequelize-auto モジュールはデータベース モデルを生成し、それを使用します

Sequelize-auto は、テーブル構造を迅速に生成するのに役立つツールです。

https://www.npmjs.com/package/sequelize-auto-import

node.js プロジェクトでは、sequelize-auto を使用してデータベースに対して対応するモデルを自動的に生成し、sequelize-auto を使用してデータベースに対して対応するモデルを自動的に生成して、データベースの追加、削除、変更、確認時の SQL ステートメントの記述を削減します。 。

npm install -g mysql

npm install -g mysql2

npm install -g sequelize-auto

npm install -g tedious

 次のコマンドを実行します

sequelize-auto -h "数据库地址" -d "数据库名" -u "用户名" -x "密码" -p "端口号"  --dialect mysql

モデルフォルダー内のデータベース内のテーブル構造を同期できます。

たとえば、1 つのテーブルの内容は次のとおりです。

 

 

 

module.exports = function(sequelize, DataTypes) {
  return sequelize.define('t_group_public', {
    id: {
      type: DataTypes.INTEGER(11),
      allowNull: false,
      primaryKey: true,
      autoIncrement: true
    },
    group: {
      type: DataTypes.CHAR(14),
      allowNull: false,
      defaultValue: ''
    },
    activation: {
      type: DataTypes.INTEGER(11),
      allowNull: false,
      defaultValue: '1'
    },
    app: {
      type: DataTypes.INTEGER(11),
      allowNull: false,
      defaultValue: '0'
    },
    type: {
      type: DataTypes.INTEGER(11),
      allowNull: false,
      defaultValue: '0'
    }
  }, {
    tableName: 't_group_public'
  });
};

 db レイヤーは、モデルの特定のテーブル ファイルを導入します。

db.prototype.model=function(name){
    if(!this.sequelize.isDefined(name)){
        this.sequelize.import(path.join(this.config.path,name));
    }
    return this.sequelize.model(name);
};

よく使用される追加、削除、変更、クエリ ステートメントは次のように使用できます。

db.prototype.add=async function(table,data,ignore,option){
    var model=this.model(table);
    var opt={fields:Object.keys(data)};
    if(ignore){
        opt.ignoreDuplicates=true;
    }
    var ret = await model.create(data,Object.assign(opt,option || {}));
    var data = ret.get({plain:true});
    if(model.autoIncrementAttribute && !model.rawAttributes[model.autoIncrementAttribute]._autoGenerated){
        return data[model.autoIncrementAttribute];
    }
    return data;
};

db.prototype.update=async function(table,data,where,limit,option){
    var model=this.model(table);
    var opt={};
    opt.where=where;
    opt.limit=limit;
    var [affectedRows]=await model.update(data,Object.assign(opt,option || {}));
    return affectedRows;
};

db.prototype.del=async function(table,where,limit,option){
    var model=this.model(table);
    var opt={};
    opt.where=where;
    opt.limit=limit;
    var affectedRows = await model.destroy(Object.assign(opt,option || {}))
    return affectedRows;
};


db.prototype.find=async function(table,field,where,order,option){
    var opt={where:where,raw:true};
    if(field!=='*'){
        opt.attributes=field.split(',');
    }
    if(order){
        opt.order=[order];
    }
    var ret= this.model(table).findOne(Object.assign(opt,option || {}));
    return ret;
};

db.prototype.select= async function(table,field,where,order,limit,option){
    var opt={where:where,raw:true};
    if(field!=='*'){
        opt.attributes=field.split(',');
    }
    if(order){
        opt.order=[order];
    }
    if(limit){
        opt.limit=limit[0];
        opt.offset=limit[1];
    }
    var ret= await this.model(table).findAll(Object.assign(opt,option || {}));
    return ret;
};

インターフェースの例:

async function getGroupPublic(req,res){
    var {group,app} = req.query;
    var where = {};
    if (group) {
        where.group = group;
    }
    if (app) {
        where.app = app;
    }
    var order = ['id', 'asc'];
    try {
        var list = await global.DB.select('t_group_public','*',where,order);
        return res.ok(list);
    } catch (error) {
        return res.err(error);
    }
}
userRouter.get('/getGroupPublic', CHK.getGroupPublic, getGroupPublic);

 

 

 

おすすめ

転載: blog.csdn.net/sm9sun/article/details/94616729
おすすめ