@nocobase/database 0.7.0-alpha.9 → 0.7.1-alpha.6
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.
- package/lib/collection-importer.js +85 -68
- package/lib/collection.d.ts +6 -2
- package/lib/collection.js +371 -210
- package/lib/database.d.ts +23 -4
- package/lib/database.js +599 -273
- package/lib/fields/array-field.js +45 -25
- package/lib/fields/belongs-to-field.js +101 -54
- package/lib/fields/belongs-to-many-field.js +98 -53
- package/lib/fields/boolean-field.js +24 -9
- package/lib/fields/context-field.js +77 -45
- package/lib/fields/date-field.js +24 -9
- package/lib/fields/field.d.ts +4 -1
- package/lib/fields/field.js +231 -75
- package/lib/fields/formula-field.d.ts +19 -0
- package/lib/fields/formula-field.js +184 -0
- package/lib/fields/has-inverse-field.js +4 -2
- package/lib/fields/has-many-field.js +105 -56
- package/lib/fields/has-one-field.js +105 -54
- package/lib/fields/index.d.ts +5 -1
- package/lib/fields/index.js +290 -32
- package/lib/fields/json-field.js +36 -16
- package/lib/fields/number-field.js +53 -26
- package/lib/fields/password-field.js +120 -73
- package/lib/fields/radio-field.js +75 -47
- package/lib/fields/relation-field.js +41 -28
- package/lib/fields/sort-field.js +165 -89
- package/lib/fields/string-field.js +24 -9
- package/lib/fields/text-field.js +24 -9
- package/lib/fields/time-field.js +24 -9
- package/lib/fields/uid-field.js +57 -28
- package/lib/fields/uuid-field.d.ts +9 -0
- package/lib/fields/uuid-field.js +39 -0
- package/lib/fields/virtual-field.js +24 -9
- package/lib/filter-parser.js +288 -179
- package/lib/index.d.ts +1 -0
- package/lib/index.js +224 -29
- package/lib/magic-attribute-model.js +123 -71
- package/lib/migration.d.ts +35 -0
- package/lib/migration.js +90 -0
- package/lib/mock-database.d.ts +1 -0
- package/lib/mock-database.js +69 -34
- package/lib/model-hook.d.ts +5 -5
- package/lib/model-hook.js +109 -60
- package/lib/model.js +116 -81
- package/lib/operators/array.js +136 -96
- package/lib/operators/association.js +30 -14
- package/lib/operators/boolean.d.ts +13 -0
- package/lib/operators/boolean.js +35 -0
- package/lib/operators/date.js +78 -34
- package/lib/operators/empty.js +113 -75
- package/lib/operators/index.js +15 -3
- package/lib/operators/ne.js +27 -12
- package/{esm/operators/ne.d.ts → lib/operators/notIn.d.ts} +2 -2
- package/lib/operators/notIn.js +29 -0
- package/lib/operators/string.js +56 -35
- package/lib/operators/utils.js +18 -10
- package/lib/options-parser.js +345 -215
- package/lib/playground.js +66 -53
- package/lib/relation-repository/belongs-to-many-repository.js +281 -198
- package/lib/relation-repository/belongs-to-repository.js +10 -6
- package/lib/relation-repository/hasmany-repository.js +168 -121
- package/lib/relation-repository/hasone-repository.js +10 -6
- package/lib/relation-repository/multiple-relation-repository.d.ts +3 -3
- package/lib/relation-repository/multiple-relation-repository.js +263 -148
- package/lib/relation-repository/relation-repository.d.ts +1 -1
- package/lib/relation-repository/relation-repository.js +163 -93
- package/lib/relation-repository/single-relation-repository.d.ts +6 -6
- package/lib/relation-repository/single-relation-repository.js +145 -99
- package/lib/relation-repository/types.js +4 -2
- package/lib/repository.d.ts +4 -7
- package/lib/repository.js +473 -291
- package/lib/transaction-decorator.js +80 -67
- package/lib/update-associations.d.ts +1 -2
- package/lib/update-associations.js +525 -321
- package/lib/update-guard.js +160 -117
- package/package.json +9 -9
- package/src/__tests__/collection.test.ts +27 -0
- package/src/__tests__/database.test.ts +47 -0
- package/src/__tests__/fields/formula-field.test.ts +69 -0
- package/src/__tests__/fields/uuid-field.test.ts +30 -0
- package/src/__tests__/fixtures/migrations/m1.ts +7 -0
- package/src/__tests__/fixtures/migrations/m2.ts +7 -0
- package/src/__tests__/hooks/afterCreateWithAssociations.test.ts +33 -0
- package/src/__tests__/migrator.test.ts +70 -0
- package/src/__tests__/model-hook.test.ts +54 -0
- package/src/__tests__/operator/notIn.test.ts +33 -0
- package/src/__tests__/option-parser.test.ts +30 -6
- package/src/__tests__/relation-repository/belongs-to-many-repository.test.ts +1 -1
- package/src/__tests__/sequelize-hooks.test.ts +69 -0
- package/src/__tests__/sort.test.ts +51 -0
- package/src/__tests__/update-associations.test.ts +3 -3
- package/src/collection-importer.ts +12 -20
- package/src/collection.ts +26 -2
- package/src/database.ts +144 -14
- package/src/fields/field.ts +88 -1
- package/src/fields/formula-field.ts +106 -0
- package/src/fields/index.ts +6 -0
- package/src/fields/password-field.ts +2 -0
- package/src/fields/uuid-field.ts +21 -0
- package/src/index.ts +1 -0
- package/src/migration.ts +76 -0
- package/src/mock-database.ts +2 -1
- package/src/model-hook.ts +26 -22
- package/src/operators/boolean.ts +18 -0
- package/src/operators/index.ts +2 -0
- package/src/operators/notIn.ts +12 -0
- package/src/options-parser.ts +14 -10
- package/src/relation-repository/multiple-relation-repository.ts +14 -6
- package/src/relation-repository/relation-repository.ts +12 -6
- package/src/relation-repository/single-relation-repository.ts +11 -7
- package/src/repository.ts +20 -10
- package/src/update-associations.ts +2 -3
- package/esm/collection-importer.d.ts +0 -7
- package/esm/collection-importer.js +0 -49
- package/esm/collection-importer.js.map +0 -1
- package/esm/collection.d.ts +0 -73
- package/esm/collection.js +0 -224
- package/esm/collection.js.map +0 -1
- package/esm/database.d.ts +0 -101
- package/esm/database.js +0 -275
- package/esm/database.js.map +0 -1
- package/esm/fields/array-field.d.ts +0 -11
- package/esm/fields/array-field.js +0 -26
- package/esm/fields/array-field.js.map +0 -1
- package/esm/fields/belongs-to-field.d.ts +0 -12
- package/esm/fields/belongs-to-field.js +0 -57
- package/esm/fields/belongs-to-field.js.map +0 -1
- package/esm/fields/belongs-to-many-field.d.ts +0 -11
- package/esm/fields/belongs-to-many-field.js +0 -55
- package/esm/fields/belongs-to-many-field.js.map +0 -1
- package/esm/fields/boolean-field.d.ts +0 -8
- package/esm/fields/boolean-field.js +0 -8
- package/esm/fields/boolean-field.js.map +0 -1
- package/esm/fields/context-field.d.ts +0 -13
- package/esm/fields/context-field.js +0 -43
- package/esm/fields/context-field.js.map +0 -1
- package/esm/fields/date-field.d.ts +0 -8
- package/esm/fields/date-field.js +0 -8
- package/esm/fields/date-field.js.map +0 -1
- package/esm/fields/field.d.ts +0 -37
- package/esm/fields/field.js +0 -74
- package/esm/fields/field.js.map +0 -1
- package/esm/fields/has-inverse-field.d.ts +0 -4
- package/esm/fields/has-inverse-field.js +0 -2
- package/esm/fields/has-inverse-field.js.map +0 -1
- package/esm/fields/has-many-field.d.ts +0 -64
- package/esm/fields/has-many-field.js +0 -58
- package/esm/fields/has-many-field.js.map +0 -1
- package/esm/fields/has-one-field.d.ts +0 -64
- package/esm/fields/has-one-field.js +0 -57
- package/esm/fields/has-one-field.js.map +0 -1
- package/esm/fields/index.d.ts +0 -40
- package/esm/fields/index.js +0 -21
- package/esm/fields/index.js.map +0 -1
- package/esm/fields/json-field.d.ts +0 -14
- package/esm/fields/json-field.js +0 -17
- package/esm/fields/json-field.js.map +0 -1
- package/esm/fields/number-field.d.ts +0 -32
- package/esm/fields/number-field.js +0 -28
- package/esm/fields/number-field.js.map +0 -1
- package/esm/fields/password-field.d.ts +0 -21
- package/esm/fields/password-field.js +0 -71
- package/esm/fields/password-field.js.map +0 -1
- package/esm/fields/radio-field.d.ts +0 -14
- package/esm/fields/radio-field.js +0 -49
- package/esm/fields/radio-field.js.map +0 -1
- package/esm/fields/relation-field.d.ts +0 -20
- package/esm/fields/relation-field.js +0 -27
- package/esm/fields/relation-field.js.map +0 -1
- package/esm/fields/sort-field.d.ts +0 -16
- package/esm/fields/sort-field.js +0 -90
- package/esm/fields/sort-field.js.map +0 -1
- package/esm/fields/string-field.d.ts +0 -8
- package/esm/fields/string-field.js +0 -8
- package/esm/fields/string-field.js.map +0 -1
- package/esm/fields/text-field.d.ts +0 -8
- package/esm/fields/text-field.js +0 -8
- package/esm/fields/text-field.js.map +0 -1
- package/esm/fields/time-field.d.ts +0 -8
- package/esm/fields/time-field.js +0 -8
- package/esm/fields/time-field.js.map +0 -1
- package/esm/fields/uid-field.d.ts +0 -10
- package/esm/fields/uid-field.js +0 -27
- package/esm/fields/uid-field.js.map +0 -1
- package/esm/fields/virtual-field.d.ts +0 -8
- package/esm/fields/virtual-field.js +0 -8
- package/esm/fields/virtual-field.js.map +0 -1
- package/esm/filter-parser.d.ts +0 -27
- package/esm/filter-parser.js +0 -185
- package/esm/filter-parser.js.map +0 -1
- package/esm/index.d.ts +0 -15
- package/esm/index.js +0 -16
- package/esm/index.js.map +0 -1
- package/esm/magic-attribute-model.d.ts +0 -7
- package/esm/magic-attribute-model.js +0 -70
- package/esm/magic-attribute-model.js.map +0 -1
- package/esm/mock-database.d.ts +0 -22
- package/esm/mock-database.js +0 -34
- package/esm/mock-database.js.map +0 -1
- package/esm/model-hook.d.ts +0 -12
- package/esm/model-hook.js +0 -61
- package/esm/model-hook.js.map +0 -1
- package/esm/model.d.ts +0 -15
- package/esm/model.js +0 -80
- package/esm/model.js.map +0 -1
- package/esm/operators/array.d.ts +0 -26
- package/esm/operators/array.js +0 -105
- package/esm/operators/array.js.map +0 -1
- package/esm/operators/association.d.ts +0 -10
- package/esm/operators/association.js +0 -14
- package/esm/operators/association.js.map +0 -1
- package/esm/operators/date.d.ts +0 -34
- package/esm/operators/date.js +0 -35
- package/esm/operators/date.js.map +0 -1
- package/esm/operators/empty.d.ts +0 -28
- package/esm/operators/empty.js +0 -58
- package/esm/operators/empty.js.map +0 -1
- package/esm/operators/index.d.ts +0 -2
- package/esm/operators/index.js +0 -2
- package/esm/operators/index.js.map +0 -1
- package/esm/operators/ne.js +0 -12
- package/esm/operators/ne.js.map +0 -1
- package/esm/operators/string.d.ts +0 -21
- package/esm/operators/string.js +0 -35
- package/esm/operators/string.js.map +0 -1
- package/esm/operators/utils.d.ts +0 -4
- package/esm/operators/utils.js +0 -11
- package/esm/operators/utils.js.map +0 -1
- package/esm/options-parser.d.ts +0 -31
- package/esm/options-parser.js +0 -225
- package/esm/options-parser.js.map +0 -1
- package/esm/playground.d.ts +0 -1
- package/esm/playground.js +0 -53
- package/esm/playground.js.map +0 -1
- package/esm/relation-repository/belongs-to-many-repository.d.ts +0 -36
- package/esm/relation-repository/belongs-to-many-repository.js +0 -199
- package/esm/relation-repository/belongs-to-many-repository.js.map +0 -1
- package/esm/relation-repository/belongs-to-repository.d.ts +0 -17
- package/esm/relation-repository/belongs-to-repository.js +0 -4
- package/esm/relation-repository/belongs-to-repository.js.map +0 -1
- package/esm/relation-repository/hasmany-repository.d.ts +0 -23
- package/esm/relation-repository/hasmany-repository.js +0 -125
- package/esm/relation-repository/hasmany-repository.js.map +0 -1
- package/esm/relation-repository/hasone-repository.d.ts +0 -17
- package/esm/relation-repository/hasone-repository.js +0 -4
- package/esm/relation-repository/hasone-repository.js.map +0 -1
- package/esm/relation-repository/multiple-relation-repository.d.ts +0 -23
- package/esm/relation-repository/multiple-relation-repository.js +0 -149
- package/esm/relation-repository/multiple-relation-repository.js.map +0 -1
- package/esm/relation-repository/relation-repository.d.ts +0 -32
- package/esm/relation-repository/relation-repository.js +0 -93
- package/esm/relation-repository/relation-repository.js.map +0 -1
- package/esm/relation-repository/single-relation-repository.d.ts +0 -23
- package/esm/relation-repository/single-relation-repository.js +0 -96
- package/esm/relation-repository/single-relation-repository.js.map +0 -1
- package/esm/relation-repository/types.d.ts +0 -7
- package/esm/relation-repository/types.js +0 -2
- package/esm/relation-repository/types.js.map +0 -1
- package/esm/repository.d.ts +0 -165
- package/esm/repository.js +0 -276
- package/esm/repository.js.map +0 -1
- package/esm/transaction-decorator.d.ts +0 -1
- package/esm/transaction-decorator.js +0 -63
- package/esm/transaction-decorator.js.map +0 -1
- package/esm/update-associations.d.ts +0 -60
- package/esm/update-associations.js +0 -362
- package/esm/update-associations.js.map +0 -1
- package/esm/update-guard.d.ts +0 -26
- package/esm/update-guard.js +0 -122
- package/esm/update-guard.js.map +0 -1
- package/lib/collection-importer.js.map +0 -1
- package/lib/collection.js.map +0 -1
- package/lib/database.js.map +0 -1
- package/lib/fields/array-field.js.map +0 -1
- package/lib/fields/belongs-to-field.js.map +0 -1
- package/lib/fields/belongs-to-many-field.js.map +0 -1
- package/lib/fields/boolean-field.js.map +0 -1
- package/lib/fields/context-field.js.map +0 -1
- package/lib/fields/date-field.js.map +0 -1
- package/lib/fields/field.js.map +0 -1
- package/lib/fields/has-inverse-field.js.map +0 -1
- package/lib/fields/has-many-field.js.map +0 -1
- package/lib/fields/has-one-field.js.map +0 -1
- package/lib/fields/index.js.map +0 -1
- package/lib/fields/json-field.js.map +0 -1
- package/lib/fields/number-field.js.map +0 -1
- package/lib/fields/password-field.js.map +0 -1
- package/lib/fields/radio-field.js.map +0 -1
- package/lib/fields/relation-field.js.map +0 -1
- package/lib/fields/sort-field.js.map +0 -1
- package/lib/fields/string-field.js.map +0 -1
- package/lib/fields/text-field.js.map +0 -1
- package/lib/fields/time-field.js.map +0 -1
- package/lib/fields/uid-field.js.map +0 -1
- package/lib/fields/virtual-field.js.map +0 -1
- package/lib/filter-parser.js.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/magic-attribute-model.js.map +0 -1
- package/lib/mock-database.js.map +0 -1
- package/lib/model-hook.js.map +0 -1
- package/lib/model.js.map +0 -1
- package/lib/operators/array.js.map +0 -1
- package/lib/operators/association.js.map +0 -1
- package/lib/operators/date.js.map +0 -1
- package/lib/operators/empty.js.map +0 -1
- package/lib/operators/index.js.map +0 -1
- package/lib/operators/ne.js.map +0 -1
- package/lib/operators/string.js.map +0 -1
- package/lib/operators/utils.js.map +0 -1
- package/lib/options-parser.js.map +0 -1
- package/lib/playground.js.map +0 -1
- package/lib/relation-repository/belongs-to-many-repository.js.map +0 -1
- package/lib/relation-repository/belongs-to-repository.js.map +0 -1
- package/lib/relation-repository/hasmany-repository.js.map +0 -1
- package/lib/relation-repository/hasone-repository.js.map +0 -1
- package/lib/relation-repository/multiple-relation-repository.js.map +0 -1
- package/lib/relation-repository/relation-repository.js.map +0 -1
- package/lib/relation-repository/single-relation-repository.js.map +0 -1
- package/lib/relation-repository/types.js.map +0 -1
- package/lib/repository.js.map +0 -1
- package/lib/transaction-decorator.js.map +0 -1
- package/lib/update-associations.js.map +0 -1
- package/lib/update-guard.js.map +0 -1
- package/tsconfig.build.json +0 -9
- package/tsconfig.json +0 -5
package/lib/mock-database.js
CHANGED
|
@@ -1,40 +1,75 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.MockDatabase = void 0;
|
|
7
|
+
exports.getConfigByEnv = getConfigByEnv;
|
|
8
|
+
exports.mockDatabase = mockDatabase;
|
|
9
|
+
|
|
10
|
+
function _utils() {
|
|
11
|
+
const data = require("@nocobase/utils");
|
|
12
|
+
|
|
13
|
+
_utils = function _utils() {
|
|
14
|
+
return data;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
return data;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function _path() {
|
|
21
|
+
const data = require("path");
|
|
22
|
+
|
|
23
|
+
_path = function _path() {
|
|
24
|
+
return data;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
return data;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
var _database = require("./database");
|
|
31
|
+
|
|
32
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
33
|
+
|
|
34
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
35
|
+
|
|
36
|
+
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
37
|
+
|
|
38
|
+
class MockDatabase extends _database.Database {
|
|
39
|
+
constructor(options) {
|
|
40
|
+
super(_objectSpread({
|
|
41
|
+
storage: ':memory:',
|
|
42
|
+
tablePrefix: `mock_${(0, _utils().uid)(6)}_`,
|
|
43
|
+
dialect: 'sqlite'
|
|
44
|
+
}, options));
|
|
45
|
+
this.sequelize.beforeDefine((model, opts) => {
|
|
46
|
+
opts.tableName = `${this.getTablePrefix()}${opts.tableName || opts.modelName || opts.name.plural}`;
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
14
50
|
}
|
|
51
|
+
|
|
15
52
|
exports.MockDatabase = MockDatabase;
|
|
53
|
+
|
|
16
54
|
function getConfigByEnv() {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
};
|
|
55
|
+
return {
|
|
56
|
+
username: process.env.DB_USER,
|
|
57
|
+
password: process.env.DB_PASSWORD,
|
|
58
|
+
database: process.env.DB_DATABASE,
|
|
59
|
+
host: process.env.DB_HOST,
|
|
60
|
+
port: process.env.DB_PORT,
|
|
61
|
+
dialect: process.env.DB_DIALECT,
|
|
62
|
+
logging: process.env.DB_LOGGING === 'on' ? console.log : false,
|
|
63
|
+
storage: process.env.DB_STORAGE && process.env.DB_STORAGE !== ':memory:' ? (0, _path().resolve)(process.cwd(), process.env.DB_STORAGE) : ':memory:',
|
|
64
|
+
define: {
|
|
65
|
+
charset: 'utf8mb4',
|
|
66
|
+
collate: 'utf8mb4_unicode_ci'
|
|
67
|
+
},
|
|
68
|
+
timezone: process.env.DB_TIMEZONE
|
|
69
|
+
};
|
|
33
70
|
}
|
|
34
|
-
|
|
71
|
+
|
|
35
72
|
function mockDatabase(options = {}) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}
|
|
39
|
-
exports.mockDatabase = mockDatabase;
|
|
40
|
-
//# sourceMappingURL=mock-database.js.map
|
|
73
|
+
const dbOptions = (0, _utils().merge)(getConfigByEnv(), options);
|
|
74
|
+
return new MockDatabase(dbOptions);
|
|
75
|
+
}
|
package/lib/model-hook.d.ts
CHANGED
|
@@ -2,11 +2,11 @@ import type { SequelizeHooks } from 'sequelize/types/lib/hooks';
|
|
|
2
2
|
import Database from './database';
|
|
3
3
|
export declare class ModelHook {
|
|
4
4
|
database: Database;
|
|
5
|
-
|
|
5
|
+
boundEvents: Set<string>;
|
|
6
6
|
constructor(database: Database);
|
|
7
|
-
|
|
7
|
+
match(event: string | Symbol): keyof SequelizeHooks | null;
|
|
8
8
|
findModelName(hookArgs: any): any;
|
|
9
|
-
bindEvent(
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
bindEvent(type: any): void;
|
|
10
|
+
hasBoundEvent(type: any): boolean;
|
|
11
|
+
buildSequelizeHook(type: any): (...args: any[]) => Promise<void>;
|
|
12
12
|
}
|
package/lib/model-hook.js
CHANGED
|
@@ -1,68 +1,117 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
-
};
|
|
14
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
15
6
|
exports.ModelHook = void 0;
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
const
|
|
7
|
+
|
|
8
|
+
function _lodash() {
|
|
9
|
+
const data = _interopRequireDefault(require("lodash"));
|
|
10
|
+
|
|
11
|
+
_lodash = function _lodash() {
|
|
12
|
+
return data;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
return data;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
19
|
+
|
|
20
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
21
|
+
|
|
22
|
+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
23
|
+
|
|
24
|
+
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
|
|
25
|
+
|
|
26
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
27
|
+
|
|
28
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
29
|
+
|
|
30
|
+
const _require = require('sequelize/lib/hooks'),
|
|
31
|
+
hooks = _require.hooks;
|
|
32
|
+
|
|
19
33
|
class ModelHook {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
34
|
+
constructor(database) {
|
|
35
|
+
this.database = void 0;
|
|
36
|
+
this.boundEvents = new Set();
|
|
37
|
+
this.database = database;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
match(event) {
|
|
41
|
+
// NOTE: skip Symbol event
|
|
42
|
+
if (!_lodash().default.isString(event)) {
|
|
43
|
+
return null;
|
|
23
44
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
45
|
+
|
|
46
|
+
const type = event.split('.').pop();
|
|
47
|
+
return type in hooks ? type : null;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
findModelName(hookArgs) {
|
|
51
|
+
var _iterator = _createForOfIteratorHelper(hookArgs),
|
|
52
|
+
_step;
|
|
53
|
+
|
|
54
|
+
try {
|
|
55
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
56
|
+
const arg = _step.value;
|
|
57
|
+
|
|
58
|
+
if (arg === null || arg === void 0 ? void 0 : arg._previousDataValues) {
|
|
59
|
+
return arg.constructor.name;
|
|
30
60
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
61
|
+
|
|
62
|
+
if (_lodash().default.isPlainObject(arg)) {
|
|
63
|
+
var _arg$name, _arg$name2;
|
|
64
|
+
|
|
65
|
+
if (arg['model']) {
|
|
66
|
+
return arg['model'].name;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const plural = arg === null || arg === void 0 ? void 0 : (_arg$name = arg.name) === null || _arg$name === void 0 ? void 0 : _arg$name.plural;
|
|
70
|
+
|
|
71
|
+
if (this.database.sequelize.isDefined(plural)) {
|
|
72
|
+
return plural;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const singular = arg === null || arg === void 0 ? void 0 : (_arg$name2 = arg.name) === null || _arg$name2 === void 0 ? void 0 : _arg$name2.singular;
|
|
76
|
+
|
|
77
|
+
if (this.database.sequelize.isDefined(singular)) {
|
|
78
|
+
return singular;
|
|
79
|
+
}
|
|
46
80
|
}
|
|
47
|
-
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
hasBindEvent(eventName) {
|
|
53
|
-
return this.boundEvent.has(eventName);
|
|
54
|
-
}
|
|
55
|
-
sequelizeHookBuilder(eventName) {
|
|
56
|
-
return (...args) => __awaiter(this, void 0, void 0, function* () {
|
|
57
|
-
const modelName = this.findModelName(args);
|
|
58
|
-
if (modelName) {
|
|
59
|
-
// emit model event
|
|
60
|
-
yield this.database.emitAsync(`${modelName}.${eventName}`, ...args);
|
|
61
|
-
}
|
|
62
|
-
// emit sequelize global event
|
|
63
|
-
yield this.database.emitAsync(eventName, ...args);
|
|
64
|
-
});
|
|
81
|
+
}
|
|
82
|
+
} catch (err) {
|
|
83
|
+
_iterator.e(err);
|
|
84
|
+
} finally {
|
|
85
|
+
_iterator.f();
|
|
65
86
|
}
|
|
87
|
+
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
bindEvent(type) {
|
|
92
|
+
this.boundEvents.add(type);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
hasBoundEvent(type) {
|
|
96
|
+
return this.boundEvents.has(type);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
buildSequelizeHook(type) {
|
|
100
|
+
var _this = this;
|
|
101
|
+
|
|
102
|
+
return /*#__PURE__*/_asyncToGenerator(function* (...args) {
|
|
103
|
+
const modelName = _this.findModelName(args);
|
|
104
|
+
|
|
105
|
+
if (modelName) {
|
|
106
|
+
// emit model event
|
|
107
|
+
yield _this.database.emitAsync(`${modelName}.${type}`, ...args);
|
|
108
|
+
} // emit sequelize global event
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
yield _this.database.emitAsync(type, ...args);
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
|
|
66
115
|
}
|
|
67
|
-
|
|
68
|
-
|
|
116
|
+
|
|
117
|
+
exports.ModelHook = ModelHook;
|
package/lib/model.js
CHANGED
|
@@ -1,87 +1,122 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
6
|
exports.Model = void 0;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
7
|
+
|
|
8
|
+
function _sequelize() {
|
|
9
|
+
const data = require("sequelize");
|
|
10
|
+
|
|
11
|
+
_sequelize = function _sequelize() {
|
|
12
|
+
return data;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
return data;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function _lodash() {
|
|
19
|
+
const data = _interopRequireDefault(require("lodash"));
|
|
20
|
+
|
|
21
|
+
_lodash = function _lodash() {
|
|
22
|
+
return data;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
return data;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
29
|
+
|
|
30
|
+
class Model extends _sequelize().Model {
|
|
31
|
+
toJSON() {
|
|
32
|
+
const handleObj = (obj, options) => {
|
|
33
|
+
const handles = [data => {
|
|
34
|
+
if (data instanceof Model) {
|
|
35
|
+
return data.toJSON();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return data;
|
|
39
|
+
}, this.hiddenObjKey];
|
|
40
|
+
return handles.reduce((carry, fn) => fn.apply(this, [carry, options]), obj);
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const handleArray = (arrayOfObj, options) => {
|
|
44
|
+
const handles = [this.sortAssociations];
|
|
45
|
+
return handles.reduce((carry, fn) => fn.apply(this, [carry, options]), arrayOfObj || []);
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const opts = {
|
|
49
|
+
model: this.constructor,
|
|
50
|
+
collection: this.constructor.collection,
|
|
51
|
+
db: this.constructor.database
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const traverseJSON = (data, options) => {
|
|
55
|
+
const model = options.model,
|
|
56
|
+
db = options.db,
|
|
57
|
+
collection = options.collection; // handle Object
|
|
58
|
+
|
|
59
|
+
data = handleObj(data, options);
|
|
60
|
+
const result = {};
|
|
61
|
+
|
|
62
|
+
for (var _i = 0, _Object$keys = Object.keys(data); _i < _Object$keys.length; _i++) {
|
|
63
|
+
const key = _Object$keys[_i];
|
|
64
|
+
|
|
65
|
+
// @ts-ignore
|
|
66
|
+
if (model.hasAlias(key)) {
|
|
67
|
+
const association = model.associations[key];
|
|
68
|
+
const opts = {
|
|
69
|
+
model: association.target,
|
|
70
|
+
collection: db.getCollection(association.target.name),
|
|
71
|
+
db,
|
|
72
|
+
key,
|
|
73
|
+
field: collection.getField(key)
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
if (['HasMany', 'BelongsToMany'].includes(association.associationType)) {
|
|
77
|
+
result[key] = handleArray(data[key], opts).map(item => traverseJSON(item, opts));
|
|
78
|
+
} else {
|
|
79
|
+
result[key] = data[key] ? traverseJSON(data[key], opts) : null;
|
|
80
|
+
}
|
|
81
|
+
} else {
|
|
82
|
+
result[key] = data[key];
|
|
76
83
|
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return result;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
return traverseJSON(super.toJSON(), opts);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
hiddenObjKey(obj, options) {
|
|
93
|
+
const hiddenFields = Array.from(options.collection.fields.values()).filter(field => field.options.hidden).map(field => field.options.name);
|
|
94
|
+
return _lodash().default.omit(obj, hiddenFields);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
sortAssociations(data, {
|
|
98
|
+
field
|
|
99
|
+
}) {
|
|
100
|
+
const sortBy = field.options.sortBy;
|
|
101
|
+
return sortBy ? this.sortArray(data, sortBy) : data;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
sortArray(data, sortBy) {
|
|
105
|
+
if (!_lodash().default.isArray(sortBy)) {
|
|
106
|
+
sortBy = [sortBy];
|
|
84
107
|
}
|
|
108
|
+
|
|
109
|
+
const orderItems = [];
|
|
110
|
+
const orderDirections = [];
|
|
111
|
+
sortBy.forEach(sortItem => {
|
|
112
|
+
orderDirections.push(sortItem.startsWith('-') ? 'desc' : 'asc');
|
|
113
|
+
orderItems.push(sortItem.replace('-', ''));
|
|
114
|
+
});
|
|
115
|
+
return _lodash().default.orderBy(data, orderItems, orderDirections);
|
|
116
|
+
}
|
|
117
|
+
|
|
85
118
|
}
|
|
119
|
+
|
|
86
120
|
exports.Model = Model;
|
|
87
|
-
|
|
121
|
+
Model.database = void 0;
|
|
122
|
+
Model.collection = void 0;
|