@abtnode/models 1.16.8-next-d1e52353

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/README.md +1 -0
  2. package/lib/index.d.ts +5 -0
  3. package/lib/index.js +21 -0
  4. package/lib/migrate.d.ts +13 -0
  5. package/lib/migrate.js +127 -0
  6. package/lib/migrations/blocklet/20230422000000-genesis.js +21 -0
  7. package/lib/migrations/certificate-manager/20230422000000-genesis.js +19 -0
  8. package/lib/migrations/connect/20230422000000-genesis.js +13 -0
  9. package/lib/migrations/server/20230422000000-genesis.js +45 -0
  10. package/lib/migrations/service/20230422000000-genesis.js +13 -0
  11. package/lib/models/access-key.d.ts +6 -0
  12. package/lib/models/access-key.js +56 -0
  13. package/lib/models/account.d.ts +11 -0
  14. package/lib/models/account.js +56 -0
  15. package/lib/models/audit-log.d.ts +7 -0
  16. package/lib/models/audit-log.js +79 -0
  17. package/lib/models/backup.d.ts +6 -0
  18. package/lib/models/backup.js +72 -0
  19. package/lib/models/blocklet-extra.d.ts +7 -0
  20. package/lib/models/blocklet-extra.js +63 -0
  21. package/lib/models/blocklet.d.ts +6 -0
  22. package/lib/models/blocklet.js +120 -0
  23. package/lib/models/cache.d.ts +8 -0
  24. package/lib/models/cache.js +44 -0
  25. package/lib/models/certificate.d.ts +7 -0
  26. package/lib/models/certificate.js +70 -0
  27. package/lib/models/connected-account.d.ts +8 -0
  28. package/lib/models/connected-account.js +59 -0
  29. package/lib/models/connection.d.ts +23 -0
  30. package/lib/models/connection.js +89 -0
  31. package/lib/models/http-challenge.d.ts +9 -0
  32. package/lib/models/http-challenge.js +49 -0
  33. package/lib/models/index.d.ts +63 -0
  34. package/lib/models/index.js +149 -0
  35. package/lib/models/job.d.ts +13 -0
  36. package/lib/models/job.js +62 -0
  37. package/lib/models/message.d.ts +9 -0
  38. package/lib/models/message.js +54 -0
  39. package/lib/models/migration.d.ts +6 -0
  40. package/lib/models/migration.js +53 -0
  41. package/lib/models/notification.d.ts +4 -0
  42. package/lib/models/notification.js +77 -0
  43. package/lib/models/passport.d.ts +8 -0
  44. package/lib/models/passport.js +89 -0
  45. package/lib/models/rbac.d.ts +13 -0
  46. package/lib/models/rbac.js +65 -0
  47. package/lib/models/server.d.ts +10 -0
  48. package/lib/models/server.js +130 -0
  49. package/lib/models/session.d.ts +10 -0
  50. package/lib/models/session.js +54 -0
  51. package/lib/models/site.d.ts +8 -0
  52. package/lib/models/site.js +62 -0
  53. package/lib/models/user.d.ts +6 -0
  54. package/lib/models/user.js +105 -0
  55. package/lib/models/webhook.d.ts +6 -0
  56. package/lib/models/webhook.js +47 -0
  57. package/lib/states/base.d.ts +40 -0
  58. package/lib/states/base.js +144 -0
  59. package/lib/states/factory.d.ts +1 -0
  60. package/lib/states/factory.js +21 -0
  61. package/lib/types.d.ts +49 -0
  62. package/lib/types.js +2 -0
  63. package/lib/util.d.ts +6 -0
  64. package/lib/util.js +108 -0
  65. package/package.json +58 -0
