@wxn0brp/db-core 0.12.2 → 0.12.3
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/dist/helpers/relation.js +70 -14
- package/dist/types/relation.d.ts +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +1 -2
package/dist/helpers/relation.js
CHANGED
|
@@ -67,6 +67,8 @@ export async function processRelations(dbs, cfg, data, parentList = null) {
|
|
|
67
67
|
: [
|
|
68
68
|
data,
|
|
69
69
|
];
|
|
70
|
+
if (targets.length === 0)
|
|
71
|
+
return;
|
|
70
72
|
for (const key in cfg) {
|
|
71
73
|
if (!Object.hasOwn(cfg, key))
|
|
72
74
|
continue;
|
|
@@ -158,29 +160,83 @@ export async function processRelations(dbs, cfg, data, parentList = null) {
|
|
|
158
160
|
else if (type === "nm") {
|
|
159
161
|
if (!through || !through.table || !through.pk || !through.fk)
|
|
160
162
|
throw new Error(`Relation type "nm" requires a defined "through" in "${key}"`);
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
163
|
+
const allSourceIds = targets.map(i => i[pk]);
|
|
164
|
+
const pivotDb = dbs[through.db || dbKey];
|
|
165
|
+
const allPivots = await pivotDb.find({
|
|
166
|
+
collection: through.table,
|
|
167
|
+
search: {
|
|
168
|
+
$in: {
|
|
169
|
+
[through.pk]: allSourceIds,
|
|
167
170
|
},
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
|
|
171
|
+
},
|
|
172
|
+
});
|
|
173
|
+
const pivotMap = new Map();
|
|
174
|
+
for (const p of allPivots) {
|
|
175
|
+
const srcId = p[through.pk];
|
|
176
|
+
const arr = pivotMap.get(srcId) || [];
|
|
177
|
+
arr.push(p[through.fk]);
|
|
178
|
+
pivotMap.set(srcId, arr);
|
|
179
|
+
}
|
|
180
|
+
const allTargetIds = [
|
|
181
|
+
...new Set(allPivots.map(p => p[through.fk])),
|
|
182
|
+
];
|
|
183
|
+
const allRelated = await db.find({
|
|
184
|
+
collection,
|
|
185
|
+
search: {
|
|
186
|
+
$in: {
|
|
187
|
+
[fk]: allTargetIds,
|
|
188
|
+
},
|
|
189
|
+
},
|
|
190
|
+
findOpts: {
|
|
191
|
+
select,
|
|
192
|
+
},
|
|
193
|
+
});
|
|
194
|
+
const targetMap = new Map(allRelated.map(r => [
|
|
195
|
+
r[fk],
|
|
196
|
+
r,
|
|
197
|
+
]));
|
|
198
|
+
for (const item of targets) {
|
|
199
|
+
const targetIds = pivotMap.get(item[pk]) || [];
|
|
200
|
+
const related = targetIds.map(id => targetMap.get(id)).filter(Boolean);
|
|
201
|
+
item[as] = related;
|
|
202
|
+
if (rel.relations && related.length > 0) {
|
|
203
|
+
await Promise.all(related.map(row => processRelations(dbs, rel.relations, row)));
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
else if (type === "n1") {
|
|
208
|
+
const allFkIds = [
|
|
209
|
+
...new Set(targets.flatMap(i => i[pk] || [])),
|
|
210
|
+
];
|
|
211
|
+
if (allFkIds.length === 0) {
|
|
212
|
+
for (const item of targets)
|
|
213
|
+
item[as] = null;
|
|
214
|
+
}
|
|
215
|
+
else {
|
|
216
|
+
const [selectSafe, deleteSelect] = autoSelect(rel, fk);
|
|
217
|
+
const results = await db.find({
|
|
171
218
|
collection,
|
|
172
219
|
search: {
|
|
173
220
|
$in: {
|
|
174
|
-
[fk]:
|
|
221
|
+
[fk]: allFkIds,
|
|
175
222
|
},
|
|
176
223
|
},
|
|
177
224
|
findOpts: {
|
|
178
|
-
select,
|
|
225
|
+
select: selectSafe,
|
|
179
226
|
},
|
|
180
227
|
});
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
228
|
+
const map = new Map(results.map(row => [
|
|
229
|
+
row[fk],
|
|
230
|
+
row,
|
|
231
|
+
]));
|
|
232
|
+
for (const item of targets) {
|
|
233
|
+
const fks = item[pk] || [];
|
|
234
|
+
const result = map.get(fks[0]) || null;
|
|
235
|
+
if (result && rel.relations)
|
|
236
|
+
await processRelations(dbs, rel.relations, result);
|
|
237
|
+
if (deleteSelect && result)
|
|
238
|
+
delete result[fk];
|
|
239
|
+
item[as] = result;
|
|
184
240
|
}
|
|
185
241
|
}
|
|
186
242
|
}
|
package/dist/types/relation.d.ts
CHANGED
package/dist/version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const version = "0.12.
|
|
1
|
+
export declare const version = "0.12.3";
|
package/dist/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const version = "0.12.
|
|
1
|
+
export const version = "0.12.3";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wxn0brp/db-core",
|
|
3
|
-
"version": "0.12.
|
|
3
|
+
"version": "0.12.3",
|
|
4
4
|
"types": "dist/index.d.ts",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"author": "wxn0brP",
|
|
@@ -17,7 +17,6 @@
|
|
|
17
17
|
},
|
|
18
18
|
"devDependencies": {
|
|
19
19
|
"@types/bun": "*",
|
|
20
|
-
"@types/node": "*",
|
|
21
20
|
"fast-check": "^4",
|
|
22
21
|
"tsc-alias": "^1",
|
|
23
22
|
"typescript": "^7"
|