@wxn0brp/db 0.7.0 → 0.7.2
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
|
@@ -38,7 +38,10 @@ async function processRelations(dbs, cfg, data, parentList = null) {
|
|
|
38
38
|
return;
|
|
39
39
|
const batchMode = Array.isArray(parentList);
|
|
40
40
|
const targets = batchMode ? parentList : [data];
|
|
41
|
-
for (const
|
|
41
|
+
for (const key in cfg) {
|
|
42
|
+
if (!cfg.hasOwnProperty(key))
|
|
43
|
+
continue;
|
|
44
|
+
const rel = cfg[key];
|
|
42
45
|
const { pk = "_id", fk = "_id", type = "1", path, as = key, select, findOpts, through } = rel;
|
|
43
46
|
const [dbKey, coll] = path;
|
|
44
47
|
const db = dbs[dbKey];
|
|
@@ -53,7 +56,7 @@ async function processRelations(dbs, cfg, data, parentList = null) {
|
|
|
53
56
|
}
|
|
54
57
|
else if (type === "1n") {
|
|
55
58
|
const ids = targets.map(i => i[pk]);
|
|
56
|
-
const results = await db.find(coll, {
|
|
59
|
+
const results = await db.find(coll, { $in: { [fk]: ids } }, { ...findOpts, projection: select });
|
|
57
60
|
const grouped = results.reduce((acc, row) => {
|
|
58
61
|
const id = row[fk];
|
|
59
62
|
(acc[id] ||= []).push(row);
|
|
@@ -97,7 +100,7 @@ class Relation {
|
|
|
97
100
|
const data = await db.findOne(coll, search);
|
|
98
101
|
if (!data)
|
|
99
102
|
return null;
|
|
100
|
-
if (typeof select === "object") {
|
|
103
|
+
if (typeof select === "object" && !Array.isArray(select)) {
|
|
101
104
|
select = convertSearchObjToSearchArray(select);
|
|
102
105
|
}
|
|
103
106
|
await processRelations(this.dbs, relations, data);
|
|
@@ -106,10 +109,10 @@ class Relation {
|
|
|
106
109
|
async find(path, search, relations, select, findOpts = {}) {
|
|
107
110
|
const [dbKey, coll] = path;
|
|
108
111
|
const db = this.dbs[dbKey];
|
|
109
|
-
const data = await db.find(coll, search, findOpts);
|
|
112
|
+
const data = await db.find(coll, search, {}, findOpts);
|
|
110
113
|
if (relations)
|
|
111
114
|
await processRelations(this.dbs, relations, null, data);
|
|
112
|
-
if (typeof select === "object") {
|
|
115
|
+
if (typeof select === "object" && !Array.isArray(select)) {
|
|
113
116
|
select = convertSearchObjToSearchArray(select);
|
|
114
117
|
}
|
|
115
118
|
return select ? data.map(d => pickByPath(d, select)) : data;
|
package/package.json
CHANGED