@nocobase/database 0.9.1-alpha.1 → 0.9.2-alpha.1
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-group-manager.d.ts +13 -0
- package/lib/collection-group-manager.js +91 -0
- package/lib/collection-importer.js +0 -24
- package/lib/collection.d.ts +26 -4
- package/lib/collection.js +190 -234
- package/lib/database-utils/index.js +3 -15
- package/lib/database.d.ts +9 -0
- package/lib/database.js +171 -299
- package/lib/decorators/must-have-filter-decorator.js +0 -7
- package/lib/decorators/transaction-decorator.js +5 -18
- package/lib/errors/identifier-error.js +0 -3
- package/lib/features/ReferencesMap.js +1 -14
- package/lib/features/referential-integrity-check.js +7 -21
- package/lib/field-repository/array-field-repository.js +5 -45
- package/lib/fields/array-field.js +0 -13
- package/lib/fields/belongs-to-field.js +24 -50
- package/lib/fields/belongs-to-many-field.js +29 -47
- package/lib/fields/boolean-field.js +0 -7
- package/lib/fields/context-field.js +2 -23
- package/lib/fields/date-field.d.ts +4 -0
- package/lib/fields/date-field.js +15 -7
- package/lib/fields/field.js +32 -85
- package/lib/fields/has-many-field.js +16 -49
- package/lib/fields/has-one-field.js +18 -52
- package/lib/fields/index.js +0 -44
- package/lib/fields/json-field.js +0 -12
- package/lib/fields/number-field.js +0 -23
- package/lib/fields/password-field.js +8 -35
- package/lib/fields/radio-field.js +0 -18
- package/lib/fields/relation-field.js +4 -16
- package/lib/fields/set-field.js +0 -8
- package/lib/fields/sort-field.js +84 -73
- package/lib/fields/string-field.js +0 -7
- package/lib/fields/text-field.js +0 -7
- package/lib/fields/time-field.js +0 -7
- package/lib/fields/uid-field.js +4 -22
- package/lib/fields/uuid-field.js +3 -12
- package/lib/fields/virtual-field.js +0 -7
- package/lib/filter-match.js +7 -22
- package/lib/filter-parser.js +38 -102
- package/lib/index.d.ts +3 -0
- package/lib/index.js +36 -42
- package/lib/inherited-collection.js +15 -62
- package/lib/inherited-map.js +7 -48
- package/lib/listeners/adjacency-list.d.ts +3 -0
- package/lib/listeners/adjacency-list.js +91 -0
- package/lib/listeners/index.d.ts +2 -0
- package/lib/listeners/index.js +12 -0
- package/lib/magic-attribute-model.js +58 -114
- package/lib/migration.js +7 -28
- package/lib/mock-database.d.ts +4 -4
- package/lib/mock-database.js +15 -18
- package/lib/model-hook.js +4 -35
- package/lib/model.js +12 -54
- package/lib/operators/array.js +2 -32
- package/lib/operators/association.js +0 -6
- package/lib/operators/boolean.js +0 -6
- package/lib/operators/child-collection.d.ts +2 -0
- package/lib/operators/child-collection.js +32 -0
- package/lib/operators/date.js +123 -60
- package/lib/operators/empty.js +3 -32
- package/lib/operators/eq.d.ts +2 -0
- package/lib/operators/eq.js +26 -0
- package/lib/operators/index.js +4 -7
- package/lib/operators/ne.js +5 -5
- package/lib/operators/notIn.js +0 -5
- package/lib/operators/string.js +0 -11
- package/lib/operators/utils.js +0 -6
- package/lib/options-parser.d.ts +1 -1
- package/lib/options-parser.js +47 -107
- package/lib/playground.js +0 -4
- package/lib/query-interface/mysql-query-interface.d.ts +18 -0
- package/lib/query-interface/mysql-query-interface.js +88 -0
- package/lib/query-interface/postgres-query-interface.d.ts +14 -0
- package/lib/query-interface/postgres-query-interface.js +99 -0
- package/lib/query-interface/query-interface-builder.d.ts +2 -0
- package/lib/query-interface/query-interface-builder.js +18 -0
- package/lib/query-interface/query-interface.d.ts +21 -0
- package/lib/query-interface/query-interface.js +48 -0
- package/lib/query-interface/sqlite-query-interface.d.ts +17 -0
- package/lib/query-interface/sqlite-query-interface.js +89 -0
- package/lib/relation-repository/belongs-to-many-repository.js +21 -78
- package/lib/relation-repository/belongs-to-repository.js +0 -3
- package/lib/relation-repository/hasmany-repository.js +8 -44
- package/lib/relation-repository/hasone-repository.js +0 -3
- package/lib/relation-repository/multiple-relation-repository.js +16 -68
- package/lib/relation-repository/relation-repository.js +5 -42
- package/lib/relation-repository/single-relation-repository.js +6 -43
- package/lib/repository.d.ts +1 -0
- package/lib/repository.js +36 -182
- package/lib/sql-parser/index.js +10527 -0
- package/lib/sql-parser/sql.pegjs +1297 -0
- package/lib/sync-runner.d.ts +1 -1
- package/lib/sync-runner.js +26 -64
- package/lib/update-associations.js +58 -157
- package/lib/update-guard.js +10 -49
- package/lib/utils.js +16 -54
- package/lib/value-parsers/array-value-parser.js +3 -21
- package/lib/value-parsers/base-value-parser.js +0 -13
- package/lib/value-parsers/boolean-value-parser.js +4 -10
- package/lib/value-parsers/date-value-parser.js +0 -23
- package/lib/value-parsers/index.js +0 -10
- package/lib/value-parsers/json-value-parser.js +0 -7
- package/lib/value-parsers/number-value-parser.js +0 -9
- package/lib/value-parsers/string-value-parser.js +3 -20
- package/lib/value-parsers/to-many-value-parser.js +1 -42
- package/lib/value-parsers/to-one-value-parser.js +0 -14
- package/lib/view/field-type-map.d.ts +47 -0
- package/lib/view/field-type-map.js +56 -0
- package/lib/view/view-inference.d.ts +31 -0
- package/lib/view/view-inference.js +92 -0
- package/lib/view-collection.d.ts +6 -0
- package/lib/view-collection.js +24 -0
- package/package.json +4 -3
- package/src/__tests__/collection.test.ts +44 -0
- package/src/__tests__/fields/date.test.ts +75 -0
- package/src/__tests__/fields/sort-field.test.ts +100 -0
- package/src/__tests__/filter.test.ts +60 -0
- package/src/__tests__/group.test.ts +50 -0
- package/src/__tests__/inhertits/collection-inherits.test.ts +114 -0
- package/src/__tests__/operator/date-operator.test.ts +244 -98
- package/src/__tests__/operator/eq.test.ts +76 -0
- package/src/__tests__/operator/ne.test.ts +19 -1
- package/src/__tests__/relation-repository/belongs-to-many-repository.test.ts +82 -0
- package/src/__tests__/repository/find.test.ts +33 -0
- package/src/__tests__/repository.test.ts +88 -0
- package/src/__tests__/sql-parser.test.ts +13 -0
- package/src/__tests__/tree.test.ts +217 -0
- package/src/__tests__/view/list-view.test.ts +34 -0
- package/src/__tests__/view/view-collection.test.ts +199 -0
- package/src/__tests__/view/view-inference.test.ts +145 -0
- package/src/__tests__/view/view-repository.test.ts +67 -0
- package/src/collection-group-manager.ts +94 -0
- package/src/collection.ts +126 -16
- package/src/database-utils/index.ts +1 -0
- package/src/database.ts +98 -17
- package/src/features/ReferencesMap.ts +3 -2
- package/src/fields/belongs-to-many-field.ts +23 -4
- package/src/fields/date-field.ts +18 -0
- package/src/fields/field.ts +17 -7
- package/src/fields/json-field.ts +1 -0
- package/src/fields/sort-field.ts +90 -29
- package/src/filter-parser.ts +2 -1
- package/src/index.ts +3 -1
- package/src/listeners/adjacency-list.ts +60 -0
- package/src/listeners/index.ts +7 -0
- package/src/mock-database.ts +14 -2
- package/src/model.ts +4 -0
- package/src/operators/child-collection.ts +24 -0
- package/src/operators/date.ts +108 -24
- package/src/operators/eq.ts +14 -0
- package/src/operators/index.ts +2 -0
- package/src/operators/ne.ts +12 -7
- package/src/options-parser.ts +25 -11
- package/src/query-interface/mysql-query-interface.ts +72 -0
- package/src/query-interface/postgres-query-interface.ts +103 -0
- package/src/query-interface/query-interface-builder.ts +14 -0
- package/src/query-interface/query-interface.ts +43 -0
- package/src/query-interface/sqlite-query-interface.ts +79 -0
- package/src/relation-repository/belongs-to-many-repository.ts +20 -1
- package/src/relation-repository/hasmany-repository.ts +5 -3
- package/src/relation-repository/multiple-relation-repository.ts +13 -1
- package/src/relation-repository/single-relation-repository.ts +2 -0
- package/src/repository.ts +6 -13
- package/src/sql-parser/index.js +10698 -0
- package/src/sql-parser/readme.md +2 -0
- package/src/sql-parser/sql.pegjs +1297 -0
- package/src/sync-runner.ts +27 -32
- package/src/update-associations.ts +26 -22
- package/src/utils.ts +4 -3
- package/src/view/field-type-map.ts +56 -0
- package/src/view/view-inference.ts +106 -0
- package/src/view-collection.ts +21 -0
|
@@ -4,55 +4,41 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.PasswordField = void 0;
|
|
7
|
-
|
|
8
7
|
function _crypto() {
|
|
9
8
|
const data = _interopRequireDefault(require("crypto"));
|
|
10
|
-
|
|
11
9
|
_crypto = function _crypto() {
|
|
12
10
|
return data;
|
|
13
11
|
};
|
|
14
|
-
|
|
15
12
|
return data;
|
|
16
13
|
}
|
|
17
|
-
|
|
18
14
|
function _sequelize() {
|
|
19
15
|
const data = require("sequelize");
|
|
20
|
-
|
|
21
16
|
_sequelize = function _sequelize() {
|
|
22
17
|
return data;
|
|
23
18
|
};
|
|
24
|
-
|
|
25
19
|
return data;
|
|
26
20
|
}
|
|
27
|
-
|
|
28
21
|
var _field = require("./field");
|
|
29
|
-
|
|
30
22
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
31
|
-
|
|
32
23
|
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); } }
|
|
33
|
-
|
|
34
24
|
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); }); }; }
|
|
35
|
-
|
|
36
25
|
class PasswordField extends _field.Field {
|
|
37
26
|
get dataType() {
|
|
38
27
|
return _sequelize().DataTypes.STRING;
|
|
39
28
|
}
|
|
40
|
-
|
|
41
29
|
verify(password, hash) {
|
|
42
30
|
var _this = this;
|
|
43
|
-
|
|
44
31
|
return _asyncToGenerator(function* () {
|
|
45
32
|
password = password || '';
|
|
46
33
|
hash = hash || '';
|
|
47
34
|
const _this$options = _this.options,
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
35
|
+
_this$options$length = _this$options.length,
|
|
36
|
+
length = _this$options$length === void 0 ? 64 : _this$options$length,
|
|
37
|
+
_this$options$randomB = _this$options.randomBytesSize,
|
|
38
|
+
randomBytesSize = _this$options$randomB === void 0 ? 8 : _this$options$randomB;
|
|
52
39
|
return new Promise((resolve, reject) => {
|
|
53
40
|
const salt = hash.substring(0, randomBytesSize * 2);
|
|
54
41
|
const key = hash.substring(randomBytesSize * 2);
|
|
55
|
-
|
|
56
42
|
_crypto().default.scrypt(password, salt, length / 2 - randomBytesSize, (err, derivedKey) => {
|
|
57
43
|
if (err) reject(err);
|
|
58
44
|
resolve(key == derivedKey.toString('hex'));
|
|
@@ -60,19 +46,16 @@ class PasswordField extends _field.Field {
|
|
|
60
46
|
});
|
|
61
47
|
})();
|
|
62
48
|
}
|
|
63
|
-
|
|
64
49
|
hash(password) {
|
|
65
50
|
var _this2 = this;
|
|
66
|
-
|
|
67
51
|
return _asyncToGenerator(function* () {
|
|
68
52
|
const _this2$options = _this2.options,
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
53
|
+
_this2$options$length = _this2$options.length,
|
|
54
|
+
length = _this2$options$length === void 0 ? 64 : _this2$options$length,
|
|
55
|
+
_this2$options$random = _this2$options.randomBytesSize,
|
|
56
|
+
randomBytesSize = _this2$options$random === void 0 ? 8 : _this2$options$random;
|
|
73
57
|
return new Promise((resolve, reject) => {
|
|
74
58
|
const salt = _crypto().default.randomBytes(randomBytesSize).toString('hex');
|
|
75
|
-
|
|
76
59
|
_crypto().default.scrypt(password, salt, length / 2 - randomBytesSize, (err, derivedKey) => {
|
|
77
60
|
if (err) reject(err);
|
|
78
61
|
resolve(salt + derivedKey.toString('hex'));
|
|
@@ -80,20 +63,15 @@ class PasswordField extends _field.Field {
|
|
|
80
63
|
});
|
|
81
64
|
})();
|
|
82
65
|
}
|
|
83
|
-
|
|
84
66
|
init() {
|
|
85
67
|
var _this3 = this;
|
|
86
|
-
|
|
87
68
|
const name = this.options.name;
|
|
88
|
-
|
|
89
69
|
this.listener = /*#__PURE__*/function () {
|
|
90
70
|
var _ref = _asyncToGenerator(function* (model) {
|
|
91
71
|
if (!model.changed(name)) {
|
|
92
72
|
return;
|
|
93
73
|
}
|
|
94
|
-
|
|
95
74
|
const value = model.get(name);
|
|
96
|
-
|
|
97
75
|
if (value) {
|
|
98
76
|
const hash = yield _this3.hash(value);
|
|
99
77
|
model.set(name, hash);
|
|
@@ -101,25 +79,20 @@ class PasswordField extends _field.Field {
|
|
|
101
79
|
model.set(name, null);
|
|
102
80
|
}
|
|
103
81
|
});
|
|
104
|
-
|
|
105
82
|
return function (_x) {
|
|
106
83
|
return _ref.apply(this, arguments);
|
|
107
84
|
};
|
|
108
85
|
}();
|
|
109
86
|
}
|
|
110
|
-
|
|
111
87
|
bind() {
|
|
112
88
|
super.bind();
|
|
113
89
|
this.on('beforeCreate', this.listener);
|
|
114
90
|
this.on('beforeUpdate', this.listener);
|
|
115
91
|
}
|
|
116
|
-
|
|
117
92
|
unbind() {
|
|
118
93
|
super.unbind();
|
|
119
94
|
this.off('beforeCreate', this.listener);
|
|
120
95
|
this.off('beforeUpdate', this.listener);
|
|
121
96
|
}
|
|
122
|
-
|
|
123
97
|
}
|
|
124
|
-
|
|
125
98
|
exports.PasswordField = PasswordField;
|
|
@@ -4,45 +4,33 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.RadioField = void 0;
|
|
7
|
-
|
|
8
7
|
function _sequelize() {
|
|
9
8
|
const data = require("sequelize");
|
|
10
|
-
|
|
11
9
|
_sequelize = function _sequelize() {
|
|
12
10
|
return data;
|
|
13
11
|
};
|
|
14
|
-
|
|
15
12
|
return data;
|
|
16
13
|
}
|
|
17
|
-
|
|
18
14
|
var _field = require("./field");
|
|
19
|
-
|
|
20
15
|
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
21
|
-
|
|
22
16
|
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
23
|
-
|
|
24
17
|
/**
|
|
25
18
|
* 暂时只支持全局,不支持批量
|
|
26
19
|
*/
|
|
27
20
|
class RadioField extends _field.Field {
|
|
28
21
|
constructor(...args) {
|
|
29
22
|
var _this;
|
|
30
|
-
|
|
31
23
|
super(...args);
|
|
32
24
|
_this = this;
|
|
33
|
-
|
|
34
25
|
this.listener = /*#__PURE__*/function () {
|
|
35
26
|
var _ref = _asyncToGenerator(function* (model, {
|
|
36
27
|
transaction
|
|
37
28
|
}) {
|
|
38
29
|
const name = _this.options.name;
|
|
39
|
-
|
|
40
30
|
if (!model.changed(name)) {
|
|
41
31
|
return;
|
|
42
32
|
}
|
|
43
|
-
|
|
44
33
|
const value = model.get(name);
|
|
45
|
-
|
|
46
34
|
if (value) {
|
|
47
35
|
const M = _this.collection.model;
|
|
48
36
|
yield M.update({
|
|
@@ -56,29 +44,23 @@ class RadioField extends _field.Field {
|
|
|
56
44
|
});
|
|
57
45
|
}
|
|
58
46
|
});
|
|
59
|
-
|
|
60
47
|
return function (_x, _x2) {
|
|
61
48
|
return _ref.apply(this, arguments);
|
|
62
49
|
};
|
|
63
50
|
}();
|
|
64
51
|
}
|
|
65
|
-
|
|
66
52
|
get dataType() {
|
|
67
53
|
return _sequelize().DataTypes.BOOLEAN;
|
|
68
54
|
}
|
|
69
|
-
|
|
70
55
|
bind() {
|
|
71
56
|
super.bind();
|
|
72
57
|
this.on('beforeCreate', this.listener);
|
|
73
58
|
this.on('beforeUpdate', this.listener);
|
|
74
59
|
}
|
|
75
|
-
|
|
76
60
|
unbind() {
|
|
77
61
|
super.unbind();
|
|
78
62
|
this.off('beforeCreate', this.listener);
|
|
79
63
|
this.off('beforeUpdate', this.listener);
|
|
80
64
|
}
|
|
81
|
-
|
|
82
65
|
}
|
|
83
|
-
|
|
84
66
|
exports.RadioField = RadioField;
|
|
@@ -4,28 +4,23 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.RelationField = void 0;
|
|
7
|
-
|
|
8
7
|
var _field = require("./field");
|
|
9
|
-
|
|
10
8
|
class RelationField extends _field.Field {
|
|
11
9
|
/**
|
|
12
10
|
* target relation name
|
|
13
11
|
*/
|
|
14
12
|
get target() {
|
|
15
13
|
const _this$options = this.options,
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
target = _this$options.target,
|
|
15
|
+
name = _this$options.name;
|
|
18
16
|
return target || name;
|
|
19
17
|
}
|
|
20
|
-
|
|
21
18
|
get foreignKey() {
|
|
22
19
|
return this.options.foreignKey;
|
|
23
20
|
}
|
|
24
|
-
|
|
25
21
|
get sourceKey() {
|
|
26
22
|
return this.options.sourceKey || this.collection.model.primaryKeyAttribute;
|
|
27
23
|
}
|
|
28
|
-
|
|
29
24
|
get targetKey() {
|
|
30
25
|
return this.options.targetKey || this.TargetModel.primaryKeyAttribute;
|
|
31
26
|
}
|
|
@@ -33,28 +28,21 @@ class RelationField extends _field.Field {
|
|
|
33
28
|
* get target model from database by it's name
|
|
34
29
|
* @constructor
|
|
35
30
|
*/
|
|
36
|
-
|
|
37
|
-
|
|
38
31
|
get TargetModel() {
|
|
39
32
|
return this.context.database.sequelize.models[this.target];
|
|
40
33
|
}
|
|
41
|
-
|
|
42
34
|
clearAccessors() {
|
|
43
35
|
const collection = this.context.collection;
|
|
44
36
|
const association = collection.model.associations[this.name];
|
|
45
|
-
|
|
46
37
|
if (!association) {
|
|
47
38
|
return;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
|
|
39
|
+
}
|
|
40
|
+
// @ts-ignore
|
|
51
41
|
const accessors = Object.values(association.accessors);
|
|
52
42
|
accessors.forEach(accessor => {
|
|
53
43
|
// @ts-ignore
|
|
54
44
|
delete collection.model.prototype[accessor];
|
|
55
45
|
});
|
|
56
46
|
}
|
|
57
|
-
|
|
58
47
|
}
|
|
59
|
-
|
|
60
48
|
exports.RelationField = RelationField;
|
package/lib/fields/set-field.js
CHANGED
|
@@ -4,32 +4,24 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.SetField = void 0;
|
|
7
|
-
|
|
8
7
|
var _arrayField = require("./array-field");
|
|
9
|
-
|
|
10
8
|
class SetField extends _arrayField.ArrayField {
|
|
11
9
|
constructor(...args) {
|
|
12
10
|
super(...args);
|
|
13
|
-
|
|
14
11
|
this.beforeSave = model => {
|
|
15
12
|
const oldValue = model.get(this.options.name);
|
|
16
|
-
|
|
17
13
|
if (oldValue) {
|
|
18
14
|
model.set(this.options.name, [...new Set(oldValue)]);
|
|
19
15
|
}
|
|
20
16
|
};
|
|
21
17
|
}
|
|
22
|
-
|
|
23
18
|
bind() {
|
|
24
19
|
super.bind();
|
|
25
20
|
this.on('beforeSave', this.beforeSave);
|
|
26
21
|
}
|
|
27
|
-
|
|
28
22
|
unbind() {
|
|
29
23
|
super.unbind();
|
|
30
24
|
this.off('beforeSave', this.beforeSave);
|
|
31
25
|
}
|
|
32
|
-
|
|
33
26
|
}
|
|
34
|
-
|
|
35
27
|
exports.SetField = SetField;
|
package/lib/fields/sort-field.js
CHANGED
|
@@ -4,85 +4,60 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.SortField = void 0;
|
|
7
|
-
|
|
8
7
|
function _asyncMutex() {
|
|
9
8
|
const data = require("async-mutex");
|
|
10
|
-
|
|
11
9
|
_asyncMutex = function _asyncMutex() {
|
|
12
10
|
return data;
|
|
13
11
|
};
|
|
14
|
-
|
|
15
12
|
return data;
|
|
16
13
|
}
|
|
17
|
-
|
|
18
14
|
function _lodash() {
|
|
19
15
|
const data = require("lodash");
|
|
20
|
-
|
|
21
16
|
_lodash = function _lodash() {
|
|
22
17
|
return data;
|
|
23
18
|
};
|
|
24
|
-
|
|
25
19
|
return data;
|
|
26
20
|
}
|
|
27
|
-
|
|
28
21
|
function _sequelize() {
|
|
29
22
|
const data = require("sequelize");
|
|
30
|
-
|
|
31
23
|
_sequelize = function _sequelize() {
|
|
32
24
|
return data;
|
|
33
25
|
};
|
|
34
|
-
|
|
35
26
|
return data;
|
|
36
27
|
}
|
|
37
|
-
|
|
38
28
|
var _field = require("./field");
|
|
39
|
-
|
|
40
29
|
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; } } }; }
|
|
41
|
-
|
|
42
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); }
|
|
43
|
-
|
|
44
31
|
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
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; }
|
|
47
|
-
|
|
48
33
|
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; }
|
|
49
|
-
|
|
50
|
-
function
|
|
51
|
-
|
|
34
|
+
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
35
|
+
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
|
|
36
|
+
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
|
52
37
|
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); } }
|
|
53
|
-
|
|
54
38
|
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); }); }; }
|
|
55
|
-
|
|
56
39
|
const sortFieldMutex = new (_asyncMutex().Mutex)();
|
|
57
|
-
|
|
58
40
|
class SortField extends _field.Field {
|
|
59
41
|
constructor(...args) {
|
|
60
42
|
var _this;
|
|
61
|
-
|
|
62
43
|
super(...args);
|
|
63
44
|
_this = this;
|
|
64
|
-
|
|
65
45
|
this.setSortValue = /*#__PURE__*/function () {
|
|
66
46
|
var _ref = _asyncToGenerator(function* (instance, options) {
|
|
67
47
|
const _this$options = _this.options,
|
|
68
|
-
|
|
69
|
-
|
|
48
|
+
name = _this$options.name,
|
|
49
|
+
scopeKey = _this$options.scopeKey;
|
|
70
50
|
const model = _this.context.collection.model;
|
|
71
|
-
|
|
72
51
|
if ((0, _lodash().isNumber)(instance.get(name)) && instance._previousDataValues[scopeKey] == instance[scopeKey]) {
|
|
73
52
|
return;
|
|
74
53
|
}
|
|
75
|
-
|
|
76
54
|
const where = {};
|
|
77
|
-
|
|
78
55
|
if (scopeKey) {
|
|
79
56
|
const value = instance.get(scopeKey);
|
|
80
|
-
|
|
81
57
|
if (value !== undefined && value !== null) {
|
|
82
58
|
where[scopeKey] = value;
|
|
83
59
|
}
|
|
84
60
|
}
|
|
85
|
-
|
|
86
61
|
yield sortFieldMutex.runExclusive( /*#__PURE__*/_asyncToGenerator(function* () {
|
|
87
62
|
const max = yield model.max(name, _objectSpread(_objectSpread({}, options), {}, {
|
|
88
63
|
where
|
|
@@ -91,107 +66,143 @@ class SortField extends _field.Field {
|
|
|
91
66
|
instance.set(name, newValue);
|
|
92
67
|
}));
|
|
93
68
|
});
|
|
94
|
-
|
|
95
69
|
return function (_x, _x2) {
|
|
96
70
|
return _ref.apply(this, arguments);
|
|
97
71
|
};
|
|
98
72
|
}();
|
|
99
|
-
|
|
100
73
|
this.onScopeChange = /*#__PURE__*/function () {
|
|
101
74
|
var _ref3 = _asyncToGenerator(function* (instance, options) {
|
|
102
75
|
const scopeKey = _this.options.scopeKey;
|
|
103
|
-
|
|
104
76
|
if (scopeKey && !instance.isNewRecord && instance._previousDataValues[scopeKey] != instance[scopeKey]) {
|
|
105
77
|
yield _this.setSortValue(instance, options);
|
|
106
78
|
}
|
|
107
79
|
});
|
|
108
|
-
|
|
109
80
|
return function (_x3, _x4) {
|
|
110
81
|
return _ref3.apply(this, arguments);
|
|
111
82
|
};
|
|
112
83
|
}();
|
|
113
|
-
|
|
114
84
|
this.initRecordsSortValue = /*#__PURE__*/function () {
|
|
115
85
|
var _ref4 = _asyncToGenerator(function* ({
|
|
116
86
|
transaction
|
|
117
87
|
}) {
|
|
118
|
-
const
|
|
119
|
-
transaction
|
|
120
|
-
});
|
|
121
|
-
const emptyCount = yield _this.collection.repository.count({
|
|
122
|
-
filter: {
|
|
123
|
-
[_this.name]: null
|
|
124
|
-
},
|
|
125
|
-
transaction
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
const orderKey = (() => {
|
|
88
|
+
const orderField = (() => {
|
|
129
89
|
const model = _this.collection.model;
|
|
130
|
-
|
|
131
90
|
if (model.primaryKeyAttribute) {
|
|
132
91
|
return model.primaryKeyAttribute;
|
|
133
92
|
}
|
|
134
|
-
|
|
135
93
|
if (model.rawAttributes['createdAt']) {
|
|
136
|
-
return 'createdAt';
|
|
94
|
+
return model.rawAttributes['createdAt'].field;
|
|
137
95
|
}
|
|
138
|
-
|
|
139
96
|
throw new Error(`can not find order key for collection ${_this.collection.name}`);
|
|
140
97
|
})();
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
98
|
+
const needInit = /*#__PURE__*/function () {
|
|
99
|
+
var _ref5 = _asyncToGenerator(function* (scopeKey = null, scopeValue = null) {
|
|
100
|
+
const filter = {};
|
|
101
|
+
if (scopeKey && scopeValue) {
|
|
102
|
+
filter[scopeKey] = scopeValue;
|
|
103
|
+
}
|
|
104
|
+
const totalCount = yield _this.collection.repository.count({
|
|
105
|
+
filter,
|
|
106
|
+
transaction
|
|
107
|
+
});
|
|
108
|
+
const emptyCount = yield _this.collection.repository.count({
|
|
109
|
+
filter: _objectSpread({
|
|
110
|
+
[_this.name]: null
|
|
111
|
+
}, filter),
|
|
112
|
+
transaction
|
|
113
|
+
});
|
|
114
|
+
return emptyCount === totalCount && emptyCount > 0;
|
|
115
|
+
});
|
|
116
|
+
return function needInit() {
|
|
117
|
+
return _ref5.apply(this, arguments);
|
|
118
|
+
};
|
|
119
|
+
}();
|
|
120
|
+
const doInit = /*#__PURE__*/function () {
|
|
121
|
+
var _ref6 = _asyncToGenerator(function* (scopeKey = null, scopeValue = null) {
|
|
122
|
+
const queryInterface = _this.collection.db.sequelize.getQueryInterface();
|
|
123
|
+
const quotedOrderField = queryInterface.quoteIdentifier(orderField);
|
|
124
|
+
const sql = `
|
|
125
|
+
WITH ordered_table AS (
|
|
126
|
+
SELECT *, ROW_NUMBER() OVER (${scopeKey ? `PARTITION BY ${queryInterface.quoteIdentifier(scopeKey)}` : ''} ORDER BY ${quotedOrderField}) AS new_sequence_number
|
|
127
|
+
FROM ${_this.collection.quotedTableName()}
|
|
128
|
+
${(() => {
|
|
129
|
+
if (scopeKey && scopeValue) {
|
|
130
|
+
const hasNull = scopeValue.includes(null);
|
|
131
|
+
return `WHERE ${queryInterface.quoteIdentifier(scopeKey)} IN (${scopeValue.filter(v => v !== null).map(v => `'${v}'`).join(',')}) ${hasNull ? `OR ${queryInterface.quoteIdentifier(scopeKey)} IS NULL` : ''} `;
|
|
132
|
+
}
|
|
133
|
+
return '';
|
|
134
|
+
})()}
|
|
135
|
+
|
|
136
|
+
)
|
|
137
|
+
${_this.collection.db.inDialect('mysql') ? `
|
|
138
|
+
UPDATE ${_this.collection.quotedTableName()}, ordered_table
|
|
139
|
+
SET ${_this.collection.quotedTableName()}.${_this.name} = ordered_table.new_sequence_number
|
|
140
|
+
WHERE ${_this.collection.quotedTableName()}.${quotedOrderField} = ordered_table.${quotedOrderField}
|
|
141
|
+
` : `
|
|
142
|
+
UPDATE ${_this.collection.quotedTableName()}
|
|
143
|
+
SET ${queryInterface.quoteIdentifier(_this.name)} = ordered_table.new_sequence_number
|
|
144
|
+
FROM ordered_table
|
|
145
|
+
WHERE ${_this.collection.quotedTableName()}.${quotedOrderField} = ${queryInterface.quoteIdentifier('ordered_table')}.${quotedOrderField};
|
|
146
|
+
`}
|
|
147
|
+
|
|
148
|
+
`;
|
|
149
|
+
yield _this.collection.db.sequelize.query(sql, {
|
|
150
|
+
transaction
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
return function doInit() {
|
|
154
|
+
return _ref6.apply(this, arguments);
|
|
155
|
+
};
|
|
156
|
+
}();
|
|
157
|
+
const scopeKey = _this.options.scopeKey;
|
|
158
|
+
if (scopeKey) {
|
|
159
|
+
const groups = yield _this.collection.repository.find({
|
|
160
|
+
attributes: [scopeKey],
|
|
161
|
+
group: [scopeKey],
|
|
162
|
+
raw: true,
|
|
145
163
|
transaction
|
|
146
164
|
});
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
_step;
|
|
151
|
-
|
|
165
|
+
const needInitGroups = [];
|
|
166
|
+
var _iterator = _createForOfIteratorHelper(groups),
|
|
167
|
+
_step;
|
|
152
168
|
try {
|
|
153
169
|
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
154
|
-
const
|
|
155
|
-
yield
|
|
156
|
-
|
|
157
|
-
}
|
|
158
|
-
transaction,
|
|
159
|
-
silent: true
|
|
160
|
-
});
|
|
161
|
-
start += 1;
|
|
170
|
+
const group = _step.value;
|
|
171
|
+
if (yield needInit(scopeKey, group[scopeKey])) {
|
|
172
|
+
needInitGroups.push(group[scopeKey]);
|
|
173
|
+
}
|
|
162
174
|
}
|
|
163
175
|
} catch (err) {
|
|
164
176
|
_iterator.e(err);
|
|
165
177
|
} finally {
|
|
166
178
|
_iterator.f();
|
|
167
179
|
}
|
|
180
|
+
if (needInitGroups.length > 0) {
|
|
181
|
+
yield doInit(scopeKey, needInitGroups);
|
|
182
|
+
}
|
|
183
|
+
} else if (yield needInit()) {
|
|
184
|
+
yield doInit();
|
|
168
185
|
}
|
|
169
186
|
});
|
|
170
|
-
|
|
171
187
|
return function (_x5) {
|
|
172
188
|
return _ref4.apply(this, arguments);
|
|
173
189
|
};
|
|
174
190
|
}();
|
|
175
191
|
}
|
|
176
|
-
|
|
177
192
|
get dataType() {
|
|
178
193
|
return _sequelize().DataTypes.BIGINT;
|
|
179
194
|
}
|
|
180
|
-
|
|
181
195
|
bind() {
|
|
182
196
|
super.bind();
|
|
183
197
|
this.on('afterSync', this.initRecordsSortValue);
|
|
184
198
|
this.on('beforeUpdate', this.onScopeChange);
|
|
185
199
|
this.on('beforeCreate', this.setSortValue);
|
|
186
200
|
}
|
|
187
|
-
|
|
188
201
|
unbind() {
|
|
189
202
|
super.unbind();
|
|
190
203
|
this.off('beforeUpdate', this.onScopeChange);
|
|
191
204
|
this.off('beforeCreate', this.setSortValue);
|
|
192
205
|
this.off('afterSync', this.initRecordsSortValue);
|
|
193
206
|
}
|
|
194
|
-
|
|
195
207
|
}
|
|
196
|
-
|
|
197
208
|
exports.SortField = SortField;
|
|
@@ -4,24 +4,17 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.StringField = void 0;
|
|
7
|
-
|
|
8
7
|
function _sequelize() {
|
|
9
8
|
const data = require("sequelize");
|
|
10
|
-
|
|
11
9
|
_sequelize = function _sequelize() {
|
|
12
10
|
return data;
|
|
13
11
|
};
|
|
14
|
-
|
|
15
12
|
return data;
|
|
16
13
|
}
|
|
17
|
-
|
|
18
14
|
var _field = require("./field");
|
|
19
|
-
|
|
20
15
|
class StringField extends _field.Field {
|
|
21
16
|
get dataType() {
|
|
22
17
|
return _sequelize().DataTypes.STRING;
|
|
23
18
|
}
|
|
24
|
-
|
|
25
19
|
}
|
|
26
|
-
|
|
27
20
|
exports.StringField = StringField;
|
package/lib/fields/text-field.js
CHANGED
|
@@ -4,24 +4,17 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.TextField = void 0;
|
|
7
|
-
|
|
8
7
|
function _sequelize() {
|
|
9
8
|
const data = require("sequelize");
|
|
10
|
-
|
|
11
9
|
_sequelize = function _sequelize() {
|
|
12
10
|
return data;
|
|
13
11
|
};
|
|
14
|
-
|
|
15
12
|
return data;
|
|
16
13
|
}
|
|
17
|
-
|
|
18
14
|
var _field = require("./field");
|
|
19
|
-
|
|
20
15
|
class TextField extends _field.Field {
|
|
21
16
|
get dataType() {
|
|
22
17
|
return _sequelize().DataTypes.TEXT;
|
|
23
18
|
}
|
|
24
|
-
|
|
25
19
|
}
|
|
26
|
-
|
|
27
20
|
exports.TextField = TextField;
|
package/lib/fields/time-field.js
CHANGED
|
@@ -4,24 +4,17 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.TimeField = void 0;
|
|
7
|
-
|
|
8
7
|
function _sequelize() {
|
|
9
8
|
const data = require("sequelize");
|
|
10
|
-
|
|
11
9
|
_sequelize = function _sequelize() {
|
|
12
10
|
return data;
|
|
13
11
|
};
|
|
14
|
-
|
|
15
12
|
return data;
|
|
16
13
|
}
|
|
17
|
-
|
|
18
14
|
var _field = require("./field");
|
|
19
|
-
|
|
20
15
|
class TimeField extends _field.Field {
|
|
21
16
|
get dataType() {
|
|
22
17
|
return _sequelize().DataTypes.TIME;
|
|
23
18
|
}
|
|
24
|
-
|
|
25
19
|
}
|
|
26
|
-
|
|
27
20
|
exports.TimeField = TimeField;
|