@wxn0brp/db-core 0.3.2 → 0.5.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.
Files changed (48) hide show
  1. package/dist/base/actions.d.ts +6 -7
  2. package/dist/base/actions.js +5 -6
  3. package/dist/base/custom.d.ts +7 -7
  4. package/dist/base/custom.js +5 -3
  5. package/dist/customFileCpu.d.ts +7 -8
  6. package/dist/customFileCpu.js +18 -20
  7. package/dist/db/memory.d.ts +5 -5
  8. package/dist/db/memory.js +3 -3
  9. package/dist/db/multiStorage.d.ts +5 -5
  10. package/dist/db/multiStorage.js +9 -11
  11. package/dist/db/routedStorage.d.ts +5 -5
  12. package/dist/db/routedStorage.js +1 -1
  13. package/dist/db/valthera.d.ts +19 -24
  14. package/dist/db/valthera.js +52 -27
  15. package/dist/helpers/addId.d.ts +1 -1
  16. package/dist/helpers/addId.js +1 -1
  17. package/dist/helpers/collection.d.ts +49 -0
  18. package/dist/helpers/collection.js +59 -0
  19. package/dist/helpers/executor.d.ts +2 -4
  20. package/dist/helpers/executor.js +1 -3
  21. package/dist/helpers/forge.d.ts +4 -4
  22. package/dist/helpers/forge.js +2 -2
  23. package/dist/helpers/gen.d.ts +18 -2
  24. package/dist/helpers/gen.js +36 -30
  25. package/dist/helpers/relation.d.ts +5 -2
  26. package/dist/helpers/relation.js +44 -33
  27. package/dist/index.d.ts +11 -13
  28. package/dist/index.js +7 -8
  29. package/dist/types/Id.d.ts +1 -2
  30. package/dist/types/data.d.ts +0 -1
  31. package/dist/types/fileCpu.d.ts +6 -7
  32. package/dist/types/options.d.ts +3 -3
  33. package/dist/types/query.d.ts +12 -0
  34. package/dist/types/relation.d.ts +1 -1
  35. package/dist/types/valthera.d.ts +21 -30
  36. package/dist/utils/action.d.ts +2 -2
  37. package/dist/utils/hasFields.d.ts +1 -1
  38. package/dist/utils/hasFields.js +1 -1
  39. package/dist/utils/hasFieldsAdvanced.d.ts +1 -1
  40. package/dist/utils/hasFieldsAdvanced.js +2 -2
  41. package/dist/utils/updateFindObject.d.ts +1 -1
  42. package/dist/utils/updateFindObject.js +1 -1
  43. package/dist/utils/updateObject.d.ts +1 -1
  44. package/dist/utils/updateObject.js +1 -1
  45. package/dist/version.js +1 -1
  46. package/package.json +1 -1
  47. package/dist/helpers/CollectionManager.d.ts +0 -47
  48. package/dist/helpers/CollectionManager.js +0 -63
