@wxn0brp/db-core 0.12.2-alpha.0 → 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.
@@ -6,10 +6,7 @@ export declare abstract class ActionsBase implements ActionsBaseInterface {
6
6
  _inited: boolean;
7
7
  adapterOpts: AdapterOpts;
8
8
  smartExecutor: boolean;
9
- get numberId(): boolean;
10
- set numberId(value: boolean);
11
- get idKey(): string;
12
- set idKey(value: string);
9
+ version: string;
13
10
  init(...args: any[]): Promise<void>;
14
11
  close(...args: any[]): Promise<void>;
15
12
  abstract getCollections(): Promise<string[]>;
@@ -1,4 +1,5 @@
1
1
  import { setDataForToggleOne, setDataForUpdateOneOrAdd, } from "../helpers/assignDataPush.js";
2
+ import { version } from "../version.js";
2
3
  export class ActionsBase {
3
4
  _inited = true;
4
5
  adapterOpts = {
@@ -6,18 +7,7 @@ export class ActionsBase {
6
7
  idKey: "_id",
7
8
  };
8
9
  smartExecutor = false;
9
- get numberId() {
10
- return this.adapterOpts.numberId ?? false;
11
- }
12
- set numberId(value) {
13
- this.adapterOpts.numberId = value;
14
- }
15
- get idKey() {
16
- return this.adapterOpts.idKey ?? "_id";
17
- }
18
- set idKey(value) {
19
- this.adapterOpts.idKey = value;
20
- }
10
+ version = "ActionsBase-core-" + version;
21
11
  async init(...args) { }
22
12
  async close(...args) { }
23
13
  async updateOneOrAdd(config) {
@@ -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
- for (const item of targets) {
162
- const pivotDb = dbs[through.db || dbKey];
163
- const pivots = await pivotDb.find({
164
- collection: through.table,
165
- search: {
166
- [through.pk]: item[pk],
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
- const ids = pivots.map(p => p[through.fk]);
170
- const related = await db.find({
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]: ids,
221
+ [fk]: allFkIds,
175
222
  },
176
223
  },
177
224
  findOpts: {
178
- select,
225
+ select: selectSafe,
179
226
  },
180
227
  });
181
- item[as] = related;
182
- if (rel.relations) {
183
- await Promise.all(related.map(row => processRelations(dbs, rel.relations, row)));
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
  }
@@ -19,7 +19,7 @@ export declare namespace RelationTypes {
19
19
  as?: string;
20
20
  select?: string[];
21
21
  dbFindOpts?: DbFindOpts;
22
- type?: "1" | "11" | "1n" | "nm";
22
+ type?: "1" | "11" | "1n" | "nm" | "n1";
23
23
  relations?: Relation;
24
24
  through?: {
25
25
  table: string;
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const version = "0.0.1";
1
+ export declare const version = "0.12.3";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const version = "0.12.2-alpha.0";
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.2-alpha.0",
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"