@@ -0,0 +1,130 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createServerModel = void 0;
4
+ const sequelize_1 = require("sequelize");
5
+ function createServerModel() {
6
+ var _a;
7
+ return _a = class Server extends sequelize_1.Model {
8
+ static initialize(sequelize) {
9
+ this.init({
10
+ ...this.GENESIS_ATTRIBUTES,
11
+ }, {
12
+ sequelize,
13
+ modelName: 'Server',
14
+ tableName: 'servers',
15
+ timestamps: true,
16
+ });
17
+ }
18
+ },
19
+ // CAUTION: do not edit this
20
+ _a.GENESIS_ATTRIBUTES = {
21
+ did: {
22
+ type: sequelize_1.DataTypes.STRING(40),
23
+ primaryKey: true,
24
+ unique: true,
25
+ },
26
+ autoUpgrade: {
27
+ type: sequelize_1.DataTypes.BOOLEAN,
28
+ },
29
+ blockletRegistryList: {
30
+ type: sequelize_1.DataTypes.JSON,
31
+ },
32
+ registerUrl: {
33
+ type: sequelize_1.DataTypes.STRING(255),
34
+ },
35
+ webWalletUrl: {
36
+ type: sequelize_1.DataTypes.STRING(255),
37
+ },
38
+ name: {
39
+ type: sequelize_1.DataTypes.STRING(64),
40
+ },
41
+ description: {
42
+ type: sequelize_1.DataTypes.STRING(255),
43
+ },
44
+ pk: {
45
+ type: sequelize_1.DataTypes.STRING(255),
46
+ },
47
+ sk: {
48
+ type: sequelize_1.DataTypes.STRING(512),
49
+ },
50
+ initialized: {
51
+ type: sequelize_1.DataTypes.BOOLEAN,
52
+ },
53
+ version: {
54
+ type: sequelize_1.DataTypes.STRING(32),
55
+ },
56
+ port: {
57
+ type: sequelize_1.DataTypes.INTEGER,
58
+ },
59
+ routing: {
60
+ type: sequelize_1.DataTypes.JSON,
61
+ },
62
+ mode: {
63
+ type: sequelize_1.DataTypes.STRING(32),
64
+ },
65
+ enableWelcomePage: {
66
+ type: sequelize_1.DataTypes.BOOLEAN,
67
+ },
68
+ runtimeConfig: {
69
+ type: sequelize_1.DataTypes.JSON,
70
+ },
71
+ ownerNft: {
72
+ type: sequelize_1.DataTypes.JSON,
73
+ },
74
+ diskAlertThreshold: {
75
+ type: sequelize_1.DataTypes.INTEGER,
76
+ },
77
+ didRegistry: {
78
+ type: sequelize_1.DataTypes.STRING(255),
79
+ },
80
+ didDomain: {
81
+ type: sequelize_1.DataTypes.STRING(255),
82
+ },
83
+ enablePassportIssuance: {
84
+ type: sequelize_1.DataTypes.BOOLEAN,
85
+ },
86
+ trustedPassports: {
87
+ type: sequelize_1.DataTypes.JSON,
88
+ },
89
+ trustedFactories: {
90
+ type: sequelize_1.DataTypes.JSON,
91
+ },
92
+ customBlockletNumber: {
93
+ type: sequelize_1.DataTypes.INTEGER,
94
+ },
95
+ createdAt: {
96
+ type: sequelize_1.DataTypes.DATE,
97
+ defaultValue: sequelize_1.DataTypes.NOW,
98
+ },
99
+ updatedAt: {
100
+ type: sequelize_1.DataTypes.DATE,
101
+ defaultValue: sequelize_1.DataTypes.NOW,
102
+ },
103
+ nodeOwner: {
104
+ type: sequelize_1.DataTypes.JSON,
105
+ },
106
+ startedAt: {
107
+ type: sequelize_1.DataTypes.DATE,
108
+ },
109
+ initializedAt: {
110
+ type: sequelize_1.DataTypes.DATE,
111
+ },
112
+ nextVersion: {
113
+ type: sequelize_1.DataTypes.STRING(32),
114
+ },
115
+ previousMode: {
116
+ type: sequelize_1.DataTypes.STRING(32),
117
+ },
118
+ status: {
119
+ type: sequelize_1.DataTypes.INTEGER,
120
+ },
121
+ enableBetaRelease: {
122
+ type: sequelize_1.DataTypes.BOOLEAN,
123
+ },
124
+ upgradeSessionId: {
125
+ type: sequelize_1.DataTypes.STRING(32),
126
+ },
127
+ },
128
+ _a;
129
+ }
130
+ exports.createServerModel = createServerModel;
@@ -0,0 +1,10 @@
1
+ import { DynamicModel } from '../types';
2
+ export type SessionState = {
3
+ id: string;
4
+ type: string;
5
+ key: string;
6
+ __data: any;
7
+ createdAt: Date;
8
+ updatedAt: Date;
9
+ };
10
+ export declare function createSessionModel(): DynamicModel<SessionState>;
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createSessionModel = void 0;
4
+ // Storage for multi-step conversations or long running tasks
5
+ const sequelize_1 = require("sequelize");
6
+ const util_1 = require("../util");
7
+ function createSessionModel() {
8
+ var _a;
9
+ return _a = class Session extends sequelize_1.Model {
10
+ static initialize(sequelize) {
11
+ this.init({
12
+ ...this.GENESIS_ATTRIBUTES,
13
+ }, {
14
+ sequelize,
15
+ indexes: [{ fields: ['type', 'id'] }],
16
+ modelName: 'Session',
17
+ tableName: 'sessions',
18
+ timestamps: true,
19
+ });
20
+ }
21
+ },
22
+ _a.GENESIS_ATTRIBUTES = {
23
+ id: {
24
+ type: sequelize_1.DataTypes.STRING(40),
25
+ primaryKey: true,
26
+ allowNull: false,
27
+ defaultValue: util_1.generateId,
28
+ },
29
+ type: {
30
+ type: sequelize_1.DataTypes.STRING(64),
31
+ index: true,
32
+ allowNull: true,
33
+ },
34
+ key: {
35
+ type: sequelize_1.DataTypes.STRING(64),
36
+ index: true,
37
+ },
38
+ __data: {
39
+ type: sequelize_1.DataTypes.JSON,
40
+ allowNull: false,
41
+ defaultValue: {},
42
+ },
43
+ createdAt: {
44
+ type: sequelize_1.DataTypes.DATE,
45
+ defaultValue: sequelize_1.DataTypes.NOW,
46
+ },
47
+ updatedAt: {
48
+ type: sequelize_1.DataTypes.DATE,
49
+ defaultValue: sequelize_1.DataTypes.NOW,
50
+ },
51
+ },
52
+ _a;
53
+ }
54
+ exports.createSessionModel = createSessionModel;
@@ -0,0 +1,8 @@
1
+ import { TRoutingSite } from '@abtnode/types';
2
+ import { DynamicModel } from '../types';
3
+ export type SiteState = TRoutingSite & {
4
+ id: string;
5
+ createdAt: Date;
6
+ updatedAt: Date;
7
+ };
8
+ export declare function createSiteModel(): DynamicModel<SiteState>;
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createSiteModel = void 0;
4
+ const sequelize_1 = require("sequelize");
5
+ const util_1 = require("../util");
6
+ function createSiteModel() {
7
+ var _a;
8
+ return _a = class Site extends sequelize_1.Model {
9
+ static initialize(sequelize) {
10
+ this.init({
11
+ ...this.GENESIS_ATTRIBUTES,
12
+ }, {
13
+ sequelize,
14
+ modelName: 'Site',
15
+ tableName: 'sites',
16
+ timestamps: true,
17
+ });
18
+ }
19
+ },
20
+ _a.GENESIS_ATTRIBUTES = {
21
+ id: {
22
+ type: sequelize_1.DataTypes.STRING(40),
23
+ primaryKey: true,
24
+ defaultValue: util_1.generateId,
25
+ },
26
+ domain: {
27
+ type: sequelize_1.DataTypes.STRING(255),
28
+ unique: true,
29
+ },
30
+ domainAliases: {
31
+ type: sequelize_1.DataTypes.JSON,
32
+ defaultValue: [],
33
+ },
34
+ name: {
35
+ type: sequelize_1.DataTypes.STRING(32),
36
+ },
37
+ isProtected: {
38
+ type: sequelize_1.DataTypes.BOOLEAN,
39
+ defaultValue: false,
40
+ },
41
+ rules: {
42
+ type: sequelize_1.DataTypes.JSON,
43
+ defaultValue: [],
44
+ },
45
+ createdAt: {
46
+ type: sequelize_1.DataTypes.DATE,
47
+ defaultValue: sequelize_1.DataTypes.NOW,
48
+ index: true,
49
+ },
50
+ updatedAt: {
51
+ type: sequelize_1.DataTypes.DATE,
52
+ defaultValue: sequelize_1.DataTypes.NOW,
53
+ index: true,
54
+ },
55
+ corsAllowedOrigins: {
56
+ type: sequelize_1.DataTypes.JSON,
57
+ defaultValue: [],
58
+ },
59
+ },
60
+ _a;
61
+ }
62
+ exports.createSiteModel = createSiteModel;
@@ -0,0 +1,6 @@
1
+ import { TUserInfo } from '@abtnode/types';
2
+ import { DynamicModel } from '../types';
3
+ export type UserState = Omit<TUserInfo, 'passports' | 'connectedAccounts'> & {
4
+ extra: Record<string, any>;
5
+ };
6
+ export declare function createUserModel(): DynamicModel<UserState>;
@@ -0,0 +1,105 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createUserModel = void 0;
4
+ const sequelize_1 = require("sequelize");
5
+ function createUserModel() {
6
+ var _a;
7
+ return _a = class User extends sequelize_1.Model {
8
+ static initialize(sequelize) {
9
+ this.init({
10
+ ...this.GENESIS_ATTRIBUTES,
11
+ }, {
12
+ sequelize,
13
+ modelName: 'User',
14
+ tableName: 'users',
15
+ timestamps: true,
16
+ indexes: [{ fields: ['email'] }, { fields: ['fullName'] }],
17
+ });
18
+ }
19
+ static associate(models) {
20
+ User.hasMany(models.Passport, {
21
+ foreignKey: 'userDid',
22
+ onDelete: 'CASCADE',
23
+ as: 'passports',
24
+ });
25
+ User.hasMany(models.ConnectedAccount, {
26
+ foreignKey: 'userDid',
27
+ onDelete: 'CASCADE',
28
+ as: 'connectedAccounts',
29
+ });
30
+ }
31
+ },
32
+ _a.GENESIS_ATTRIBUTES = {
33
+ did: {
34
+ type: sequelize_1.DataTypes.STRING(40),
35
+ primaryKey: true,
36
+ unique: true,
37
+ },
38
+ pk: {
39
+ type: sequelize_1.DataTypes.STRING(256),
40
+ allowNull: false,
41
+ },
42
+ fullName: {
43
+ type: sequelize_1.DataTypes.STRING(128),
44
+ },
45
+ email: {
46
+ type: sequelize_1.DataTypes.STRING(128),
47
+ },
48
+ avatar: {
49
+ // FIXME: make this a shorter string?
50
+ type: sequelize_1.DataTypes.TEXT,
51
+ },
52
+ role: {
53
+ type: sequelize_1.DataTypes.STRING(64),
54
+ },
55
+ remark: {
56
+ type: sequelize_1.DataTypes.STRING(128),
57
+ defaultValue: '',
58
+ },
59
+ sourceProvider: {
60
+ type: sequelize_1.DataTypes.STRING(32),
61
+ },
62
+ locale: {
63
+ type: sequelize_1.DataTypes.STRING(8),
64
+ defaultValue: 'en',
65
+ },
66
+ approved: {
67
+ type: sequelize_1.DataTypes.BOOLEAN,
68
+ index: true,
69
+ defaultValue: true,
70
+ },
71
+ // server extended
72
+ extra: {
73
+ type: sequelize_1.DataTypes.JSON,
74
+ defaultValue: {},
75
+ },
76
+ // app extended
77
+ meta: {
78
+ type: sequelize_1.DataTypes.JSON,
79
+ defaultValue: {},
80
+ },
81
+ firstLoginAt: {
82
+ type: sequelize_1.DataTypes.DATE,
83
+ index: true,
84
+ },
85
+ lastLoginAt: {
86
+ type: sequelize_1.DataTypes.DATE,
87
+ index: true,
88
+ },
89
+ lastLoginIp: {
90
+ type: sequelize_1.DataTypes.STRING,
91
+ },
92
+ createdAt: {
93
+ type: sequelize_1.DataTypes.DATE,
94
+ defaultValue: sequelize_1.DataTypes.NOW,
95
+ index: true,
96
+ },
97
+ updatedAt: {
98
+ type: sequelize_1.DataTypes.DATE,
99
+ defaultValue: sequelize_1.DataTypes.NOW,
100
+ index: true,
101
+ },
102
+ },
103
+ _a;
104
+ }
105
+ exports.createUserModel = createUserModel;
@@ -0,0 +1,6 @@
1
+ import { TWebHook } from '@abtnode/types';
2
+ import { DynamicModel } from '../types';
3
+ export type WebHookState = TWebHook & {
4
+ id: string;
5
+ };
6
+ export declare function createWebHookModel(): DynamicModel<WebHookState>;
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createWebHookModel = void 0;
4
+ const sequelize_1 = require("sequelize");
5
+ const util_1 = require("../util");
6
+ function createWebHookModel() {
7
+ var _a;
8
+ return _a = class WebHook extends sequelize_1.Model {
9
+ static initialize(sequelize) {
10
+ this.init({
11
+ ...this.GENESIS_ATTRIBUTES,
12
+ }, {
13
+ sequelize,
14
+ modelName: 'WebHook',
15
+ tableName: 'webhooks',
16
+ timestamps: true,
17
+ });
18
+ }
19
+ },
20
+ _a.GENESIS_ATTRIBUTES = {
21
+ id: {
22
+ type: sequelize_1.DataTypes.STRING(40),
23
+ primaryKey: true,
24
+ defaultValue: util_1.generateId,
25
+ },
26
+ type: {
27
+ type: sequelize_1.DataTypes.STRING(16),
28
+ allowNull: false,
29
+ },
30
+ params: {
31
+ type: sequelize_1.DataTypes.JSON,
32
+ allowNull: false,
33
+ },
34
+ createdAt: {
35
+ type: sequelize_1.DataTypes.DATE,
36
+ defaultValue: sequelize_1.DataTypes.NOW,
37
+ index: true,
38
+ },
39
+ updatedAt: {
40
+ type: sequelize_1.DataTypes.DATE,
41
+ defaultValue: sequelize_1.DataTypes.NOW,
42
+ index: true,
43
+ },
44
+ },
45
+ _a;
46
+ }
47
+ exports.createWebHookModel = createWebHookModel;
@@ -0,0 +1,40 @@
1
+ /// <reference types="node" />
2
+ import EventEmitter from 'events';
3
+ import { ModelDefined } from 'sequelize';
4
+ import { AnyObject, SelectOptions, SortOptions, UpdateOptions, ConditionOptions, PagingOptions } from '../types';
5
+ export declare class BaseState<T> extends EventEmitter {
6
+ model: ModelDefined<T, T>;
7
+ jsonAttributes: string[];
8
+ config: AnyObject;
9
+ ready: boolean;
10
+ readyCallbacks: Function[];
11
+ constructor(model: ModelDefined<T, T>, config?: AnyObject);
12
+ find(condition?: ConditionOptions<T>, selection?: SelectOptions<T>, sort?: SortOptions<T>): Promise<T[]>;
13
+ findOne(condition?: ConditionOptions<T>, selection?: SelectOptions<T>, sort?: SortOptions<T>): Promise<T>;
14
+ count(condition?: ConditionOptions<T>): Promise<number>;
15
+ insert(doc: any): Promise<T>;
16
+ insertMany(docs: any[]): Promise<T[]>;
17
+ update(condition: string | ConditionOptions<T>, updates: AnyObject, options?: UpdateOptions): Promise<[number, T[]]>;
18
+ updateById(id: string, updates: AnyObject, options?: UpdateOptions): Promise<[number, T[]]>;
19
+ upsert(condition: ConditionOptions<T>, updates: AnyObject): Promise<T>;
20
+ paginate(condition: ConditionOptions<T>, sort: SortOptions<T>, paging: PagingOptions, selection?: SelectOptions<T>): Promise<{
21
+ list: T[];
22
+ paging: {
23
+ total: number;
24
+ pageSize: number;
25
+ pageCount: number;
26
+ page: number;
27
+ };
28
+ }>;
29
+ remove(condition: ConditionOptions<T>): Promise<number>;
30
+ max(field: keyof T, condition?: ConditionOptions<T>): Promise<unknown>;
31
+ min(field: keyof T, condition?: ConditionOptions<T>): Promise<unknown>;
32
+ sum(field: keyof T, condition?: ConditionOptions<T>): Promise<number>;
33
+ query(query: string): Promise<object[]>;
34
+ reset(): Promise<number>;
35
+ loadDatabase(cb?: Function): void;
36
+ closeDatabase(cb?: Function): void;
37
+ compactDatafile(cb?: Function): void;
38
+ onReady(cb: Function): void;
39
+ static renameIdFiledName(docs: any): any;
40
+ }
@@ -0,0 +1,144 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.BaseState = void 0;
7
+ const events_1 = __importDefault(require("events"));
8
+ const sequelize_1 = require("sequelize");
9
+ const lodash_clonedeep_1 = __importDefault(require("lodash.clonedeep"));
10
+ const util_1 = require("../util");
11
+ class BaseState extends events_1.default {
12
+ constructor(model, config = {}) {
13
+ super();
14
+ this.model = model;
15
+ this.config = Object.freeze((0, lodash_clonedeep_1.default)(config));
16
+ // Just used to minic ready callbacks of @abtnode/db
17
+ this.ready = false;
18
+ this.readyCallbacks = [];
19
+ setImmediate(() => {
20
+ this.ready = true;
21
+ if (this.readyCallbacks.length) {
22
+ this.readyCallbacks.forEach((x) => x());
23
+ }
24
+ });
25
+ }
26
+ async find(condition = {}, selection = {}, sort = {}) {
27
+ const params = (0, util_1.formatParams)({
28
+ attributes: (0, util_1.formatSelection)(selection),
29
+ order: (0, util_1.formatOrder)(sort),
30
+ ...(0, util_1.formatConditions)(condition),
31
+ nest: true,
32
+ });
33
+ return this.model.findAll(params).then((x) => x.map((y) => y.toJSON()));
34
+ }
35
+ async findOne(condition = {}, selection = {}, sort = {}) {
36
+ const params = (0, util_1.formatParams)({
37
+ attributes: (0, util_1.formatSelection)(selection),
38
+ order: (0, util_1.formatOrder)(sort),
39
+ ...(0, util_1.formatConditions)(condition),
40
+ });
41
+ return this.model.findOne(params).then((x) => x?.toJSON());
42
+ }
43
+ async count(condition = {}) {
44
+ return this.model.count({ ...(0, util_1.formatConditions)(condition) });
45
+ }
46
+ async insert(doc) {
47
+ const newDoc = await this.model.create(doc, { returning: true });
48
+ await newDoc.save();
49
+ return newDoc.toJSON();
50
+ }
51
+ async insertMany(docs) {
52
+ const newDocs = await this.model.bulkCreate(docs, { returning: true });
53
+ return newDocs.map((x) => x.toJSON());
54
+ }
55
+ async update(condition, updates, options = {}) {
56
+ if (arguments.length < 2) {
57
+ throw new Error('condition and update param are required to update database record');
58
+ }
59
+ if (typeof condition === 'string') {
60
+ return this.updateById(condition, updates, options);
61
+ }
62
+ const [affectedRows] = await this.model.update(updates.$set || updates, {
63
+ where: (0, util_1.formatConditions)(condition).where,
64
+ });
65
+ const docs = options.returnUpdatedDocs ? await this.find(condition) : [];
66
+ return [affectedRows, docs];
67
+ }
68
+ async updateById(id, updates, options = {}) {
69
+ return this.update({ id }, updates, options);
70
+ }
71
+ async upsert(condition, updates) {
72
+ const [instance] = await this.model.upsert(Object.assign({}, condition, updates.$set || updates));
73
+ return instance.toJSON();
74
+ }
75
+ async paginate(condition, sort, paging, selection = {}) {
76
+ const { pageSize: size = 20, page = 1 } = paging || {};
77
+ const pageSize = Math.min(100, size);
78
+ const params = (0, util_1.formatParams)({
79
+ attributes: (0, util_1.formatSelection)(selection),
80
+ order: (0, util_1.formatOrder)(sort),
81
+ limit: pageSize,
82
+ offset: (page - 1) * pageSize,
83
+ ...(0, util_1.formatConditions)(condition),
84
+ nest: true,
85
+ });
86
+ const [count, rows] = await Promise.all([
87
+ this.model.count({ distinct: true, where: params.where }),
88
+ this.model.findAll(params),
89
+ ]);
90
+ return {
91
+ list: rows.map((x) => x.toJSON()),
92
+ paging: {
93
+ total: count,
94
+ pageSize,
95
+ pageCount: Math.ceil(count / pageSize),
96
+ page,
97
+ },
98
+ };
99
+ }
100
+ remove(condition) {
101
+ return this.model.destroy((0, util_1.formatConditions)(condition));
102
+ }
103
+ max(field, condition = {}) {
104
+ return this.model.max(field, (0, util_1.formatConditions)(condition));
105
+ }
106
+ min(field, condition = {}) {
107
+ return this.model.min(field, (0, util_1.formatConditions)(condition));
108
+ }
109
+ sum(field, condition = {}) {
110
+ return this.model.sum(field, (0, util_1.formatConditions)(condition));
111
+ }
112
+ query(query) {
113
+ return this.model.sequelize.query(query, { type: sequelize_1.QueryTypes.SELECT });
114
+ }
115
+ reset() {
116
+ return this.model.destroy({ truncate: true });
117
+ }
118
+ loadDatabase(cb) {
119
+ if (typeof cb === 'function') {
120
+ setImmediate(() => cb());
121
+ }
122
+ }
123
+ closeDatabase(cb = () => { }) {
124
+ // @ts-ignore
125
+ this.model.sequelize.close().then(cb).catch(cb);
126
+ }
127
+ compactDatafile(cb) {
128
+ if (typeof cb === 'function') {
129
+ setImmediate(() => cb());
130
+ }
131
+ }
132
+ onReady(cb) {
133
+ if (this.ready) {
134
+ cb();
135
+ }
136
+ else {
137
+ this.readyCallbacks.push(cb);
138
+ }
139
+ }
140
+ static renameIdFiledName(docs) {
141
+ return docs;
142
+ }
143
+ }
144
+ exports.BaseState = BaseState;
@@ -0,0 +1 @@
1
+ export declare function createStateFactory(initializer: Function, models: any): any;
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createStateFactory = void 0;
4
+ function createStateFactory(initializer, models) {
5
+ const states = {};
6
+ return new Proxy({}, {
7
+ get(target, prop) {
8
+ if (prop === 'init') {
9
+ return (...args) => Object.assign(states, initializer(...args));
10
+ }
11
+ if (prop === 'models') {
12
+ return models;
13
+ }
14
+ if (states[prop]) {
15
+ return states[prop];
16
+ }
17
+ throw new Error(`State ${String(prop)} may not be initialized`);
18
+ },
19
+ });
20
+ }
21
+ exports.createStateFactory = createStateFactory;