@nocobase/database 0.7.2-alpha.7 → 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 +31 -30
- package/lib/fields/uid-field.d.ts +3 -0
- package/lib/fields/uid-field.js +25 -5
- 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 -21
- package/src/fields/uid-field.ts +23 -5
- 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
|
}
|
|
@@ -169,7 +168,7 @@ class DatabaseVersion {
|
|
|
169
168
|
const dialect = _Object$keys[_i];
|
|
170
169
|
|
|
171
170
|
if (_this.db.inDialect(dialect)) {
|
|
172
|
-
var _result
|
|
171
|
+
var _result$;
|
|
173
172
|
|
|
174
173
|
if (!(versions === null || versions === void 0 ? void 0 : versions[dialect])) {
|
|
175
174
|
return false;
|
|
@@ -179,8 +178,7 @@ class DatabaseVersion {
|
|
|
179
178
|
_yield$_this$db$seque2 = _slicedToArray(_yield$_this$db$seque, 1),
|
|
180
179
|
result = _yield$_this$db$seque2[0];
|
|
181
180
|
|
|
182
|
-
|
|
183
|
-
return _semver().default.satisfies(dialects[dialect].get(result === null || result === void 0 ? void 0 : (_result$2 = result[0]) === null || _result$2 === void 0 ? void 0 : _result$2.version), versions[dialect]);
|
|
181
|
+
return _semver().default.satisfies(dialects[dialect].get(result === null || result === void 0 ? void 0 : (_result$ = result[0]) === null || _result$ === void 0 ? void 0 : _result$.version), versions[dialect]);
|
|
184
182
|
}
|
|
185
183
|
}
|
|
186
184
|
|
|
@@ -216,7 +214,7 @@ class Database extends _events().EventEmitter {
|
|
|
216
214
|
},
|
|
217
215
|
force: false
|
|
218
216
|
}
|
|
219
|
-
}, options);
|
|
217
|
+
}, _lodash().default.clone(options));
|
|
220
218
|
|
|
221
219
|
if (options.storage && options.storage !== ':memory:') {
|
|
222
220
|
if (!(0, _path().isAbsolute)(options.storage)) {
|
|
@@ -280,6 +278,11 @@ class Database extends _events().EventEmitter {
|
|
|
280
278
|
sequelize: this.sequelize
|
|
281
279
|
}))
|
|
282
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
|
+
});
|
|
283
286
|
}
|
|
284
287
|
|
|
285
288
|
addMigration(item) {
|
|
@@ -621,6 +624,21 @@ class Database extends _events().EventEmitter {
|
|
|
621
624
|
return super.on(event, listener);
|
|
622
625
|
}
|
|
623
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
|
+
|
|
624
642
|
import(options) {
|
|
625
643
|
var _this8 = this;
|
|
626
644
|
|
|
@@ -637,17 +655,7 @@ class Database extends _events().EventEmitter {
|
|
|
637
655
|
const module = _step2.value;
|
|
638
656
|
|
|
639
657
|
if (module.extend) {
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
const existCollection = _this8.getCollection(collectionName);
|
|
643
|
-
|
|
644
|
-
if (existCollection) {
|
|
645
|
-
existCollection.updateOptions(module.collectionOptions, module.mergeOptions);
|
|
646
|
-
} else {
|
|
647
|
-
const existDelayExtends = _this8.delayCollectionExtend.get(collectionName) || [];
|
|
648
|
-
|
|
649
|
-
_this8.delayCollectionExtend.set(collectionName, [...existDelayExtends, module]);
|
|
650
|
-
}
|
|
658
|
+
_this8.extendCollection(module.collectionOptions, module.mergeOptions);
|
|
651
659
|
} else {
|
|
652
660
|
const collection = _this8.collection(module);
|
|
653
661
|
|
|
@@ -668,7 +676,7 @@ class Database extends _events().EventEmitter {
|
|
|
668
676
|
|
|
669
677
|
exports.Database = Database;
|
|
670
678
|
|
|
671
|
-
function
|
|
679
|
+
function extendCollection(collectionOptions, mergeOptions) {
|
|
672
680
|
return {
|
|
673
681
|
collectionOptions,
|
|
674
682
|
mergeOptions,
|
|
@@ -676,21 +684,14 @@ function extend(collectionOptions, mergeOptions) {
|
|
|
676
684
|
};
|
|
677
685
|
}
|
|
678
686
|
|
|
687
|
+
const extend = extendCollection;
|
|
688
|
+
exports.extend = extend;
|
|
689
|
+
|
|
679
690
|
const defineCollection = collectionOptions => {
|
|
680
691
|
return collectionOptions;
|
|
681
692
|
};
|
|
682
693
|
|
|
683
694
|
exports.defineCollection = defineCollection;
|
|
684
|
-
|
|
685
|
-
const extendCollection = (collectionOptions, mergeOptions) => {
|
|
686
|
-
return {
|
|
687
|
-
collectionOptions,
|
|
688
|
-
mergeOptions,
|
|
689
|
-
extend: true
|
|
690
|
-
};
|
|
691
|
-
};
|
|
692
|
-
|
|
693
|
-
exports.extendCollection = extendCollection;
|
|
694
695
|
(0, _utils().applyMixins)(Database, [_utils().AsyncEmitter]);
|
|
695
696
|
var _default = Database;
|
|
696
697
|
exports.default = _default;
|
|
@@ -3,8 +3,11 @@ import { BaseColumnFieldOptions, Field } from './field';
|
|
|
3
3
|
export declare class UidField extends Field {
|
|
4
4
|
get dataType(): DataTypes.StringDataTypeConstructor;
|
|
5
5
|
init(): void;
|
|
6
|
+
bind(): void;
|
|
7
|
+
unbind(): void;
|
|
6
8
|
}
|
|
7
9
|
export interface UidFieldOptions extends BaseColumnFieldOptions {
|
|
8
10
|
type: 'uid';
|
|
9
11
|
prefix?: string;
|
|
12
|
+
pattern?: string;
|
|
10
13
|
}
|
package/lib/fields/uid-field.js
CHANGED
|
@@ -40,19 +40,39 @@ class UidField extends _field.Field {
|
|
|
40
40
|
const _this$options = this.options,
|
|
41
41
|
name = _this$options.name,
|
|
42
42
|
_this$options$prefix = _this$options.prefix,
|
|
43
|
-
prefix = _this$options$prefix === void 0 ? '' : _this$options$prefix
|
|
44
|
-
|
|
45
|
-
|
|
43
|
+
prefix = _this$options$prefix === void 0 ? '' : _this$options$prefix,
|
|
44
|
+
pattern = _this$options.pattern;
|
|
45
|
+
const re = new RegExp(pattern || '^[A-Za-z0-9][A-Za-z0-9_-]*$');
|
|
46
|
+
|
|
47
|
+
this.listener = /*#__PURE__*/function () {
|
|
46
48
|
var _ref = _asyncToGenerator(function* (instance) {
|
|
47
|
-
|
|
49
|
+
const value = instance.get(name);
|
|
50
|
+
|
|
51
|
+
if (!value) {
|
|
48
52
|
instance.set(name, `${prefix}${(0, _utils().uid)()}`);
|
|
53
|
+
} else if (re.test(value)) {
|
|
54
|
+
instance.set(name, value);
|
|
55
|
+
} else {
|
|
56
|
+
throw new Error(`uid '${value}' is invalid`);
|
|
49
57
|
}
|
|
50
58
|
});
|
|
51
59
|
|
|
52
60
|
return function (_x) {
|
|
53
61
|
return _ref.apply(this, arguments);
|
|
54
62
|
};
|
|
55
|
-
}()
|
|
63
|
+
}();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
bind() {
|
|
67
|
+
super.bind();
|
|
68
|
+
this.on('beforeCreate', this.listener);
|
|
69
|
+
this.on('beforeUpdate', this.listener);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
unbind() {
|
|
73
|
+
super.unbind();
|
|
74
|
+
this.off('beforeCreate', this.listener);
|
|
75
|
+
this.off('beforeUpdate', this.listener);
|
|
56
76
|
}
|
|
57
77
|
|
|
58
78
|
}
|
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.
|
|
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.
|
|
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
|
},
|
|
@@ -98,7 +98,6 @@ class DatabaseVersion {
|
|
|
98
98
|
return false;
|
|
99
99
|
}
|
|
100
100
|
const [result] = (await this.db.sequelize.query(dialects[dialect].sql)) as any;
|
|
101
|
-
console.log(`db version: ${dialects[dialect].get(result?.[0]?.version)}`);
|
|
102
101
|
return semver.satisfies(dialects[dialect].get(result?.[0]?.version), versions[dialect]);
|
|
103
102
|
}
|
|
104
103
|
}
|
|
@@ -136,7 +135,7 @@ export class Database extends EventEmitter implements AsyncEmitter {
|
|
|
136
135
|
},
|
|
137
136
|
force: false,
|
|
138
137
|
},
|
|
139
|
-
...options,
|
|
138
|
+
...lodash.clone(options),
|
|
140
139
|
};
|
|
141
140
|
|
|
142
141
|
if (options.storage && options.storage !== ':memory:') {
|
|
@@ -198,6 +197,12 @@ export class Database extends EventEmitter implements AsyncEmitter {
|
|
|
198
197
|
sequelize: this.sequelize,
|
|
199
198
|
}),
|
|
200
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
|
+
});
|
|
201
206
|
}
|
|
202
207
|
|
|
203
208
|
addMigration(item) {
|
|
@@ -461,6 +466,18 @@ export class Database extends EventEmitter implements AsyncEmitter {
|
|
|
461
466
|
return super.on(event, listener);
|
|
462
467
|
}
|
|
463
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
|
+
|
|
464
481
|
async import(options: { directory: string; extensions?: ImportFileExtension[] }): Promise<Map<string, Collection>> {
|
|
465
482
|
const reader = new ImporterReader(options.directory, options.extensions);
|
|
466
483
|
const modules = await reader.read();
|
|
@@ -468,15 +485,7 @@ export class Database extends EventEmitter implements AsyncEmitter {
|
|
|
468
485
|
|
|
469
486
|
for (const module of modules) {
|
|
470
487
|
if (module.extend) {
|
|
471
|
-
|
|
472
|
-
const existCollection = this.getCollection(collectionName);
|
|
473
|
-
if (existCollection) {
|
|
474
|
-
existCollection.updateOptions(module.collectionOptions, module.mergeOptions);
|
|
475
|
-
} else {
|
|
476
|
-
const existDelayExtends = this.delayCollectionExtend.get(collectionName) || [];
|
|
477
|
-
|
|
478
|
-
this.delayCollectionExtend.set(collectionName, [...existDelayExtends, module]);
|
|
479
|
-
}
|
|
488
|
+
this.extendCollection(module.collectionOptions, module.mergeOptions);
|
|
480
489
|
} else {
|
|
481
490
|
const collection = this.collection(module);
|
|
482
491
|
result.set(collection.name, collection);
|
|
@@ -489,7 +498,7 @@ export class Database extends EventEmitter implements AsyncEmitter {
|
|
|
489
498
|
declare emitAsync: (event: string | symbol, ...args: any[]) => Promise<boolean>;
|
|
490
499
|
}
|
|
491
500
|
|
|
492
|
-
export function
|
|
501
|
+
export function extendCollection(collectionOptions: CollectionOptions, mergeOptions?: MergeOptions) {
|
|
493
502
|
return {
|
|
494
503
|
collectionOptions,
|
|
495
504
|
mergeOptions,
|
|
@@ -497,18 +506,12 @@ export function extend(collectionOptions: CollectionOptions, mergeOptions?: Merg
|
|
|
497
506
|
};
|
|
498
507
|
}
|
|
499
508
|
|
|
509
|
+
export const extend = extendCollection;
|
|
510
|
+
|
|
500
511
|
export const defineCollection = (collectionOptions: CollectionOptions) => {
|
|
501
512
|
return collectionOptions;
|
|
502
513
|
};
|
|
503
514
|
|
|
504
|
-
export const extendCollection = (collectionOptions: CollectionOptions, mergeOptions?: MergeOptions) => {
|
|
505
|
-
return {
|
|
506
|
-
collectionOptions,
|
|
507
|
-
mergeOptions,
|
|
508
|
-
extend: true,
|
|
509
|
-
};
|
|
510
|
-
};
|
|
511
|
-
|
|
512
515
|
applyMixins(Database, [AsyncEmitter]);
|
|
513
516
|
|
|
514
517
|
export default Database;
|
package/src/fields/uid-field.ts
CHANGED
|
@@ -8,17 +8,35 @@ export class UidField extends Field {
|
|
|
8
8
|
}
|
|
9
9
|
|
|
10
10
|
init() {
|
|
11
|
-
const { name, prefix = '' } = this.options;
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
const { name, prefix = '', pattern } = this.options;
|
|
12
|
+
const re = new RegExp(pattern || '^[A-Za-z0-9][A-Za-z0-9_-]*$');
|
|
13
|
+
this.listener = async (instance) => {
|
|
14
|
+
const value = instance.get(name);
|
|
15
|
+
if (!value) {
|
|
15
16
|
instance.set(name, `${prefix}${uid()}`);
|
|
17
|
+
} else if (re.test(value)) {
|
|
18
|
+
instance.set(name, value);
|
|
19
|
+
} else {
|
|
20
|
+
throw new Error(`uid '${value}' is invalid`);
|
|
16
21
|
}
|
|
17
|
-
}
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
bind() {
|
|
26
|
+
super.bind();
|
|
27
|
+
this.on('beforeCreate', this.listener);
|
|
28
|
+
this.on('beforeUpdate', this.listener);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
unbind() {
|
|
32
|
+
super.unbind();
|
|
33
|
+
this.off('beforeCreate', this.listener);
|
|
34
|
+
this.off('beforeUpdate', this.listener);
|
|
18
35
|
}
|
|
19
36
|
}
|
|
20
37
|
|
|
21
38
|
export interface UidFieldOptions extends BaseColumnFieldOptions {
|
|
22
39
|
type: 'uid';
|
|
23
40
|
prefix?: string;
|
|
41
|
+
pattern?: string;
|
|
24
42
|
}
|
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;
|