@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/fields/uuid-field.js
CHANGED
|
@@ -1,15 +1,39 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
3
6
|
exports.UuidField = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
7
|
+
|
|
8
|
+
function _sequelize() {
|
|
9
|
+
const data = require("sequelize");
|
|
10
|
+
|
|
11
|
+
_sequelize = function _sequelize() {
|
|
12
|
+
return data;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
return data;
|
|
13
16
|
}
|
|
14
|
-
|
|
15
|
-
|
|
17
|
+
|
|
18
|
+
var _field = require("./field");
|
|
19
|
+
|
|
20
|
+
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; }
|
|
21
|
+
|
|
22
|
+
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; }
|
|
23
|
+
|
|
24
|
+
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; }
|
|
25
|
+
|
|
26
|
+
class UuidField extends _field.Field {
|
|
27
|
+
constructor(options, context) {
|
|
28
|
+
super(_objectSpread({
|
|
29
|
+
defaultValue: _sequelize().DataTypes.UUIDV4
|
|
30
|
+
}, options), context);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
get dataType() {
|
|
34
|
+
return _sequelize().DataTypes.UUID;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
exports.UuidField = UuidField;
|
|
@@ -1,12 +1,27 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
3
6
|
exports.VirtualField = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
|
|
8
|
+
function _sequelize() {
|
|
9
|
+
const data = require("sequelize");
|
|
10
|
+
|
|
11
|
+
_sequelize = function _sequelize() {
|
|
12
|
+
return data;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
return data;
|
|
10
16
|
}
|
|
11
|
-
|
|
12
|
-
|
|
17
|
+
|
|
18
|
+
var _field = require("./field");
|
|
19
|
+
|
|
20
|
+
class VirtualField extends _field.Field {
|
|
21
|
+
get dataType() {
|
|
22
|
+
return _sequelize().DataTypes.VIRTUAL;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
exports.VirtualField = VirtualField;
|
package/lib/filter-parser.js
CHANGED
|
@@ -1,191 +1,300 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
function _flat() {
|
|
9
|
+
const data = require("flat");
|
|
10
|
+
|
|
11
|
+
_flat = function _flat() {
|
|
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
|
+
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(_e2) { throw _e2; }, 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(_e3) { didErr = true; err = _e3; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
|
|
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
|
+
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
39
|
+
|
|
40
|
+
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
41
|
+
|
|
42
|
+
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); }
|
|
43
|
+
|
|
44
|
+
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; }
|
|
45
|
+
|
|
46
|
+
function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
|
|
47
|
+
|
|
48
|
+
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
49
|
+
|
|
8
50
|
const debug = require('debug')('noco-database');
|
|
51
|
+
|
|
9
52
|
class FilterParser {
|
|
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
|
-
|
|
53
|
+
constructor(filter, context) {
|
|
54
|
+
this.collection = void 0;
|
|
55
|
+
this.database = void 0;
|
|
56
|
+
this.model = void 0;
|
|
57
|
+
this.filter = void 0;
|
|
58
|
+
this.context = void 0;
|
|
59
|
+
const collection = context.collection;
|
|
60
|
+
this.collection = collection;
|
|
61
|
+
this.context = context;
|
|
62
|
+
this.model = collection.model;
|
|
63
|
+
this.filter = this.prepareFilter(filter);
|
|
64
|
+
this.database = collection.context.database;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
prepareFilter(filter) {
|
|
68
|
+
if (_lodash().default.isPlainObject(filter)) {
|
|
69
|
+
const renamedKey = {};
|
|
70
|
+
|
|
71
|
+
for (var _i = 0, _Object$keys = Object.keys(filter); _i < _Object$keys.length; _i++) {
|
|
72
|
+
const key = _Object$keys[_i];
|
|
73
|
+
|
|
74
|
+
if (key.endsWith('.$exists') || key.endsWith('.$notExists')) {
|
|
75
|
+
const keyArr = key.split('.');
|
|
76
|
+
|
|
77
|
+
if (keyArr[keyArr.length - 2] == 'id') {
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
keyArr.splice(keyArr.length - 1, 0, 'id');
|
|
82
|
+
renamedKey[key] = keyArr.join('.');
|
|
36
83
|
}
|
|
37
|
-
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
for (var _i2 = 0, _Object$entries = Object.entries(renamedKey); _i2 < _Object$entries.length; _i2++) {
|
|
87
|
+
const _Object$entries$_i = _slicedToArray(_Object$entries[_i2], 2),
|
|
88
|
+
oldKey = _Object$entries$_i[0],
|
|
89
|
+
newKey = _Object$entries$_i[1];
|
|
90
|
+
|
|
91
|
+
// @ts-ignore
|
|
92
|
+
filter[newKey] = filter[oldKey];
|
|
93
|
+
delete filter[oldKey];
|
|
94
|
+
}
|
|
38
95
|
}
|
|
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
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
debug('associationKeys %o', associationKeys);
|
|
116
|
-
// set sequelize include option
|
|
117
|
-
lodash_1.default.set(include, firstKey, {
|
|
118
|
-
association: firstKey,
|
|
119
|
-
attributes: [], // out put empty fields by default
|
|
120
|
-
});
|
|
121
|
-
// association target model
|
|
122
|
-
let target = associations[firstKey].target;
|
|
123
|
-
debug('association target %o', target);
|
|
124
|
-
while (target) {
|
|
125
|
-
const attr = keys.shift();
|
|
126
|
-
origins.push(attr);
|
|
127
|
-
// if it is target model attribute
|
|
128
|
-
if (target.rawAttributes[attr]) {
|
|
129
|
-
associationKeys.push(attr);
|
|
130
|
-
target = null;
|
|
131
|
-
}
|
|
132
|
-
else if (target.associations[attr]) {
|
|
133
|
-
// if it is target model association (nested association filter)
|
|
134
|
-
associationKeys.push(attr);
|
|
135
|
-
const assoc = [];
|
|
136
|
-
associationKeys.forEach((associationKey, index) => {
|
|
137
|
-
if (index > 0) {
|
|
138
|
-
assoc.push('include');
|
|
139
|
-
}
|
|
140
|
-
assoc.push(associationKey);
|
|
141
|
-
});
|
|
142
|
-
lodash_1.default.set(include, assoc, {
|
|
143
|
-
association: attr,
|
|
144
|
-
attributes: [],
|
|
145
|
-
});
|
|
146
|
-
target = target.associations[attr].target;
|
|
147
|
-
}
|
|
148
|
-
else {
|
|
149
|
-
throw new Error(`${attr} neither ${firstKey}'s association nor ${firstKey}'s attribute`);
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
debug('associationKeys %o', associationKeys);
|
|
153
|
-
if (associationKeys.length > 1) {
|
|
154
|
-
paths.push(`$${associationKeys.join('.')}$`);
|
|
155
|
-
}
|
|
156
|
-
else {
|
|
157
|
-
paths.push(firstKey);
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
debug('where %o, paths %o, value, %o', where, paths, value);
|
|
161
|
-
const values = lodash_1.default.get(where, paths);
|
|
162
|
-
if (values && typeof values === 'object' && value && typeof value === 'object') {
|
|
163
|
-
value = Object.assign(Object.assign({}, value), values);
|
|
96
|
+
|
|
97
|
+
return filter;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
toSequelizeParams() {
|
|
101
|
+
debug('filter %o', this.filter);
|
|
102
|
+
|
|
103
|
+
if (!this.filter) {
|
|
104
|
+
return {};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const filter = this.filter;
|
|
108
|
+
const model = this.model; // supported operators
|
|
109
|
+
|
|
110
|
+
const operators = this.database.operators;
|
|
111
|
+
|
|
112
|
+
const originalFiler = _lodash().default.cloneDeep(filter || {});
|
|
113
|
+
|
|
114
|
+
const flattenedFilter = (0, _flat().flatten)(filter || {});
|
|
115
|
+
debug('flattened filter %o', flattenedFilter);
|
|
116
|
+
const include = {};
|
|
117
|
+
const where = {};
|
|
118
|
+
|
|
119
|
+
const filter2 = _lodash().default.cloneDeep(flattenedFilter);
|
|
120
|
+
|
|
121
|
+
let skipPrefix = null;
|
|
122
|
+
const associations = model.associations;
|
|
123
|
+
debug('associations %O', associations);
|
|
124
|
+
|
|
125
|
+
for (var _i3 = 0, _Object$entries2 = Object.entries(flattenedFilter); _i3 < _Object$entries2.length; _i3++) {
|
|
126
|
+
let _Object$entries2$_i = _slicedToArray(_Object$entries2[_i3], 2),
|
|
127
|
+
key = _Object$entries2$_i[0],
|
|
128
|
+
value = _Object$entries2$_i[1];
|
|
129
|
+
|
|
130
|
+
// 处理 filter 条件
|
|
131
|
+
if (skipPrefix && key.startsWith(skipPrefix)) {
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
debug('handle filter key "%s: "%s"', key, value);
|
|
136
|
+
let keys = key.split('.'); // paths ?
|
|
137
|
+
|
|
138
|
+
const paths = []; // origins ?
|
|
139
|
+
|
|
140
|
+
const origins = [];
|
|
141
|
+
|
|
142
|
+
while (keys.length) {
|
|
143
|
+
debug('keys: %o, paths: %o, origins: %o', keys, paths, origins); // move key from keys to origins
|
|
144
|
+
|
|
145
|
+
const firstKey = keys.shift();
|
|
146
|
+
origins.push(firstKey);
|
|
147
|
+
debug('origins: %o', origins);
|
|
148
|
+
|
|
149
|
+
if (firstKey.startsWith('$')) {
|
|
150
|
+
if (operators.has(firstKey)) {
|
|
151
|
+
debug('%s is operator', firstKey); // if firstKey is operator
|
|
152
|
+
|
|
153
|
+
const opKey = operators.get(firstKey);
|
|
154
|
+
debug('operator key %s, operator: %o', firstKey, opKey); // 默认操作符
|
|
155
|
+
|
|
156
|
+
if (typeof opKey === 'symbol') {
|
|
157
|
+
paths.push(opKey);
|
|
158
|
+
continue;
|
|
159
|
+
} else if (typeof opKey === 'function') {
|
|
160
|
+
skipPrefix = origins.join('.');
|
|
161
|
+
|
|
162
|
+
const queryValue = _lodash().default.get((0, _flat().unflatten)(originalFiler), skipPrefix);
|
|
163
|
+
|
|
164
|
+
value = opKey(queryValue, {
|
|
165
|
+
app: this.context.app,
|
|
166
|
+
db: this.database,
|
|
167
|
+
path: skipPrefix,
|
|
168
|
+
fieldName: this.getFieldNameFromQueryPath(skipPrefix),
|
|
169
|
+
model: this.model
|
|
170
|
+
});
|
|
171
|
+
break;
|
|
164
172
|
}
|
|
165
|
-
|
|
173
|
+
} else {
|
|
174
|
+
paths.push(firstKey);
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
} // firstKey is number
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
if (!_lodash().default.isNaN(parseInt(firstKey))) {
|
|
181
|
+
paths.push(firstKey);
|
|
182
|
+
continue;
|
|
183
|
+
} // firstKey is not association
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
if (!associations[firstKey]) {
|
|
187
|
+
paths.push(firstKey);
|
|
188
|
+
continue;
|
|
166
189
|
}
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
190
|
+
|
|
191
|
+
const associationKeys = [];
|
|
192
|
+
associationKeys.push(firstKey);
|
|
193
|
+
debug('associationKeys %o', associationKeys); // set sequelize include option
|
|
194
|
+
|
|
195
|
+
_lodash().default.set(include, firstKey, {
|
|
196
|
+
association: firstKey,
|
|
197
|
+
attributes: [] // out put empty fields by default
|
|
198
|
+
|
|
199
|
+
}); // association target model
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
let target = associations[firstKey].target;
|
|
203
|
+
debug('association target %o', target);
|
|
204
|
+
|
|
205
|
+
while (target) {
|
|
206
|
+
const attr = keys.shift();
|
|
207
|
+
origins.push(attr); // if it is target model attribute
|
|
208
|
+
|
|
209
|
+
if (target.rawAttributes[attr]) {
|
|
210
|
+
associationKeys.push(attr);
|
|
211
|
+
target = null;
|
|
212
|
+
} else if (target.associations[attr]) {
|
|
213
|
+
// if it is target model association (nested association filter)
|
|
214
|
+
associationKeys.push(attr);
|
|
215
|
+
const assoc = [];
|
|
216
|
+
associationKeys.forEach((associationKey, index) => {
|
|
217
|
+
if (index > 0) {
|
|
218
|
+
assoc.push('include');
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
assoc.push(associationKey);
|
|
173
222
|
});
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
223
|
+
|
|
224
|
+
_lodash().default.set(include, assoc, {
|
|
225
|
+
association: attr,
|
|
226
|
+
attributes: []
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
target = target.associations[attr].target;
|
|
230
|
+
} else {
|
|
231
|
+
throw new Error(`${attr} neither ${firstKey}'s association nor ${firstKey}'s attribute`);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
debug('associationKeys %o', associationKeys);
|
|
236
|
+
|
|
237
|
+
if (associationKeys.length > 1) {
|
|
238
|
+
paths.push(`$${associationKeys.join('.')}$`);
|
|
239
|
+
} else {
|
|
240
|
+
paths.push(firstKey);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
debug('where %o, paths %o, value, %o', where, paths, value);
|
|
245
|
+
|
|
246
|
+
const values = _lodash().default.get(where, paths);
|
|
247
|
+
|
|
248
|
+
if (values && typeof values === 'object' && value && typeof value === 'object') {
|
|
249
|
+
value = _objectSpread(_objectSpread({}, value), values);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
_lodash().default.set(where, paths, value);
|
|
177
253
|
}
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
254
|
+
|
|
255
|
+
const toInclude = items => {
|
|
256
|
+
return Object.values(items).map(item => {
|
|
257
|
+
if (item.include) {
|
|
258
|
+
item.include = toInclude(item.include);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
return item;
|
|
262
|
+
});
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
debug('where %o, include %o', where, include);
|
|
266
|
+
return {
|
|
267
|
+
where,
|
|
268
|
+
include: toInclude(include)
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
getFieldNameFromQueryPath(queryPath) {
|
|
273
|
+
const paths = queryPath.split('.');
|
|
274
|
+
let fieldName;
|
|
275
|
+
|
|
276
|
+
var _iterator = _createForOfIteratorHelper(paths),
|
|
277
|
+
_step;
|
|
278
|
+
|
|
279
|
+
try {
|
|
280
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
281
|
+
const path = _step.value;
|
|
282
|
+
|
|
283
|
+
if (path.startsWith('$') || !_lodash().default.isNaN(parseInt(path))) {
|
|
284
|
+
continue;
|
|
186
285
|
}
|
|
187
|
-
|
|
286
|
+
|
|
287
|
+
fieldName = path;
|
|
288
|
+
}
|
|
289
|
+
} catch (err) {
|
|
290
|
+
_iterator.e(err);
|
|
291
|
+
} finally {
|
|
292
|
+
_iterator.f();
|
|
188
293
|
}
|
|
294
|
+
|
|
295
|
+
return fieldName;
|
|
296
|
+
}
|
|
297
|
+
|
|
189
298
|
}
|
|
190
|
-
|
|
191
|
-
|
|
299
|
+
|
|
300
|
+
exports.default = FilterParser;
|