@wxn0brp/db 0.7.2 → 0.7.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/relation.js +8 -5
- package/package.json +1 -1
package/dist/relation.js
CHANGED
|
@@ -42,21 +42,24 @@ async function processRelations(dbs, cfg, data, parentList = null) {
|
|
|
42
42
|
if (!cfg.hasOwnProperty(key))
|
|
43
43
|
continue;
|
|
44
44
|
const rel = cfg[key];
|
|
45
|
-
const { pk = "_id", fk = "_id", type = "1", path, as = key, select, findOpts, through } = rel;
|
|
45
|
+
const { pk = "_id", fk = "_id", type = "1", path, as = key, select = [], findOpts = {}, through } = rel;
|
|
46
46
|
const [dbKey, coll] = path;
|
|
47
47
|
const db = dbs[dbKey];
|
|
48
48
|
if (type === "1") {
|
|
49
|
+
const keys = [...new Set(targets.map(i => i[pk]))];
|
|
50
|
+
const results = await db.find(coll, { [fk]: { $in: keys } }, {}, {}, { select });
|
|
51
|
+
const map = new Map(results.map(row => [row[fk], row]));
|
|
49
52
|
for (const item of targets) {
|
|
50
|
-
const result =
|
|
53
|
+
const result = map.get(item[pk]) || null;
|
|
51
54
|
if (result && rel.relations) {
|
|
52
55
|
await processRelations(dbs, rel.relations, result);
|
|
53
56
|
}
|
|
54
|
-
item[as] = result
|
|
57
|
+
item[as] = result;
|
|
55
58
|
}
|
|
56
59
|
}
|
|
57
60
|
else if (type === "1n") {
|
|
58
61
|
const ids = targets.map(i => i[pk]);
|
|
59
|
-
const results = await db.find(coll, { $in: { [fk]: ids } }, {
|
|
62
|
+
const results = await db.find(coll, { $in: { [fk]: ids } }, {}, findOpts || {}, { select });
|
|
60
63
|
const grouped = results.reduce((acc, row) => {
|
|
61
64
|
const id = row[fk];
|
|
62
65
|
(acc[id] ||= []).push(row);
|
|
@@ -77,7 +80,7 @@ async function processRelations(dbs, cfg, data, parentList = null) {
|
|
|
77
80
|
const pivotDb = dbs[through.db || dbKey];
|
|
78
81
|
const pivots = await pivotDb.find(through.table, { [through.pk]: item[pk] });
|
|
79
82
|
const ids = pivots.map(p => p[through.fk]);
|
|
80
|
-
const related = await db.find(coll, { [fk]: { $in: ids } }, {
|
|
83
|
+
const related = await db.find(coll, { [fk]: { $in: ids } }, {}, {}, { select });
|
|
81
84
|
item[as] = related;
|
|
82
85
|
if (rel.relations) {
|
|
83
86
|
await Promise.all(related.map(row => processRelations(dbs, rel.relations, row)));
|
package/package.json
CHANGED