@tugou/sequelize
Advanced tools
+1
-1
| export { SequelizeConfiguration as Configuration } from './configuration'; | ||
| export { Model } from './model/decorator'; | ||
| export { BaseModel } from './model/base'; | ||
| export { DataTypes, Op } from 'sequelize'; | ||
| export { DataTypes, Op, literal } from 'sequelize'; | ||
| export declare const MODEL_KEY = "decorator:model"; |
+2
-1
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.MODEL_KEY = exports.Op = exports.DataTypes = exports.BaseModel = exports.Model = exports.Configuration = void 0; | ||
| exports.MODEL_KEY = exports.literal = exports.Op = exports.DataTypes = exports.BaseModel = exports.Model = exports.Configuration = void 0; | ||
| var configuration_1 = require("./configuration"); | ||
@@ -13,2 +13,3 @@ Object.defineProperty(exports, "Configuration", { enumerable: true, get: function () { return configuration_1.SequelizeConfiguration; } }); | ||
| Object.defineProperty(exports, "Op", { enumerable: true, get: function () { return sequelize_1.Op; } }); | ||
| Object.defineProperty(exports, "literal", { enumerable: true, get: function () { return sequelize_1.literal; } }); | ||
| exports.MODEL_KEY = 'decorator:model'; |
@@ -6,4 +6,9 @@ export declare class BaseModel { | ||
| constructor(table_name: any); | ||
| init(): void; | ||
| findOne(account: any): Promise<any>; | ||
| init(): Promise<void>; | ||
| findOne(options: object): Promise<any>; | ||
| create(values: object, options?: object): Promise<any>; | ||
| update(values: object, options?: object): Promise<any>; | ||
| destroy(options: object): Promise<any>; | ||
| findAll(options: object): any; | ||
| findAndCountAll(options: any): any; | ||
| } |
+76
-14
@@ -19,5 +19,5 @@ "use strict"; | ||
| } | ||
| init() { | ||
| async init() { | ||
| const meta = decorator_1.getClassMetadata(__1.MODEL_KEY, this); | ||
| console.debug('[sequelize] ', JSON.stringify(meta.define)); | ||
| console.info('[sequelize] [init] ', JSON.stringify(meta.define)); | ||
| this.entity = this.sequelize_conn.define(this.table_name, meta.define, { | ||
@@ -27,20 +27,82 @@ tableName: this.table_name | ||
| } | ||
| async findOne(account) { | ||
| const attributes = ['uid', 'auth_groups']; | ||
| const where = { | ||
| account, | ||
| status: 1 | ||
| }; | ||
| async findOne(options) { | ||
| try { | ||
| const ret = await this.entity.findOne({ | ||
| raw: true, | ||
| attributes, | ||
| where | ||
| }); | ||
| 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 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 findAll(options) { | ||
| try { | ||
| const ret = await this.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.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; | ||
| } | ||
| } | ||
| } | ||
@@ -55,4 +117,4 @@ __decorate([ | ||
| __metadata("design:paramtypes", []), | ||
| __metadata("design:returntype", void 0) | ||
| __metadata("design:returntype", Promise) | ||
| ], BaseModel.prototype, "init", null); | ||
| exports.BaseModel = BaseModel; |
+2
-2
| { | ||
| "name": "@tugou/sequelize", | ||
| "version": "1.0.12", | ||
| "version": "1.0.13", | ||
| "description": "midwayjs sequelize", | ||
@@ -29,3 +29,3 @@ "main": "dist/index", | ||
| "license": "MIT", | ||
| "gitHead": "8b9f995d9d16d9ad48da02273d7dca82fe82127d" | ||
| "gitHead": "37378633151d99980cc22f3d84dbae4043bfd5d7" | ||
| } |
10465
33.67%240
39.53%