@wxn0brp/db 0.8.2 → 0.9.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.
@@ -1,6 +1,7 @@
1
1
  import { existsSync, mkdirSync, promises, statSync } from "fs";
2
2
  import dbActionBase from "../base/actions.js";
3
3
  import gen from "../helpers/gen.js";
4
+ import { resolve, sep } from "path";
4
5
  /**
5
6
  * A class representing database actions on files.
6
7
  * @class
@@ -37,9 +38,11 @@ class dbActionC extends dbActionBase {
37
38
  const collections = allCollections
38
39
  .filter(dirent => dirent.isDirectory())
39
40
  .map(dirent => {
40
- if (dirent.parentPath === this.folder)
41
+ const parentPath = resolve(dirent.parentPath);
42
+ const baseFolder = resolve(this.folder);
43
+ if (parentPath === baseFolder)
41
44
  return dirent.name;
42
- return dirent.parentPath.replace(this.folder + "/", "") + "/" + dirent.name;
45
+ return parentPath.replace(baseFolder + sep, "") + "/" + dirent.name;
43
46
  });
44
47
  return collections;
45
48
  }
@@ -19,6 +19,13 @@ function pickByPath(obj, paths) {
19
19
  }
20
20
  return result;
21
21
  }
22
+ function autoSelect(rel, key) {
23
+ const select = Array.isArray(rel.select) ? [...rel.select] : undefined;
24
+ const shouldDelete = select && !select.includes(key);
25
+ if (shouldDelete)
26
+ select.push(key);
27
+ return [select, shouldDelete];
28
+ }
22
29
  function convertSearchObjToSearchArray(obj, parentKeys = []) {
23
30
  return Object.entries(obj).reduce((acc, [key, value]) => {
24
31
  const currentPath = [...parentKeys, key];
@@ -47,33 +54,40 @@ async function processRelations(dbs, cfg, data, parentList = null) {
47
54
  const db = dbs[dbKey];
48
55
  if (type === "1") {
49
56
  const keys = [...new Set(targets.map(i => i[pk]))];
50
- const results = await db.find(coll, { $in: { [fk]: keys } }, {}, {}, { select });
57
+ const [selectSafe, deleteSelect] = autoSelect(rel, fk);
58
+ const results = await db.find(coll, { $in: { [fk]: keys } }, {}, {}, { select: selectSafe });
51
59
  const map = new Map(results.map(row => [row[fk], row]));
52
60
  for (const item of targets) {
53
61
  const result = map.get(item[pk]) || null;
54
62
  if (result && rel.relations) {
55
63
  await processRelations(dbs, rel.relations, result);
56
64
  }
65
+ if (deleteSelect && result)
66
+ delete result[fk];
57
67
  item[as] = result;
58
68
  }
59
69
  }
60
70
  else if (type === "11") {
61
71
  const cache = new Map();
72
+ const [selectSafe, deleteSelect] = autoSelect(rel, fk);
62
73
  for (const item of targets) {
63
74
  const id = item[pk];
64
75
  if (!cache.has(id)) {
65
- cache.set(id, await db.findOne(coll, { [fk]: id }, {}, { select }));
76
+ cache.set(id, await db.findOne(coll, { [fk]: id }, {}, { select: selectSafe }));
66
77
  }
67
78
  const result = cache.get(id) || null;
68
79
  if (result && rel.relations) {
69
80
  await processRelations(dbs, rel.relations, result);
70
81
  }
82
+ if (deleteSelect && result)
83
+ delete result[fk];
71
84
  item[as] = result;
72
85
  }
73
86
  }
74
87
  else if (type === "1n") {
75
88
  const ids = targets.map(i => i[pk]);
76
- const results = await db.find(coll, { $in: { [fk]: ids } }, {}, findOpts || {}, { select });
89
+ const [selectSafe, deleteSelect] = autoSelect(rel, fk);
90
+ const results = await db.find(coll, { $in: { [fk]: ids } }, {}, findOpts || {}, { select: selectSafe });
77
91
  const grouped = results.reduce((acc, row) => {
78
92
  const id = row[fk];
79
93
  (acc[id] ||= []).push(row);
@@ -85,6 +99,9 @@ async function processRelations(dbs, cfg, data, parentList = null) {
85
99
  if (rel.relations) {
86
100
  await Promise.all(results.map(row => processRelations(dbs, rel.relations, row)));
87
101
  }
102
+ if (deleteSelect)
103
+ for (const r of results)
104
+ delete r[fk];
88
105
  }
89
106
  else if (type === "nm") {
90
107
  if (!through || !through.table || !through.pk || !through.fk) {
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const version = "0.8.2";
1
+ export const version = "0.9.1";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/db",
3
- "version": "0.8.2",
3
+ "version": "0.9.1",
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.",