@wxn0brp/db-core 0.1.0 → 0.1.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,4 +1,5 @@
1
1
  import { CustomActionsBase } from "../base/custom.js";
2
+ import CollectionManager from "../helpers/CollectionManager.js";
2
3
  import Data from "../types/data.js";
3
4
  import { VQuery } from "../types/query.js";
4
5
  import ValtheraClass from "./valthera.js";
@@ -15,4 +16,6 @@ export declare class MemoryAction extends CustomActionsBase {
15
16
  export default class ValtheraMemory extends ValtheraClass {
16
17
  constructor(...args: any[]);
17
18
  }
18
- export declare function createMemoryValthera<T extends Record<string, Data[]>>(data?: T): ValtheraMemory;
19
+ export declare function createMemoryValthera<T extends Record<string, Data[]>>(data?: T): ValtheraMemory & {
20
+ [K in keyof T]: CollectionManager<T[K][number]>;
21
+ };
package/dist/db/memory.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { CustomActionsBase } from "../base/custom.js";
2
2
  import CustomFileCpu from "../customFileCpu.js";
3
+ import { forgeValthera } from "../utils/forge.js";
3
4
  import ValtheraClass from "./valthera.js";
4
5
  export class MemoryAction extends CustomActionsBase {
5
6
  memory;
@@ -42,9 +43,9 @@ export default class ValtheraMemory extends ValtheraClass {
42
43
  export function createMemoryValthera(data) {
43
44
  const db = new ValtheraMemory();
44
45
  if (!data)
45
- return db;
46
+ return forgeValthera(db);
46
47
  for (const collection of Object.keys(data)) {
47
48
  db.dbAction.memory.set(collection, data[collection]);
48
49
  }
49
- return db;
50
+ return forgeValthera(db);
50
51
  }
@@ -22,7 +22,7 @@ declare class ValtheraClass implements ValtheraCompatible {
22
22
  /**
23
23
  * Create a new instance of a CollectionManager class.
24
24
  */
25
- c(collection: string): CollectionManager;
25
+ c<T = Data>(collection: string): CollectionManager<T>;
26
26
  /**
27
27
  * Get the names of all available databases.
28
28
  */
@@ -3,41 +3,41 @@ import { DbFindOpts, FindOpts } from "../types/options.js";
3
3
  import { VContext } from "../types/types.js";
4
4
  import Data from "../types/data.js";
5
5
  import { ValtheraCompatible } from "../types/valthera.js";
6
- declare class CollectionManager {
6
+ declare class CollectionManager<D = Data> {
7
7
  private db;
8
8
  private collection;
9
9
  constructor(db: ValtheraCompatible, collection: string);
10
10
  /**
11
11
  * Add data to a database.
12
12
  */
13
- add<T = Data>(data: Arg<T>, id_gen?: boolean): Promise<T>;
13
+ add<T = D>(data: Arg<T>, id_gen?: boolean): Promise<T>;
14
14
  /**
15
15
  * Find data in a database.
16
16
  */
17
- find<T = Data>(search: Search<T>, context?: VContext, options?: DbFindOpts<T>, findOpts?: FindOpts<T>): Promise<T[]>;
17
+ find<T = D>(search?: Search<T>, context?: VContext, options?: DbFindOpts<T>, findOpts?: FindOpts<T>): Promise<T[]>;
18
18
  /**
19
19
  * Find one data entry in a database.
20
20
  */
21
- findOne<T = Data>(search: Search<T>, context?: VContext, findOpts?: FindOpts<T>): Promise<T>;
21
+ findOne<T = D>(search?: Search<T>, context?: VContext, findOpts?: FindOpts<T>): Promise<T>;
22
22
  /**
23
23
  * Update data in a database.
24
24
  */
25
- update<T = Data>(search: Search<T>, updater: Updater<T>, context?: VContext): Promise<boolean>;
25
+ update<T = D>(search: Search<T>, updater: Updater<T>, context?: VContext): Promise<boolean>;
26
26
  /**
27
27
  * Update one data entry in a database.
28
28
  */
29
- updateOne<T = Data>(search: Search<T>, updater: Updater<T>, context?: VContext): Promise<boolean>;
29
+ updateOne<T = D>(search: Search<T>, updater: Updater<T>, context?: VContext): Promise<boolean>;
30
30
  /**
31
31
  * Remove data from a database.
32
32
  */
33
- remove<T = Data>(search: Search<T>, context?: VContext): Promise<boolean>;
33
+ remove<T = D>(search: Search<T>, context?: VContext): Promise<boolean>;
34
34
  /**
35
35
  * Remove one data entry from a database.
36
36
  */
37
- removeOne<T = Data>(search: Search<T>, context?: VContext): Promise<boolean>;
37
+ removeOne<T = D>(search: Search<T>, context?: VContext): Promise<boolean>;
38
38
  /**
39
39
  * Asynchronously updates one entry in a database or adds a new one if it doesn't exist.
40
40
  */
41
- updateOneOrAdd<T = Data>(search: Search<T>, updater: Updater<T>, add_arg?: Arg<T>, context?: VContext, id_gen?: boolean): Promise<boolean>;
41
+ updateOneOrAdd<T = D>(search: Search<T>, updater: Updater<T>, add_arg?: Arg<T>, context?: VContext, id_gen?: boolean): Promise<boolean>;
42
42
  }
43
43
  export default CollectionManager;
@@ -14,13 +14,13 @@ class CollectionManager {
14
14
  /**
15
15
  * Find data in a database.
16
16
  */
17
- async find(search, context = {}, options = {}, findOpts = {}) {
17
+ async find(search = {}, context = {}, options = {}, findOpts = {}) {
18
18
  return await this.db.find(this.collection, search, context, options, findOpts);
19
19
  }
20
20
  /**
21
21
  * Find one data entry in a database.
22
22
  */
23
- async findOne(search, context = {}, findOpts = {}) {
23
+ async findOne(search = {}, context = {}, findOpts = {}) {
24
24
  return await this.db.findOne(this.collection, search, context, findOpts);
25
25
  }
26
26
  /**
package/dist/index.d.ts CHANGED
@@ -1,11 +1,12 @@
1
+ import CustomFileCpu from "./customFileCpu.js";
2
+ import ValtheraMemory, { createMemoryValthera } from "./db/memory.js";
3
+ import ValtheraClass from "./db/valthera.js";
1
4
  import genId from "./helpers/gen.js";
2
5
  import Relation from "./helpers/relation.js";
6
+ import { ValtheraTypes } from "./types/export.js";
3
7
  import { RelationTypes } from "./types/relation.js";
4
8
  import { ValtheraCompatible } from "./types/valthera.js";
5
- import { ValtheraTypes } from "./types/export.js";
6
- import CustomFileCpu from "./customFileCpu.js";
7
- import ValtheraClass from "./db/valthera.js";
8
- import ValtheraMemory, { createMemoryValthera } from "./db/memory.js";
9
- export { ValtheraClass, ValtheraMemory, createMemoryValthera, Relation, genId, CustomFileCpu, };
10
- export type { ValtheraCompatible, RelationTypes, ValtheraTypes };
9
+ import { forgeValthera } from "./utils/forge.js";
10
+ export { createMemoryValthera, CustomFileCpu, forgeValthera, genId, Relation, ValtheraClass, ValtheraMemory };
11
+ export type { RelationTypes, ValtheraCompatible, ValtheraTypes };
11
12
  export type Id = import("./types/Id.js").Id;
package/dist/index.js CHANGED
@@ -1,6 +1,7 @@
1
- import genId from "./helpers/gen.js";
2
- import Relation from "./helpers/relation.js";
3
1
  import CustomFileCpu from "./customFileCpu.js";
4
- import ValtheraClass from "./db/valthera.js";
5
2
  import ValtheraMemory, { createMemoryValthera } from "./db/memory.js";
6
- export { ValtheraClass, ValtheraMemory, createMemoryValthera, Relation, genId, CustomFileCpu, };
3
+ import ValtheraClass from "./db/valthera.js";
4
+ import genId from "./helpers/gen.js";
5
+ import Relation from "./helpers/relation.js";
6
+ import { forgeValthera } from "./utils/forge.js";
7
+ export { createMemoryValthera, CustomFileCpu, forgeValthera, genId, Relation, ValtheraClass, ValtheraMemory };
@@ -0,0 +1,3 @@
1
+ import CollectionManager from "../helpers/CollectionManager.js";
2
+ import { ValtheraCompatible } from "../types/valthera.js";
3
+ export declare function forgeValthera<T extends string>(target: ValtheraCompatible): ValtheraCompatible & { [K in T]: CollectionManager; };
@@ -0,0 +1,17 @@
1
+ import CollectionManager from "../helpers/CollectionManager.js";
2
+ export function forgeValthera(target) {
3
+ return new Proxy(target, {
4
+ get(target, prop, receiver) {
5
+ if (prop in target) {
6
+ return Reflect.get(target, prop, receiver);
7
+ }
8
+ const collection = new CollectionManager(target, prop);
9
+ target[prop] = collection;
10
+ return collection;
11
+ },
12
+ set(target, prop, value, receiver) {
13
+ return Reflect.set(target, prop, value, receiver);
14
+ }
15
+ });
16
+ }
17
+ ;
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const version = "0.1.0";
1
+ export const version = "0.1.1";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/db-core",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "types": "dist/index.d.ts",
5
5
  "main": "dist/index.js",
6
6
  "author": "wxn0brP",