@wxn0brp/db 0.7.1 → 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.
Files changed (2) hide show
  1. package/dist/relation.js +9 -6
  2. 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 = await db.findOne(coll, { [fk]: item[pk] }, { projection: select });
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 || null;
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 } }, { ...findOpts, projection: select });
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 } }, { projection: select });
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)));
@@ -109,7 +112,7 @@ class Relation {
109
112
  async find(path, search, relations, select, findOpts = {}) {
110
113
  const [dbKey, coll] = path;
111
114
  const db = this.dbs[dbKey];
112
- const data = await db.find(coll, search, findOpts);
115
+ const data = await db.find(coll, search, {}, findOpts);
113
116
  if (relations)
114
117
  await processRelations(this.dbs, relations, null, data);
115
118
  if (typeof select === "object" && !Array.isArray(select)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/db",
3
- "version": "0.7.1",
3
+ "version": "0.7.3",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "description": "A simple file-based database management system with support for CRUD operations, custom queries, and graph structures.",