@nocobase/database 0.9.2-alpha.4 → 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 +104 -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/fields/field.js +1 -0
- package/lib/filter-parser.d.ts +1 -7
- package/lib/filter-parser.js +27 -7
- package/lib/listeners/adjacency-list.d.ts +1 -2
- package/lib/listeners/adjacency-list.js +2 -71
- 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 -1
- package/lib/mock-database.js +3 -1
- package/lib/operators/array.js +7 -4
- package/lib/operators/string.js +1 -1
- package/lib/options-parser.js +14 -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 +106 -90
- package/lib/sql-parser/postgres.js +41 -0
- package/lib/tree-repository/adjacency-list-repository.d.ts +9 -0
- package/lib/tree-repository/adjacency-list-repository.js +165 -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__/collection.test.ts +19 -0
- 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 +267 -1
- package/src/__tests__/repository.test.ts +30 -0
- package/src/__tests__/tree.test.ts +266 -3
- package/src/__tests__/update-guard.test.ts +13 -0
- package/src/collection.ts +79 -65
- package/src/database.ts +26 -42
- package/src/eager-loading/eager-loading-tree.ts +304 -0
- package/src/fields/field.ts +4 -0
- package/src/filter-parser.ts +16 -2
- package/src/listeners/adjacency-list.ts +1 -44
- package/src/listeners/append-child-collection-name-after-repository-find.ts +31 -0
- package/src/listeners/index.ts +3 -2
- package/src/mock-database.ts +3 -1
- package/src/operators/array.ts +8 -4
- package/src/operators/notIn.ts +1 -0
- package/src/operators/string.ts +1 -1
- package/src/options-parser.ts +17 -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 +83 -38
- package/src/sql-parser/postgres.js +25505 -0
- package/src/tree-repository/adjacency-list-repository.ts +159 -0
- package/src/update-guard.ts +21 -16
- package/src/utils.ts +0 -61
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { FindOptions, Repository } from '../repository';
|
|
2
|
+
import lodash from 'lodash';
|
|
3
|
+
|
|
4
|
+
export class AdjacencyListRepository extends Repository {
|
|
5
|
+
async update(options): Promise<any> {
|
|
6
|
+
return super.update({
|
|
7
|
+
...(options || {}),
|
|
8
|
+
addIndex: false,
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
async find(options: FindOptions & { addIndex?: boolean } = {}): Promise<any> {
|
|
13
|
+
if (options.raw || !options.tree) {
|
|
14
|
+
return await super.find(options);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const collection = this.collection;
|
|
18
|
+
const primaryKey = collection.model.primaryKeyAttribute;
|
|
19
|
+
|
|
20
|
+
if (options.fields && !options.fields.includes(primaryKey)) {
|
|
21
|
+
options.fields.push(primaryKey);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const parentNodes = await super.find(options);
|
|
25
|
+
if (parentNodes.length === 0) {
|
|
26
|
+
return [];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const { treeParentField } = collection;
|
|
30
|
+
const foreignKey = treeParentField.options.foreignKey;
|
|
31
|
+
|
|
32
|
+
const childrenKey = collection.treeChildrenField?.name ?? 'children';
|
|
33
|
+
|
|
34
|
+
const parentIds = parentNodes.map((node) => node[primaryKey]);
|
|
35
|
+
|
|
36
|
+
if (parentIds.length == 0) {
|
|
37
|
+
this.database.logger.warn('parentIds is empty');
|
|
38
|
+
return parentNodes;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const sql = this.querySQL(parentIds, collection);
|
|
42
|
+
|
|
43
|
+
const childNodes = await this.database.sequelize.query(sql, {
|
|
44
|
+
type: 'SELECT',
|
|
45
|
+
transaction: options.transaction,
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
const childIds = childNodes.map((node) => node[primaryKey]);
|
|
49
|
+
|
|
50
|
+
const findChildrenOptions = {
|
|
51
|
+
...lodash.omit(options, ['limit', 'offset', 'filterByTk']),
|
|
52
|
+
filter: {
|
|
53
|
+
[primaryKey]: childIds,
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
if (findChildrenOptions.fields) {
|
|
58
|
+
[primaryKey, foreignKey].forEach((field) => {
|
|
59
|
+
if (!findChildrenOptions.fields.includes(field)) {
|
|
60
|
+
findChildrenOptions.fields.push(field);
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const childInstances = await super.find(findChildrenOptions);
|
|
66
|
+
|
|
67
|
+
const nodeMap = {};
|
|
68
|
+
|
|
69
|
+
childInstances.forEach((node) => {
|
|
70
|
+
if (!nodeMap[`${node[foreignKey]}`]) {
|
|
71
|
+
nodeMap[`${node[foreignKey]}`] = [];
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
nodeMap[`${node[foreignKey]}`].push(node);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
function buildTree(parentId) {
|
|
78
|
+
const children = nodeMap[parentId];
|
|
79
|
+
|
|
80
|
+
if (!children) {
|
|
81
|
+
return [];
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return children.map((child) => {
|
|
85
|
+
const childrenValues = buildTree(child.id);
|
|
86
|
+
if (childrenValues.length > 0) {
|
|
87
|
+
child.setDataValue(childrenKey, childrenValues);
|
|
88
|
+
}
|
|
89
|
+
return child;
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
for (const parent of parentNodes) {
|
|
94
|
+
const parentId = parent[primaryKey];
|
|
95
|
+
const children = buildTree(parentId);
|
|
96
|
+
if (children.length > 0) {
|
|
97
|
+
parent.setDataValue(childrenKey, children);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
this.addIndex(parentNodes, childrenKey, options);
|
|
102
|
+
|
|
103
|
+
return parentNodes;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
private addIndex(treeArray, childrenKey, options) {
|
|
107
|
+
function traverse(node, index) {
|
|
108
|
+
// patch for sequelize toJSON
|
|
109
|
+
if (node._options.includeNames && !node._options.includeNames.includes(childrenKey)) {
|
|
110
|
+
node._options.includeNames.push(childrenKey);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (options.addIndex !== false) {
|
|
114
|
+
node.setDataValue('__index', `${index}`);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const children = node.getDataValue(childrenKey);
|
|
118
|
+
|
|
119
|
+
if (children && children.length === 0) {
|
|
120
|
+
node.setDataValue(childrenKey, undefined);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (children && children.length > 0) {
|
|
124
|
+
children.forEach((child, i) => {
|
|
125
|
+
traverse(child, `${index}.${childrenKey}.${i}`);
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
treeArray.forEach((tree, i) => {
|
|
131
|
+
traverse(tree, i);
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
private querySQL(rootIds, collection) {
|
|
136
|
+
const { treeParentField } = collection;
|
|
137
|
+
const foreignKey = treeParentField.options.foreignKey;
|
|
138
|
+
const foreignKeyField = collection.model.rawAttributes[foreignKey].field;
|
|
139
|
+
|
|
140
|
+
const primaryKey = collection.model.primaryKeyAttribute;
|
|
141
|
+
|
|
142
|
+
const queryInterface = this.database.sequelize.getQueryInterface();
|
|
143
|
+
const q = queryInterface.quoteIdentifier.bind(queryInterface);
|
|
144
|
+
|
|
145
|
+
return `
|
|
146
|
+
WITH RECURSIVE cte AS (
|
|
147
|
+
SELECT ${q(primaryKey)}, ${q(foreignKeyField)}, 1 AS level
|
|
148
|
+
FROM ${collection.quotedTableName()}
|
|
149
|
+
WHERE ${q(foreignKeyField)} IN (${rootIds.join(',')})
|
|
150
|
+
UNION ALL
|
|
151
|
+
SELECT t.${q(primaryKey)}, t.${q(foreignKeyField)}, cte.level + 1 AS level
|
|
152
|
+
FROM ${collection.quotedTableName()} t
|
|
153
|
+
JOIN cte ON t.${q(foreignKeyField)} = cte.${q(primaryKey)}
|
|
154
|
+
)
|
|
155
|
+
SELECT ${q(primaryKey)}, ${q(foreignKeyField)} as ${q(foreignKey)}, level
|
|
156
|
+
FROM cte
|
|
157
|
+
`;
|
|
158
|
+
}
|
|
159
|
+
}
|
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
|
}
|