@@ -15,7 +15,7 @@ export declare namespace RelationTypes {
15
15
  fk?: string;
16
16
  as?: string;
17
17
  select?: string[];
18
- findOpts?: DbFindOpts;
18
+ dbFindOpts?: DbFindOpts;
19
19
  type?: "1" | "11" | "1n" | "nm";
20
20
  relations?: Relation;
21
21
  through?: {
@@ -1,43 +1,34 @@
1
- import CollectionManager from "../helpers/CollectionManager.js";
2
- import { Arg, Search, Updater } from "./arg.js";
3
- import Data from "./data.js";
4
- import { DbFindOpts, FindOpts } from "./options.js";
1
+ import { Collection } from "../helpers/collection.js";
2
+ import { Arg } from "./arg.js";
3
+ import { Data } from "./data.js";
5
4
  import { VQuery } from "./query.js";
6
5
  import { VContext } from "./types.js";
6
+ export type QueryBase = Required<Pick<VQuery, "collection" | "search">> & Pick<VQuery, "control">;
7
+ export type AddQuery = Required<Pick<VQuery, "data" | "collection">> & Pick<VQuery, "id_gen" | "control">;
8
+ export type FindQuery = QueryBase & Pick<VQuery, "findOpts" | "dbFindOpts" | "context">;
9
+ export type FindOneQuery = QueryBase & Pick<VQuery, "findOpts" | "context">;
10
+ export type UpdateQuery = QueryBase & Required<Pick<VQuery, "updater">> & Pick<VQuery, "context">;
11
+ export type RemoveQuery = QueryBase & Pick<VQuery, "context">;
12
+ export type UpdateOneOrAddQuery = QueryBase & UpdateQuery & Pick<VQuery, "add_arg" | "id_gen">;
13
+ export type ToggleOneQuery = QueryBase & Pick<VQuery, "data" | "context">;
7
14
  export interface ValtheraCompatible {
8
- c(collection: string): CollectionManager;
15
+ c(collection: string): Collection;
9
16
  getCollections(): Promise<string[]>;
10
17
  ensureCollection(collection: string): Promise<boolean>;
11
18
  issetCollection(collection: string): Promise<boolean>;
12
- add<T = Data>(collection: string, data: Arg<T>, id_gen?: boolean): Promise<T>;
13
- find<T = Data>(collection: string, search?: Search<T>, options?: DbFindOpts<T>, findOpts?: FindOpts<T>, context?: VContext): Promise<T[]>;
14
- findOne<T = Data>(collection: string, search?: Search<T>, findOpts?: FindOpts<T>, context?: VContext): Promise<T | null>;
15
- update<T = Data>(collection: string, search: Search<T>, updater: Updater<T>, context?: VContext): Promise<boolean>;
16
- updateOne<T = Data>(collection: string, search: Search<T>, updater: Updater<T>, context?: VContext): Promise<boolean>;
17
- remove<T = Data>(collection: string, search: Search<T>, context?: VContext): Promise<boolean>;
18
- removeOne<T = Data>(collection: string, search: Search<T>, context?: VContext): Promise<boolean>;
19
+ add<T = Data>(query: AddQuery): Promise<T>;
20
+ find<T = Data>(query: FindQuery): Promise<T[]>;
21
+ findOne<T = Data>(query: FindOneQuery): Promise<T | null>;
22
+ update<T = Data>(query: UpdateQuery): Promise<T[] | null>;
23
+ updateOne<T = Data>(query: UpdateQuery): Promise<T | null>;
24
+ remove<T = Data>(query: RemoveQuery): Promise<T[] | null>;
25
+ removeOne<T = Data>(query: RemoveQuery): Promise<T | null>;
19
26
  removeCollection(collection: string): Promise<boolean>;
20
- updateOneOrAdd<T = Data>(collection: string, search: Search<T>, updater: Updater<T>, opts?: UpdateOneOrAdd<T>): Promise<boolean>;
21
- toggleOne<T = Data>(collection: string, search: Search<T>, data?: Arg<T>, context?: VContext): Promise<boolean>;
27
+ updateOneOrAdd<T = Data>(query: UpdateOneOrAddQuery): Promise<T>;
28
+ toggleOne<T = Data>(query: ToggleOneQuery): Promise<T | null>;
22
29
  }
23
30
  export interface UpdateOneOrAdd<T> {
24
31
  add_arg?: Arg<T>;
25
32
  id_gen?: boolean;
26
33
  context?: VContext;
27
34
  }
28
- export interface ValtheraCompatibleInternal {
29
- c(config: VQuery): CollectionManager;
30
- getCollections(): Promise<string[]>;
31
- ensureCollection(config: VQuery): Promise<boolean>;
32
- issetCollection(config: VQuery): Promise<boolean>;
33
- add<T = Data>(config: VQuery): Promise<T>;
34
- find<T = Data>(config: VQuery): Promise<T[]>;
35
- findOne<T = Data>(config: VQuery): Promise<T | null>;
36
- update(config: VQuery): Promise<boolean>;
37
- updateOne(config: VQuery): Promise<boolean>;
38
- remove(config: VQuery): Promise<boolean>;
39
- removeOne(config: VQuery): Promise<boolean>;
40
- removeCollection(config: VQuery): Promise<boolean>;
41
- updateOneOrAdd(config: VQuery): Promise<boolean>;
42
- toggleOne(config: VQuery): Promise<boolean>;
43
- }
@@ -1,4 +1,4 @@
1
- import Data from "../types/data.js";
2
- import FileCpu from "../types/fileCpu.js";
1
+ import { Data } from "../types/data.js";
2
+ import { FileCpu } from "../types/fileCpu.js";
3
3
  import { VQuery } from "../types/query.js";
4
4
  export declare function findUtil(query: VQuery, fileCpu: FileCpu, files: string[]): Promise<Data[]>;
@@ -4,4 +4,4 @@
4
4
  * @param fields - Criteria to compare.
5
5
  * @returns Whether the object matches the criteria.
6
6
  */
7
- export default function hasFields(obj: Object, fields: Object): boolean;
7
+ export declare function hasFields(obj: Object, fields: Object): boolean;
@@ -4,7 +4,7 @@
4
4
  * @param fields - Criteria to compare.
5
5
  * @returns Whether the object matches the criteria.
6
6
  */
7
- export default function hasFields(obj, fields) {
7
+ export function hasFields(obj, fields) {
8
8
  const keys = Object.keys(fields);
9
9
  return keys.every(key => {
10
10
  if (obj[key] !== undefined) {
@@ -2,4 +2,4 @@ import { Arg } from "../types/arg.js";
2
2
  /**
3
3
  * Checks if an object meets the criteria specified in the fields with operators.
4
4
  */
5
- export default function hasFieldsAdvanced(obj: Object, fields: Arg): any;
5
+ export declare function hasFieldsAdvanced(obj: Object, fields: Arg): any;
@@ -1,9 +1,9 @@
1
- import hasFields from "./hasFields.js";
1
+ import { hasFields } from "./hasFields.js";
2
2
  import { compareIds } from "./id.js";
3
3
  /**
4
4
  * Checks if an object meets the criteria specified in the fields with operators.
5
5
  */
6
- export default function hasFieldsAdvanced(obj, fields) {
6
+ export function hasFieldsAdvanced(obj, fields) {
7
7
  if (typeof fields !== "object" || fields === null) {
8
8
  throw new Error("Fields must be an object");
9
9
  }
@@ -8,4 +8,4 @@ import { FindOpts } from "../types/options.js";
8
8
  * @param [findOpts.exclude] - An array of fields to exclude from the target object.
9
9
  * @returns The updated object.
10
10
  */
11
- export default function updateFindObject(obj: Object, findOpts: FindOpts): Object;
11
+ export declare function updateFindObject(obj: Object, findOpts: FindOpts): Object;
@@ -7,7 +7,7 @@
7
7
  * @param [findOpts.exclude] - An array of fields to exclude from the target object.
8
8
  * @returns The updated object.
9
9
  */
10
- export default function updateFindObject(obj, findOpts) {
10
+ export function updateFindObject(obj, findOpts) {
11
11
  const { transform, select, exclude, } = findOpts;
12
12
  if (typeof transform === "function")
13
13
  obj = transform(obj);
@@ -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 default function updateObjectAdvanced(obj: Record<string, any>, field: UpdaterArg): Record<string, any>;
7
+ export declare function updateObjectAdvanced(obj: Record<string, any>, field: UpdaterArg): Record<string, any>;
@@ -4,7 +4,7 @@ import { _deepMerge } from "./merge.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 default function updateObjectAdvanced(obj, field) {
7
+ export function updateObjectAdvanced(obj, field) {
8
8
  if (typeof field !== "object" || field === null) {
9
9
  throw new Error("Fields must be an object or object array");
10
10
  }
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const version = "0.3.2";
1
+ export const version = "0.5.0-alpha.0";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/db-core",
3
- "version": "0.3.2",
3
+ "version": "0.5.0-alpha.0",
4
4
  "types": "dist/index.d.ts",
5
5
  "main": "dist/index.js",
6
6
  "author": "wxn0brP",
@@ -1,47 +0,0 @@
1
- import { Arg, Search, Updater } from "../types/arg.js";
2
- import { DbFindOpts, FindOpts } from "../types/options.js";
3
- import { VContext } from "../types/types.js";
4
- import Data from "../types/data.js";
5
- import { UpdateOneOrAdd, ValtheraCompatible } from "../types/valthera.js";
6
- declare class CollectionManager<D = Data> {
7
- private db;
8
- private collection;
9
- constructor(db: ValtheraCompatible, collection: string);
10
- /**
11
- * Add data to a database.
12
- */
13
- add<T = Data>(data: Arg<T & D>, id_gen?: boolean): Promise<T>;
14
- /**
15
- * Find data in a database.
16
- */
17
- find<T = Data>(search?: Search<T & D>, options?: DbFindOpts<T & D>, findOpts?: FindOpts<T & D>, context?: VContext): Promise<T[]>;
18
- /**
19
- * Find one data entry in a database.
20
- */
21
- findOne<T = Data>(search?: Search<T & D>, findOpts?: FindOpts<T & D>, context?: VContext): Promise<T>;
22
- /**
23
- * Update data in a database.
24
- */
25
- update<T = Data>(search: Search<T & D>, updater: Updater<T & D>, context?: VContext): Promise<boolean>;
26
- /**
27
- * Update one data entry in a database.
28
- */
29
- updateOne<T = Data>(search: Search<T & D>, updater: Updater<T & D>, context?: VContext): Promise<boolean>;
30
- /**
31
- * Remove data from a database.
32
- */
33
- remove<T = Data>(search: Search<T & D>, context?: VContext): Promise<boolean>;
34
- /**
35
- * Remove one data entry from a database.
36
- */
37
- removeOne<T = Data>(search: Search<T & D>, context?: VContext): Promise<boolean>;
38
- /**
39
- * Asynchronously updates one entry in a database or adds a new one if it doesn't exist.
40
- */
41
- updateOneOrAdd<T = Data>(search: Search<T & D>, updater: Updater<T & D>, { add_arg, context, id_gen }?: UpdateOneOrAdd<T & D>): Promise<boolean>;
42
- /**
43
- * Asynchronously removes one entry in a database or adds a new one if it doesn't exist. Usage e.g. for toggling a flag.
44
- */
45
- toggleOne<T = Data>(search: Search<T & D>, data?: Arg<T & D>, context?: VContext): Promise<boolean>;
46
- }
47
- export default CollectionManager;
@@ -1,63 +0,0 @@
1
- class CollectionManager {
2
- db;
3
- collection;
4
- constructor(db, collection) {
5
- this.db = db;
6
- this.collection = collection;
7
- }
8
- /**
9
- * Add data to a database.
10
- */
11
- async add(data, id_gen = true) {
12
- return await this.db.add(this.collection, data, id_gen);
13
- }
14
- /**
15
- * Find data in a database.
16
- */
17
- async find(search = {}, options = {}, findOpts = {}, context = {}) {
18
- return await this.db.find(this.collection, search, options, findOpts, context);
19
- }
20
- /**
21
- * Find one data entry in a database.
22
- */
23
- async findOne(search = {}, findOpts = {}, context = {}) {
24
- return await this.db.findOne(this.collection, search, findOpts, context);
25
- }
26
- /**
27
- * Update data in a database.
28
- */
29
- async update(search, updater, context = {}) {
30
- return await this.db.update(this.collection, search, updater, context);
31
- }
32
- /**
33
- * Update one data entry in a database.
34
- */
35
- async updateOne(search, updater, context = {}) {
36
- return await this.db.updateOne(this.collection, search, updater, context);
37
- }
38
- /**
39
- * Remove data from a database.
40
- */
41
- async remove(search, context = {}) {
42
- return await this.db.remove(this.collection, search, context);
43
- }
44
- /**
45
- * Remove one data entry from a database.
46
- */
47
- async removeOne(search, context = {}) {
48
- return await this.db.removeOne(this.collection, search, context);
49
- }
50
- /**
51
- * Asynchronously updates one entry in a database or adds a new one if it doesn't exist.
52
- */
53
- async updateOneOrAdd(search, updater, { add_arg = {}, context = {}, id_gen = true } = {}) {
54
- return await this.db.updateOneOrAdd(this.collection, search, updater, { add_arg, context, id_gen });
55
- }
56
- /**
57
- * Asynchronously removes one entry in a database or adds a new one if it doesn't exist. Usage e.g. for toggling a flag.
58
- */
59
- async toggleOne(search, data = {}, context = {}) {
60
- return await this.db.toggleOne(this.collection, search, data, context);
61
- }
62
- }
63
- export default CollectionManager;