UNPKG

@tugou/sequelize

Version:

midwayjs sequelize

141 lines (140 loc) 5.08 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseModel = void 0; const decorator_1 = require("@midwayjs/decorator"); const __1 = require(".."); class BaseModel { constructor(table_name) { this.table_name = table_name; } async init() { const meta = decorator_1.getClassMetadata(__1.MODEL_KEY, this); console.info('[sequelize] [init] ', JSON.stringify(meta.define)); this.entity = this.sequelize_conn.define(this.table_name, meta.define, { tableName: this.table_name }); } async findOne(options) { try { const ret = await this.entity.findOne(Object.assign({ raw: true }, options)); console.info('[sequelize] [findOne] ', JSON.stringify(ret)); return ret; } catch (e) { console.error('[sequelize] [findOne] ', e); return false; } } async create(values, options = {}) { try { let ret = await this.entity.create(values, options); ret = ret.toJSON(); console.info('[sequelize] [create] ', JSON.stringify(ret)); // 返回创建好的 Json return ret; } catch (e) { console.error('[sequelize] [create] ', e); return false; } } async bulkCreate(values, options = {}) { try { let ret = await this.entity.bulkCreate(values, options); console.info('[sequelize] [bulkCreate] ', JSON.stringify(ret)); // 返回创建好的 Json return ret; } catch (e) { console.error('[sequelize] [bulkCreate] ', e); return false; } } async update(values, options = {}) { try { const ret = await this.entity.update(values, options); console.info('[sequelize] [update] ', ret); // 返回数组, 第一个元素是影响行数, 可能是0 return ret[0]; } catch (e) { console.error('[sequelize] [update] ', e); return false; } } async destroy(options) { try { const ret = await this.entity.destroy(options); console.info('[sequelize] [destroy] ', ret); return ret; } catch (e) { console.error('[sequelize] [destroy] ', e); return false; } } async count(options) { try { const ret = await this.entity.count(options); console.info('[sequelize] [count] ', ret); return ret; } catch (e) { console.error('[sequelize] [count] ', e); return false; } } async findAll(options) { try { const ret = await this.entity.findAll(Object.assign({ raw: true }, options)); console.info('[sequelize] [findAll] ', JSON.stringify(ret)); // 如果没有值,默认返回空数组 [], ,改为返回: null return ret.length === 0 ? null : ret; } catch (e) { console.error('[sequelize] [findAll] ', e); return false; } } async findAndCountAll(options) { const { current = 1, limit = 10 } = options; try { const detail = await this.entity.findAndCountAll(Object.assign({ offset: (current - 1) * limit, limit }, options)); console.info('[sequelize] [findAndCountAll] ', JSON.stringify(detail)); return { current, limit, detail: detail.rows, total: detail.count, last: Math.ceil(detail.count / limit) }; } catch (e) { console.error('[sequelize] [findAndCountAll] ', e); return false; } } } __decorate([ decorator_1.Inject('sequelize:sequelize_conn'), __metadata("design:type", Object) ], BaseModel.prototype, "sequelize_conn", void 0); __decorate([ decorator_1.Init(), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], BaseModel.prototype, "init", null); exports.BaseModel = BaseModel;