@wxn0brp/db-core 0.12.5 → 0.12.7

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.
@@ -8,7 +8,13 @@ export class MultiBackend extends ActionsBase {
8
8
  this.primaryBackend = backends[primaryIndex] || backends[0];
9
9
  }
10
10
  async init(...args) {
11
- await Promise.all(this.backends.map(b => b.init?.(...args)));
11
+ for (const b of this.backends) {
12
+ b.adapterOpts = {
13
+ ...b.adapterOpts,
14
+ ...this.adapterOpts,
15
+ };
16
+ }
17
+ await Promise.all(this.backends.map(b => b.init(...args)));
12
18
  }
13
19
  async close(...args) {
14
20
  await Promise.all(this.backends.map(b => b.close(...args)));
@@ -43,7 +43,16 @@ export class RoutedStorage extends ActionsBase {
43
43
  return await fn(first);
44
44
  }
45
45
  async init(...args) {
46
- const all = new Set(this.rules.flatMap(r => r.backends));
46
+ const all = new Set([
47
+ ...this.rules.flatMap(r => r.backends),
48
+ ...this.defaultBackend,
49
+ ]);
50
+ for (const b of all) {
51
+ b.adapterOpts = {
52
+ ...b.adapterOpts,
53
+ ...this.adapterOpts,
54
+ };
55
+ }
47
56
  await Promise.all(Array.from(all).map(b => b.init?.(...args)));
48
57
  }
49
58
  async close(...args) {
@@ -54,14 +63,15 @@ export class RoutedStorage extends ActionsBase {
54
63
  await Promise.all(Array.from(all).map(b => b.close(...args)));
55
64
  }
56
65
  async add(config) {
57
- const res = await this._withFirst(config, b => b.add(config));
58
- await this._withAll({
59
- ...config,
60
- data: res,
61
- }, b => b.add({
62
- ...config,
63
- data: res,
64
- }));
66
+ const backends = this._matchBackends(config);
67
+ const [first, ...rest] = backends;
68
+ const res = await first.add(config);
69
+ if (rest.length > 0) {
70
+ await Promise.all(rest.map(b => b.add({
71
+ ...config,
72
+ data: res,
73
+ })));
74
+ }
65
75
  return res;
66
76
  }
67
77
  async find(config) {
@@ -92,7 +102,10 @@ export class RoutedStorage extends ActionsBase {
92
102
  return this._withAll(collection, b => b.removeCollection(collection), true);
93
103
  }
94
104
  async getCollections() {
95
- const all = await Promise.all(Array.from(new Set(this.rules.flatMap(r => r.backends))).map(b => b.getCollections()));
105
+ const all = await Promise.all(Array.from(new Set([
106
+ ...this.rules.flatMap(r => r.backends),
107
+ ...this.defaultBackend,
108
+ ])).map(b => b.getCollections()));
96
109
  const merged = new Set(all.flat());
97
110
  return Array.from(merged);
98
111
  }
@@ -26,7 +26,7 @@ export declare class ValtheraClass implements ValtheraCompatible {
26
26
  version: string;
27
27
  _plugins: ValtheraPlugin[];
28
28
  _collections: Map<string, Collection<any>>;
29
- plugin(p: ValtheraPlugin): () => void;
29
+ plugin(p: ValtheraPlugin, collections?: string[] | string): () => void;
30
30
  constructor(options: DbOpts);
31
31
  init(...args: any[]): Promise<any>;
32
32
  close(...args: any[]): Promise<any>;
@@ -17,7 +17,14 @@ export class ValtheraClass {
17
17
  version = version;
18
18
  _plugins = [];
19
19
  _collections = new Map();
20
- plugin(p) {
20
+ plugin(p, collections) {
21
+ if (collections) {
22
+ p.collections = Array.isArray(collections)
23
+ ? collections
24
+ : [
25
+ collections,
26
+ ];
27
+ }
21
28
  p.init?.(this);
22
29
  this._plugins.push(p);
23
30
  return () => {
@@ -29,15 +36,13 @@ export class ValtheraClass {
29
36
  constructor(options) {
30
37
  this.options = options;
31
38
  this.executor = options.executor || new SmartExecutor(undefined, false);
32
- if (typeof options.adapter === "function")
33
- return;
34
- else {
39
+ options.executorAware ??= true;
40
+ if (typeof options.adapter !== "function") {
35
41
  this.adapter = options.adapter;
36
42
  if (options.adapterOpts) {
37
43
  this.adapter.adapterOpts = options.adapterOpts;
38
44
  }
39
45
  }
40
- options.executorAware ??= true;
41
46
  }
42
47
  async init(...args) {
43
48
  if (this.adapter?._inited)
@@ -65,11 +70,11 @@ export class ValtheraClass {
65
70
  });
66
71
  }
67
72
  async close(...args) {
68
- if (!this.adapter._inited)
73
+ if (!this.adapter?._inited)
69
74
  return;
70
75
  const self = this;
71
76
  return await this.executor.addOp(async () => {
72
- if (!self.adapter._inited)
77
+ if (!self.adapter?._inited)
73
78
  return;
74
79
  await self.adapter.close(...args);
75
80
  self.adapter._inited = false;
@@ -79,7 +84,8 @@ export class ValtheraClass {
79
84
  await this.init();
80
85
  if (txHandle && typeof query === "object")
81
86
  query.transaction = txHandle;
82
- const plugins = this._plugins;
87
+ const collection = typeof query === "string" ? query : query.collection;
88
+ const plugins = this._plugins.filter(p => !p.collections || p.collections.includes(collection));
83
89
  const self = this;
84
90
  let idx = 0;
85
91
  const ctx = {
@@ -24,7 +24,7 @@ export declare class Collection<D = Data> {
24
24
  /**
25
25
  * Find one data entry in a database.
26
26
  */
27
- findOne(search?: Search<D>, findOpts?: FindOpts<D>, context?: VContext): Promise<D>;
27
+ findOne(search?: Search<D>, findOpts?: FindOpts<D>, context?: VContext): Promise<D | null>;
28
28
  /**
29
29
  * Update data in a database.
30
30
  */
@@ -1,13 +1,10 @@
1
- import { Collection } from "./collection.js";
2
1
  export function forgeTypedValthera(target) {
3
2
  return new Proxy(target, {
4
3
  get(target, prop, receiver) {
5
4
  if (prop in target) {
6
5
  return Reflect.get(target, prop, receiver);
7
6
  }
8
- const collection = new Collection(target, prop);
9
- target[prop] = collection;
10
- return collection;
7
+ return target.c(prop);
11
8
  },
12
9
  set(target, prop, value, receiver) {
13
10
  return Reflect.set(target, prop, value, receiver);
@@ -88,6 +88,7 @@ export async function processRelations(dbs, cfg, data, parentList = null) {
88
88
  [fk]: keys,
89
89
  },
90
90
  },
91
+ dbFindOpts,
91
92
  findOpts: {
92
93
  select: selectSafe,
93
94
  },
@@ -180,6 +181,7 @@ export async function processRelations(dbs, cfg, data, parentList = null) {
180
181
  const allTargetIds = [
181
182
  ...new Set(allPivots.map(p => p[through.fk])),
182
183
  ];
184
+ const [selectSafe, deleteSelect] = autoSelect(rel, fk);
183
185
  const allRelated = await db.find({
184
186
  collection,
185
187
  search: {
@@ -187,8 +189,9 @@ export async function processRelations(dbs, cfg, data, parentList = null) {
187
189
  [fk]: allTargetIds,
188
190
  },
189
191
  },
192
+ dbFindOpts,
190
193
  findOpts: {
191
- select,
194
+ select: selectSafe,
192
195
  },
193
196
  });
194
197
  const targetMap = new Map(allRelated.map(r => [
@@ -203,6 +206,9 @@ export async function processRelations(dbs, cfg, data, parentList = null) {
203
206
  await Promise.all(related.map(row => processRelations(dbs, rel.relations, row)));
204
207
  }
205
208
  }
209
+ if (deleteSelect)
210
+ for (const r of allRelated)
211
+ delete r[fk];
206
212
  }
207
213
  else if (type === "n1") {
208
214
  const allFkIds = [
@@ -221,6 +227,7 @@ export async function processRelations(dbs, cfg, data, parentList = null) {
221
227
  [fk]: allFkIds,
222
228
  },
223
229
  },
230
+ dbFindOpts,
224
231
  findOpts: {
225
232
  select: selectSafe,
226
233
  },
@@ -8,4 +8,5 @@ export interface ValtheraPlugin {
8
8
  name: string;
9
9
  execute(ctx: PluginContext): Promise<any>;
10
10
  init?: (db: ValtheraClass) => void;
11
+ collections?: string[];
11
12
  }
@@ -53,14 +53,16 @@ function mainCheck(obj, fields) {
53
53
  in: (data, value) => value.includes(data),
54
54
  nin: (data, value) => !value.includes(data),
55
55
  type: (data, value) => typeof data === value,
56
- exists: (_, shouldExist, key) => {
57
- if (shouldExist && !(key in obj))
56
+ exists: (targetObj, shouldExist, key) => {
57
+ if (shouldExist && !(key in targetObj))
58
58
  return false;
59
- if (!shouldExist && key in obj)
59
+ if (!shouldExist && key in targetObj)
60
60
  return false;
61
61
  return true;
62
62
  },
63
63
  regex: (data, regexData) => {
64
+ if (typeof data !== "string")
65
+ return false;
64
66
  const regex = typeof regexData === "string" ? new RegExp(regexData) : regexData;
65
67
  return regex.test(data);
66
68
  },
@@ -101,30 +103,48 @@ function _for(fields, obj, opts) {
101
103
  continue;
102
104
  for (const [key, val] of Object.entries(value)) {
103
105
  const targetValue = obj?.[key];
104
- if (typeof val === "object" && val !== null && !Array.isArray(val)) {
105
- if (!deepCheck(val, targetValue, fieldFn))
106
+ if (typeof val === "object" &&
107
+ val !== null &&
108
+ !Array.isArray(val) &&
109
+ !(val instanceof RegExp)) {
110
+ if (!deepCheck(val, targetValue, fieldFn, fieldRaw))
106
111
  return false;
107
112
  }
108
113
  else {
109
- if (!fieldFn(targetValue, val, key))
110
- return false;
114
+ if (fieldRaw === "exists") {
115
+ if (!fieldFn(obj, val, key))
116
+ return false;
117
+ }
118
+ else {
119
+ if (!fieldFn(targetValue, val, key))
120
+ return false;
121
+ }
111
122
  }
112
123
  }
113
124
  }
114
125
  return true;
115
126
  }
116
- function deepCheck(valueObj, targetObj, fieldFn) {
127
+ function deepCheck(valueObj, targetObj, fieldFn, fieldRaw) {
117
128
  if (typeof targetObj !== "object" || targetObj === null)
118
129
  return false;
119
130
  for (const [k, v] of Object.entries(valueObj)) {
120
131
  const targetValue = targetObj[k];
121
- if (typeof v === "object" && v !== null && !Array.isArray(v)) {
122
- if (!deepCheck(v, targetValue, fieldFn))
132
+ if (typeof v === "object" &&
133
+ v !== null &&
134
+ !Array.isArray(v) &&
135
+ !(v instanceof RegExp)) {
136
+ if (!deepCheck(v, targetValue, fieldFn, fieldRaw))
123
137
  return false;
124
138
  }
125
139
  else {
126
- if (!fieldFn(targetValue, v, k))
127
- return false;
140
+ if (fieldRaw === "exists") {
141
+ if (!fieldFn(targetObj, v, k))
142
+ return false;
143
+ }
144
+ else {
145
+ if (!fieldFn(targetValue, v, k))
146
+ return false;
147
+ }
128
148
  }
129
149
  }
130
150
  return true;
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const version = "0.12.5";
1
+ export declare const version = "0.12.7";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const version = "0.12.5";
1
+ export const version = "0.12.7";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/db-core",
3
- "version": "0.12.5",
3
+ "version": "0.12.7",
4
4
  "types": "dist/index.d.ts",
5
5
  "main": "dist/index.js",
6
6
  "author": "wxn0brP",