@nocobase/database 0.7.0-alpha.34 → 0.7.0-alpha.57
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 +101 -67
- package/lib/collection.js +339 -210
- package/lib/database.js +488 -304
- 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.js +114 -75
- 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.js +277 -33
- package/lib/fields/json-field.js +36 -16
- package/lib/fields/number-field.js +53 -26
- package/lib/fields/password-field.js +118 -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.js +36 -12
- package/lib/fields/virtual-field.js +24 -9
- package/lib/filter-parser.js +288 -179
- package/lib/index.js +210 -29
- package/lib/magic-attribute-model.js +123 -71
- package/lib/mock-database.js +68 -34
- package/lib/model-hook.js +101 -59
- package/lib/model.js +116 -81
- package/lib/operators/array.js +136 -96
- package/lib/operators/association.js +30 -14
- 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/lib/operators/notIn.js +27 -12
- package/lib/operators/string.js +56 -35
- package/lib/operators/utils.js +18 -10
- package/lib/options-parser.js +323 -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.js +263 -148
- package/lib/relation-repository/relation-repository.js +155 -97
- package/lib/relation-repository/single-relation-repository.js +138 -99
- package/lib/relation-repository/types.js +4 -2
- package/lib/repository.js +465 -291
- package/lib/transaction-decorator.js +80 -67
- package/lib/update-associations.js +525 -321
- package/lib/update-guard.js +160 -117
- package/package.json +3 -8
- package/src/database.ts +9 -3
- package/src/mock-database.ts +1 -1
- 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 -104
- package/esm/database.js +0 -305
- 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 -42
- package/esm/fields/index.js +0 -22
- 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/uuid-field.d.ts +0 -9
- package/esm/fields/uuid-field.js +0 -11
- package/esm/fields/uuid-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 -60
- 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.d.ts +0 -10
- package/esm/operators/ne.js +0 -12
- package/esm/operators/ne.js.map +0 -1
- package/esm/operators/notIn.d.ts +0 -10
- package/esm/operators/notIn.js +0 -12
- package/esm/operators/notIn.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 -95
- 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/uuid-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/notIn.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/collection.js
CHANGED
|
@@ -1,231 +1,360 @@
|
|
|
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 __rest = (this && this.__rest) || function (s, e) {
|
|
12
|
-
var t = {};
|
|
13
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
14
|
-
t[p] = s[p];
|
|
15
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
16
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
17
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
18
|
-
t[p[i]] = s[p[i]];
|
|
19
|
-
}
|
|
20
|
-
return t;
|
|
21
|
-
};
|
|
22
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
23
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
24
|
-
};
|
|
25
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
26
6
|
exports.Collection = void 0;
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
7
|
+
|
|
8
|
+
function _deepmerge() {
|
|
9
|
+
const data = _interopRequireDefault(require("deepmerge"));
|
|
10
|
+
|
|
11
|
+
_deepmerge = function _deepmerge() {
|
|
12
|
+
return data;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
return data;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function _events() {
|
|
19
|
+
const data = require("events");
|
|
20
|
+
|
|
21
|
+
_events = function _events() {
|
|
22
|
+
return data;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
return data;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function _lodash() {
|
|
29
|
+
const data = _interopRequireDefault(require("lodash"));
|
|
30
|
+
|
|
31
|
+
_lodash = function _lodash() {
|
|
32
|
+
return data;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
return data;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
var _model = require("./model");
|
|
39
|
+
|
|
40
|
+
var _repository = require("./repository");
|
|
41
|
+
|
|
42
|
+
const _excluded = ["name"],
|
|
43
|
+
_excluded2 = ["name"];
|
|
44
|
+
|
|
45
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
46
|
+
|
|
47
|
+
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); } }
|
|
48
|
+
|
|
49
|
+
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); }); }; }
|
|
50
|
+
|
|
51
|
+
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
|
|
52
|
+
|
|
53
|
+
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
|
|
54
|
+
|
|
55
|
+
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; } } }; }
|
|
56
|
+
|
|
57
|
+
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); }
|
|
58
|
+
|
|
59
|
+
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; }
|
|
60
|
+
|
|
61
|
+
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; }
|
|
62
|
+
|
|
63
|
+
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; }
|
|
64
|
+
|
|
65
|
+
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; }
|
|
66
|
+
|
|
67
|
+
class Collection extends _events().EventEmitter {
|
|
68
|
+
get filterTargetKey() {
|
|
69
|
+
return _lodash().default.get(this.options, 'filterTargetKey', this.model.primaryKeyAttribute);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
get name() {
|
|
73
|
+
return this.options.name;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
constructor(options, context) {
|
|
77
|
+
super();
|
|
78
|
+
this.options = void 0;
|
|
79
|
+
this.context = void 0;
|
|
80
|
+
this.isThrough = void 0;
|
|
81
|
+
this.fields = new Map();
|
|
82
|
+
this.model = void 0;
|
|
83
|
+
this.repository = void 0;
|
|
84
|
+
this.context = context;
|
|
85
|
+
this.options = options;
|
|
86
|
+
this.bindFieldEventListener();
|
|
87
|
+
this.modelInit();
|
|
88
|
+
this.setFields(options.fields);
|
|
89
|
+
this.setRepository(options.repository);
|
|
90
|
+
this.setSortable(options.sortable);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
sequelizeModelOptions() {
|
|
94
|
+
const _this$options = this.options,
|
|
95
|
+
name = _this$options.name,
|
|
96
|
+
tableName = _this$options.tableName;
|
|
97
|
+
return _objectSpread(_objectSpread({}, _lodash().default.omit(this.options, ['name', 'fields', 'model', 'targetKey'])), {}, {
|
|
98
|
+
modelName: name,
|
|
99
|
+
sequelize: this.context.database.sequelize,
|
|
100
|
+
tableName: tableName || name
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* TODO
|
|
105
|
+
*/
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
modelInit() {
|
|
109
|
+
if (this.model) {
|
|
110
|
+
return;
|
|
53
111
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
// @ts-ignore
|
|
67
|
-
this.model = m;
|
|
68
|
-
// @ts-ignore
|
|
69
|
-
this.model.database = this.context.database;
|
|
70
|
-
// @ts-ignore
|
|
71
|
-
this.model.collection = this;
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
if (typeof model === 'string') {
|
|
76
|
-
M = this.context.database.models.get(model) || model_1.Model;
|
|
77
|
-
}
|
|
78
|
-
else if (model) {
|
|
79
|
-
M = model;
|
|
80
|
-
}
|
|
81
|
-
// @ts-ignore
|
|
82
|
-
this.model = class extends M {
|
|
83
|
-
};
|
|
84
|
-
this.model.init(null, this.sequelizeModelOptions());
|
|
85
|
-
if (!autoGenId) {
|
|
86
|
-
this.model.removeAttribute('id');
|
|
87
|
-
}
|
|
88
|
-
// @ts-ignore
|
|
89
|
-
this.model.database = this.context.database;
|
|
112
|
+
|
|
113
|
+
const _this$options2 = this.options,
|
|
114
|
+
name = _this$options2.name,
|
|
115
|
+
model = _this$options2.model,
|
|
116
|
+
_this$options2$autoGe = _this$options2.autoGenId,
|
|
117
|
+
autoGenId = _this$options2$autoGe === void 0 ? true : _this$options2$autoGe;
|
|
118
|
+
let M = _model.Model;
|
|
119
|
+
|
|
120
|
+
if (this.context.database.sequelize.isDefined(name)) {
|
|
121
|
+
const m = this.context.database.sequelize.model(name);
|
|
122
|
+
|
|
123
|
+
if (m.isThrough) {
|
|
90
124
|
// @ts-ignore
|
|
125
|
+
this.model = m; // @ts-ignore
|
|
126
|
+
|
|
127
|
+
this.model.database = this.context.database; // @ts-ignore
|
|
128
|
+
|
|
91
129
|
this.model.collection = this;
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
92
132
|
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
133
|
+
|
|
134
|
+
if (typeof model === 'string') {
|
|
135
|
+
M = this.context.database.models.get(model) || _model.Model;
|
|
136
|
+
} else if (model) {
|
|
137
|
+
M = model;
|
|
138
|
+
} // @ts-ignore
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
this.model = class extends M {};
|
|
142
|
+
this.model.init(null, this.sequelizeModelOptions());
|
|
143
|
+
|
|
144
|
+
if (!autoGenId) {
|
|
145
|
+
this.model.removeAttribute('id');
|
|
146
|
+
} // @ts-ignore
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
this.model.database = this.context.database; // @ts-ignore
|
|
150
|
+
|
|
151
|
+
this.model.collection = this;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
setRepository(repository) {
|
|
155
|
+
let repo = _repository.Repository;
|
|
156
|
+
|
|
157
|
+
if (typeof repository === 'string') {
|
|
158
|
+
repo = this.context.database.repositories.get(repository) || _repository.Repository;
|
|
99
159
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
160
|
+
|
|
161
|
+
this.repository = new repo(this);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
bindFieldEventListener() {
|
|
165
|
+
this.on('field.afterAdd', field => {
|
|
166
|
+
field.bind();
|
|
167
|
+
});
|
|
168
|
+
this.on('field.afterRemove', field => field.unbind());
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
forEachField(callback) {
|
|
172
|
+
return [...this.fields.values()].forEach(callback);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
findField(callback) {
|
|
176
|
+
return [...this.fields.values()].find(callback);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
hasField(name) {
|
|
180
|
+
return this.fields.has(name);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
getField(name) {
|
|
184
|
+
return this.fields.get(name);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
addField(name, options) {
|
|
188
|
+
return this.setField(name, options);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
setField(name, options) {
|
|
192
|
+
const database = this.context.database;
|
|
193
|
+
const field = database.buildField(_objectSpread({
|
|
194
|
+
name
|
|
195
|
+
}, options), _objectSpread(_objectSpread({}, this.context), {}, {
|
|
196
|
+
collection: this
|
|
197
|
+
}));
|
|
198
|
+
this.removeField(name);
|
|
199
|
+
this.fields.set(name, field);
|
|
200
|
+
this.emit('field.afterAdd', field);
|
|
201
|
+
return field;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
setFields(fields, resetFields = true) {
|
|
205
|
+
if (!Array.isArray(fields)) {
|
|
206
|
+
return;
|
|
105
207
|
}
|
|
106
|
-
|
|
107
|
-
|
|
208
|
+
|
|
209
|
+
if (resetFields) {
|
|
210
|
+
this.resetFields();
|
|
108
211
|
}
|
|
109
|
-
|
|
110
|
-
|
|
212
|
+
|
|
213
|
+
var _iterator = _createForOfIteratorHelper(fields),
|
|
214
|
+
_step;
|
|
215
|
+
|
|
216
|
+
try {
|
|
217
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
218
|
+
const _ref = _step.value;
|
|
219
|
+
|
|
220
|
+
const name = _ref.name,
|
|
221
|
+
options = _objectWithoutProperties(_ref, _excluded);
|
|
222
|
+
|
|
223
|
+
this.addField(name, options);
|
|
224
|
+
}
|
|
225
|
+
} catch (err) {
|
|
226
|
+
_iterator.e(err);
|
|
227
|
+
} finally {
|
|
228
|
+
_iterator.f();
|
|
111
229
|
}
|
|
112
|
-
|
|
113
|
-
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
resetFields() {
|
|
233
|
+
const fieldNames = this.fields.keys();
|
|
234
|
+
|
|
235
|
+
var _iterator2 = _createForOfIteratorHelper(fieldNames),
|
|
236
|
+
_step2;
|
|
237
|
+
|
|
238
|
+
try {
|
|
239
|
+
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
240
|
+
const fieldName = _step2.value;
|
|
241
|
+
this.removeField(fieldName);
|
|
242
|
+
}
|
|
243
|
+
} catch (err) {
|
|
244
|
+
_iterator2.e(err);
|
|
245
|
+
} finally {
|
|
246
|
+
_iterator2.f();
|
|
114
247
|
}
|
|
115
|
-
|
|
116
|
-
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
removeField(name) {
|
|
251
|
+
if (!this.fields.has(name)) {
|
|
252
|
+
return;
|
|
117
253
|
}
|
|
118
|
-
|
|
119
|
-
|
|
254
|
+
|
|
255
|
+
const field = this.fields.get(name);
|
|
256
|
+
const bool = this.fields.delete(name);
|
|
257
|
+
|
|
258
|
+
if (bool) {
|
|
259
|
+
this.emit('field.afterRemove', field);
|
|
120
260
|
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
261
|
+
|
|
262
|
+
return bool;
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* TODO
|
|
266
|
+
*/
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
updateOptions(options, mergeOptions) {
|
|
270
|
+
let newOptions = _lodash().default.cloneDeep(options);
|
|
271
|
+
|
|
272
|
+
newOptions = (0, _deepmerge().default)(this.options, newOptions, mergeOptions);
|
|
273
|
+
this.context.database.emit('beforeUpdateCollection', this, newOptions);
|
|
274
|
+
this.setFields(options.fields, false);
|
|
275
|
+
this.setRepository(options.repository);
|
|
276
|
+
this.context.database.emit('afterUpdateCollection', this);
|
|
277
|
+
return this;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
setSortable(sortable) {
|
|
281
|
+
if (!sortable) {
|
|
282
|
+
return;
|
|
128
283
|
}
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
}
|
|
136
|
-
for (let _a of fields) {
|
|
137
|
-
const { name } = _a, options = __rest(_a, ["name"]);
|
|
138
|
-
this.addField(name, options);
|
|
139
|
-
}
|
|
284
|
+
|
|
285
|
+
if (sortable === true) {
|
|
286
|
+
this.setField('sort', {
|
|
287
|
+
type: 'sort',
|
|
288
|
+
hidden: true
|
|
289
|
+
});
|
|
140
290
|
}
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
291
|
+
|
|
292
|
+
if (typeof sortable === 'string') {
|
|
293
|
+
this.setField(sortable, {
|
|
294
|
+
type: 'sort',
|
|
295
|
+
hidden: true
|
|
296
|
+
});
|
|
297
|
+
} else if (typeof sortable === 'object') {
|
|
298
|
+
const name = sortable.name,
|
|
299
|
+
opts = _objectWithoutProperties(sortable, _excluded2);
|
|
300
|
+
|
|
301
|
+
this.setField(name || 'sort', _objectSpread({
|
|
302
|
+
type: 'sort',
|
|
303
|
+
hidden: true
|
|
304
|
+
}, opts));
|
|
146
305
|
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* TODO
|
|
309
|
+
*
|
|
310
|
+
* @param name
|
|
311
|
+
* @param options
|
|
312
|
+
*/
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
updateField(name, options) {
|
|
316
|
+
if (!this.hasField(name)) {
|
|
317
|
+
throw new Error(`field ${name} not exists`);
|
|
157
318
|
}
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
updateOptions(options, mergeOptions) {
|
|
162
|
-
let newOptions = lodash_1.default.cloneDeep(options);
|
|
163
|
-
newOptions = (0, deepmerge_1.default)(this.options, newOptions, mergeOptions);
|
|
164
|
-
this.context.database.emit('beforeUpdateCollection', this, newOptions);
|
|
165
|
-
this.setFields(options.fields, false);
|
|
166
|
-
this.setRepository(options.repository);
|
|
167
|
-
this.context.database.emit('afterUpdateCollection', this);
|
|
168
|
-
return this;
|
|
319
|
+
|
|
320
|
+
if (options.name && options.name !== name) {
|
|
321
|
+
this.removeField(name);
|
|
169
322
|
}
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
323
|
+
|
|
324
|
+
this.setField(options.name || name, options);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
sync(syncOptions) {
|
|
328
|
+
var _this = this;
|
|
329
|
+
|
|
330
|
+
return _asyncToGenerator(function* () {
|
|
331
|
+
const modelNames = [_this.model.name];
|
|
332
|
+
const associations = _this.model.associations;
|
|
333
|
+
|
|
334
|
+
for (const associationKey in associations) {
|
|
335
|
+
const association = associations[associationKey];
|
|
336
|
+
modelNames.push(association.target.name);
|
|
337
|
+
|
|
338
|
+
if (association.through) {
|
|
339
|
+
modelNames.push(association.through.model.name);
|
|
173
340
|
}
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
const models = []; // @ts-ignore
|
|
344
|
+
|
|
345
|
+
_this.context.database.sequelize.modelManager.forEachModel(model => {
|
|
346
|
+
if (modelNames.includes(model.name)) {
|
|
347
|
+
models.push(model);
|
|
179
348
|
}
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
/**
|
|
192
|
-
* TODO
|
|
193
|
-
*
|
|
194
|
-
* @param name
|
|
195
|
-
* @param options
|
|
196
|
-
*/
|
|
197
|
-
updateField(name, options) {
|
|
198
|
-
if (!this.hasField(name)) {
|
|
199
|
-
throw new Error(`field ${name} not exists`);
|
|
200
|
-
}
|
|
201
|
-
if (options.name && options.name !== name) {
|
|
202
|
-
this.removeField(name);
|
|
203
|
-
}
|
|
204
|
-
this.setField(options.name || name, options);
|
|
205
|
-
}
|
|
206
|
-
sync(syncOptions) {
|
|
207
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
208
|
-
const modelNames = [this.model.name];
|
|
209
|
-
const associations = this.model.associations;
|
|
210
|
-
for (const associationKey in associations) {
|
|
211
|
-
const association = associations[associationKey];
|
|
212
|
-
modelNames.push(association.target.name);
|
|
213
|
-
if (association.through) {
|
|
214
|
-
modelNames.push(association.through.model.name);
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
const models = [];
|
|
218
|
-
// @ts-ignore
|
|
219
|
-
this.context.database.sequelize.modelManager.forEachModel((model) => {
|
|
220
|
-
if (modelNames.includes(model.name)) {
|
|
221
|
-
models.push(model);
|
|
222
|
-
}
|
|
223
|
-
});
|
|
224
|
-
for (const model of models) {
|
|
225
|
-
yield model.sync(syncOptions);
|
|
226
|
-
}
|
|
227
|
-
});
|
|
228
|
-
}
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
for (var _i = 0, _models = models; _i < _models.length; _i++) {
|
|
352
|
+
const model = _models[_i];
|
|
353
|
+
yield model.sync(syncOptions);
|
|
354
|
+
}
|
|
355
|
+
})();
|
|
356
|
+
}
|
|
357
|
+
|
|
229
358
|
}
|
|
230
|
-
|
|
231
|
-
|
|
359
|
+
|
|
360
|
+
exports.Collection = Collection;
|