@wxn0brp/db-core 0.5.0-alpha.1 → 0.6.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.
@@ -1,20 +1,21 @@
1
- import { Data } from "../types/data.js";
1
+ import { ActionsBaseInterface } from "../types/action.js";
2
+ import { DataInternal } from "../types/data.js";
2
3
  import { VQuery } from "../types/query.js";
3
- export declare class ActionsBase {
4
+ export declare abstract class ActionsBase implements ActionsBaseInterface {
4
5
  _inited: boolean;
5
6
  numberId: boolean;
6
7
  init(...args: any[]): Promise<void>;
7
- getCollections(): Promise<string[]>;
8
- ensureCollection(config: VQuery): Promise<boolean>;
9
- issetCollection(config: VQuery): Promise<boolean>;
10
- add(config: VQuery): Promise<Data>;
11
- find(config: VQuery): Promise<Data[]>;
12
- findOne(config: VQuery): Promise<Data | null>;
13
- update(config: VQuery): Promise<Data>;
14
- updateOne(config: VQuery): Promise<Data>;
15
- remove(config: VQuery): Promise<Data>;
16
- removeOne(config: VQuery): Promise<Data>;
17
- removeCollection(config: VQuery): Promise<boolean>;
18
- updateOneOrAdd(config: VQuery): Promise<boolean>;
19
- toggleOne(config: VQuery): Promise<boolean>;
8
+ abstract getCollections(): Promise<string[]>;
9
+ abstract ensureCollection(config: VQuery): Promise<boolean>;
10
+ abstract issetCollection(config: VQuery): Promise<boolean>;
11
+ abstract add(config: VQuery): Promise<DataInternal>;
12
+ abstract find(config: VQuery): Promise<DataInternal[]>;
13
+ abstract findOne(config: VQuery): Promise<DataInternal | null>;
14
+ abstract update(config: VQuery): Promise<DataInternal[] | null>;
15
+ abstract updateOne(config: VQuery): Promise<DataInternal | null>;
16
+ abstract remove(config: VQuery): Promise<DataInternal[] | null>;
17
+ abstract removeOne(config: VQuery): Promise<DataInternal | null>;
18
+ abstract removeCollection(config: VQuery): Promise<boolean>;
19
+ updateOneOrAdd(config: VQuery): Promise<DataInternal>;
20
+ toggleOne(config: VQuery): Promise<DataInternal | null>;
20
21
  }
@@ -3,64 +3,26 @@ export class ActionsBase {
3
3
  _inited = true;
4
4
  numberId = false;
5
5
  async init(...args) { }
6
- async getCollections() {
7
- throw new Error("Not implemented");
8
- return [];
9
- }
10
- async ensureCollection(config) {
11
- throw new Error("Not implemented");
12
- return false;
13
- }
14
- async issetCollection(config) {
15
- throw new Error("Not implemented");
16
- return false;
17
- }
18
- async add(config) {
19
- throw new Error("Not implemented");
20
- return {};
21
- }
22
- async find(config) {
23
- throw new Error("Not implemented");
24
- return [];
25
- }
26
- async findOne(config) {
27
- throw new Error("Not implemented");
28
- return {};
29
- }
30
- async update(config) {
31
- throw new Error("Not implemented");
32
- return [];
33
- }
34
- async updateOne(config) {
35
- throw new Error("Not implemented");
36
- return {};
37
- }
38
- async remove(config) {
39
- throw new Error("Not implemented");
40
- return [];
41
- }
42
- async removeOne(config) {
43
- throw new Error("Not implemented");
44
- return {};
45
- }
46
- async removeCollection(config) {
47
- throw new Error("Not implemented");
48
- return false;
49
- }
50
6
  async updateOneOrAdd(config) {
51
7
  const res = await this.updateOne(config);
8
+ const controlData = { updated: true };
9
+ config.control ||= {};
10
+ config.control.updateOneOrAdd = controlData;
52
11
  if (res)
53
- return true;
12
+ return res;
54
13
  setDataForUpdateOneOrAdd(config);
55
- await this.add(config);
56
- return false;
14
+ config.control.updateOneOrAdd.updated = false;
15
+ return await this.add(config);
57
16
  }
58
17
  async toggleOne(config) {
59
18
  const res = await this.removeOne(config);
19
+ const controlData = { created: false };
20
+ config.control ||= {};
21
+ config.control.toggleOne = controlData;
60
22
  if (res)
61
- return true;
23
+ return res;
62
24
  setDataForToggleOne(config);
63
- await this.add(config);
64
- return false;
25
+ config.control.toggleOne.created = true;
26
+ return await this.add(config);
65
27
  }
66
28
  }
@@ -2,7 +2,7 @@ import { CustomFileCpu } from "../customFileCpu.js";
2
2
  import { Data } from "../types/data.js";
3
3
  import { VQuery } from "../types/query.js";
4
4
  import { ActionsBase } from "./actions.js";
5
- export declare class CustomActionsBase extends ActionsBase {
5
+ export declare abstract class CustomActionsBase extends ActionsBase {
6
6
  fileCpu: CustomFileCpu;
7
7
  constructor();
8
8
  /**
@@ -21,13 +21,17 @@ export class CustomFileCpu {
21
21
  file = pathRepair(file);
22
22
  const entries = await this._readFile(file);
23
23
  const results = entries.filter(entry => typeof search === "function" ? search(entry, context) : hasFieldsAdvanced(entry, search));
24
- return results.length ? results.map(res => updateFindObject(res, findOpts)) : [];
24
+ return results.length ?
25
+ results.map(res => updateFindObject(res, findOpts)) :
26
+ [];
25
27
  }
26
28
  async findOne(file, search, context = {}, findOpts = {}) {
27
29
  file = pathRepair(file);
28
30
  const entries = await this._readFile(file);
29
31
  const result = entries.find(entry => typeof search === "function" ? search(entry, context) : hasFieldsAdvanced(entry, search));
30
- return result ? updateFindObject(result, findOpts) : false;
32
+ return result ?
33
+ updateFindObject(result, findOpts) :
34
+ false;
31
35
  }
32
36
  async remove(file, one, search, context = {}) {
33
37
  file = pathRepair(file);
@@ -5,13 +5,13 @@ export declare class MultiBackend extends ActionsBase {
5
5
  primaryBackend: ActionsBase;
6
6
  constructor(backends: ActionsBase[], primaryIndex?: number);
7
7
  init(...args: any[]): Promise<void>;
8
- add(config: VQuery): Promise<import("../types/data.js").Data>;
9
- find(config: VQuery): Promise<import("../types/data.js").Data[]>;
10
- findOne(config: VQuery): Promise<import("../types/data.js").Data>;
11
- update(config: VQuery): Promise<import("../types/data.js").Data[]>;
12
- updateOne(config: VQuery): Promise<import("../types/data.js").Data>;
13
- remove(config: VQuery): Promise<import("../types/data.js").Data[]>;
14
- removeOne(config: VQuery): Promise<import("../types/data.js").Data>;
8
+ add(config: VQuery): Promise<import("../types/data.js").DataInternal>;
9
+ find(config: VQuery): Promise<import("../types/data.js").DataInternal[]>;
10
+ findOne(config: VQuery): Promise<import("../types/data.js").DataInternal>;
11
+ update(config: VQuery): Promise<import("../types/data.js").DataInternal[]>;
12
+ updateOne(config: VQuery): Promise<import("../types/data.js").DataInternal>;
13
+ remove(config: VQuery): Promise<import("../types/data.js").DataInternal[]>;
14
+ removeOne(config: VQuery): Promise<import("../types/data.js").DataInternal>;
15
15
  ensureCollection(config: VQuery): Promise<boolean>;
16
16
  issetCollection(config: VQuery): Promise<boolean>;
17
17
  removeCollection(config: VQuery): Promise<boolean>;
@@ -13,13 +13,13 @@ export declare class RoutedStorage extends ActionsBase {
13
13
  _withAll<T>(config: VQuery, fn: (b: ActionsBase) => Promise<T>, gather?: boolean): Promise<T>;
14
14
  _withFirst<T>(config: VQuery, fn: (b: ActionsBase) => Promise<T>): Promise<T>;
15
15
  init(...args: any[]): Promise<void>;
16
- add(config: VQuery): Promise<import("../types/data.js").Data>;
17
- find(config: VQuery): Promise<import("../types/data.js").Data[]>;
18
- findOne(config: VQuery): Promise<import("../types/data.js").Data>;
19
- update(config: VQuery): Promise<import("../types/data.js").Data>;
20
- updateOne(config: VQuery): Promise<import("../types/data.js").Data>;
21
- remove(config: VQuery): Promise<import("../types/data.js").Data>;
22
- removeOne(config: VQuery): Promise<import("../types/data.js").Data>;
16
+ add(config: VQuery): Promise<import("../types/data.js").DataInternal>;
17
+ find(config: VQuery): Promise<import("../types/data.js").DataInternal[]>;
18
+ findOne(config: VQuery): Promise<import("../types/data.js").DataInternal>;
19
+ update(config: VQuery): Promise<import("../types/data.js").DataInternal[]>;
20
+ updateOne(config: VQuery): Promise<import("../types/data.js").DataInternal>;
21
+ remove(config: VQuery): Promise<import("../types/data.js").DataInternal[]>;
22
+ removeOne(config: VQuery): Promise<import("../types/data.js").DataInternal>;
23
23
  ensureCollection(config: VQuery): Promise<boolean>;
24
24
  issetCollection(config: VQuery): Promise<boolean>;
25
25
  removeCollection(config: VQuery): Promise<boolean>;
@@ -6,9 +6,6 @@ import { Data } from "../types/data.js";
6
6
  import { DbOpts } from "../types/options.js";
7
7
  import { VQuery } from "../types/query.js";
8
8
  import { AddQuery, FindOneQuery, FindQuery, RemoveQuery, ToggleOneQuery, UpdateOneOrAddQuery, UpdateQuery, ValtheraCompatible } from "../types/valthera.js";
9
- type DbActionsFns = keyof {
10
- [K in keyof ActionsBase as ActionsBase[K] extends (...args: any[]) => any ? K : never]: any;
11
- };
12
9
  /**
13
10
  * Represents a database management class for performing CRUD operations.
14
11
  * @class
@@ -17,14 +14,14 @@ export declare class ValtheraClass implements ValtheraCompatible {
17
14
  dbAction: ActionsBase;
18
15
  executor: Executor;
19
16
  emiter: VEE<{
20
- [K in Exclude<DbActionsFns, "init">]: (query: VQuery, result: Awaited<ReturnType<ValtheraCompatible[K]>>) => void;
17
+ [K in keyof ValtheraCompatible]: (query: VQuery, result: Awaited<ReturnType<ValtheraCompatible[K]>>) => void;
21
18
  } & {
22
- "*": (name: DbActionsFns, query: VQuery, result: any) => void;
19
+ "*": (name: keyof ValtheraCompatible, query: VQuery, result: any) => void;
23
20
  }>;
24
21
  version: string;
25
- constructor(options?: DbOpts);
22
+ constructor(options: DbOpts);
26
23
  init(...args: any[]): Promise<unknown>;
27
- execute<T>(name: DbActionsFns, query: VQuery): Promise<T>;
24
+ execute<T>(name: keyof ValtheraCompatible, query: VQuery): Promise<T>;
28
25
  /**
29
26
  * Create a new instance of a CollectionManager class.
30
27
  */
@@ -82,4 +79,3 @@ export declare class ValtheraClass implements ValtheraCompatible {
82
79
  */
83
80
  removeCollection(collection: string): Promise<boolean>;
84
81
  }
85
- export {};
@@ -1,5 +1,4 @@
1
1
  import { VEE } from "@wxn0brp/event-emitter";
2
- import { ActionsBase } from "../base/actions.js";
3
2
  import { Collection } from "../helpers/collection.js";
4
3
  import { Executor } from "../helpers/executor.js";
5
4
  import { version } from "../version.js";
@@ -10,12 +9,11 @@ import { version } from "../version.js";
10
9
  export class ValtheraClass {
11
10
  dbAction;
12
11
  executor;
13
- emiter;
12
+ emiter = new VEE();
14
13
  version = version;
15
- constructor(options = {}) {
16
- this.dbAction = options.dbAction || new ActionsBase();
14
+ constructor(options) {
15
+ this.dbAction = options.dbAction;
17
16
  this.executor = options.executor || new Executor();
18
- this.emiter = new VEE();
19
17
  if (options.numberId)
20
18
  this.dbAction.numberId = true;
21
19
  }
@@ -34,7 +32,6 @@ export class ValtheraClass {
34
32
  await this.init();
35
33
  const result = await this.executor.addOp(this.dbAction[name].bind(this.dbAction), query);
36
34
  this.emiter.emit(name, query, result);
37
- this.emiter.emit("*", name, query, result);
38
35
  return result;
39
36
  }
40
37
  /**
@@ -12,7 +12,7 @@ export async function addId(query, actions, defaultGen = true) {
12
12
  return;
13
13
  }
14
14
  const find = await actions.findOne({ collection: "__vdb_id", search: { c: collection } });
15
- data._id = find ? find.i + 1 : 1;
15
+ data._id = find?.i ? find.i + 1 : 1;
16
16
  await actions.updateOneOrAdd({
17
17
  collection: "__vdb_id",
18
18
  search: { c: collection },
@@ -0,0 +1,20 @@
1
+ import { DataInternal } from "./data.js";
2
+ import { VQuery } from "./query.js";
3
+ export interface ActionsBaseInterface {
4
+ _inited: boolean;
5
+ numberId: boolean;
6
+ init(...args: any[]): Promise<void>;
7
+ getCollections(): Promise<string[]>;
8
+ ensureCollection(config: VQuery): Promise<boolean>;
9
+ issetCollection(config: VQuery): Promise<boolean>;
10
+ add(config: VQuery): Promise<DataInternal>;
11
+ find(config: VQuery): Promise<DataInternal[]>;
12
+ findOne(config: VQuery): Promise<DataInternal | null>;
13
+ update(config: VQuery): Promise<DataInternal[] | null>;
14
+ updateOne(config: VQuery): Promise<DataInternal | null>;
15
+ remove(config: VQuery): Promise<DataInternal[] | null>;
16
+ removeOne(config: VQuery): Promise<DataInternal | null>;
17
+ removeCollection(config: VQuery): Promise<boolean>;
18
+ updateOneOrAdd(config: VQuery): Promise<DataInternal>;
19
+ toggleOne(config: VQuery): Promise<DataInternal | null>;
20
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -1,3 +1,4 @@
1
1
  export interface Data {
2
2
  [key: string]: any;
3
3
  }
4
+ export type DataInternal = Record<string, unknown>;
@@ -1,5 +1,5 @@
1
1
  import { Search, Updater } from "./arg.js";
2
- import { Data } from "./data.js";
2
+ import { Data, DataInternal } from "./data.js";
3
3
  import { FindOpts } from "./options.js";
4
4
  import { VContext } from "./types.js";
5
5
  export interface FileCpu {
@@ -18,7 +18,7 @@ export interface FileCpu {
18
18
  * @param findOpts Additional options for searching.
19
19
  * @returns A promise resolving to an array of found entries, or `false` if the file does not exist.
20
20
  */
21
- find(file: string, search: Search, context?: VContext, findOpts?: FindOpts): Promise<Data[]>;
21
+ find(file: string, search: Search, context?: VContext, findOpts?: FindOpts): Promise<DataInternal[]>;
22
22
  /**
23
23
  * Asynchronously finds one entry in a file based on search criteria.
24
24
  * @param file The path to the file.
@@ -27,7 +27,7 @@ export interface FileCpu {
27
27
  * @param findOpts Additional options for searching.
28
28
  * @returns A promise resolving to the found entry or `false` if not found.
29
29
  */
30
- findOne(file: string, search: Search, context?: VContext, findOpts?: FindOpts): Promise<Data | false>;
30
+ findOne(file: string, search: Search, context?: VContext, findOpts?: FindOpts): Promise<DataInternal | false>;
31
31
  /**
32
32
  * Asynchronously removes entries from a file based on search criteria.
33
33
  * @param file The path to the file.
@@ -36,7 +36,7 @@ export interface FileCpu {
36
36
  * @param one If `true`, removes only the first matching entry.
37
37
  * @returns A promise resolving to `true` if at least one entry was removed, otherwise `false`.
38
38
  */
39
- remove(file: string, one: boolean, search: Search, context?: VContext): Promise<Data[]>;
39
+ remove(file: string, one: boolean, search: Search, context?: VContext): Promise<DataInternal[]>;
40
40
  /**
41
41
  * Asynchronously updates entries in a file based on search criteria and an updater function or object.
42
42
  * @param file The path to the file.
@@ -46,5 +46,5 @@ export interface FileCpu {
46
46
  * @param one If `true`, updates only the first matching entry.
47
47
  * @returns A promise resolving to `true` if at least one entry was updated, otherwise `false`.
48
48
  */
49
- update(file: string, one: boolean, search: Search, updater: Updater, context?: VContext): Promise<Data[]>;
49
+ update(file: string, one: boolean, search: Search, updater: Updater, context?: VContext): Promise<DataInternal[]>;
50
50
  }
@@ -2,8 +2,7 @@ import { ActionsBase } from "../base/actions.js";
2
2
  import { Executor } from "../helpers/executor.js";
3
3
  import { KeysMatching } from "./utils.js";
4
4
  export interface DbOpts {
5
- maxFileSize?: number;
6
- dbAction?: ActionsBase;
5
+ dbAction: ActionsBase;
7
6
  executor?: Executor;
8
7
  numberId?: boolean;
9
8
  }
@@ -4,13 +4,19 @@ import { DbFindOpts, FindOpts } from "./options.js";
4
4
  /**
5
5
  * To extend via adapters
6
6
  * @example
7
- * declare module "@wxn0brp/db-core" {
7
+ * declare module "@wxn0brp/db-core/types/query" {
8
8
  * export interface VQuery_Control {
9
9
  * key?: "value";
10
10
  * }
11
11
  * }
12
12
  */
13
13
  export interface VQuery_Control {
14
+ updateOneOrAdd?: {
15
+ updated?: boolean;
16
+ };
17
+ toggleOne?: {
18
+ created?: boolean;
19
+ };
14
20
  }
15
21
  export interface VQuery {
16
22
  collection?: string;
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const version = "0.5.0-alpha.1";
1
+ export const version = "0.6.0";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/db-core",
3
- "version": "0.5.0-alpha.1",
3
+ "version": "0.6.0",
4
4
  "types": "dist/index.d.ts",
5
5
  "main": "dist/index.js",
6
6
  "author": "wxn0brP",
@@ -13,7 +13,7 @@
13
13
  },
14
14
  "homepage": "https://github.com/wxn0brP/ValtheraDB",
15
15
  "dependencies": {
16
- "@wxn0brp/event-emitter": "^0.0.4"
16
+ "@wxn0brp/event-emitter": "0.0.5"
17
17
  },
18
18
  "devDependencies": {
19
19
  "@types/bun": "*",