@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
package/lib/database.js
CHANGED
|
@@ -5,154 +5,102 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.extend = exports.defineCollection = exports.default = exports.DialectVersionAccessors = exports.Database = void 0;
|
|
7
7
|
exports.extendCollection = extendCollection;
|
|
8
|
-
|
|
9
8
|
function _utils() {
|
|
10
9
|
const data = require("@nocobase/utils");
|
|
11
|
-
|
|
12
10
|
_utils = function _utils() {
|
|
13
11
|
return data;
|
|
14
12
|
};
|
|
15
|
-
|
|
16
13
|
return data;
|
|
17
14
|
}
|
|
18
|
-
|
|
19
15
|
function _events() {
|
|
20
16
|
const data = require("events");
|
|
21
|
-
|
|
22
17
|
_events = function _events() {
|
|
23
18
|
return data;
|
|
24
19
|
};
|
|
25
|
-
|
|
26
20
|
return data;
|
|
27
21
|
}
|
|
28
|
-
|
|
29
22
|
function _glob() {
|
|
30
23
|
const data = _interopRequireDefault(require("glob"));
|
|
31
|
-
|
|
32
24
|
_glob = function _glob() {
|
|
33
25
|
return data;
|
|
34
26
|
};
|
|
35
|
-
|
|
36
27
|
return data;
|
|
37
28
|
}
|
|
38
|
-
|
|
39
29
|
function _lodash() {
|
|
40
30
|
const data = _interopRequireDefault(require("lodash"));
|
|
41
|
-
|
|
42
31
|
_lodash = function _lodash() {
|
|
43
32
|
return data;
|
|
44
33
|
};
|
|
45
|
-
|
|
46
34
|
return data;
|
|
47
35
|
}
|
|
48
|
-
|
|
49
36
|
function _path() {
|
|
50
37
|
const data = require("path");
|
|
51
|
-
|
|
52
38
|
_path = function _path() {
|
|
53
39
|
return data;
|
|
54
40
|
};
|
|
55
|
-
|
|
56
41
|
return data;
|
|
57
42
|
}
|
|
58
|
-
|
|
59
43
|
function _semver() {
|
|
60
44
|
const data = _interopRequireDefault(require("semver"));
|
|
61
|
-
|
|
62
45
|
_semver = function _semver() {
|
|
63
46
|
return data;
|
|
64
47
|
};
|
|
65
|
-
|
|
66
48
|
return data;
|
|
67
49
|
}
|
|
68
|
-
|
|
69
50
|
function _sequelize() {
|
|
70
51
|
const data = require("sequelize");
|
|
71
|
-
|
|
72
52
|
_sequelize = function _sequelize() {
|
|
73
53
|
return data;
|
|
74
54
|
};
|
|
75
|
-
|
|
76
55
|
return data;
|
|
77
56
|
}
|
|
78
|
-
|
|
79
57
|
function _umzug() {
|
|
80
58
|
const data = require("umzug");
|
|
81
|
-
|
|
82
59
|
_umzug = function _umzug() {
|
|
83
60
|
return data;
|
|
84
61
|
};
|
|
85
|
-
|
|
86
62
|
return data;
|
|
87
63
|
}
|
|
88
|
-
|
|
89
64
|
var _collection = require("./collection");
|
|
90
|
-
|
|
91
65
|
var _collectionImporter = require("./collection-importer");
|
|
92
|
-
|
|
93
66
|
var _ReferencesMap = _interopRequireDefault(require("./features/ReferencesMap"));
|
|
94
|
-
|
|
95
67
|
var _referentialIntegrityCheck = require("./features/referential-integrity-check");
|
|
96
|
-
|
|
97
68
|
var FieldTypes = _interopRequireWildcard(require("./fields"));
|
|
98
|
-
|
|
99
69
|
var _inheritedCollection = require("./inherited-collection");
|
|
100
|
-
|
|
101
70
|
var _inheritedMap = _interopRequireDefault(require("./inherited-map"));
|
|
102
|
-
|
|
103
71
|
var _migration = require("./migration");
|
|
104
|
-
|
|
105
72
|
var _modelHook = require("./model-hook");
|
|
106
|
-
|
|
107
73
|
var _operators = _interopRequireDefault(require("./operators"));
|
|
108
|
-
|
|
109
74
|
var _utils2 = require("./utils");
|
|
110
|
-
|
|
111
75
|
var _databaseUtils = _interopRequireDefault(require("./database-utils"));
|
|
112
|
-
|
|
76
|
+
var _listeners = require("./listeners");
|
|
113
77
|
var _valueParsers = require("./value-parsers");
|
|
114
|
-
|
|
78
|
+
var _queryInterfaceBuilder = _interopRequireDefault(require("./query-interface/query-interface-builder"));
|
|
79
|
+
var _collectionGroupManager = require("./collection-group-manager");
|
|
80
|
+
var _viewCollection = require("./view-collection");
|
|
115
81
|
const _excluded = ["drop"],
|
|
116
|
-
|
|
117
|
-
|
|
82
|
+
_excluded2 = ["retry"];
|
|
118
83
|
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
119
|
-
|
|
120
84
|
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
121
|
-
|
|
122
85
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
123
|
-
|
|
124
86
|
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
|
|
125
|
-
|
|
126
87
|
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
|
|
127
|
-
|
|
128
88
|
function _toArray(arr) { return _arrayWithHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableRest(); }
|
|
129
|
-
|
|
130
89
|
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
|
131
|
-
|
|
132
90
|
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; } } }; }
|
|
133
|
-
|
|
134
91
|
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; }
|
|
135
|
-
|
|
136
92
|
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; }
|
|
137
|
-
|
|
138
|
-
function
|
|
139
|
-
|
|
93
|
+
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; }
|
|
94
|
+
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
|
|
95
|
+
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); }
|
|
140
96
|
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
141
|
-
|
|
142
97
|
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."); }
|
|
143
|
-
|
|
144
98
|
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); }
|
|
145
|
-
|
|
146
99
|
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; }
|
|
147
|
-
|
|
148
|
-
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; }
|
|
149
|
-
|
|
100
|
+
function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
|
|
150
101
|
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
151
|
-
|
|
152
102
|
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); } }
|
|
153
|
-
|
|
154
103
|
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); }); }; }
|
|
155
|
-
|
|
156
104
|
const DialectVersionAccessors = {
|
|
157
105
|
sqlite: {
|
|
158
106
|
sql: 'select sqlite_version() as version',
|
|
@@ -164,7 +112,6 @@ const DialectVersionAccessors = {
|
|
|
164
112
|
if (v.toLowerCase().includes('mariadb')) {
|
|
165
113
|
return '';
|
|
166
114
|
}
|
|
167
|
-
|
|
168
115
|
const m = /([\d+\.]+)/.exec(v);
|
|
169
116
|
return m[0];
|
|
170
117
|
}
|
|
@@ -178,43 +125,32 @@ const DialectVersionAccessors = {
|
|
|
178
125
|
}
|
|
179
126
|
};
|
|
180
127
|
exports.DialectVersionAccessors = DialectVersionAccessors;
|
|
181
|
-
|
|
182
128
|
class DatabaseVersion {
|
|
183
129
|
constructor(db) {
|
|
184
130
|
this.db = void 0;
|
|
185
131
|
this.db = db;
|
|
186
132
|
}
|
|
187
|
-
|
|
188
133
|
satisfies(versions) {
|
|
189
134
|
var _this = this;
|
|
190
|
-
|
|
191
135
|
return _asyncToGenerator(function* () {
|
|
192
136
|
const accessors = DialectVersionAccessors;
|
|
193
|
-
|
|
194
137
|
for (var _i = 0, _Object$keys = Object.keys(accessors); _i < _Object$keys.length; _i++) {
|
|
195
138
|
const dialect = _Object$keys[_i];
|
|
196
|
-
|
|
197
139
|
if (_this.db.inDialect(dialect)) {
|
|
198
140
|
var _result$;
|
|
199
|
-
|
|
200
141
|
if (!(versions === null || versions === void 0 ? void 0 : versions[dialect])) {
|
|
201
142
|
return false;
|
|
202
143
|
}
|
|
203
|
-
|
|
204
144
|
const _yield$_this$db$seque = yield _this.db.sequelize.query(accessors[dialect].sql),
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
145
|
+
_yield$_this$db$seque2 = _slicedToArray(_yield$_this$db$seque, 1),
|
|
146
|
+
result = _yield$_this$db$seque2[0];
|
|
208
147
|
return _semver().default.satisfies(accessors[dialect].get(result === null || result === void 0 ? void 0 : (_result$ = result[0]) === null || _result$ === void 0 ? void 0 : _result$.version), versions[dialect]);
|
|
209
148
|
}
|
|
210
149
|
}
|
|
211
|
-
|
|
212
150
|
return false;
|
|
213
151
|
})();
|
|
214
152
|
}
|
|
215
|
-
|
|
216
153
|
}
|
|
217
|
-
|
|
218
154
|
class Database extends _events().EventEmitter {
|
|
219
155
|
constructor(options) {
|
|
220
156
|
super();
|
|
@@ -231,6 +167,7 @@ class Database extends _events().EventEmitter {
|
|
|
231
167
|
this.pendingFields = new Map();
|
|
232
168
|
this.modelCollection = new Map();
|
|
233
169
|
this.tableNameCollectionMap = new Map();
|
|
170
|
+
this.queryInterface = void 0;
|
|
234
171
|
this.utils = new _databaseUtils.default(this);
|
|
235
172
|
this.referenceMap = new _ReferencesMap.default();
|
|
236
173
|
this.inheritanceMap = new _inheritedMap.default();
|
|
@@ -238,8 +175,9 @@ class Database extends _events().EventEmitter {
|
|
|
238
175
|
this.modelHook = void 0;
|
|
239
176
|
this.version = void 0;
|
|
240
177
|
this.delayCollectionExtend = new Map();
|
|
178
|
+
this.logger = void 0;
|
|
179
|
+
this.collectionGroupManager = new _collectionGroupManager.CollectionGroupManager(this);
|
|
241
180
|
this.version = new DatabaseVersion(this);
|
|
242
|
-
|
|
243
181
|
const opts = _objectSpread({
|
|
244
182
|
sync: {
|
|
245
183
|
alter: {
|
|
@@ -248,64 +186,55 @@ class Database extends _events().EventEmitter {
|
|
|
248
186
|
force: false
|
|
249
187
|
}
|
|
250
188
|
}, _lodash().default.clone(options));
|
|
251
|
-
|
|
252
189
|
if (options.storage && options.storage !== ':memory:') {
|
|
253
190
|
if (!(0, _path().isAbsolute)(options.storage)) {
|
|
254
191
|
opts.storage = (0, _path().resolve)(process.cwd(), options.storage);
|
|
255
192
|
}
|
|
256
193
|
}
|
|
257
|
-
|
|
258
194
|
if (options.dialect === 'sqlite') {
|
|
259
195
|
delete opts.timezone;
|
|
260
196
|
} else if (!opts.timezone) {
|
|
261
197
|
opts.timezone = '+00:00';
|
|
262
198
|
}
|
|
263
|
-
|
|
264
199
|
if (options.dialect === 'postgres') {
|
|
265
200
|
// https://github.com/sequelize/sequelize/issues/1774
|
|
266
201
|
require('pg').defaults.parseInt8 = true;
|
|
267
202
|
}
|
|
268
|
-
|
|
269
203
|
this.options = opts;
|
|
270
|
-
this.sequelize = new (_sequelize().Sequelize)(
|
|
204
|
+
this.sequelize = new (_sequelize().Sequelize)(this.sequelizeOptions(this.options));
|
|
205
|
+
this.queryInterface = (0, _queryInterfaceBuilder.default)(this);
|
|
271
206
|
this.collections = new Map();
|
|
272
207
|
this.modelHook = new _modelHook.ModelHook(this);
|
|
273
208
|
this.on('afterDefineCollection', collection => {
|
|
274
209
|
var _this$pendingFields$g, _this$delayCollection;
|
|
275
|
-
|
|
276
210
|
// after collection defined, call bind method on pending fields
|
|
277
211
|
(_this$pendingFields$g = this.pendingFields.get(collection.name)) === null || _this$pendingFields$g === void 0 ? void 0 : _this$pendingFields$g.forEach(field => field.bind());
|
|
278
212
|
(_this$delayCollection = this.delayCollectionExtend.get(collection.name)) === null || _this$delayCollection === void 0 ? void 0 : _this$delayCollection.forEach(collectionExtend => {
|
|
279
213
|
collection.updateOptions(collectionExtend.collectionOptions, collectionExtend.mergeOptions);
|
|
280
214
|
});
|
|
281
|
-
});
|
|
282
|
-
|
|
215
|
+
});
|
|
216
|
+
// register database field types
|
|
283
217
|
for (var _i2 = 0, _Object$entries = Object.entries(FieldTypes); _i2 < _Object$entries.length; _i2++) {
|
|
284
218
|
const _Object$entries$_i = _slicedToArray(_Object$entries[_i2], 2),
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
219
|
+
name = _Object$entries$_i[0],
|
|
220
|
+
field = _Object$entries$_i[1];
|
|
288
221
|
if (['Field', 'RelationField'].includes(name)) {
|
|
289
222
|
continue;
|
|
290
223
|
}
|
|
291
|
-
|
|
292
224
|
let key = name.replace(/Field$/g, '');
|
|
293
225
|
key = key.substring(0, 1).toLowerCase() + key.substring(1);
|
|
294
226
|
this.registerFieldTypes({
|
|
295
227
|
[key]: field
|
|
296
228
|
});
|
|
297
229
|
}
|
|
298
|
-
|
|
299
230
|
(0, _valueParsers.registerFieldValueParsers)(this);
|
|
300
231
|
this.initOperators();
|
|
301
232
|
const migratorOptions = this.options.migrator || {};
|
|
302
|
-
|
|
303
233
|
const context = _objectSpread({
|
|
304
234
|
db: this,
|
|
305
235
|
sequelize: this.sequelize,
|
|
306
236
|
queryInterface: this.sequelize.getQueryInterface()
|
|
307
237
|
}, migratorOptions.context);
|
|
308
|
-
|
|
309
238
|
this.migrations = new _migration.Migrations(context);
|
|
310
239
|
this.migrator = new (_umzug().Umzug)({
|
|
311
240
|
logger: migratorOptions.logger || console,
|
|
@@ -321,7 +250,7 @@ class Database extends _events().EventEmitter {
|
|
|
321
250
|
name: 'migrations',
|
|
322
251
|
autoGenId: false,
|
|
323
252
|
timestamps: false,
|
|
324
|
-
namespace: 'core',
|
|
253
|
+
namespace: 'core.migration',
|
|
325
254
|
duplicator: 'required',
|
|
326
255
|
fields: [{
|
|
327
256
|
type: 'string',
|
|
@@ -336,48 +265,70 @@ class Database extends _events().EventEmitter {
|
|
|
336
265
|
this.initListener();
|
|
337
266
|
(0, _utils2.patchSequelizeQueryInterface)(this);
|
|
338
267
|
}
|
|
339
|
-
|
|
268
|
+
setLogger(logger) {
|
|
269
|
+
this.logger = logger;
|
|
270
|
+
}
|
|
271
|
+
sequelizeOptions(options) {
|
|
272
|
+
if (options.dialect === 'postgres') {
|
|
273
|
+
options.hooks = {
|
|
274
|
+
afterConnect: function () {
|
|
275
|
+
var _afterConnect = _asyncToGenerator(function* (connection) {
|
|
276
|
+
yield connection.query('SET search_path TO public;');
|
|
277
|
+
});
|
|
278
|
+
function afterConnect(_x2) {
|
|
279
|
+
return _afterConnect.apply(this, arguments);
|
|
280
|
+
}
|
|
281
|
+
return afterConnect;
|
|
282
|
+
}()
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
return options;
|
|
286
|
+
}
|
|
340
287
|
initListener() {
|
|
341
288
|
var _this2 = this;
|
|
342
|
-
|
|
289
|
+
this.on('afterConnect', /*#__PURE__*/function () {
|
|
290
|
+
var _ref = _asyncToGenerator(function* (client) {
|
|
291
|
+
if (_this2.inDialect('postgres')) {
|
|
292
|
+
yield client.query('SET search_path = public');
|
|
293
|
+
}
|
|
294
|
+
});
|
|
295
|
+
return function (_x3) {
|
|
296
|
+
return _ref.apply(this, arguments);
|
|
297
|
+
};
|
|
298
|
+
}());
|
|
343
299
|
this.on('beforeDefine', (model, options) => {
|
|
344
|
-
if (this.options.underscored) {
|
|
300
|
+
if (this.options.underscored && options.underscored === undefined) {
|
|
345
301
|
options.underscored = true;
|
|
346
302
|
}
|
|
347
303
|
});
|
|
348
304
|
this.on('afterCreate', /*#__PURE__*/function () {
|
|
349
|
-
var
|
|
305
|
+
var _ref2 = _asyncToGenerator(function* (instance) {
|
|
350
306
|
var _instance$toChangedWi;
|
|
351
|
-
|
|
352
307
|
instance === null || instance === void 0 ? void 0 : (_instance$toChangedWi = instance.toChangedWithAssociations) === null || _instance$toChangedWi === void 0 ? void 0 : _instance$toChangedWi.call(instance);
|
|
353
308
|
});
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
return _ref.apply(this, arguments);
|
|
309
|
+
return function (_x4) {
|
|
310
|
+
return _ref2.apply(this, arguments);
|
|
357
311
|
};
|
|
358
312
|
}());
|
|
359
313
|
this.on('afterUpdate', /*#__PURE__*/function () {
|
|
360
|
-
var
|
|
314
|
+
var _ref3 = _asyncToGenerator(function* (instance) {
|
|
361
315
|
var _instance$toChangedWi2;
|
|
362
|
-
|
|
363
316
|
instance === null || instance === void 0 ? void 0 : (_instance$toChangedWi2 = instance.toChangedWithAssociations) === null || _instance$toChangedWi2 === void 0 ? void 0 : _instance$toChangedWi2.call(instance);
|
|
364
317
|
});
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
return _ref2.apply(this, arguments);
|
|
318
|
+
return function (_x5) {
|
|
319
|
+
return _ref3.apply(this, arguments);
|
|
368
320
|
};
|
|
369
321
|
}());
|
|
370
322
|
this.on('beforeDestroy', /*#__PURE__*/function () {
|
|
371
|
-
var
|
|
323
|
+
var _ref4 = _asyncToGenerator(function* (instance, options) {
|
|
372
324
|
yield (0, _referentialIntegrityCheck.referentialIntegrityCheck)({
|
|
373
325
|
db: _this2,
|
|
374
326
|
referencedInstance: instance,
|
|
375
327
|
transaction: options.transaction
|
|
376
328
|
});
|
|
377
329
|
});
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
return _ref3.apply(this, arguments);
|
|
330
|
+
return function (_x6, _x7) {
|
|
331
|
+
return _ref4.apply(this, arguments);
|
|
381
332
|
};
|
|
382
333
|
}());
|
|
383
334
|
this.on('afterRemoveCollection', collection => {
|
|
@@ -386,7 +337,6 @@ class Database extends _events().EventEmitter {
|
|
|
386
337
|
this.on('afterDefine', model => {
|
|
387
338
|
if (_lodash().default.get(this.options, 'usingBigIntForId', true)) {
|
|
388
339
|
const idAttribute = model.rawAttributes['id'];
|
|
389
|
-
|
|
390
340
|
if (idAttribute && idAttribute.primaryKey) {
|
|
391
341
|
model.rawAttributes['id'].type = _sequelize().DataTypes.BIGINT;
|
|
392
342
|
model.refreshAttributes();
|
|
@@ -399,11 +349,13 @@ class Database extends _events().EventEmitter {
|
|
|
399
349
|
}
|
|
400
350
|
});
|
|
401
351
|
this.on('beforeDefineCollection', options => {
|
|
352
|
+
if (this.options.underscored && options.underscored === undefined) {
|
|
353
|
+
options.underscored = true;
|
|
354
|
+
}
|
|
402
355
|
if (options.underscored) {
|
|
403
356
|
if (_lodash().default.get(options, 'sortable.scopeKey')) {
|
|
404
357
|
options.sortable.scopeKey = (0, _utils2.snakeCase)(options.sortable.scopeKey);
|
|
405
358
|
}
|
|
406
|
-
|
|
407
359
|
if (_lodash().default.get(options, 'indexes')) {
|
|
408
360
|
// change index fields to snake case
|
|
409
361
|
options.indexes = options.indexes.map(index => {
|
|
@@ -412,40 +364,62 @@ class Database extends _events().EventEmitter {
|
|
|
412
364
|
return (0, _utils2.snakeCase)(field);
|
|
413
365
|
});
|
|
414
366
|
}
|
|
415
|
-
|
|
416
367
|
return index;
|
|
417
368
|
});
|
|
418
369
|
}
|
|
419
370
|
}
|
|
420
|
-
|
|
421
371
|
if (this.options.schema && !options.schema) {
|
|
422
372
|
options.schema = this.options.schema;
|
|
423
373
|
}
|
|
424
374
|
});
|
|
375
|
+
this.on('afterRepositoryFind', ({
|
|
376
|
+
findOptions,
|
|
377
|
+
dataCollection,
|
|
378
|
+
data
|
|
379
|
+
}) => {
|
|
380
|
+
if (dataCollection.isParent()) {
|
|
381
|
+
var _iterator = _createForOfIteratorHelper(data),
|
|
382
|
+
_step;
|
|
383
|
+
try {
|
|
384
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
385
|
+
const row = _step.value;
|
|
386
|
+
const rowCollection = this.tableNameCollectionMap.get(findOptions.raw ? `${row['__schemaName']}.${row['__tableName']}` : `${row.get('__schemaName')}.${row.get('__tableName')}`);
|
|
387
|
+
if (!rowCollection) {
|
|
388
|
+
this.logger.warn(`Can not find collection by table name ${JSON.stringify(row)}, current collections: ${Array.from(this.tableNameCollectionMap.keys()).join(', ')}`);
|
|
389
|
+
return;
|
|
390
|
+
}
|
|
391
|
+
const rowCollectionName = rowCollection.name;
|
|
392
|
+
findOptions.raw ? row['__collection'] = rowCollectionName : row.set('__collection', rowCollectionName, {
|
|
393
|
+
raw: true
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
} catch (err) {
|
|
397
|
+
_iterator.e(err);
|
|
398
|
+
} finally {
|
|
399
|
+
_iterator.f();
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
});
|
|
403
|
+
(0, _listeners.registerBuiltInListeners)(this);
|
|
425
404
|
}
|
|
426
|
-
|
|
427
405
|
addMigration(item) {
|
|
428
406
|
return this.migrations.add(item);
|
|
429
407
|
}
|
|
430
|
-
|
|
431
408
|
addMigrations(options) {
|
|
432
409
|
const namespace = options.namespace,
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
410
|
+
context = options.context,
|
|
411
|
+
_options$extensions = options.extensions,
|
|
412
|
+
extensions = _options$extensions === void 0 ? ['js', 'ts'] : _options$extensions,
|
|
413
|
+
directory = options.directory;
|
|
437
414
|
const patten = `${directory}/*.{${extensions.join(',')}}`;
|
|
438
|
-
|
|
439
415
|
const files = _glob().default.sync(patten, {
|
|
440
416
|
ignore: ['**/*.d.ts']
|
|
441
417
|
});
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
_step;
|
|
445
|
-
|
|
418
|
+
var _iterator2 = _createForOfIteratorHelper(files),
|
|
419
|
+
_step2;
|
|
446
420
|
try {
|
|
447
|
-
for (
|
|
448
|
-
const file =
|
|
421
|
+
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
422
|
+
const file = _step2.value;
|
|
449
423
|
let filename = (0, _path().basename)(file);
|
|
450
424
|
filename = filename.substring(0, filename.lastIndexOf('.')) || filename;
|
|
451
425
|
this.migrations.add({
|
|
@@ -455,349 +429,278 @@ class Database extends _events().EventEmitter {
|
|
|
455
429
|
});
|
|
456
430
|
}
|
|
457
431
|
} catch (err) {
|
|
458
|
-
|
|
432
|
+
_iterator2.e(err);
|
|
459
433
|
} finally {
|
|
460
|
-
|
|
434
|
+
_iterator2.f();
|
|
461
435
|
}
|
|
462
436
|
}
|
|
463
|
-
|
|
464
437
|
inDialect(...dialect) {
|
|
465
438
|
return dialect.includes(this.sequelize.getDialect());
|
|
466
439
|
}
|
|
440
|
+
escapeId(identifier) {
|
|
441
|
+
return this.inDialect('mysql') ? `\`${identifier}\`` : `"${identifier}"`;
|
|
442
|
+
}
|
|
467
443
|
/**
|
|
468
444
|
* Add collection to database
|
|
469
445
|
* @param options
|
|
470
446
|
*/
|
|
471
|
-
|
|
472
|
-
|
|
473
447
|
collection(options) {
|
|
448
|
+
options = _lodash().default.cloneDeep(options);
|
|
474
449
|
if (this.options.underscored) {
|
|
475
450
|
options.underscored = true;
|
|
476
451
|
}
|
|
477
|
-
|
|
478
452
|
this.emit('beforeDefineCollection', options);
|
|
479
|
-
|
|
480
453
|
const hasValidInheritsOptions = (() => {
|
|
481
454
|
return options.inherits && _lodash().default.castArray(options.inherits).length > 0;
|
|
482
455
|
})();
|
|
483
|
-
|
|
484
|
-
const
|
|
485
|
-
|
|
486
|
-
|
|
456
|
+
const hasViewOptions = options.viewName || options.view;
|
|
457
|
+
const collectionKlass = (() => {
|
|
458
|
+
if (hasValidInheritsOptions) {
|
|
459
|
+
return _inheritedCollection.InheritedCollection;
|
|
460
|
+
}
|
|
461
|
+
if (hasViewOptions) {
|
|
462
|
+
return _viewCollection.ViewCollection;
|
|
463
|
+
}
|
|
464
|
+
return _collection.Collection;
|
|
465
|
+
})();
|
|
466
|
+
const collection = new collectionKlass(options, {
|
|
487
467
|
database: this
|
|
488
468
|
});
|
|
489
469
|
this.collections.set(collection.name, collection);
|
|
490
470
|
this.emit('afterDefineCollection', collection);
|
|
491
471
|
return collection;
|
|
492
472
|
}
|
|
493
|
-
|
|
494
473
|
getTablePrefix() {
|
|
495
474
|
return this.options.tablePrefix || '';
|
|
496
475
|
}
|
|
497
|
-
|
|
498
476
|
getFieldByPath(path) {
|
|
499
477
|
if (!path) {
|
|
500
478
|
return;
|
|
501
479
|
}
|
|
502
|
-
|
|
503
480
|
const _path$split = path.split('.'),
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
481
|
+
_path$split2 = _toArray(_path$split),
|
|
482
|
+
collectionName = _path$split2[0],
|
|
483
|
+
associationName = _path$split2[1],
|
|
484
|
+
args = _path$split2.slice(2);
|
|
509
485
|
let collection = this.getCollection(collectionName);
|
|
510
|
-
|
|
511
486
|
if (!collection) {
|
|
512
487
|
return;
|
|
513
488
|
}
|
|
514
|
-
|
|
515
489
|
const field = collection.getField(associationName);
|
|
516
|
-
|
|
517
490
|
if (!field) {
|
|
518
491
|
return;
|
|
519
492
|
}
|
|
520
|
-
|
|
521
493
|
if (args.length > 0) {
|
|
522
494
|
return this.getFieldByPath(`${field === null || field === void 0 ? void 0 : field.target}.${args.join('.')}`);
|
|
523
495
|
}
|
|
524
|
-
|
|
525
496
|
return field;
|
|
526
497
|
}
|
|
527
498
|
/**
|
|
528
499
|
* get exists collection by its name
|
|
529
500
|
* @param name
|
|
530
501
|
*/
|
|
531
|
-
|
|
532
|
-
|
|
533
502
|
getCollection(name) {
|
|
534
503
|
if (!name) {
|
|
535
504
|
return null;
|
|
536
505
|
}
|
|
537
|
-
|
|
538
506
|
const _name$split = name.split('.'),
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
507
|
+
_name$split2 = _slicedToArray(_name$split, 2),
|
|
508
|
+
collectionName = _name$split2[0],
|
|
509
|
+
associationName = _name$split2[1];
|
|
543
510
|
let collection = this.collections.get(collectionName);
|
|
544
|
-
|
|
545
511
|
if (associationName) {
|
|
546
512
|
var _collection$getField;
|
|
547
|
-
|
|
548
513
|
const target = (_collection$getField = collection.getField(associationName)) === null || _collection$getField === void 0 ? void 0 : _collection$getField.target;
|
|
549
514
|
return target ? this.collections.get(target) : null;
|
|
550
515
|
}
|
|
551
|
-
|
|
552
516
|
return collection;
|
|
553
517
|
}
|
|
554
|
-
|
|
555
518
|
hasCollection(name) {
|
|
556
519
|
return this.collections.has(name);
|
|
557
520
|
}
|
|
558
|
-
|
|
559
521
|
removeCollection(name) {
|
|
560
522
|
const collection = this.collections.get(name);
|
|
561
523
|
this.emit('beforeRemoveCollection', collection);
|
|
562
524
|
collection.resetFields();
|
|
563
525
|
const result = this.collections.delete(name);
|
|
564
526
|
this.sequelize.modelManager.removeModel(collection.model);
|
|
565
|
-
|
|
566
527
|
if (result) {
|
|
567
528
|
this.emit('afterRemoveCollection', collection);
|
|
568
529
|
}
|
|
569
|
-
|
|
570
530
|
return collection;
|
|
571
531
|
}
|
|
572
|
-
|
|
573
532
|
getModel(name) {
|
|
574
533
|
return this.getCollection(name).model;
|
|
575
534
|
}
|
|
576
|
-
|
|
577
535
|
getRepository(name, relationId) {
|
|
578
536
|
var _this$getCollection;
|
|
579
|
-
|
|
580
537
|
const _name$split3 = name.split('.'),
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
538
|
+
_name$split4 = _slicedToArray(_name$split3, 2),
|
|
539
|
+
collection = _name$split4[0],
|
|
540
|
+
relation = _name$split4[1];
|
|
585
541
|
if (relation) {
|
|
586
542
|
var _this$getRepository, _this$getRepository$r;
|
|
587
|
-
|
|
588
543
|
return (_this$getRepository = this.getRepository(collection)) === null || _this$getRepository === void 0 ? void 0 : (_this$getRepository$r = _this$getRepository.relation(relation)) === null || _this$getRepository$r === void 0 ? void 0 : _this$getRepository$r.of(relationId);
|
|
589
544
|
}
|
|
590
|
-
|
|
591
545
|
return (_this$getCollection = this.getCollection(name)) === null || _this$getCollection === void 0 ? void 0 : _this$getCollection.repository;
|
|
592
546
|
}
|
|
593
|
-
|
|
594
547
|
addPendingField(field) {
|
|
595
548
|
const associating = this.pendingFields;
|
|
596
549
|
const items = this.pendingFields.get(field.target) || [];
|
|
597
550
|
items.push(field);
|
|
598
551
|
associating.set(field.target, items);
|
|
599
552
|
}
|
|
600
|
-
|
|
601
553
|
removePendingField(field) {
|
|
602
554
|
const items = this.pendingFields.get(field.target) || [];
|
|
603
555
|
const index = items.indexOf(field);
|
|
604
|
-
|
|
605
556
|
if (index !== -1) {
|
|
606
557
|
delete items[index];
|
|
607
558
|
this.pendingFields.set(field.target, items);
|
|
608
559
|
}
|
|
609
560
|
}
|
|
610
|
-
|
|
611
561
|
registerFieldTypes(fieldTypes) {
|
|
612
562
|
for (var _i3 = 0, _Object$entries2 = Object.entries(fieldTypes); _i3 < _Object$entries2.length; _i3++) {
|
|
613
563
|
const _Object$entries2$_i = _slicedToArray(_Object$entries2[_i3], 2),
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
564
|
+
type = _Object$entries2$_i[0],
|
|
565
|
+
fieldType = _Object$entries2$_i[1];
|
|
617
566
|
this.fieldTypes.set(type, fieldType);
|
|
618
567
|
}
|
|
619
568
|
}
|
|
620
|
-
|
|
621
569
|
registerFieldValueParsers(parsers) {
|
|
622
570
|
for (var _i4 = 0, _Object$entries3 = Object.entries(parsers); _i4 < _Object$entries3.length; _i4++) {
|
|
623
571
|
const _Object$entries3$_i = _slicedToArray(_Object$entries3[_i4], 2),
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
572
|
+
type = _Object$entries3$_i[0],
|
|
573
|
+
parser = _Object$entries3$_i[1];
|
|
627
574
|
this.fieldValueParsers.set(type, parser);
|
|
628
575
|
}
|
|
629
576
|
}
|
|
630
|
-
|
|
631
577
|
buildFieldValueParser(field, ctx) {
|
|
632
578
|
const Parser = this.fieldValueParsers.has(field.type) ? this.fieldValueParsers.get(field.type) : this.fieldValueParsers.get('default');
|
|
633
579
|
const parser = new Parser(field, ctx);
|
|
634
580
|
return parser;
|
|
635
581
|
}
|
|
636
|
-
|
|
637
582
|
registerModels(models) {
|
|
638
583
|
for (var _i5 = 0, _Object$entries4 = Object.entries(models); _i5 < _Object$entries4.length; _i5++) {
|
|
639
584
|
const _Object$entries4$_i = _slicedToArray(_Object$entries4[_i5], 2),
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
585
|
+
type = _Object$entries4$_i[0],
|
|
586
|
+
schemaType = _Object$entries4$_i[1];
|
|
643
587
|
this.models.set(type, schemaType);
|
|
644
588
|
}
|
|
645
589
|
}
|
|
646
|
-
|
|
647
590
|
registerRepositories(repositories) {
|
|
648
591
|
for (var _i6 = 0, _Object$entries5 = Object.entries(repositories); _i6 < _Object$entries5.length; _i6++) {
|
|
649
592
|
const _Object$entries5$_i = _slicedToArray(_Object$entries5[_i6], 2),
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
593
|
+
type = _Object$entries5$_i[0],
|
|
594
|
+
schemaType = _Object$entries5$_i[1];
|
|
653
595
|
this.repositories.set(type, schemaType);
|
|
654
596
|
}
|
|
655
597
|
}
|
|
656
|
-
|
|
657
598
|
initOperators() {
|
|
658
|
-
const operators = new Map();
|
|
659
|
-
|
|
599
|
+
const operators = new Map();
|
|
600
|
+
// Sequelize 内置
|
|
660
601
|
for (const key in _sequelize().Op) {
|
|
661
602
|
operators.set('$' + key, _sequelize().Op[key]);
|
|
662
|
-
|
|
663
603
|
const val = _sequelize().Utils.underscoredIf(key, true);
|
|
664
|
-
|
|
665
604
|
operators.set('$' + val, _sequelize().Op[key]);
|
|
666
605
|
operators.set('$' + val.replace(/_/g, ''), _sequelize().Op[key]);
|
|
667
606
|
}
|
|
668
|
-
|
|
669
607
|
this.operators = operators;
|
|
670
608
|
this.registerOperators(_objectSpread({}, _operators.default));
|
|
671
609
|
}
|
|
672
|
-
|
|
673
610
|
registerOperators(operators) {
|
|
674
611
|
for (var _i7 = 0, _Object$entries6 = Object.entries(operators); _i7 < _Object$entries6.length; _i7++) {
|
|
675
612
|
const _Object$entries6$_i = _slicedToArray(_Object$entries6[_i7], 2),
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
613
|
+
key = _Object$entries6$_i[0],
|
|
614
|
+
operator = _Object$entries6$_i[1];
|
|
679
615
|
this.operators.set(key, operator);
|
|
680
616
|
}
|
|
681
617
|
}
|
|
682
|
-
|
|
683
618
|
buildField(options, context) {
|
|
684
619
|
const type = options.type;
|
|
685
620
|
const Field = this.fieldTypes.get(type);
|
|
686
|
-
|
|
687
621
|
if (!Field) {
|
|
688
622
|
throw Error(`unsupported field type ${type}`);
|
|
689
623
|
}
|
|
690
|
-
|
|
691
|
-
if (options.field && this.options.underscored) {
|
|
624
|
+
if (options.field && context.collection.options.underscored) {
|
|
692
625
|
options.field = (0, _utils2.snakeCase)(options.field);
|
|
693
626
|
}
|
|
694
|
-
|
|
695
627
|
return new Field(options, context);
|
|
696
628
|
}
|
|
697
|
-
|
|
698
629
|
sync(options) {
|
|
699
630
|
var _this3 = this;
|
|
700
|
-
|
|
701
631
|
return _asyncToGenerator(function* () {
|
|
702
632
|
const isMySQL = _this3.sequelize.getDialect() === 'mysql';
|
|
703
|
-
|
|
704
633
|
if (isMySQL) {
|
|
705
634
|
yield _this3.sequelize.query('SET FOREIGN_KEY_CHECKS = 0', null);
|
|
706
635
|
}
|
|
707
|
-
|
|
708
636
|
if (_this3.options.schema && _this3.inDialect('postgres')) {
|
|
709
637
|
yield _this3.sequelize.query(`CREATE SCHEMA IF NOT EXISTS "${_this3.options.schema}"`, null);
|
|
710
638
|
}
|
|
711
|
-
|
|
712
639
|
const result = yield _this3.sequelize.sync(options);
|
|
713
|
-
|
|
714
640
|
if (isMySQL) {
|
|
715
641
|
yield _this3.sequelize.query('SET FOREIGN_KEY_CHECKS = 1', null);
|
|
716
642
|
}
|
|
717
|
-
|
|
718
643
|
return result;
|
|
719
644
|
})();
|
|
720
645
|
}
|
|
721
|
-
|
|
722
646
|
clean(options) {
|
|
723
647
|
var _this4 = this;
|
|
724
|
-
|
|
725
648
|
return _asyncToGenerator(function* () {
|
|
726
|
-
const
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
649
|
+
const _ref5 = options || {},
|
|
650
|
+
drop = _ref5.drop,
|
|
651
|
+
others = _objectWithoutProperties(_ref5, _excluded);
|
|
730
652
|
if (drop !== true) {
|
|
731
653
|
return;
|
|
732
654
|
}
|
|
733
|
-
|
|
734
655
|
if (_this4.options.schema) {
|
|
735
656
|
const tableNames = (yield _this4.sequelize.getQueryInterface().showAllTables()).map(table => {
|
|
736
657
|
return `"${_this4.options.schema}"."${table}"`;
|
|
737
658
|
});
|
|
738
|
-
const skip = options.skip || [];
|
|
739
|
-
|
|
740
|
-
var
|
|
741
|
-
|
|
742
|
-
|
|
659
|
+
const skip = options.skip || [];
|
|
660
|
+
// @ts-ignore
|
|
661
|
+
var _iterator3 = _createForOfIteratorHelper(tableNames),
|
|
662
|
+
_step3;
|
|
743
663
|
try {
|
|
744
|
-
for (
|
|
745
|
-
const tableName =
|
|
746
|
-
|
|
664
|
+
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
|
|
665
|
+
const tableName = _step3.value;
|
|
747
666
|
if (skip.includes(tableName)) {
|
|
748
667
|
continue;
|
|
749
668
|
}
|
|
750
|
-
|
|
751
669
|
yield _this4.sequelize.query(`DROP TABLE IF EXISTS ${tableName} CASCADE`);
|
|
752
670
|
}
|
|
753
671
|
} catch (err) {
|
|
754
|
-
|
|
672
|
+
_iterator3.e(err);
|
|
755
673
|
} finally {
|
|
756
|
-
|
|
674
|
+
_iterator3.f();
|
|
757
675
|
}
|
|
758
|
-
|
|
759
676
|
return;
|
|
760
677
|
}
|
|
761
|
-
|
|
762
|
-
yield _this4.sequelize.getQueryInterface().dropAllTables(others);
|
|
678
|
+
yield _this4.queryInterface.dropAll(options);
|
|
763
679
|
})();
|
|
764
680
|
}
|
|
765
|
-
|
|
766
681
|
collectionExistsInDb(name, options) {
|
|
767
682
|
var _this5 = this;
|
|
768
|
-
|
|
769
683
|
return _asyncToGenerator(function* () {
|
|
770
684
|
const collection = _this5.getCollection(name);
|
|
771
|
-
|
|
772
685
|
if (!collection) {
|
|
773
686
|
return false;
|
|
774
687
|
}
|
|
775
|
-
|
|
776
|
-
const tables = yield _this5.sequelize.getQueryInterface().showAllTables({
|
|
777
|
-
transaction: options === null || options === void 0 ? void 0 : options.transaction
|
|
778
|
-
});
|
|
779
|
-
return tables.includes(_this5.getCollection(name).model.tableName);
|
|
688
|
+
return yield _this5.queryInterface.collectionTableExists(collection, options);
|
|
780
689
|
})();
|
|
781
690
|
}
|
|
782
|
-
|
|
783
691
|
isSqliteMemory() {
|
|
784
692
|
return this.sequelize.getDialect() === 'sqlite' && _lodash().default.get(this.options, 'storage') == ':memory:';
|
|
785
693
|
}
|
|
786
|
-
|
|
787
694
|
auth(options = {}) {
|
|
788
695
|
var _this6 = this;
|
|
789
|
-
|
|
790
696
|
return _asyncToGenerator(function* () {
|
|
791
697
|
const _options$retry = options.retry,
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
698
|
+
retry = _options$retry === void 0 ? 10 : _options$retry,
|
|
699
|
+
others = _objectWithoutProperties(options, _excluded2);
|
|
795
700
|
const delay = ms => new Promise(yea => setTimeout(yea, ms));
|
|
796
|
-
|
|
797
701
|
let count = 1;
|
|
798
|
-
|
|
799
702
|
const authenticate = /*#__PURE__*/function () {
|
|
800
|
-
var
|
|
703
|
+
var _ref6 = _asyncToGenerator(function* () {
|
|
801
704
|
try {
|
|
802
705
|
yield _this6.sequelize.authenticate(others);
|
|
803
706
|
console.log('Connection has been established successfully.');
|
|
@@ -806,86 +709,69 @@ class Database extends _events().EventEmitter {
|
|
|
806
709
|
if (count >= retry) {
|
|
807
710
|
throw new Error('Connection failed, please check your database connection credentials and try again.');
|
|
808
711
|
}
|
|
809
|
-
|
|
810
712
|
console.log('reconnecting...', count);
|
|
811
713
|
++count;
|
|
812
714
|
yield delay(500);
|
|
813
715
|
return yield authenticate();
|
|
814
716
|
}
|
|
815
717
|
});
|
|
816
|
-
|
|
817
718
|
return function authenticate() {
|
|
818
|
-
return
|
|
719
|
+
return _ref6.apply(this, arguments);
|
|
819
720
|
};
|
|
820
721
|
}();
|
|
821
|
-
|
|
822
722
|
return yield authenticate();
|
|
823
723
|
})();
|
|
824
724
|
}
|
|
825
|
-
|
|
826
725
|
prepare() {
|
|
827
726
|
var _this7 = this;
|
|
828
|
-
|
|
829
727
|
return _asyncToGenerator(function* () {
|
|
830
728
|
if (_this7.inDialect('postgres') && _this7.options.schema && _this7.options.schema != 'public') {
|
|
831
729
|
yield _this7.sequelize.query(`CREATE SCHEMA IF NOT EXISTS "${_this7.options.schema}"`, null);
|
|
832
730
|
}
|
|
833
731
|
})();
|
|
834
732
|
}
|
|
835
|
-
|
|
836
733
|
reconnect() {
|
|
837
734
|
var _this8 = this;
|
|
838
|
-
|
|
839
735
|
return _asyncToGenerator(function* () {
|
|
840
736
|
if (_this8.isSqliteMemory()) {
|
|
841
737
|
return;
|
|
842
|
-
}
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
738
|
+
}
|
|
739
|
+
// @ts-ignore
|
|
740
|
+
const ConnectionManager = _this8.sequelize.dialect.connectionManager.constructor;
|
|
741
|
+
// @ts-ignore
|
|
742
|
+
const connectionManager = new ConnectionManager(_this8.sequelize.dialect, _this8.sequelize);
|
|
743
|
+
// @ts-ignore
|
|
744
|
+
_this8.sequelize.dialect.connectionManager = connectionManager;
|
|
745
|
+
// @ts-ignore
|
|
851
746
|
_this8.sequelize.connectionManager = connectionManager;
|
|
852
747
|
})();
|
|
853
748
|
}
|
|
854
|
-
|
|
855
749
|
closed() {
|
|
856
750
|
// @ts-ignore
|
|
857
751
|
return this.sequelize.connectionManager.pool._draining;
|
|
858
752
|
}
|
|
859
|
-
|
|
860
753
|
close() {
|
|
861
754
|
var _this9 = this;
|
|
862
|
-
|
|
863
755
|
return _asyncToGenerator(function* () {
|
|
864
756
|
if (_this9.isSqliteMemory()) {
|
|
865
757
|
return;
|
|
866
758
|
}
|
|
867
|
-
|
|
868
759
|
return _this9.sequelize.close();
|
|
869
760
|
})();
|
|
870
761
|
}
|
|
871
|
-
|
|
872
762
|
on(event, listener) {
|
|
873
763
|
// NOTE: to match if event is a sequelize or model type
|
|
874
764
|
const type = this.modelHook.match(event);
|
|
875
|
-
|
|
876
765
|
if (type && !this.modelHook.hasBoundEvent(type)) {
|
|
877
766
|
this.sequelize.addHook(type, this.modelHook.buildSequelizeHook(type));
|
|
878
767
|
this.modelHook.bindEvent(type);
|
|
879
768
|
}
|
|
880
|
-
|
|
881
769
|
return super.on(event, listener);
|
|
882
770
|
}
|
|
883
|
-
|
|
884
771
|
extendCollection(collectionOptions, mergeOptions) {
|
|
885
772
|
collectionOptions = _lodash().default.cloneDeep(collectionOptions);
|
|
886
773
|
const collectionName = collectionOptions.name;
|
|
887
774
|
const existCollection = this.getCollection(collectionName);
|
|
888
|
-
|
|
889
775
|
if (existCollection) {
|
|
890
776
|
existCollection.updateOptions(collectionOptions, mergeOptions);
|
|
891
777
|
} else {
|
|
@@ -896,48 +782,37 @@ class Database extends _events().EventEmitter {
|
|
|
896
782
|
}]);
|
|
897
783
|
}
|
|
898
784
|
}
|
|
899
|
-
|
|
900
785
|
import(options) {
|
|
901
786
|
var _this10 = this;
|
|
902
|
-
|
|
903
787
|
return _asyncToGenerator(function* () {
|
|
904
788
|
const reader = new _collectionImporter.ImporterReader(options.directory, options.extensions);
|
|
905
789
|
const modules = yield reader.read();
|
|
906
790
|
const result = new Map();
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
_step3;
|
|
910
|
-
|
|
791
|
+
var _iterator4 = _createForOfIteratorHelper(modules),
|
|
792
|
+
_step4;
|
|
911
793
|
try {
|
|
912
|
-
for (
|
|
913
|
-
const module =
|
|
914
|
-
|
|
794
|
+
for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
|
|
795
|
+
const module = _step4.value;
|
|
915
796
|
if (module.extend) {
|
|
916
797
|
_this10.extendCollection(module.collectionOptions, module.mergeOptions);
|
|
917
798
|
} else {
|
|
918
799
|
const collection = _this10.collection(module);
|
|
919
|
-
|
|
920
800
|
if (options.from) {
|
|
921
801
|
_this10.importedFrom.set(options.from, [...(_this10.importedFrom.get(options.from) || []), collection.name]);
|
|
922
802
|
}
|
|
923
|
-
|
|
924
803
|
result.set(collection.name, collection);
|
|
925
804
|
}
|
|
926
805
|
}
|
|
927
806
|
} catch (err) {
|
|
928
|
-
|
|
807
|
+
_iterator4.e(err);
|
|
929
808
|
} finally {
|
|
930
|
-
|
|
809
|
+
_iterator4.f();
|
|
931
810
|
}
|
|
932
|
-
|
|
933
811
|
return result;
|
|
934
812
|
})();
|
|
935
813
|
}
|
|
936
|
-
|
|
937
814
|
}
|
|
938
|
-
|
|
939
815
|
exports.Database = Database;
|
|
940
|
-
|
|
941
816
|
function extendCollection(collectionOptions, mergeOptions) {
|
|
942
817
|
return {
|
|
943
818
|
collectionOptions,
|
|
@@ -945,14 +820,11 @@ function extendCollection(collectionOptions, mergeOptions) {
|
|
|
945
820
|
extend: true
|
|
946
821
|
};
|
|
947
822
|
}
|
|
948
|
-
|
|
949
823
|
const extend = extendCollection;
|
|
950
824
|
exports.extend = extend;
|
|
951
|
-
|
|
952
825
|
const defineCollection = collectionOptions => {
|
|
953
826
|
return collectionOptions;
|
|
954
827
|
};
|
|
955
|
-
|
|
956
828
|
exports.defineCollection = defineCollection;
|
|
957
829
|
(0, _utils().applyMixins)(Database, [_utils().AsyncEmitter]);
|
|
958
830
|
var _default = Database;
|