@nocobase/database 0.7.0-alpha.83 → 0.7.1-alpha.5
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 +25 -42
- package/lib/collection.d.ts +6 -2
- package/lib/collection.js +37 -5
- package/lib/database.d.ts +14 -4
- package/lib/database.js +106 -32
- package/lib/fields/field.d.ts +4 -1
- package/lib/fields/field.js +117 -0
- package/lib/fields/formula-field.d.ts +19 -0
- package/lib/fields/formula-field.js +184 -0
- package/lib/fields/index.d.ts +3 -1
- package/lib/fields/index.js +13 -0
- package/lib/migration.d.ts +3 -1
- package/lib/migration.js +18 -4
- package/lib/model-hook.d.ts +5 -5
- package/lib/model-hook.js +26 -18
- package/lib/options-parser.js +65 -43
- package/lib/relation-repository/relation-repository.js +11 -1
- package/lib/relation-repository/single-relation-repository.js +8 -1
- package/lib/repository.js +12 -4
- package/lib/update-associations.js +1 -1
- package/package.json +7 -3
- package/src/__tests__/collection.test.ts +27 -0
- package/src/__tests__/database.test.ts +47 -0
- package/src/__tests__/fields/formula-field.test.ts +69 -0
- package/src/__tests__/fixtures/migrations/m1.ts +7 -0
- package/src/__tests__/fixtures/migrations/m2.ts +7 -0
- package/src/__tests__/hooks/afterCreateWithAssociations.test.ts +33 -0
- package/src/__tests__/migrator.test.ts +18 -0
- package/src/__tests__/model-hook.test.ts +54 -0
- package/src/__tests__/option-parser.test.ts +10 -6
- package/src/__tests__/relation-repository/belongs-to-many-repository.test.ts +1 -1
- package/src/__tests__/sequelize-hooks.test.ts +69 -0
- package/src/__tests__/sort.test.ts +51 -0
- package/src/__tests__/update-associations.test.ts +3 -3
- package/src/collection-importer.ts +12 -20
- package/src/collection.ts +26 -2
- package/src/database.ts +62 -13
- package/src/fields/field.ts +88 -1
- package/src/fields/formula-field.ts +106 -0
- package/src/fields/index.ts +3 -0
- package/src/migration.ts +4 -3
- package/src/model-hook.ts +25 -21
- package/src/options-parser.ts +13 -9
- package/src/relation-repository/multiple-relation-repository.ts +8 -2
- package/src/relation-repository/relation-repository.ts +1 -0
- package/src/relation-repository/single-relation-repository.ts +5 -1
- package/src/repository.ts +16 -4
- package/src/update-associations.ts +1 -1
|
@@ -5,10 +5,20 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.ImporterReader = void 0;
|
|
7
7
|
|
|
8
|
-
function
|
|
9
|
-
const data =
|
|
8
|
+
function _path() {
|
|
9
|
+
const data = _interopRequireDefault(require("path"));
|
|
10
|
+
|
|
11
|
+
_path = function _path() {
|
|
12
|
+
return data;
|
|
13
|
+
};
|
|
10
14
|
|
|
11
|
-
|
|
15
|
+
return data;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function _promises() {
|
|
19
|
+
const data = require("fs/promises");
|
|
20
|
+
|
|
21
|
+
_promises = function _promises() {
|
|
12
22
|
return data;
|
|
13
23
|
};
|
|
14
24
|
|
|
@@ -16,7 +26,7 @@ function fs() {
|
|
|
16
26
|
}
|
|
17
27
|
|
|
18
28
|
function _lodash() {
|
|
19
|
-
const data =
|
|
29
|
+
const data = require("lodash");
|
|
20
30
|
|
|
21
31
|
_lodash = function _lodash() {
|
|
22
32
|
return data;
|
|
@@ -25,10 +35,10 @@ function _lodash() {
|
|
|
25
35
|
return data;
|
|
26
36
|
}
|
|
27
37
|
|
|
28
|
-
function
|
|
29
|
-
const data =
|
|
38
|
+
function _utils() {
|
|
39
|
+
const data = require("@nocobase/utils");
|
|
30
40
|
|
|
31
|
-
|
|
41
|
+
_utils = function _utils() {
|
|
32
42
|
return data;
|
|
33
43
|
};
|
|
34
44
|
|
|
@@ -37,33 +47,10 @@ function _path() {
|
|
|
37
47
|
|
|
38
48
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
39
49
|
|
|
40
|
-
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); }
|
|
41
|
-
|
|
42
|
-
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; }
|
|
43
|
-
|
|
44
50
|
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); } }
|
|
45
51
|
|
|
46
52
|
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); }); }; }
|
|
47
53
|
|
|
48
|
-
function requireModule(_x) {
|
|
49
|
-
return _requireModule.apply(this, arguments);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function _requireModule() {
|
|
53
|
-
_requireModule = _asyncToGenerator(function* (module) {
|
|
54
|
-
if (typeof module === 'string') {
|
|
55
|
-
module = require(module);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
if (typeof module !== 'object') {
|
|
59
|
-
return module;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
return module.__esModule ? module.default : module;
|
|
63
|
-
});
|
|
64
|
-
return _requireModule.apply(this, arguments);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
54
|
class ImporterReader {
|
|
68
55
|
constructor(directory, extensions) {
|
|
69
56
|
this.directory = void 0;
|
|
@@ -81,9 +68,10 @@ class ImporterReader {
|
|
|
81
68
|
var _this = this;
|
|
82
69
|
|
|
83
70
|
return _asyncToGenerator(function* () {
|
|
84
|
-
const
|
|
71
|
+
const files = yield (0, _promises().readdir)(_this.directory, {
|
|
85
72
|
encoding: 'utf-8'
|
|
86
|
-
})
|
|
73
|
+
});
|
|
74
|
+
const modules = files.filter(fileName => {
|
|
87
75
|
if (fileName.endsWith('.d.ts')) {
|
|
88
76
|
return false;
|
|
89
77
|
}
|
|
@@ -91,16 +79,11 @@ class ImporterReader {
|
|
|
91
79
|
const ext = _path().default.parse(fileName).ext.replace('.', '');
|
|
92
80
|
|
|
93
81
|
return _this.extensions.has(ext);
|
|
94
|
-
}).map(
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
return function (_x2) {
|
|
100
|
-
return _ref.apply(this, arguments);
|
|
101
|
-
};
|
|
102
|
-
}());
|
|
103
|
-
return (yield Promise.all(modules)).filter(module => _lodash().default.isPlainObject(module));
|
|
82
|
+
}).map(fileName => {
|
|
83
|
+
const mod = (0, _utils().requireModule)(_path().default.join(_this.directory, fileName));
|
|
84
|
+
return typeof mod === 'function' ? mod() : mod;
|
|
85
|
+
});
|
|
86
|
+
return (yield Promise.all(modules)).filter(module => (0, _lodash().isPlainObject)(module));
|
|
104
87
|
})();
|
|
105
88
|
}
|
|
106
89
|
|
package/lib/collection.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { EventEmitter } from 'events';
|
|
3
|
-
import { ModelCtor, ModelOptions, SyncOptions } from 'sequelize';
|
|
3
|
+
import { ModelCtor, ModelOptions, QueryInterfaceDropTableOptions, SyncOptions, Transactionable } from 'sequelize';
|
|
4
4
|
import { Database } from './database';
|
|
5
5
|
import { Field, FieldOptions } from './fields';
|
|
6
6
|
import { Model } from './model';
|
|
@@ -40,6 +40,7 @@ export declare class Collection<TModelAttributes extends {} = any, TCreationAttr
|
|
|
40
40
|
repository: Repository<TModelAttributes, TCreationAttributes>;
|
|
41
41
|
get filterTargetKey(): string;
|
|
42
42
|
get name(): string;
|
|
43
|
+
get db(): Database;
|
|
43
44
|
constructor(options: CollectionOptions, context?: CollectionContext);
|
|
44
45
|
private sequelizeModelOptions;
|
|
45
46
|
/**
|
|
@@ -56,7 +57,10 @@ export declare class Collection<TModelAttributes extends {} = any, TCreationAttr
|
|
|
56
57
|
setField(name: string, options: FieldOptions): Field;
|
|
57
58
|
setFields(fields: FieldOptions[], resetFields?: boolean): void;
|
|
58
59
|
resetFields(): void;
|
|
59
|
-
|
|
60
|
+
remove(): void;
|
|
61
|
+
removeFromDb(options?: QueryInterfaceDropTableOptions): Promise<void>;
|
|
62
|
+
existsInDb(options?: Transactionable): Promise<boolean>;
|
|
63
|
+
removeField(name: any): Field;
|
|
60
64
|
/**
|
|
61
65
|
* TODO
|
|
62
66
|
*/
|
package/lib/collection.js
CHANGED
|
@@ -73,6 +73,10 @@ class Collection extends _events().EventEmitter {
|
|
|
73
73
|
return this.options.name;
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
+
get db() {
|
|
77
|
+
return this.context.database;
|
|
78
|
+
}
|
|
79
|
+
|
|
76
80
|
constructor(options, context) {
|
|
77
81
|
super();
|
|
78
82
|
this.options = void 0;
|
|
@@ -247,6 +251,34 @@ class Collection extends _events().EventEmitter {
|
|
|
247
251
|
}
|
|
248
252
|
}
|
|
249
253
|
|
|
254
|
+
remove() {
|
|
255
|
+
this.context.database.removeCollection(this.name);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
removeFromDb(options) {
|
|
259
|
+
var _this = this;
|
|
260
|
+
|
|
261
|
+
return _asyncToGenerator(function* () {
|
|
262
|
+
if (yield _this.existsInDb({
|
|
263
|
+
transaction: options === null || options === void 0 ? void 0 : options.transaction
|
|
264
|
+
})) {
|
|
265
|
+
const queryInterface = _this.db.sequelize.getQueryInterface();
|
|
266
|
+
|
|
267
|
+
yield queryInterface.dropTable(_this.model.tableName, options);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
_this.remove();
|
|
271
|
+
})();
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
existsInDb(options) {
|
|
275
|
+
var _this2 = this;
|
|
276
|
+
|
|
277
|
+
return _asyncToGenerator(function* () {
|
|
278
|
+
return _this2.db.collectionExistsInDb(_this2.name, options);
|
|
279
|
+
})();
|
|
280
|
+
}
|
|
281
|
+
|
|
250
282
|
removeField(name) {
|
|
251
283
|
if (!this.fields.has(name)) {
|
|
252
284
|
return;
|
|
@@ -259,7 +291,7 @@ class Collection extends _events().EventEmitter {
|
|
|
259
291
|
this.emit('field.afterRemove', field);
|
|
260
292
|
}
|
|
261
293
|
|
|
262
|
-
return
|
|
294
|
+
return field;
|
|
263
295
|
}
|
|
264
296
|
/**
|
|
265
297
|
* TODO
|
|
@@ -325,11 +357,11 @@ class Collection extends _events().EventEmitter {
|
|
|
325
357
|
}
|
|
326
358
|
|
|
327
359
|
sync(syncOptions) {
|
|
328
|
-
var
|
|
360
|
+
var _this3 = this;
|
|
329
361
|
|
|
330
362
|
return _asyncToGenerator(function* () {
|
|
331
|
-
const modelNames = [
|
|
332
|
-
const associations =
|
|
363
|
+
const modelNames = [_this3.model.name];
|
|
364
|
+
const associations = _this3.model.associations;
|
|
333
365
|
|
|
334
366
|
for (const associationKey in associations) {
|
|
335
367
|
const association = associations[associationKey];
|
|
@@ -342,7 +374,7 @@ class Collection extends _events().EventEmitter {
|
|
|
342
374
|
|
|
343
375
|
const models = []; // @ts-ignore
|
|
344
376
|
|
|
345
|
-
|
|
377
|
+
_this3.context.database.sequelize.modelManager.forEachModel(model => {
|
|
346
378
|
if (modelNames.includes(model.name)) {
|
|
347
379
|
models.push(model);
|
|
348
380
|
}
|
package/lib/database.d.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { AsyncEmitter } from '@nocobase/utils';
|
|
3
3
|
import merge from 'deepmerge';
|
|
4
4
|
import { EventEmitter } from 'events';
|
|
5
|
-
import { ModelCtor, Options, QueryInterfaceDropAllTablesOptions, QueryOptions, Sequelize, SyncOptions } from 'sequelize';
|
|
5
|
+
import { ModelCtor, Options, QueryInterfaceDropAllTablesOptions, QueryOptions, Sequelize, SyncOptions, Transactionable } from 'sequelize';
|
|
6
6
|
import { Umzug } from 'umzug';
|
|
7
7
|
import { Collection, CollectionOptions, RepositoryType } from './collection';
|
|
8
8
|
import { ImportFileExtension } from './collection-importer';
|
|
@@ -36,6 +36,12 @@ interface RegisterOperatorsContext {
|
|
|
36
36
|
export interface CleanOptions extends QueryInterfaceDropAllTablesOptions {
|
|
37
37
|
drop?: boolean;
|
|
38
38
|
}
|
|
39
|
+
export declare type AddMigrationsOptions = {
|
|
40
|
+
context?: any;
|
|
41
|
+
namespace?: string;
|
|
42
|
+
extensions?: string[];
|
|
43
|
+
directory: string;
|
|
44
|
+
};
|
|
39
45
|
declare type OperatorFunc = (value: any, ctx?: RegisterOperatorsContext) => any;
|
|
40
46
|
export declare class Database extends EventEmitter implements AsyncEmitter {
|
|
41
47
|
sequelize: Sequelize;
|
|
@@ -56,6 +62,9 @@ export declare class Database extends EventEmitter implements AsyncEmitter {
|
|
|
56
62
|
}[]>;
|
|
57
63
|
constructor(options: DatabaseOptions);
|
|
58
64
|
addMigration(item: any): void;
|
|
65
|
+
addMigrations(options: AddMigrationsOptions): void;
|
|
66
|
+
inDialect(...dialect: string[]): boolean;
|
|
67
|
+
private requireModule;
|
|
59
68
|
/**
|
|
60
69
|
* Add collection to database
|
|
61
70
|
* @param options
|
|
@@ -68,7 +77,7 @@ export declare class Database extends EventEmitter implements AsyncEmitter {
|
|
|
68
77
|
*/
|
|
69
78
|
getCollection(name: string): Collection;
|
|
70
79
|
hasCollection(name: string): boolean;
|
|
71
|
-
removeCollection(name: string):
|
|
80
|
+
removeCollection(name: string): Collection<any, any>;
|
|
72
81
|
getModel<M extends Model>(name: string): ModelCtor<M>;
|
|
73
82
|
getRepository<R extends Repository>(name: string): R;
|
|
74
83
|
getRepository<R extends RelationRepository>(name: string, relationId: string | number): R;
|
|
@@ -82,14 +91,15 @@ export declare class Database extends EventEmitter implements AsyncEmitter {
|
|
|
82
91
|
buildField(options: any, context: FieldContext): any;
|
|
83
92
|
sync(options?: SyncOptions): Promise<Sequelize>;
|
|
84
93
|
clean(options: CleanOptions): Promise<void>;
|
|
94
|
+
collectionExistsInDb(name: any, options?: Transactionable): Promise<boolean>;
|
|
85
95
|
isSqliteMemory(): boolean;
|
|
86
96
|
auth(options?: QueryOptions & {
|
|
87
|
-
|
|
97
|
+
retry?: number;
|
|
88
98
|
}): Promise<any>;
|
|
89
99
|
reconnect(): Promise<void>;
|
|
90
100
|
closed(): any;
|
|
91
101
|
close(): Promise<void>;
|
|
92
|
-
on(event: string | symbol, listener:
|
|
102
|
+
on(event: string | symbol, listener: any): this;
|
|
93
103
|
import(options: {
|
|
94
104
|
directory: string;
|
|
95
105
|
extensions?: ImportFileExtension[];
|
package/lib/database.js
CHANGED
|
@@ -27,6 +27,16 @@ function _events() {
|
|
|
27
27
|
return data;
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
+
function _glob() {
|
|
31
|
+
const data = _interopRequireDefault(require("glob"));
|
|
32
|
+
|
|
33
|
+
_glob = function _glob() {
|
|
34
|
+
return data;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
return data;
|
|
38
|
+
}
|
|
39
|
+
|
|
30
40
|
function _lodash() {
|
|
31
41
|
const data = _interopRequireDefault(require("lodash"));
|
|
32
42
|
|
|
@@ -80,7 +90,7 @@ var _modelHook = require("./model-hook");
|
|
|
80
90
|
var _operators = _interopRequireDefault(require("./operators"));
|
|
81
91
|
|
|
82
92
|
const _excluded = ["drop"],
|
|
83
|
-
_excluded2 = ["
|
|
93
|
+
_excluded2 = ["retry"];
|
|
84
94
|
|
|
85
95
|
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); }
|
|
86
96
|
|
|
@@ -88,8 +98,6 @@ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj &&
|
|
|
88
98
|
|
|
89
99
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
90
100
|
|
|
91
|
-
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; } } }; }
|
|
92
|
-
|
|
93
101
|
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; }
|
|
94
102
|
|
|
95
103
|
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; }
|
|
@@ -98,6 +106,8 @@ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try
|
|
|
98
106
|
|
|
99
107
|
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); }); }; }
|
|
100
108
|
|
|
109
|
+
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; } } }; }
|
|
110
|
+
|
|
101
111
|
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
102
112
|
|
|
103
113
|
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."); }
|
|
@@ -209,6 +219,55 @@ class Database extends _events().EventEmitter {
|
|
|
209
219
|
addMigration(item) {
|
|
210
220
|
return this.migrations.add(item);
|
|
211
221
|
}
|
|
222
|
+
|
|
223
|
+
addMigrations(options) {
|
|
224
|
+
const namespace = options.namespace,
|
|
225
|
+
context = options.context,
|
|
226
|
+
_options$extensions = options.extensions,
|
|
227
|
+
extensions = _options$extensions === void 0 ? ['js', 'ts'] : _options$extensions,
|
|
228
|
+
directory = options.directory;
|
|
229
|
+
const patten = `${directory}/*.{${extensions.join(',')}}`;
|
|
230
|
+
|
|
231
|
+
const files = _glob().default.sync(patten, {
|
|
232
|
+
ignore: ['**/*.d.ts']
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
var _iterator = _createForOfIteratorHelper(files),
|
|
236
|
+
_step;
|
|
237
|
+
|
|
238
|
+
try {
|
|
239
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
240
|
+
const file = _step.value;
|
|
241
|
+
let filename = (0, _path().basename)(file);
|
|
242
|
+
filename = filename.substring(0, filename.lastIndexOf('.')) || filename;
|
|
243
|
+
this.migrations.add({
|
|
244
|
+
name: namespace ? `${namespace}/${filename}` : filename,
|
|
245
|
+
migration: this.requireModule(file),
|
|
246
|
+
context
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
} catch (err) {
|
|
250
|
+
_iterator.e(err);
|
|
251
|
+
} finally {
|
|
252
|
+
_iterator.f();
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
inDialect(...dialect) {
|
|
257
|
+
return dialect.includes(this.sequelize.getDialect());
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
requireModule(module) {
|
|
261
|
+
if (typeof module === 'string') {
|
|
262
|
+
module = require(module);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
if (typeof module !== 'object') {
|
|
266
|
+
return module;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
return module.__esModule ? module.default : module;
|
|
270
|
+
}
|
|
212
271
|
/**
|
|
213
272
|
* Add collection to database
|
|
214
273
|
* @param options
|
|
@@ -247,10 +306,13 @@ class Database extends _events().EventEmitter {
|
|
|
247
306
|
const collection = this.collections.get(name);
|
|
248
307
|
this.emit('beforeRemoveCollection', collection);
|
|
249
308
|
const result = this.collections.delete(name);
|
|
309
|
+
this.sequelize.modelManager.removeModel(collection.model);
|
|
250
310
|
|
|
251
311
|
if (result) {
|
|
252
312
|
this.emit('afterRemoveCollection', collection);
|
|
253
313
|
}
|
|
314
|
+
|
|
315
|
+
return collection;
|
|
254
316
|
}
|
|
255
317
|
|
|
256
318
|
getModel(name) {
|
|
@@ -391,16 +453,27 @@ class Database extends _events().EventEmitter {
|
|
|
391
453
|
})();
|
|
392
454
|
}
|
|
393
455
|
|
|
456
|
+
collectionExistsInDb(name, options) {
|
|
457
|
+
var _this3 = this;
|
|
458
|
+
|
|
459
|
+
return _asyncToGenerator(function* () {
|
|
460
|
+
const tables = yield _this3.sequelize.getQueryInterface().showAllTables({
|
|
461
|
+
transaction: options === null || options === void 0 ? void 0 : options.transaction
|
|
462
|
+
});
|
|
463
|
+
return !!tables.find(table => table === `${_this3.getTablePrefix()}${name}`);
|
|
464
|
+
})();
|
|
465
|
+
}
|
|
466
|
+
|
|
394
467
|
isSqliteMemory() {
|
|
395
468
|
return this.sequelize.getDialect() === 'sqlite' && _lodash().default.get(this.options, 'storage') == ':memory:';
|
|
396
469
|
}
|
|
397
470
|
|
|
398
471
|
auth(options = {}) {
|
|
399
|
-
var
|
|
472
|
+
var _this4 = this;
|
|
400
473
|
|
|
401
474
|
return _asyncToGenerator(function* () {
|
|
402
|
-
const _options$
|
|
403
|
-
|
|
475
|
+
const _options$retry = options.retry,
|
|
476
|
+
retry = _options$retry === void 0 ? 10 : _options$retry,
|
|
404
477
|
others = _objectWithoutProperties(options, _excluded2);
|
|
405
478
|
|
|
406
479
|
const delay = ms => new Promise(yea => setTimeout(yea, ms));
|
|
@@ -410,11 +483,11 @@ class Database extends _events().EventEmitter {
|
|
|
410
483
|
const authenticate = /*#__PURE__*/function () {
|
|
411
484
|
var _ref = _asyncToGenerator(function* () {
|
|
412
485
|
try {
|
|
413
|
-
yield
|
|
486
|
+
yield _this4.sequelize.authenticate(others);
|
|
414
487
|
console.log('Connection has been established successfully.');
|
|
415
488
|
return true;
|
|
416
489
|
} catch (error) {
|
|
417
|
-
if (count >=
|
|
490
|
+
if (count >= retry) {
|
|
418
491
|
throw error;
|
|
419
492
|
}
|
|
420
493
|
|
|
@@ -435,21 +508,21 @@ class Database extends _events().EventEmitter {
|
|
|
435
508
|
}
|
|
436
509
|
|
|
437
510
|
reconnect() {
|
|
438
|
-
var
|
|
511
|
+
var _this5 = this;
|
|
439
512
|
|
|
440
513
|
return _asyncToGenerator(function* () {
|
|
441
|
-
if (
|
|
514
|
+
if (_this5.isSqliteMemory()) {
|
|
442
515
|
return;
|
|
443
516
|
} // @ts-ignore
|
|
444
517
|
|
|
445
518
|
|
|
446
|
-
const ConnectionManager =
|
|
519
|
+
const ConnectionManager = _this5.sequelize.dialect.connectionManager.constructor; // @ts-ignore
|
|
447
520
|
|
|
448
|
-
const connectionManager = new ConnectionManager(
|
|
521
|
+
const connectionManager = new ConnectionManager(_this5.sequelize.dialect, _this5.sequelize); // @ts-ignore
|
|
449
522
|
|
|
450
|
-
|
|
523
|
+
_this5.sequelize.dialect.connectionManager = connectionManager; // @ts-ignore
|
|
451
524
|
|
|
452
|
-
|
|
525
|
+
_this5.sequelize.connectionManager = connectionManager;
|
|
453
526
|
})();
|
|
454
527
|
}
|
|
455
528
|
|
|
@@ -459,65 +532,66 @@ class Database extends _events().EventEmitter {
|
|
|
459
532
|
}
|
|
460
533
|
|
|
461
534
|
close() {
|
|
462
|
-
var
|
|
535
|
+
var _this6 = this;
|
|
463
536
|
|
|
464
537
|
return _asyncToGenerator(function* () {
|
|
465
|
-
if (
|
|
538
|
+
if (_this6.isSqliteMemory()) {
|
|
466
539
|
return;
|
|
467
540
|
}
|
|
468
541
|
|
|
469
|
-
return
|
|
542
|
+
return _this6.sequelize.close();
|
|
470
543
|
})();
|
|
471
544
|
}
|
|
472
545
|
|
|
473
546
|
on(event, listener) {
|
|
474
|
-
|
|
547
|
+
// NOTE: to match if event is a sequelize or model type
|
|
548
|
+
const type = this.modelHook.match(event);
|
|
475
549
|
|
|
476
|
-
if (
|
|
477
|
-
this.sequelize.addHook(
|
|
478
|
-
this.modelHook.bindEvent(
|
|
550
|
+
if (type && !this.modelHook.hasBoundEvent(type)) {
|
|
551
|
+
this.sequelize.addHook(type, this.modelHook.buildSequelizeHook(type));
|
|
552
|
+
this.modelHook.bindEvent(type);
|
|
479
553
|
}
|
|
480
554
|
|
|
481
555
|
return super.on(event, listener);
|
|
482
556
|
}
|
|
483
557
|
|
|
484
558
|
import(options) {
|
|
485
|
-
var
|
|
559
|
+
var _this7 = this;
|
|
486
560
|
|
|
487
561
|
return _asyncToGenerator(function* () {
|
|
488
562
|
const reader = new _collectionImporter.ImporterReader(options.directory, options.extensions);
|
|
489
563
|
const modules = yield reader.read();
|
|
490
564
|
const result = new Map();
|
|
491
565
|
|
|
492
|
-
var
|
|
493
|
-
|
|
566
|
+
var _iterator2 = _createForOfIteratorHelper(modules),
|
|
567
|
+
_step2;
|
|
494
568
|
|
|
495
569
|
try {
|
|
496
|
-
for (
|
|
497
|
-
const module =
|
|
570
|
+
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
571
|
+
const module = _step2.value;
|
|
498
572
|
|
|
499
573
|
if (module.extend) {
|
|
500
574
|
const collectionName = module.collectionOptions.name;
|
|
501
575
|
|
|
502
|
-
const existCollection =
|
|
576
|
+
const existCollection = _this7.getCollection(collectionName);
|
|
503
577
|
|
|
504
578
|
if (existCollection) {
|
|
505
579
|
existCollection.updateOptions(module.collectionOptions, module.mergeOptions);
|
|
506
580
|
} else {
|
|
507
|
-
const existDelayExtends =
|
|
581
|
+
const existDelayExtends = _this7.delayCollectionExtend.get(collectionName) || [];
|
|
508
582
|
|
|
509
|
-
|
|
583
|
+
_this7.delayCollectionExtend.set(collectionName, [...existDelayExtends, module]);
|
|
510
584
|
}
|
|
511
585
|
} else {
|
|
512
|
-
const collection =
|
|
586
|
+
const collection = _this7.collection(module);
|
|
513
587
|
|
|
514
588
|
result.set(collection.name, collection);
|
|
515
589
|
}
|
|
516
590
|
}
|
|
517
591
|
} catch (err) {
|
|
518
|
-
|
|
592
|
+
_iterator2.e(err);
|
|
519
593
|
} finally {
|
|
520
|
-
|
|
594
|
+
_iterator2.f();
|
|
521
595
|
}
|
|
522
596
|
|
|
523
597
|
return result;
|
package/lib/fields/field.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { DataType, ModelAttributeColumnOptions, ModelIndexesOptions, SyncOptions } from 'sequelize';
|
|
1
|
+
import { DataType, ModelAttributeColumnOptions, ModelIndexesOptions, QueryInterfaceOptions, SyncOptions, Transactionable } from 'sequelize';
|
|
2
2
|
import { Collection } from '../collection';
|
|
3
3
|
import { Database } from '../database';
|
|
4
4
|
export interface FieldContext {
|
|
@@ -29,6 +29,9 @@ export declare abstract class Field {
|
|
|
29
29
|
on(eventName: string, listener: (...args: any[]) => void): this;
|
|
30
30
|
off(eventName: string, listener: (...args: any[]) => void): this;
|
|
31
31
|
get(name: string): any;
|
|
32
|
+
remove(): Field;
|
|
33
|
+
removeFromDb(options?: QueryInterfaceOptions): Promise<void>;
|
|
34
|
+
existsInDb(options?: Transactionable): Promise<boolean>;
|
|
32
35
|
merge(obj: any): void;
|
|
33
36
|
bind(): void;
|
|
34
37
|
unbind(): void;
|