@wxn0brp/db-core 0.11.6 → 0.12.0-alpha.0

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/db/memory.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { CustomActionsBase } from "../base/custom.js";
2
2
  import { CustomFileCpu } from "../customFileCpu.js";
3
- import { forgeValthera } from "../helpers/forge.js";
3
+ import { forgeTypedValthera } from "../helpers/forge.js";
4
4
  import { ValtheraClass } from "./valthera.js";
5
5
  export class MemoryAction extends CustomActionsBase {
6
6
  memory;
@@ -13,10 +13,10 @@ export class MemoryAction extends CustomActionsBase {
13
13
  _readMemory(key) {
14
14
  if (!this.memory.has(key))
15
15
  return [];
16
- return this.memory.get(key);
16
+ return structuredClone(this.memory.get(key));
17
17
  }
18
18
  _writeMemory(key, data) {
19
- this.memory.set(key, data);
19
+ this.memory.set(key, structuredClone(data));
20
20
  }
21
21
  async getCollections() {
22
22
  const collections = Array.from(this.memory.keys());
@@ -48,9 +48,9 @@ export class ValtheraMemory extends ValtheraClass {
48
48
  export function createMemoryValthera(data) {
49
49
  const db = new ValtheraMemory();
50
50
  if (!data)
51
- return forgeValthera(db);
51
+ return forgeTypedValthera(db);
52
52
  for (const collection of Object.keys(data)) {
53
53
  db.adapter.memory.set(collection, data[collection]);
54
54
  }
55
- return forgeValthera(db);
55
+ return forgeTypedValthera(db);
56
56
  }
@@ -20,32 +20,10 @@ export declare class ValtheraClass implements ValtheraCompatible {
20
20
  } & {
21
21
  "*": (name: keyof ValtheraCompatible, query: VQuery, result: any) => void;
22
22
  }>;
23
- /** @deprecated typo */
24
- emiter: VEE<{
25
- c: (query: VQuery, result: Collection<unknown>) => void;
26
- getCollections: (query: VQuery, result: string[]) => void;
27
- ensureCollection: (query: VQuery, result: boolean) => void;
28
- issetCollection: (query: VQuery, result: boolean) => void;
29
- add: (query: VQuery, result: unknown) => void;
30
- find: (query: VQuery, result: unknown[]) => void;
31
- findOne: (query: VQuery, result: unknown) => void;
32
- update: (query: VQuery, result: unknown[]) => void;
33
- updateOne: (query: VQuery, result: unknown) => void;
34
- remove: (query: VQuery, result: unknown[]) => void;
35
- removeOne: (query: VQuery, result: unknown) => void;
36
- removeCollection: (query: VQuery, result: boolean) => void;
37
- updateOneOrAdd: (query: VQuery, result: VQueryT.UpdateOneOrAddResult<unknown>) => void;
38
- toggleOne: (query: VQuery, result: VQueryT.ToggleOneResult<unknown>) => void;
39
- } & {
40
- "*": (name: keyof ValtheraCompatible, query: VQuery, result: any) => void;
41
- }>;
42
23
  version: string;
43
24
  _plugins: ValtheraPlugin[];
25
+ _collections: Map<string, Collection<any>>;
44
26
  plugin(p: ValtheraPlugin): () => void;
45
- /** @deprecated use `adapter` */
46
- get dbAction(): ActionsBase;
47
- /** @deprecated use `adapter` */
48
- set dbAction(action: ActionsBase);
49
27
  constructor(options: DbOpts);
50
28
  init(...args: any[]): Promise<any>;
51
29
  close(...args: any[]): Promise<any>;
@@ -100,9 +78,6 @@ export declare class ValtheraClass implements ValtheraCompatible {
100
78
  updateOneOrAdd<T = Data>(query: VQueryT.UpdateOneOrAdd<T>): Promise<VQueryT.UpdateOneOrAddResult<T>>;
101
79
  /**
102
80
  * Asynchronously removes one entry in a database or adds a new one if it doesn't exist. Usage e.g. for toggling a flag.
103
- * Returns a promise resolving to `false` if the entry was found and removed,
104
- * or `true` if the entry was added. The returned value reflects the state of the database
105
- * after the operation.
106
81
  */
107
82
  toggleOne<T = Data>(query: VQueryT.ToggleOne<T>): Promise<VQueryT.ToggleOneResult<T>>;
108
83
  /**
@@ -11,10 +11,9 @@ export class ValtheraClass {
11
11
  adapter;
12
12
  executor;
13
13
  emitter = new VEE();
14
- /** @deprecated typo */
15
- emiter = this.emitter;
16
14
  version = version;
17
15
  _plugins = [];
16
+ _collections = new Map();
18
17
  plugin(p) {
19
18
  p.init?.(this);
20
19
  this._plugins.push(p);
@@ -24,25 +23,9 @@ export class ValtheraClass {
24
23
  this._plugins.splice(i, 1);
25
24
  };
26
25
  }
27
- /** @deprecated use `adapter` */
28
- get dbAction() {
29
- return this.adapter;
30
- }
31
- /** @deprecated use `adapter` */
32
- set dbAction(action) {
33
- this.adapter = action;
34
- }
35
26
  constructor(options) {
36
27
  this.options = options;
37
28
  this.executor = options.executor || new SmartExecutor(undefined, false);
38
- options.adapter ??= options.dbAction;
39
- options.dbAction ??= options.adapter;
40
- options.adapterOpts ??= {};
41
- if (options.numberId !== undefined &&
42
- options.adapterOpts.numberId === undefined)
43
- options.adapterOpts.numberId = options.numberId;
44
- if (options.idKey !== undefined && options.adapterOpts.idKey === undefined)
45
- options.adapterOpts.idKey = options.idKey;
46
29
  if (typeof options.adapter === "function")
47
30
  return;
48
31
  else {
@@ -109,7 +92,11 @@ export class ValtheraClass {
109
92
  * Create a new instance of a Collection class.
110
93
  */
111
94
  c(collection) {
112
- return new Collection(this, collection);
95
+ if (this._collections.has(collection))
96
+ return this._collections.get(collection);
97
+ const col = new Collection(this, collection);
98
+ this._collections.set(collection, col);
99
+ return col;
113
100
  }
114
101
  /**
115
102
  * Get the names of all available databases.
@@ -201,9 +188,6 @@ export class ValtheraClass {
201
188
  }
202
189
  /**
203
190
  * Asynchronously removes one entry in a database or adds a new one if it doesn't exist. Usage e.g. for toggling a flag.
204
- * Returns a promise resolving to `false` if the entry was found and removed,
205
- * or `true` if the entry was added. The returned value reflects the state of the database
206
- * after the operation.
207
191
  */
208
192
  toggleOne(query) {
209
193
  query.data ||= {};
@@ -46,9 +46,6 @@ export declare class Collection<D = Data> {
46
46
  updateOneOrAdd(search: Search<D>, updater: Updater<D>, { add_arg, context, id_gen }?: UpdateOneOrAdd<D>): Promise<VQueryT.UpdateOneOrAddResult<D>>;
47
47
  /**
48
48
  * Asynchronously removes one entry in a database or adds a new one if it doesn't exist. Usage e.g. for toggling a flag.
49
- * Returns a promise resolving to `false` if the entry was found and removed,
50
- * or `true` if the entry was added. The returned value reflects the state of the database
51
- * after the operation.
52
49
  */
53
50
  toggleOne(search: Search<D>, data?: Arg<D>, context?: VContext): Promise<VQueryT.ToggleOneResult<D>>;
54
51
  }
@@ -92,9 +92,6 @@ export class Collection {
92
92
  }
93
93
  /**
94
94
  * Asynchronously removes one entry in a database or adds a new one if it doesn't exist. Usage e.g. for toggling a flag.
95
- * Returns a promise resolving to `false` if the entry was found and removed,
96
- * or `true` if the entry was added. The returned value reflects the state of the database
97
- * after the operation.
98
95
  */
99
96
  toggleOne(search, data = {}, context = {}) {
100
97
  return this.db.toggleOne({
@@ -2,12 +2,6 @@ import { Collection } from "./collection.js";
2
2
  import { Data } from "../types/data.js";
3
3
  import { ValtheraCompatible } from "../types/valthera.js";
4
4
  import { ValtheraClass } from "../db/valthera.js";
5
- export declare function forgeValthera<T extends string>(target: ValtheraClass): ValtheraClass & {
6
- [K in T]: Collection;
7
- };
8
- export declare function forgeValthera<T extends string>(target: ValtheraCompatible): ValtheraCompatible & {
9
- [K in T]: Collection;
10
- };
11
5
  export declare function forgeTypedValthera<T extends Record<string, Data>>(target: ValtheraClass): ValtheraClass & {
12
6
  [K in keyof T]: Collection<T[K]>;
13
7
  };
@@ -1,7 +1,4 @@
1
1
  import { Collection } from "./collection.js";
2
- export function forgeValthera(target) {
3
- return forgeTypedValthera(target);
4
- }
5
2
  export function forgeTypedValthera(target) {
6
3
  return new Proxy(target, {
7
4
  get(target, prop, receiver) {
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  export { CustomFileCpu } from "./customFileCpu.js";
2
2
  export { createMemoryValthera, ValtheraMemory } from "./db/memory.js";
3
3
  export { ValtheraClass } from "./db/valthera.js";
4
- export { forgeTypedValthera, forgeValthera } from "./helpers/forge.js";
4
+ export { forgeTypedValthera } from "./helpers/forge.js";
5
5
  export { genId } from "./helpers/gen.js";
6
6
  export { Relation } from "./helpers/relation.js";
7
7
  export { convertIdToUnix, sortByIds } from "./utils/id.js";
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  export { CustomFileCpu } from "./customFileCpu.js";
2
2
  export { createMemoryValthera, ValtheraMemory } from "./db/memory.js";
3
3
  export { ValtheraClass } from "./db/valthera.js";
4
- export { forgeTypedValthera, forgeValthera } from "./helpers/forge.js";
4
+ export { forgeTypedValthera } from "./helpers/forge.js";
5
5
  export { genId } from "./helpers/gen.js";
6
6
  export { Relation } from "./helpers/relation.js";
7
7
  export { convertIdToUnix, sortByIds } from "./utils/id.js";
@@ -4,10 +4,6 @@ import { VQueryT } from "./query.js";
4
4
  export interface ActionsBaseInterface {
5
5
  _inited: boolean;
6
6
  adapterOpts: AdapterOpts;
7
- /** @deprecated use `adapterOpts.numberId` */
8
- numberId: boolean;
9
- /** @deprecated use `adapterOpts.idKey` */
10
- idKey: string;
11
7
  smartExecutor: boolean;
12
8
  init(...args: any[]): Promise<void>;
13
9
  close(...args: any[]): Promise<void>;
@@ -2,23 +2,11 @@ import { ActionsBase } from "../base/actions.js";
2
2
  import { Executor } from "../helpers/executor.js";
3
3
  import { KeysMatching } from "./utils.js";
4
4
  export type AdapterValue = ActionsBase | (() => Promise<ActionsBase>);
5
- type AdapterOption = {
5
+ export interface DbOpts {
6
6
  adapter: AdapterValue;
7
- /** @deprecated use `adapter` */
8
- dbAction?: AdapterValue;
9
- } | {
10
- adapter?: AdapterValue;
11
- /** @deprecated use `adapter` */
12
- dbAction: AdapterValue;
13
- };
14
- export type DbOpts = AdapterOption & {
15
7
  executor?: Executor;
16
8
  adapterOpts?: AdapterOpts;
17
- /** @deprecated use `adapterOpts.numberId` */
18
- numberId?: boolean;
19
- /** @deprecated use `adapterOpts.idKey` */
20
- idKey?: string;
21
- };
9
+ }
22
10
  export interface AdapterOpts {
23
11
  numberId?: boolean;
24
12
  idKey?: string;
@@ -46,4 +34,3 @@ export interface FindOpts<T = any> {
46
34
  exclude?: FieldPath<T>[];
47
35
  transform?: Function;
48
36
  }
49
- export {};
@@ -65,22 +65,10 @@ export type ArrayOperators<T = any> = {
65
65
  $arrInc?: {
66
66
  [K in keyof T]?: T[K] extends any[] ? T[K][number][] : T[K][];
67
67
  };
68
- /**
69
- * @deprecated Use $arrInc instead.
70
- */
71
- $arrinc?: {
72
- [K in keyof T]?: T[K] extends any[] ? T[K][number][] : T[K][];
73
- };
74
68
  /** [1, 2, 3] array includes all elements e.g. [1, 2] */
75
69
  $arrIncAll?: {
76
70
  [K in keyof T]?: T[K] extends any[] ? T[K][number][] : T[K][];
77
71
  };
78
- /**
79
- * @deprecated Use $arrIncAll instead.
80
- */
81
- $arrincall?: {
82
- [K in keyof T]?: T[K] extends any[] ? T[K][number][] : T[K][];
83
- };
84
72
  /** [1, 2, 3] has size 3 */
85
73
  $size?: NestedValue<T, number>;
86
74
  };
@@ -5,24 +5,12 @@ export type ArrayUpdater<T = any> = {
5
5
  $push?: NestedValue<T, any>;
6
6
  /** [1,2] -> $pushSet 2,3 -> [1,2,3] */
7
7
  $pushSet?: NestedValue<T, any>;
8
- /**
9
- * @deprecated Use $pushSet instead.
10
- */
11
- $pushset?: NestedValue<T, any>;
12
8
  /** [1,2,3] -> $pushAll [2,3] -> [1,2,3] */
13
9
  $pushAll?: NestedValue<T, any>;
14
- /**
15
- * @deprecated Use $pushAll instead.
16
- */
17
- $pushall?: NestedValue<T, any>;
18
10
  /** [1,2,3] -> $pull 2 -> [1,3] */
19
11
  $pull?: NestedValue<T, any>;
20
12
  /** [1,2,2,3] -> $pullAll [2] -> [1,3] */
21
13
  $pullAll?: NestedValue<T, any>;
22
- /**
23
- * @deprecated Use $pullAll instead.
24
- */
25
- $pullall?: NestedValue<T, any>;
26
14
  };
27
15
  /** Objects */
28
16
  export type ObjectUpdater<T = any> = {
@@ -1,7 +1,10 @@
1
1
  import { compareSafe } from "./compare.js";
2
2
  export async function findUtil(query, fileCpuOrData, files, fileCpuOpts) {
3
3
  const { dbFindOpts = {} } = query;
4
- const { reverse = false, offset = 0, limit = -1, sortBy, sortAsc = true, min, max, avg, sum, distinct, groupBy, count, } = dbFindOpts;
4
+ const { reverse = false, offset = 0, sortBy, sortAsc = true, min, max, avg, sum, distinct, groupBy, count, } = dbFindOpts;
5
+ let limit = dbFindOpts.limit ?? -1;
6
+ if (limit === Infinity)
7
+ limit = -1;
5
8
  const needsAllData = min || max || avg || sum || distinct || groupBy || count || sortBy;
6
9
  let datas = [];
7
10
  let skippedEntries = 0;
@@ -2,4 +2,7 @@ import { Data, DataInternal } from "../types/data.js";
2
2
  import { VQuery, VQueryT } from "../types/query.js";
3
3
  export declare function findObj(config: VQueryT.Find | VQueryT.FindOne, obj: Data): DataInternal | null;
4
4
  export declare function matchObj(config: VQuery, obj: Data): boolean;
5
+ /**
6
+ * @returns reference to object or new object
7
+ */
5
8
  export declare function updateObj(config: VQueryT.Update, obj: Data): DataInternal;
@@ -18,10 +18,15 @@ export function matchObj(config, obj) {
18
18
  return true;
19
19
  return false;
20
20
  }
21
+ /**
22
+ * @returns reference to object or new object
23
+ */
21
24
  export function updateObj(config, obj) {
22
25
  const { updater, context } = config;
23
- if (typeof updater === "object" && !Array.isArray(updater))
24
- return updateObjectAdvanced(obj, updater);
26
+ if (typeof updater === "object" && !Array.isArray(updater)) {
27
+ updateObjectAdvanced(obj, updater);
28
+ return obj;
29
+ }
25
30
  if (typeof updater === "function") {
26
31
  const updateObjValue = updater(obj, context);
27
32
  if (updateObjValue)
@@ -4,4 +4,4 @@ import { UpdaterArg } from "../types/updater.js";
4
4
  * @param obj - The object to update.
5
5
  * @param field - An object containing new values to update in the target object.
6
6
  */
7
- export declare function updateObjectAdvanced(obj: Record<string, any>, field: UpdaterArg): Record<string, any>;
7
+ export declare function updateObjectAdvanced(obj: Record<string, any>, field: UpdaterArg): void;
@@ -8,8 +8,7 @@ export function updateObjectAdvanced(obj, field) {
8
8
  if (typeof field !== "object" || Array.isArray(field) || field === null)
9
9
  throw new Error("Fields must be an object");
10
10
  if (Object.keys(field).length === 0)
11
- return obj;
12
- const newObj = structuredClone(obj);
11
+ return;
13
12
  const $fields = {};
14
13
  const subsetFields = {};
15
14
  Object.keys(field).forEach(key => {
@@ -18,10 +17,9 @@ export function updateObjectAdvanced(obj, field) {
18
17
  else
19
18
  subsetFields[key] = field[key];
20
19
  });
21
- mainUpdate(newObj, $fields);
22
- const mergedObj = deepMerge(newObj, $fields);
20
+ mainUpdate(obj, $fields);
21
+ const mergedObj = deepMerge(obj, $fields);
23
22
  updateObject(mergedObj, subsetFields);
24
- return mergedObj;
25
23
  }
26
24
  function deepMerge(obj, fields) {
27
25
  if (!("$deepmerge" in fields))
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const version = "0.11.6";
1
+ export const version = "0.12.0-alpha.0";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/db-core",
3
- "version": "0.11.6",
3
+ "version": "0.12.0-alpha.0",
4
4
  "types": "dist/index.d.ts",
5
5
  "main": "dist/index.js",
6
6
  "author": "wxn0brP",