@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/options-parser.js
CHANGED
|
@@ -1,232 +1,362 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
6
|
exports.OptionsParser = void 0;
|
|
7
|
-
|
|
8
|
-
|
|
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
|
+
var _filterParser = _interopRequireDefault(require("./filter-parser"));
|
|
19
|
+
|
|
20
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
21
|
+
|
|
22
|
+
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; }
|
|
23
|
+
|
|
24
|
+
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; }
|
|
25
|
+
|
|
26
|
+
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; }
|
|
27
|
+
|
|
28
|
+
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; } } }; }
|
|
29
|
+
|
|
30
|
+
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); }
|
|
31
|
+
|
|
32
|
+
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; }
|
|
33
|
+
|
|
9
34
|
const debug = require('debug')('noco-database');
|
|
35
|
+
|
|
10
36
|
class OptionsParser {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
37
|
+
constructor(options, context) {
|
|
38
|
+
this.options = void 0;
|
|
39
|
+
this.database = void 0;
|
|
40
|
+
this.collection = void 0;
|
|
41
|
+
this.model = void 0;
|
|
42
|
+
this.filterParser = void 0;
|
|
43
|
+
this.context = void 0;
|
|
44
|
+
const collection = context.collection;
|
|
45
|
+
this.collection = collection;
|
|
46
|
+
this.model = collection.model;
|
|
47
|
+
this.options = options;
|
|
48
|
+
this.database = collection.context.database;
|
|
49
|
+
this.filterParser = new _filterParser.default(options === null || options === void 0 ? void 0 : options.filter, {
|
|
50
|
+
collection,
|
|
51
|
+
app: {
|
|
52
|
+
ctx: options === null || options === void 0 ? void 0 : options.context
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
this.context = context;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
isAssociation(key) {
|
|
59
|
+
return this.model.associations[key] !== undefined;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
isAssociationPath(path) {
|
|
63
|
+
return this.isAssociation(path.split('.')[0]);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
toSequelizeParams() {
|
|
67
|
+
var _this$options;
|
|
68
|
+
|
|
69
|
+
const queryParams = this.filterParser.toSequelizeParams();
|
|
70
|
+
|
|
71
|
+
if ((_this$options = this.options) === null || _this$options === void 0 ? void 0 : _this$options.filterByTk) {
|
|
72
|
+
queryParams.where = {
|
|
73
|
+
[_sequelize().Op.and]: [queryParams.where, {
|
|
74
|
+
[this.context.targetKey || this.collection.filterTargetKey]: this.options.filterByTk
|
|
75
|
+
}]
|
|
76
|
+
};
|
|
30
77
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
78
|
+
|
|
79
|
+
return this.parseSort(this.parseFields(queryParams));
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* parser sort options
|
|
83
|
+
* @param filterParams
|
|
84
|
+
* @protected
|
|
85
|
+
*/
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
parseSort(filterParams) {
|
|
89
|
+
var _this$options2;
|
|
90
|
+
|
|
91
|
+
let sort = ((_this$options2 = this.options) === null || _this$options2 === void 0 ? void 0 : _this$options2.sort) || [];
|
|
92
|
+
|
|
93
|
+
if (typeof sort === 'string') {
|
|
94
|
+
sort = sort.split(',');
|
|
45
95
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
96
|
+
|
|
97
|
+
const orderParams = [];
|
|
98
|
+
|
|
99
|
+
var _iterator = _createForOfIteratorHelper(sort),
|
|
100
|
+
_step;
|
|
101
|
+
|
|
102
|
+
try {
|
|
103
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
104
|
+
const sortKey = _step.value;
|
|
105
|
+
let direction = sortKey.startsWith('-') ? 'DESC' : 'ASC';
|
|
106
|
+
let sortField = sortKey.replace('-', '').split('.');
|
|
107
|
+
|
|
108
|
+
if (this.database.inDialect('postgres', 'sqlite')) {
|
|
109
|
+
direction = `${direction} NULLS LAST`;
|
|
110
|
+
} // handle sort by association
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
if (sortField.length > 1) {
|
|
114
|
+
let associationModel = this.model;
|
|
115
|
+
|
|
116
|
+
for (let i = 0; i < sortField.length - 1; i++) {
|
|
117
|
+
const associationKey = sortField[i];
|
|
118
|
+
sortField[i] = associationModel.associations[associationKey].target;
|
|
119
|
+
associationModel = sortField[i];
|
|
120
|
+
}
|
|
56
121
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
let associationModel = this.model;
|
|
63
|
-
for (let i = 0; i < sortField.length - 1; i++) {
|
|
64
|
-
const associationKey = sortField[i];
|
|
65
|
-
sortField[i] = associationModel.associations[associationKey].target;
|
|
66
|
-
associationModel = sortField[i];
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
sortField.push(direction);
|
|
70
|
-
return sortField;
|
|
71
|
-
});
|
|
72
|
-
if (orderParams.length > 0) {
|
|
73
|
-
return Object.assign({ order: orderParams }, filterParams);
|
|
122
|
+
|
|
123
|
+
sortField.push(direction);
|
|
124
|
+
|
|
125
|
+
if (this.database.inDialect('mysql')) {
|
|
126
|
+
orderParams.push([_sequelize().Sequelize.fn('ISNULL', _sequelize().Sequelize.col(`${this.model.name}.${sortField[0]}`))]);
|
|
74
127
|
}
|
|
75
|
-
|
|
128
|
+
|
|
129
|
+
orderParams.push(sortField);
|
|
130
|
+
}
|
|
131
|
+
} catch (err) {
|
|
132
|
+
_iterator.e(err);
|
|
133
|
+
} finally {
|
|
134
|
+
_iterator.f();
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (orderParams.length > 0) {
|
|
138
|
+
return _objectSpread({
|
|
139
|
+
order: orderParams
|
|
140
|
+
}, filterParams);
|
|
76
141
|
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
142
|
+
|
|
143
|
+
return filterParams;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
parseFields(filterParams) {
|
|
147
|
+
var _this$options3, _this$options4, _this$options5;
|
|
148
|
+
|
|
149
|
+
const appends = ((_this$options3 = this.options) === null || _this$options3 === void 0 ? void 0 : _this$options3.appends) || [];
|
|
150
|
+
const except = [];
|
|
151
|
+
let attributes = {
|
|
152
|
+
include: [],
|
|
153
|
+
exclude: []
|
|
154
|
+
}; // out put all fields by default
|
|
155
|
+
|
|
156
|
+
if ((_this$options4 = this.options) === null || _this$options4 === void 0 ? void 0 : _this$options4.fields) {
|
|
157
|
+
// 将fields拆分为 attributes 和 appends
|
|
158
|
+
var _iterator2 = _createForOfIteratorHelper(this.options.fields),
|
|
159
|
+
_step2;
|
|
160
|
+
|
|
161
|
+
try {
|
|
162
|
+
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
163
|
+
const field = _step2.value;
|
|
164
|
+
|
|
165
|
+
if (this.isAssociationPath(field)) {
|
|
166
|
+
// field is association field
|
|
167
|
+
appends.push(field);
|
|
168
|
+
} else {
|
|
169
|
+
// field is model attribute, change attributes to array type
|
|
170
|
+
if (!Array.isArray(attributes)) attributes = [];
|
|
171
|
+
attributes.push(field);
|
|
172
|
+
}
|
|
99
173
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
174
|
+
} catch (err) {
|
|
175
|
+
_iterator2.e(err);
|
|
176
|
+
} finally {
|
|
177
|
+
_iterator2.f();
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if ((_this$options5 = this.options) === null || _this$options5 === void 0 ? void 0 : _this$options5.except) {
|
|
182
|
+
var _iterator3 = _createForOfIteratorHelper(this.options.except),
|
|
183
|
+
_step3;
|
|
184
|
+
|
|
185
|
+
try {
|
|
186
|
+
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
|
|
187
|
+
const exceptKey = _step3.value;
|
|
188
|
+
|
|
189
|
+
if (this.isAssociationPath(exceptKey)) {
|
|
190
|
+
// except association field
|
|
191
|
+
except.push(exceptKey);
|
|
192
|
+
} else {
|
|
193
|
+
// if attributes is array form, ignore except
|
|
194
|
+
if (Array.isArray(attributes)) continue;
|
|
195
|
+
attributes.exclude.push(exceptKey);
|
|
196
|
+
}
|
|
113
197
|
}
|
|
114
|
-
|
|
198
|
+
} catch (err) {
|
|
199
|
+
_iterator3.e(err);
|
|
200
|
+
} finally {
|
|
201
|
+
_iterator3.f();
|
|
202
|
+
}
|
|
115
203
|
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
204
|
+
|
|
205
|
+
return _objectSpread({
|
|
206
|
+
attributes
|
|
207
|
+
}, this.parseExcept(except, this.parseAppends(appends, filterParams)));
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
parseExcept(except, filterParams) {
|
|
211
|
+
if (!except) return filterParams;
|
|
212
|
+
|
|
213
|
+
const setExcept = (queryParams, except) => {
|
|
214
|
+
// split exceptKey to path form
|
|
215
|
+
// posts.comments.content => ['posts', 'comments', 'content']
|
|
216
|
+
// then set except on include attributes
|
|
217
|
+
const exceptPath = except.split('.');
|
|
218
|
+
const association = exceptPath[0];
|
|
219
|
+
const lastLevel = exceptPath.length <= 2;
|
|
220
|
+
let existIncludeIndex = queryParams['include'].findIndex(include => include['association'] == association);
|
|
221
|
+
|
|
222
|
+
if (existIncludeIndex == -1) {
|
|
223
|
+
// if include not exists, ignore this except
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
if (lastLevel) {
|
|
228
|
+
// if it not have exclude form
|
|
229
|
+
if (Array.isArray(queryParams['include'][existIncludeIndex]['attributes'])) {
|
|
230
|
+
return;
|
|
231
|
+
} else {
|
|
232
|
+
if (!queryParams['include'][existIncludeIndex]['attributes']['exclude']) {
|
|
233
|
+
queryParams['include'][existIncludeIndex]['attributes']['exclude'] = [];
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
queryParams['include'][existIncludeIndex]['attributes']['exclude'].push(exceptPath[1]);
|
|
149
237
|
}
|
|
150
|
-
|
|
238
|
+
} else {
|
|
239
|
+
setExcept(queryParams['include'][existIncludeIndex], exceptPath.filter((_, index) => index !== 0).join('.'));
|
|
240
|
+
}
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
var _iterator4 = _createForOfIteratorHelper(except),
|
|
244
|
+
_step4;
|
|
245
|
+
|
|
246
|
+
try {
|
|
247
|
+
for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
|
|
248
|
+
const exceptKey = _step4.value;
|
|
249
|
+
setExcept(filterParams, exceptKey);
|
|
250
|
+
}
|
|
251
|
+
} catch (err) {
|
|
252
|
+
_iterator4.e(err);
|
|
253
|
+
} finally {
|
|
254
|
+
_iterator4.f();
|
|
151
255
|
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
// if association not exist, create it
|
|
185
|
-
if (existIncludeIndex == -1) {
|
|
186
|
-
// association not exists
|
|
187
|
-
queryParams['include'].push({
|
|
188
|
-
association: appendAssociation,
|
|
189
|
-
});
|
|
190
|
-
existIncludeIndex = 0;
|
|
191
|
-
}
|
|
192
|
-
// end appends
|
|
193
|
-
// without nests association
|
|
194
|
-
if (lastLevel) {
|
|
195
|
-
// get exist association attributes
|
|
196
|
-
let attributes = queryParams['include'][existIncludeIndex]['attributes'] || {
|
|
197
|
-
include: [], // all fields are output by default
|
|
198
|
-
};
|
|
199
|
-
// if need set attribute
|
|
200
|
-
if (appendFields.length == 2) {
|
|
201
|
-
if (!Array.isArray(attributes)) {
|
|
202
|
-
attributes = [];
|
|
203
|
-
}
|
|
204
|
-
const attributeName = appendFields[1];
|
|
205
|
-
// push field to it
|
|
206
|
-
attributes.push(attributeName);
|
|
207
|
-
}
|
|
208
|
-
else {
|
|
209
|
-
// if attributes is empty array, change it to object
|
|
210
|
-
if (Array.isArray(attributes) && attributes.length == 0) {
|
|
211
|
-
attributes = {
|
|
212
|
-
include: [],
|
|
213
|
-
};
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
// set new attributes
|
|
217
|
-
queryParams['include'][existIncludeIndex] = Object.assign(Object.assign({}, queryParams['include'][existIncludeIndex]), { attributes });
|
|
218
|
-
}
|
|
219
|
-
else {
|
|
220
|
-
setInclude(model.associations[queryParams['include'][existIncludeIndex].association].target, queryParams['include'][existIncludeIndex], appendFields.filter((_, index) => index !== 0).join('.'));
|
|
221
|
-
}
|
|
222
|
-
};
|
|
223
|
-
// handle every appends
|
|
224
|
-
for (const append of appends) {
|
|
225
|
-
setInclude(this.model, filterParams, append);
|
|
256
|
+
|
|
257
|
+
return filterParams;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
parseAppends(appends, filterParams) {
|
|
261
|
+
if (!appends) return filterParams;
|
|
262
|
+
/**
|
|
263
|
+
* set include params
|
|
264
|
+
* @param includeRoot
|
|
265
|
+
* @param appends
|
|
266
|
+
*/
|
|
267
|
+
|
|
268
|
+
const setInclude = (model, queryParams, append) => {
|
|
269
|
+
const appendFields = append.split('.');
|
|
270
|
+
const appendAssociation = appendFields[0];
|
|
271
|
+
const associations = model.associations; // if append length less or equal 2
|
|
272
|
+
// example:
|
|
273
|
+
// appends: ['posts']
|
|
274
|
+
// appends: ['posts.title']
|
|
275
|
+
// All of these can be seen as last level
|
|
276
|
+
|
|
277
|
+
let lastLevel = false;
|
|
278
|
+
|
|
279
|
+
if (appendFields.length == 1) {
|
|
280
|
+
lastLevel = true;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
if (appendFields.length == 2) {
|
|
284
|
+
const associationModel = associations[appendFields[0]].target;
|
|
285
|
+
|
|
286
|
+
if (associationModel.rawAttributes[appendFields[1]]) {
|
|
287
|
+
lastLevel = true;
|
|
226
288
|
}
|
|
227
|
-
|
|
228
|
-
|
|
289
|
+
} // find association index
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
if (queryParams['include'] == undefined) {
|
|
293
|
+
queryParams['include'] = [];
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
let existIncludeIndex = queryParams['include'].findIndex(include => include['association'] == appendAssociation); // if association not exist, create it
|
|
297
|
+
|
|
298
|
+
if (existIncludeIndex == -1) {
|
|
299
|
+
// association not exists
|
|
300
|
+
queryParams['include'].push({
|
|
301
|
+
association: appendAssociation
|
|
302
|
+
});
|
|
303
|
+
existIncludeIndex = queryParams['include'].length - 1;
|
|
304
|
+
} // end appends
|
|
305
|
+
// without nests association
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
if (lastLevel) {
|
|
309
|
+
// get exist association attributes
|
|
310
|
+
let attributes = queryParams['include'][existIncludeIndex]['attributes'] || {
|
|
311
|
+
include: [] // all fields are output by default
|
|
312
|
+
|
|
313
|
+
}; // if need set attribute
|
|
314
|
+
|
|
315
|
+
if (appendFields.length == 2) {
|
|
316
|
+
if (!Array.isArray(attributes)) {
|
|
317
|
+
attributes = [];
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
const attributeName = appendFields[1]; // push field to it
|
|
321
|
+
|
|
322
|
+
attributes.push(attributeName);
|
|
323
|
+
} else {
|
|
324
|
+
// if attributes is empty array, change it to object
|
|
325
|
+
if (Array.isArray(attributes) && attributes.length == 0) {
|
|
326
|
+
attributes = {
|
|
327
|
+
include: []
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
} // set new attributes
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
queryParams['include'][existIncludeIndex] = _objectSpread(_objectSpread({}, queryParams['include'][existIncludeIndex]), {}, {
|
|
334
|
+
attributes
|
|
335
|
+
});
|
|
336
|
+
} else {
|
|
337
|
+
setInclude(model.associations[queryParams['include'][existIncludeIndex].association].target, queryParams['include'][existIncludeIndex], appendFields.filter((_, index) => index !== 0).join('.'));
|
|
338
|
+
}
|
|
339
|
+
}; // handle every appends
|
|
340
|
+
|
|
341
|
+
|
|
342
|
+
var _iterator5 = _createForOfIteratorHelper(appends),
|
|
343
|
+
_step5;
|
|
344
|
+
|
|
345
|
+
try {
|
|
346
|
+
for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
|
|
347
|
+
const append = _step5.value;
|
|
348
|
+
setInclude(this.model, filterParams, append);
|
|
349
|
+
}
|
|
350
|
+
} catch (err) {
|
|
351
|
+
_iterator5.e(err);
|
|
352
|
+
} finally {
|
|
353
|
+
_iterator5.f();
|
|
229
354
|
}
|
|
355
|
+
|
|
356
|
+
debug('filter params: %o', filterParams);
|
|
357
|
+
return filterParams;
|
|
358
|
+
}
|
|
359
|
+
|
|
230
360
|
}
|
|
231
|
-
|
|
232
|
-
|
|
361
|
+
|
|
362
|
+
exports.OptionsParser = OptionsParser;
|