@nocobase/database 0.9.3-alpha.1 → 0.9.4-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.d.ts +9 -9
- package/lib/collection.js +100 -96
- package/lib/database.d.ts +4 -4
- package/lib/database.js +25 -53
- package/lib/eager-loading/eager-loading-tree.d.ts +23 -0
- package/lib/eager-loading/eager-loading-tree.js +338 -0
- package/lib/filter-parser.d.ts +1 -7
- package/lib/filter-parser.js +27 -7
- package/lib/listeners/append-child-collection-name-after-repository-find.d.ts +5 -0
- package/lib/listeners/append-child-collection-name-after-repository-find.js +40 -0
- package/lib/listeners/index.js +2 -0
- package/lib/mock-database.js +3 -1
- package/lib/operators/string.js +1 -1
- package/lib/options-parser.js +4 -0
- package/lib/query-interface/postgres-query-interface.js +2 -2
- package/lib/relation-repository/belongs-to-many-repository.d.ts +2 -1
- package/lib/relation-repository/belongs-to-many-repository.js +58 -37
- package/lib/relation-repository/hasmany-repository.d.ts +2 -1
- package/lib/relation-repository/hasmany-repository.js +31 -16
- package/lib/relation-repository/multiple-relation-repository.js +8 -26
- package/lib/relation-repository/relation-repository.d.ts +1 -7
- package/lib/relation-repository/single-relation-repository.d.ts +1 -1
- package/lib/relation-repository/single-relation-repository.js +10 -16
- package/lib/repository.d.ts +11 -8
- package/lib/repository.js +104 -89
- package/lib/sql-parser/postgres.js +41 -0
- package/lib/update-guard.d.ts +1 -1
- package/lib/update-guard.js +16 -13
- package/lib/utils.d.ts +0 -7
- package/lib/utils.js +0 -76
- package/package.json +4 -4
- package/src/__tests__/eager-loading/eager-loading-tree.test.ts +393 -0
- package/src/__tests__/migrator.test.ts +4 -0
- package/src/__tests__/relation-repository/hasone-repository.test.ts +1 -0
- package/src/__tests__/repository/aggregation.test.ts +297 -0
- package/src/__tests__/repository/count.test.ts +1 -1
- package/src/__tests__/repository/find.test.ts +10 -1
- package/src/__tests__/repository.test.ts +30 -0
- package/src/__tests__/update-guard.test.ts +13 -0
- package/src/collection.ts +74 -66
- package/src/database.ts +26 -42
- package/src/eager-loading/eager-loading-tree.ts +304 -0
- package/src/filter-parser.ts +16 -2
- package/src/listeners/adjacency-list.ts +1 -3
- package/src/listeners/append-child-collection-name-after-repository-find.ts +31 -0
- package/src/listeners/index.ts +2 -0
- package/src/mock-database.ts +3 -1
- package/src/operators/notIn.ts +1 -0
- package/src/operators/string.ts +1 -1
- package/src/options-parser.ts +5 -0
- package/src/query-interface/postgres-query-interface.ts +1 -1
- package/src/relation-repository/belongs-to-many-repository.ts +33 -1
- package/src/relation-repository/hasmany-repository.ts +17 -0
- package/src/relation-repository/multiple-relation-repository.ts +14 -19
- package/src/relation-repository/single-relation-repository.ts +13 -15
- package/src/repository.ts +79 -36
- package/src/sql-parser/postgres.js +25505 -0
- package/src/update-guard.ts +21 -16
- package/src/utils.ts +0 -61
package/src/update-guard.ts
CHANGED
|
@@ -10,6 +10,7 @@ type UpdateValues = {
|
|
|
10
10
|
};
|
|
11
11
|
|
|
12
12
|
type UpdateAction = 'create' | 'update';
|
|
13
|
+
|
|
13
14
|
export class UpdateGuard {
|
|
14
15
|
model: ModelStatic<any>;
|
|
15
16
|
action: UpdateAction;
|
|
@@ -18,6 +19,21 @@ export class UpdateGuard {
|
|
|
18
19
|
private blackList: BlackList;
|
|
19
20
|
private whiteList: WhiteList;
|
|
20
21
|
|
|
22
|
+
static fromOptions(model, options) {
|
|
23
|
+
const guard = new UpdateGuard();
|
|
24
|
+
guard.setModel(model);
|
|
25
|
+
guard.setWhiteList(options.whitelist);
|
|
26
|
+
guard.setBlackList(options.blacklist);
|
|
27
|
+
guard.setAction(lodash.get(options, 'action', 'update'));
|
|
28
|
+
guard.setAssociationKeysToBeUpdate(options.updateAssociationValues);
|
|
29
|
+
|
|
30
|
+
if (options.underscored) {
|
|
31
|
+
guard.underscored = options.underscored;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return guard;
|
|
35
|
+
}
|
|
36
|
+
|
|
21
37
|
setAction(action: UpdateAction) {
|
|
22
38
|
this.action = action;
|
|
23
39
|
}
|
|
@@ -78,6 +94,10 @@ export class UpdateGuard {
|
|
|
78
94
|
let associationValues = associationsValues[association];
|
|
79
95
|
|
|
80
96
|
const filterAssociationToBeUpdate = (value) => {
|
|
97
|
+
if (value === null) {
|
|
98
|
+
return value;
|
|
99
|
+
}
|
|
100
|
+
|
|
81
101
|
const associationKeysToBeUpdate = this.associationKeysToBeUpdate || [];
|
|
82
102
|
|
|
83
103
|
if (associationKeysToBeUpdate.includes(association)) {
|
|
@@ -111,7 +131,7 @@ export class UpdateGuard {
|
|
|
111
131
|
|
|
112
132
|
if (Array.isArray(associationValues)) {
|
|
113
133
|
associationValues = associationValues.map((value) => {
|
|
114
|
-
if (typeof value == 'string' || typeof value == 'number') {
|
|
134
|
+
if (value === undefined || value === null || typeof value == 'string' || typeof value == 'number') {
|
|
115
135
|
return value;
|
|
116
136
|
} else {
|
|
117
137
|
return sanitizeValue(value);
|
|
@@ -155,19 +175,4 @@ export class UpdateGuard {
|
|
|
155
175
|
|
|
156
176
|
return result;
|
|
157
177
|
}
|
|
158
|
-
|
|
159
|
-
static fromOptions(model, options) {
|
|
160
|
-
const guard = new UpdateGuard();
|
|
161
|
-
guard.setModel(model);
|
|
162
|
-
guard.setWhiteList(options.whitelist);
|
|
163
|
-
guard.setBlackList(options.blacklist);
|
|
164
|
-
guard.setAction(lodash.get(options, 'action', 'update'));
|
|
165
|
-
guard.setAssociationKeysToBeUpdate(options.updateAssociationValues);
|
|
166
|
-
|
|
167
|
-
if (options.underscored) {
|
|
168
|
-
guard.underscored = options.underscored;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
return guard;
|
|
172
|
-
}
|
|
173
178
|
}
|
package/src/utils.ts
CHANGED
|
@@ -1,69 +1,8 @@
|
|
|
1
1
|
import crypto from 'crypto';
|
|
2
2
|
import Database from './database';
|
|
3
3
|
import { IdentifierError } from './errors/identifier-error';
|
|
4
|
-
import { Model } from './model';
|
|
5
4
|
import lodash from 'lodash';
|
|
6
5
|
|
|
7
|
-
type HandleAppendsQueryOptions = {
|
|
8
|
-
templateModel: any;
|
|
9
|
-
queryPromises: Array<any>;
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
export async function handleAppendsQuery(options: HandleAppendsQueryOptions) {
|
|
13
|
-
const { templateModel, queryPromises } = options;
|
|
14
|
-
|
|
15
|
-
if (!templateModel) {
|
|
16
|
-
return [];
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const primaryKey = templateModel.constructor.primaryKeyAttribute;
|
|
20
|
-
|
|
21
|
-
const results = await Promise.all(queryPromises);
|
|
22
|
-
|
|
23
|
-
let rows: Array<Model>;
|
|
24
|
-
|
|
25
|
-
for (const appendedResult of results) {
|
|
26
|
-
if (!rows) {
|
|
27
|
-
rows = appendedResult.rows;
|
|
28
|
-
|
|
29
|
-
if (rows.length == 0) {
|
|
30
|
-
return [];
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const modelOptions = templateModel['_options'];
|
|
34
|
-
for (const row of rows) {
|
|
35
|
-
row['_options'] = {
|
|
36
|
-
...row['_options'],
|
|
37
|
-
include: modelOptions['include'],
|
|
38
|
-
includeNames: modelOptions['includeNames'],
|
|
39
|
-
includeMap: modelOptions['includeMap'],
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
continue;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
for (let i = 0; i < appendedResult.rows.length; i++) {
|
|
46
|
-
const appendingRow = appendedResult.rows[i];
|
|
47
|
-
const key = appendedResult.include.association;
|
|
48
|
-
const val = appendingRow.get(key);
|
|
49
|
-
|
|
50
|
-
const rowKey = appendingRow.get(primaryKey);
|
|
51
|
-
|
|
52
|
-
const targetIndex = rows.findIndex((row) => row.get(primaryKey) === rowKey);
|
|
53
|
-
|
|
54
|
-
if (targetIndex === -1) {
|
|
55
|
-
throw new Error('target row not found');
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
rows[targetIndex].set(key, val, {
|
|
59
|
-
raw: true,
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
return rows;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
6
|
export function md5(value: string) {
|
|
68
7
|
return crypto.createHash('md5').update(value).digest('hex');
|
|
69
8
|
}
|