@nocobase/database 0.7.4-alpha.1 → 0.7.4-alpha.4
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/database.d.ts +3 -6
- package/lib/database.js +29 -27
- package/lib/mock-database.js +0 -3
- package/lib/repository.d.ts +1 -1
- package/package.json +3 -3
- package/src/__tests__/update-associations.test.ts +1 -1
- package/src/database.ts +24 -20
- package/src/mock-database.ts +0 -3
- package/src/repository.ts +1 -1
package/lib/database.d.ts
CHANGED
|
@@ -106,21 +106,18 @@ export declare class Database extends EventEmitter implements AsyncEmitter {
|
|
|
106
106
|
closed(): any;
|
|
107
107
|
close(): Promise<void>;
|
|
108
108
|
on(event: string | symbol, listener: any): this;
|
|
109
|
+
extendCollection(collectionOptions: CollectionOptions, mergeOptions?: MergeOptions): void;
|
|
109
110
|
import(options: {
|
|
110
111
|
directory: string;
|
|
111
112
|
extensions?: ImportFileExtension[];
|
|
112
113
|
}): Promise<Map<string, Collection>>;
|
|
113
114
|
emitAsync: (event: string | symbol, ...args: any[]) => Promise<boolean>;
|
|
114
115
|
}
|
|
115
|
-
export declare function
|
|
116
|
+
export declare function extendCollection(collectionOptions: CollectionOptions, mergeOptions?: MergeOptions): {
|
|
116
117
|
collectionOptions: CollectionOptions;
|
|
117
118
|
mergeOptions: MergeOptions;
|
|
118
119
|
extend: boolean;
|
|
119
120
|
};
|
|
121
|
+
export declare const extend: typeof extendCollection;
|
|
120
122
|
export declare const defineCollection: (collectionOptions: CollectionOptions) => CollectionOptions;
|
|
121
|
-
export declare const extendCollection: (collectionOptions: CollectionOptions, mergeOptions?: MergeOptions) => {
|
|
122
|
-
collectionOptions: CollectionOptions;
|
|
123
|
-
mergeOptions: MergeOptions;
|
|
124
|
-
extend: boolean;
|
|
125
|
-
};
|
|
126
123
|
export default Database;
|
package/lib/database.js
CHANGED
|
@@ -3,9 +3,8 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.defineCollection = exports.default = exports.Database = void 0;
|
|
7
|
-
exports.
|
|
8
|
-
exports.extendCollection = void 0;
|
|
6
|
+
exports.extend = exports.defineCollection = exports.default = exports.Database = void 0;
|
|
7
|
+
exports.extendCollection = extendCollection;
|
|
9
8
|
|
|
10
9
|
function _utils() {
|
|
11
10
|
const data = require("@nocobase/utils");
|
|
@@ -158,7 +157,7 @@ class DatabaseVersion {
|
|
|
158
157
|
postgres: {
|
|
159
158
|
sql: 'select version() as version',
|
|
160
159
|
get: v => {
|
|
161
|
-
const keys = v.split(
|
|
160
|
+
const keys = v.split(/\s|,/);
|
|
162
161
|
keys.shift();
|
|
163
162
|
return _semver().default.minVersion(keys.shift()).version;
|
|
164
163
|
}
|
|
@@ -215,7 +214,7 @@ class Database extends _events().EventEmitter {
|
|
|
215
214
|
},
|
|
216
215
|
force: false
|
|
217
216
|
}
|
|
218
|
-
}, options);
|
|
217
|
+
}, _lodash().default.clone(options));
|
|
219
218
|
|
|
220
219
|
if (options.storage && options.storage !== ':memory:') {
|
|
221
220
|
if (!(0, _path().isAbsolute)(options.storage)) {
|
|
@@ -279,6 +278,11 @@ class Database extends _events().EventEmitter {
|
|
|
279
278
|
sequelize: this.sequelize
|
|
280
279
|
}))
|
|
281
280
|
});
|
|
281
|
+
this.sequelize.beforeDefine((model, opts) => {
|
|
282
|
+
if (this.options.tablePrefix) {
|
|
283
|
+
opts.tableName = `${this.options.tablePrefix}${opts.tableName || opts.modelName || opts.name.plural}`;
|
|
284
|
+
}
|
|
285
|
+
});
|
|
282
286
|
}
|
|
283
287
|
|
|
284
288
|
addMigration(item) {
|
|
@@ -620,6 +624,21 @@ class Database extends _events().EventEmitter {
|
|
|
620
624
|
return super.on(event, listener);
|
|
621
625
|
}
|
|
622
626
|
|
|
627
|
+
extendCollection(collectionOptions, mergeOptions) {
|
|
628
|
+
const collectionName = collectionOptions.name;
|
|
629
|
+
const existCollection = this.getCollection(collectionName);
|
|
630
|
+
|
|
631
|
+
if (existCollection) {
|
|
632
|
+
existCollection.updateOptions(collectionOptions, mergeOptions);
|
|
633
|
+
} else {
|
|
634
|
+
const existDelayExtends = this.delayCollectionExtend.get(collectionName) || [];
|
|
635
|
+
this.delayCollectionExtend.set(collectionName, [...existDelayExtends, {
|
|
636
|
+
collectionOptions,
|
|
637
|
+
mergeOptions
|
|
638
|
+
}]);
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
|
|
623
642
|
import(options) {
|
|
624
643
|
var _this8 = this;
|
|
625
644
|
|
|
@@ -636,17 +655,7 @@ class Database extends _events().EventEmitter {
|
|
|
636
655
|
const module = _step2.value;
|
|
637
656
|
|
|
638
657
|
if (module.extend) {
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
const existCollection = _this8.getCollection(collectionName);
|
|
642
|
-
|
|
643
|
-
if (existCollection) {
|
|
644
|
-
existCollection.updateOptions(module.collectionOptions, module.mergeOptions);
|
|
645
|
-
} else {
|
|
646
|
-
const existDelayExtends = _this8.delayCollectionExtend.get(collectionName) || [];
|
|
647
|
-
|
|
648
|
-
_this8.delayCollectionExtend.set(collectionName, [...existDelayExtends, module]);
|
|
649
|
-
}
|
|
658
|
+
_this8.extendCollection(module.collectionOptions, module.mergeOptions);
|
|
650
659
|
} else {
|
|
651
660
|
const collection = _this8.collection(module);
|
|
652
661
|
|
|
@@ -667,7 +676,7 @@ class Database extends _events().EventEmitter {
|
|
|
667
676
|
|
|
668
677
|
exports.Database = Database;
|
|
669
678
|
|
|
670
|
-
function
|
|
679
|
+
function extendCollection(collectionOptions, mergeOptions) {
|
|
671
680
|
return {
|
|
672
681
|
collectionOptions,
|
|
673
682
|
mergeOptions,
|
|
@@ -675,21 +684,14 @@ function extend(collectionOptions, mergeOptions) {
|
|
|
675
684
|
};
|
|
676
685
|
}
|
|
677
686
|
|
|
687
|
+
const extend = extendCollection;
|
|
688
|
+
exports.extend = extend;
|
|
689
|
+
|
|
678
690
|
const defineCollection = collectionOptions => {
|
|
679
691
|
return collectionOptions;
|
|
680
692
|
};
|
|
681
693
|
|
|
682
694
|
exports.defineCollection = defineCollection;
|
|
683
|
-
|
|
684
|
-
const extendCollection = (collectionOptions, mergeOptions) => {
|
|
685
|
-
return {
|
|
686
|
-
collectionOptions,
|
|
687
|
-
mergeOptions,
|
|
688
|
-
extend: true
|
|
689
|
-
};
|
|
690
|
-
};
|
|
691
|
-
|
|
692
|
-
exports.extendCollection = extendCollection;
|
|
693
695
|
(0, _utils().applyMixins)(Database, [_utils().AsyncEmitter]);
|
|
694
696
|
var _default = Database;
|
|
695
697
|
exports.default = _default;
|
package/lib/mock-database.js
CHANGED
|
@@ -42,9 +42,6 @@ class MockDatabase extends _database.Database {
|
|
|
42
42
|
tablePrefix: `mock_${(0, _utils().uid)(6)}_`,
|
|
43
43
|
dialect: 'sqlite'
|
|
44
44
|
}, options));
|
|
45
|
-
this.sequelize.beforeDefine((model, opts) => {
|
|
46
|
-
opts.tableName = `${this.getTablePrefix()}${opts.tableName || opts.modelName || opts.name.plural}`;
|
|
47
|
-
});
|
|
48
45
|
}
|
|
49
46
|
|
|
50
47
|
}
|
package/lib/repository.d.ts
CHANGED
|
@@ -44,7 +44,7 @@ export interface CommonFindOptions extends Transactionable {
|
|
|
44
44
|
sort?: Sort;
|
|
45
45
|
context?: any;
|
|
46
46
|
}
|
|
47
|
-
interface FindOneOptions extends FindOptions
|
|
47
|
+
interface FindOneOptions extends FindOptions {
|
|
48
48
|
}
|
|
49
49
|
export interface DestroyOptions extends SequelizeDestroyOptions {
|
|
50
50
|
filter?: Filter;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nocobase/database",
|
|
3
|
-
"version": "0.7.4-alpha.
|
|
3
|
+
"version": "0.7.4-alpha.4",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"types": "./lib/index.d.ts",
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
}
|
|
13
13
|
],
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@nocobase/utils": "0.7.4-alpha.
|
|
15
|
+
"@nocobase/utils": "0.7.4-alpha.4",
|
|
16
16
|
"async-mutex": "^0.3.2",
|
|
17
17
|
"deepmerge": "^4.2.2",
|
|
18
18
|
"flat": "^5.0.2",
|
|
@@ -30,5 +30,5 @@
|
|
|
30
30
|
"url": "git+https://github.com/nocobase/nocobase.git",
|
|
31
31
|
"directory": "packages/database"
|
|
32
32
|
},
|
|
33
|
-
"gitHead": "
|
|
33
|
+
"gitHead": "726c06b721b217a6aa5c1421b899d1315e552b57"
|
|
34
34
|
}
|
|
@@ -390,7 +390,7 @@ describe('update associations', () => {
|
|
|
390
390
|
afterEach(async () => {
|
|
391
391
|
await db.close();
|
|
392
392
|
});
|
|
393
|
-
test
|
|
393
|
+
test('set through value', async () => {
|
|
394
394
|
const p1 = await Post.repository.create({
|
|
395
395
|
values: {
|
|
396
396
|
title: 'hello',
|
package/src/database.ts
CHANGED
|
@@ -86,7 +86,7 @@ class DatabaseVersion {
|
|
|
86
86
|
postgres: {
|
|
87
87
|
sql: 'select version() as version',
|
|
88
88
|
get: (v) => {
|
|
89
|
-
const keys = v.split(
|
|
89
|
+
const keys = v.split(/\s|,/);
|
|
90
90
|
keys.shift();
|
|
91
91
|
return semver.minVersion(keys.shift()).version;
|
|
92
92
|
},
|
|
@@ -135,7 +135,7 @@ export class Database extends EventEmitter implements AsyncEmitter {
|
|
|
135
135
|
},
|
|
136
136
|
force: false,
|
|
137
137
|
},
|
|
138
|
-
...options,
|
|
138
|
+
...lodash.clone(options),
|
|
139
139
|
};
|
|
140
140
|
|
|
141
141
|
if (options.storage && options.storage !== ':memory:') {
|
|
@@ -197,6 +197,12 @@ export class Database extends EventEmitter implements AsyncEmitter {
|
|
|
197
197
|
sequelize: this.sequelize,
|
|
198
198
|
}),
|
|
199
199
|
});
|
|
200
|
+
|
|
201
|
+
this.sequelize.beforeDefine((model, opts) => {
|
|
202
|
+
if (this.options.tablePrefix) {
|
|
203
|
+
opts.tableName = `${this.options.tablePrefix}${opts.tableName || opts.modelName || opts.name.plural}`;
|
|
204
|
+
}
|
|
205
|
+
});
|
|
200
206
|
}
|
|
201
207
|
|
|
202
208
|
addMigration(item) {
|
|
@@ -460,6 +466,18 @@ export class Database extends EventEmitter implements AsyncEmitter {
|
|
|
460
466
|
return super.on(event, listener);
|
|
461
467
|
}
|
|
462
468
|
|
|
469
|
+
extendCollection(collectionOptions: CollectionOptions, mergeOptions?: MergeOptions) {
|
|
470
|
+
const collectionName = collectionOptions.name;
|
|
471
|
+
const existCollection = this.getCollection(collectionName);
|
|
472
|
+
if (existCollection) {
|
|
473
|
+
existCollection.updateOptions(collectionOptions, mergeOptions);
|
|
474
|
+
} else {
|
|
475
|
+
const existDelayExtends = this.delayCollectionExtend.get(collectionName) || [];
|
|
476
|
+
|
|
477
|
+
this.delayCollectionExtend.set(collectionName, [...existDelayExtends, { collectionOptions, mergeOptions }]);
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
|
|
463
481
|
async import(options: { directory: string; extensions?: ImportFileExtension[] }): Promise<Map<string, Collection>> {
|
|
464
482
|
const reader = new ImporterReader(options.directory, options.extensions);
|
|
465
483
|
const modules = await reader.read();
|
|
@@ -467,15 +485,7 @@ export class Database extends EventEmitter implements AsyncEmitter {
|
|
|
467
485
|
|
|
468
486
|
for (const module of modules) {
|
|
469
487
|
if (module.extend) {
|
|
470
|
-
|
|
471
|
-
const existCollection = this.getCollection(collectionName);
|
|
472
|
-
if (existCollection) {
|
|
473
|
-
existCollection.updateOptions(module.collectionOptions, module.mergeOptions);
|
|
474
|
-
} else {
|
|
475
|
-
const existDelayExtends = this.delayCollectionExtend.get(collectionName) || [];
|
|
476
|
-
|
|
477
|
-
this.delayCollectionExtend.set(collectionName, [...existDelayExtends, module]);
|
|
478
|
-
}
|
|
488
|
+
this.extendCollection(module.collectionOptions, module.mergeOptions);
|
|
479
489
|
} else {
|
|
480
490
|
const collection = this.collection(module);
|
|
481
491
|
result.set(collection.name, collection);
|
|
@@ -488,7 +498,7 @@ export class Database extends EventEmitter implements AsyncEmitter {
|
|
|
488
498
|
declare emitAsync: (event: string | symbol, ...args: any[]) => Promise<boolean>;
|
|
489
499
|
}
|
|
490
500
|
|
|
491
|
-
export function
|
|
501
|
+
export function extendCollection(collectionOptions: CollectionOptions, mergeOptions?: MergeOptions) {
|
|
492
502
|
return {
|
|
493
503
|
collectionOptions,
|
|
494
504
|
mergeOptions,
|
|
@@ -496,18 +506,12 @@ export function extend(collectionOptions: CollectionOptions, mergeOptions?: Merg
|
|
|
496
506
|
};
|
|
497
507
|
}
|
|
498
508
|
|
|
509
|
+
export const extend = extendCollection;
|
|
510
|
+
|
|
499
511
|
export const defineCollection = (collectionOptions: CollectionOptions) => {
|
|
500
512
|
return collectionOptions;
|
|
501
513
|
};
|
|
502
514
|
|
|
503
|
-
export const extendCollection = (collectionOptions: CollectionOptions, mergeOptions?: MergeOptions) => {
|
|
504
|
-
return {
|
|
505
|
-
collectionOptions,
|
|
506
|
-
mergeOptions,
|
|
507
|
-
extend: true,
|
|
508
|
-
};
|
|
509
|
-
};
|
|
510
|
-
|
|
511
515
|
applyMixins(Database, [AsyncEmitter]);
|
|
512
516
|
|
|
513
517
|
export default Database;
|
package/src/mock-database.ts
CHANGED
|
@@ -10,9 +10,6 @@ export class MockDatabase extends Database {
|
|
|
10
10
|
dialect: 'sqlite',
|
|
11
11
|
...options,
|
|
12
12
|
});
|
|
13
|
-
this.sequelize.beforeDefine((model, opts) => {
|
|
14
|
-
opts.tableName = `${this.getTablePrefix()}${opts.tableName || opts.modelName || opts.name.plural}`;
|
|
15
|
-
});
|
|
16
13
|
}
|
|
17
14
|
}
|
|
18
15
|
|
package/src/repository.ts
CHANGED
|
@@ -75,7 +75,7 @@ export interface CommonFindOptions extends Transactionable {
|
|
|
75
75
|
context?: any;
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
-
interface FindOneOptions extends FindOptions
|
|
78
|
+
interface FindOneOptions extends FindOptions {}
|
|
79
79
|
|
|
80
80
|
export interface DestroyOptions extends SequelizeDestroyOptions {
|
|
81
81
|
filter?: Filter;
|