@wxn0brp/db 0.0.6 → 0.0.8

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 (144) hide show
  1. package/README.md +1 -1
  2. package/dist/cjs/CollectionManager.d.ts +11 -10
  3. package/dist/cjs/CollectionManager.js +4 -4
  4. package/dist/cjs/action.d.ts +10 -15
  5. package/dist/cjs/action.js +6 -12
  6. package/dist/cjs/client/database.d.ts +72 -0
  7. package/dist/cjs/{remote/client → client}/database.js +10 -10
  8. package/dist/cjs/database.d.ts +10 -18
  9. package/dist/cjs/database.js +9 -17
  10. package/dist/cjs/file/find.d.ts +3 -3
  11. package/dist/cjs/file/remove.d.ts +2 -2
  12. package/dist/cjs/file/update.d.ts +2 -2
  13. package/dist/cjs/file/update.js +8 -6
  14. package/dist/cjs/index.d.ts +8 -2
  15. package/dist/cjs/index.js +2 -2
  16. package/dist/cjs/relation.d.ts +3 -3
  17. package/dist/cjs/types/arg.d.ts +7 -1
  18. package/dist/cjs/types/searchOpts.d.ts +5 -4
  19. package/dist/cjs/types/types.d.ts +1 -0
  20. package/dist/cjs/types/updater.d.ts +26 -0
  21. package/dist/cjs/types/updater.js +5 -0
  22. package/dist/cjs/utils/updateObject.d.ts +4 -5
  23. package/dist/cjs/utils/updateObject.js +135 -5
  24. package/dist/esm/CollectionManager.d.ts +11 -10
  25. package/dist/esm/CollectionManager.js +4 -4
  26. package/dist/esm/action.d.ts +10 -15
  27. package/dist/esm/action.js +6 -12
  28. package/dist/esm/client/database.d.ts +72 -0
  29. package/dist/esm/{remote/client → client}/database.js +10 -10
  30. package/dist/esm/database.d.ts +10 -18
  31. package/dist/esm/database.js +9 -17
  32. package/dist/esm/file/find.d.ts +3 -3
  33. package/dist/esm/file/remove.d.ts +2 -2
  34. package/dist/esm/file/update.d.ts +2 -2
  35. package/dist/esm/file/update.js +11 -9
  36. package/dist/esm/index.d.ts +8 -2
  37. package/dist/esm/index.js +2 -2
  38. package/dist/esm/relation.d.ts +3 -3
  39. package/dist/esm/types/arg.d.ts +7 -1
  40. package/dist/esm/types/searchOpts.d.ts +5 -4
  41. package/dist/esm/types/types.d.ts +1 -0
  42. package/dist/esm/types/updater.d.ts +26 -0
  43. package/dist/esm/types/updater.js +4 -0
  44. package/dist/esm/utils/updateObject.d.ts +4 -5
  45. package/dist/esm/utils/updateObject.js +135 -5
  46. package/package.json +8 -11
  47. package/dist/cjs/remote/client/database.d.ts +0 -71
  48. package/dist/cjs/remote/server/auth.d.ts +0 -31
  49. package/dist/cjs/remote/server/auth.js +0 -99
  50. package/dist/cjs/remote/server/db.d.ts +0 -2
  51. package/dist/cjs/remote/server/db.js +0 -223
  52. package/dist/cjs/remote/server/function.d.ts +0 -2
  53. package/dist/cjs/remote/server/function.js +0 -89
  54. package/dist/cjs/remote/server/graph.d.ts +0 -2
  55. package/dist/cjs/remote/server/graph.js +0 -145
  56. package/dist/cjs/remote/server/gui/css/main.css +0 -130
  57. package/dist/cjs/remote/server/gui/css/scrool.css +0 -81
  58. package/dist/cjs/remote/server/gui/css/style.css +0 -61
  59. package/dist/cjs/remote/server/gui/favicon.svg +0 -12
  60. package/dist/cjs/remote/server/gui/html/data.html +0 -15
  61. package/dist/cjs/remote/server/gui/html/main.html +0 -46
  62. package/dist/cjs/remote/server/gui/html/nav.html +0 -25
  63. package/dist/cjs/remote/server/gui/html/popup.html +0 -51
  64. package/dist/cjs/remote/server/gui/index.html +0 -49
  65. package/dist/cjs/remote/server/gui/js/api.js +0 -166
  66. package/dist/cjs/remote/server/gui/js/index.js +0 -17
  67. package/dist/cjs/remote/server/gui/js/loadHTML.js +0 -16
  68. package/dist/cjs/remote/server/gui/js/popUp.js +0 -72
  69. package/dist/cjs/remote/server/gui/js/queryApi.js +0 -51
  70. package/dist/cjs/remote/server/gui/js/queryDb.js +0 -79
  71. package/dist/cjs/remote/server/gui/js/queryGraph.js +0 -144
  72. package/dist/cjs/remote/server/gui/js/render.js +0 -64
  73. package/dist/cjs/remote/server/gui/js/templates.js +0 -31
  74. package/dist/cjs/remote/server/gui/js/utils.js +0 -36
  75. package/dist/cjs/remote/server/gui/js/vars.js +0 -9
  76. package/dist/cjs/remote/server/gui/libs/core.js +0 -176
  77. package/dist/cjs/remote/server/gui/libs/d3.v7.min.js +0 -2
  78. package/dist/cjs/remote/server/gui/libs/handlebars.min.js +0 -29
  79. package/dist/cjs/remote/server/gui/libs/json5.min.js +0 -1
  80. package/dist/cjs/remote/server/index.d.ts +0 -1
  81. package/dist/cjs/remote/server/index.js +0 -66
  82. package/dist/cjs/remote/server/initDataBases.d.ts +0 -1
  83. package/dist/cjs/remote/server/initDataBases.js +0 -25
  84. package/dist/cjs/remote/server/pathUtils.d.ts +0 -1
  85. package/dist/cjs/remote/server/pathUtils.js +0 -12
  86. package/dist/cjs/remote/server/secret.d.ts +0 -1
  87. package/dist/cjs/remote/server/secret.js +0 -25
  88. package/dist/cjs/remote/serverMgmt/index.d.ts +0 -1
  89. package/dist/cjs/remote/serverMgmt/index.js +0 -87
  90. package/dist/esm/remote/client/database.d.ts +0 -71
  91. package/dist/esm/remote/server/auth.d.ts +0 -31
  92. package/dist/esm/remote/server/auth.js +0 -87
  93. package/dist/esm/remote/server/db.d.ts +0 -2
  94. package/dist/esm/remote/server/db.js +0 -218
  95. package/dist/esm/remote/server/function.d.ts +0 -2
  96. package/dist/esm/remote/server/function.js +0 -87
  97. package/dist/esm/remote/server/graph.d.ts +0 -2
  98. package/dist/esm/remote/server/graph.js +0 -143
  99. package/dist/esm/remote/server/gui/css/main.css +0 -130
  100. package/dist/esm/remote/server/gui/css/scrool.css +0 -81
  101. package/dist/esm/remote/server/gui/css/style.css +0 -61
  102. package/dist/esm/remote/server/gui/favicon.svg +0 -12
  103. package/dist/esm/remote/server/gui/html/data.html +0 -15
  104. package/dist/esm/remote/server/gui/html/main.html +0 -46
  105. package/dist/esm/remote/server/gui/html/nav.html +0 -25
  106. package/dist/esm/remote/server/gui/html/popup.html +0 -51
  107. package/dist/esm/remote/server/gui/index.html +0 -49
  108. package/dist/esm/remote/server/gui/js/api.js +0 -166
  109. package/dist/esm/remote/server/gui/js/index.js +0 -17
  110. package/dist/esm/remote/server/gui/js/loadHTML.js +0 -16
  111. package/dist/esm/remote/server/gui/js/popUp.js +0 -72
  112. package/dist/esm/remote/server/gui/js/queryApi.js +0 -51
  113. package/dist/esm/remote/server/gui/js/queryDb.js +0 -79
  114. package/dist/esm/remote/server/gui/js/queryGraph.js +0 -144
  115. package/dist/esm/remote/server/gui/js/render.js +0 -64
  116. package/dist/esm/remote/server/gui/js/templates.js +0 -31
  117. package/dist/esm/remote/server/gui/js/utils.js +0 -36
  118. package/dist/esm/remote/server/gui/js/vars.js +0 -9
  119. package/dist/esm/remote/server/gui/libs/core.js +0 -176
  120. package/dist/esm/remote/server/gui/libs/d3.v7.min.js +0 -2
  121. package/dist/esm/remote/server/gui/libs/handlebars.min.js +0 -29
  122. package/dist/esm/remote/server/gui/libs/json5.min.js +0 -1
  123. package/dist/esm/remote/server/index.d.ts +0 -1
  124. package/dist/esm/remote/server/index.js +0 -61
  125. package/dist/esm/remote/server/initDataBases.d.ts +0 -1
  126. package/dist/esm/remote/server/initDataBases.js +0 -20
  127. package/dist/esm/remote/server/pathUtils.d.ts +0 -1
  128. package/dist/esm/remote/server/pathUtils.js +0 -6
  129. package/dist/esm/remote/server/secret.d.ts +0 -1
  130. package/dist/esm/remote/server/secret.js +0 -19
  131. package/dist/esm/remote/serverMgmt/index.d.ts +0 -1
  132. package/dist/esm/remote/serverMgmt/index.js +0 -82
  133. /package/dist/cjs/{remote/client → client}/function.d.ts +0 -0
  134. /package/dist/cjs/{remote/client → client}/function.js +0 -0
  135. /package/dist/cjs/{remote/client → client}/graph.d.ts +0 -0
  136. /package/dist/cjs/{remote/client → client}/graph.js +0 -0
  137. /package/dist/cjs/{remote/client → client}/remote.d.ts +0 -0
  138. /package/dist/cjs/{remote/client → client}/remote.js +0 -0
  139. /package/dist/esm/{remote/client → client}/function.d.ts +0 -0
  140. /package/dist/esm/{remote/client → client}/function.js +0 -0
  141. /package/dist/esm/{remote/client → client}/graph.d.ts +0 -0
  142. /package/dist/esm/{remote/client → client}/graph.js +0 -0
  143. /package/dist/esm/{remote/client → client}/remote.d.ts +0 -0
  144. /package/dist/esm/{remote/client → client}/remote.js +0 -0
@@ -50,14 +50,6 @@ class DataBase {
50
50
  }
51
51
  /**
52
52
  * Find one data entry in a database.
53
- *
54
- * @async
55
- * @function
56
- * @param {string} collection - Name of the database collection.
57
- * @param {function|Object} search - The query. It can be an object or a function.
58
- * @param {Object} context - The context object (for functions).
59
- * @param {Object} findOpts - Update result object with findOpts options.
60
- * @returns {Promise<Object|null>} A Promise that resolves with the first matching data entry.
61
53
  */
62
54
  async findOne(collection, search, context = {}, findOpts = {}) {
63
55
  return await this.executor.addOp(this.dbAction.findOne.bind(this.dbAction), collection, search, context, findOpts);
@@ -65,14 +57,14 @@ class DataBase {
65
57
  /**
66
58
  * Update data in a database.
67
59
  */
68
- async update(collection, search, arg, context = {}) {
69
- return await this.executor.addOp(this.dbAction.update.bind(this.dbAction), collection, search, arg, context);
60
+ async update(collection, search, updater, context = {}) {
61
+ return await this.executor.addOp(this.dbAction.update.bind(this.dbAction), collection, search, updater, context);
70
62
  }
71
63
  /**
72
64
  * Update one data entry in a database.
73
65
  */
74
- async updateOne(collection, search, arg, context = {}) {
75
- return await this.executor.addOp(this.dbAction.updateOne.bind(this.dbAction), collection, search, arg, context);
66
+ async updateOne(collection, search, updater, context = {}) {
67
+ return await this.executor.addOp(this.dbAction.updateOne.bind(this.dbAction), collection, search, updater, context);
76
68
  }
77
69
  /**
78
70
  * Remove data from a database.
@@ -89,14 +81,14 @@ class DataBase {
89
81
  /**
90
82
  * Asynchronously updates one entry in a database or adds a new one if it doesn't exist.
91
83
  */
92
- async updateOneOrAdd(collection, search, arg, add_arg = {}, context = {}, id_gen = true) {
93
- const res = await this.updateOne(collection, search, arg, context);
84
+ async updateOneOrAdd(collection, search, updater, add_arg = {}, context = {}, id_gen = true) {
85
+ const res = await this.updateOne(collection, search, updater, context);
94
86
  if (!res) {
95
87
  const assignData = [];
96
88
  if (typeof search === "object" && !Array.isArray(search))
97
89
  assignData.push(search);
98
- if (typeof arg === "object" && !Array.isArray(arg))
99
- assignData.push(arg);
90
+ if (typeof updater === "object" && !Array.isArray(updater))
91
+ assignData.push(updater);
100
92
  if (typeof add_arg === "object" && !Array.isArray(add_arg))
101
93
  assignData.push(add_arg);
102
94
  await this.add(collection, Object.assign({}, ...assignData), id_gen);
@@ -106,7 +98,7 @@ class DataBase {
106
98
  /**
107
99
  * Removes a database collection from the file system.
108
100
  */
109
- removeCollection(collection) {
101
+ async removeCollection(collection) {
110
102
  this.dbAction.removeCollection(collection);
111
103
  }
112
104
  }
@@ -1,11 +1,11 @@
1
- import { ArgOrFunc } from "../types/arg.js";
1
+ import { Search } from "../types/arg.js";
2
2
  import { Context } from "../types/types.js";
3
3
  import { FindOpts } from "../types/options.js";
4
4
  /**
5
5
  * Asynchronously finds entries in a file based on search criteria.
6
6
  */
7
- export declare function find(file: string, arg: ArgOrFunc, context?: Context, findOpts?: FindOpts): Promise<unknown>;
7
+ export declare function find(file: string, arg: Search, context?: Context, findOpts?: FindOpts): Promise<unknown>;
8
8
  /**
9
9
  * Asynchronously finds one entry in a file based on search criteria.
10
10
  */
11
- export declare function findOne(file: string, arg: ArgOrFunc, context?: Context, findOpts?: FindOpts): Promise<unknown>;
11
+ export declare function findOne(file: string, arg: Search, context?: Context, findOpts?: FindOpts): Promise<unknown>;
@@ -1,7 +1,7 @@
1
- import { ArgOrFunc } from "../types/arg.js";
1
+ import { Search } from "../types/arg.js";
2
2
  import { Context } from "../types/types.js";
3
3
  /**
4
4
  * Asynchronously removes entries from a file based on search criteria.
5
5
  */
6
- declare function remove(cpath: string, arg: ArgOrFunc, context?: Context, one?: boolean): Promise<boolean>;
6
+ declare function remove(cpath: string, arg: Search, context?: Context, one?: boolean): Promise<boolean>;
7
7
  export default remove;
@@ -1,7 +1,7 @@
1
1
  import { Context } from "../types/types.js";
2
- import { ArgOrFunc } from "../types/arg.js";
2
+ import { Search, Updater } from "../types/arg.js";
3
3
  /**
4
4
  * Asynchronously updates entries in a file based on search criteria and an updater function or object.
5
5
  */
6
- declare function update(cpath: string, arg: ArgOrFunc, obj: ArgOrFunc, context?: Context, one?: boolean): Promise<boolean>;
6
+ declare function update(cpath: string, arg: Search, updater: Updater, context?: Context, one?: boolean): Promise<boolean>;
7
7
  export default update;
@@ -1,8 +1,8 @@
1
- import { existsSync, promises, appendFileSync, readdirSync } from "fs";
1
+ import { existsSync, promises, readdirSync } from "fs";
2
2
  import { pathRepair, createRL } from "./utils.js";
3
3
  import { parse, stringify } from "../format.js";
4
4
  import hasFieldsAdvanced from "../utils/hasFieldsAdvanced.js";
5
- import updateObject from "../utils/updateObject.js";
5
+ import updateObjectAdvanced from "../utils/updateObject.js";
6
6
  /**
7
7
  * Updates a file based on search criteria and an updater function or object.
8
8
  */
@@ -18,7 +18,7 @@ async function updateWorker(file, search, updater, context = {}, one = false) {
18
18
  let updated = false;
19
19
  for await (let line of rl) {
20
20
  if (one && updated) {
21
- appendFileSync(file, line + "\n");
21
+ await promises.appendFile(file, line + "\n");
22
22
  continue;
23
23
  }
24
24
  const data = parse(line);
@@ -30,17 +30,19 @@ async function updateWorker(file, search, updater, context = {}, one = false) {
30
30
  ob = hasFieldsAdvanced(data, search);
31
31
  }
32
32
  if (ob) {
33
- let updateObj;
33
+ let updateObj = data;
34
34
  if (typeof updater === "function") {
35
- updateObj = updater(data, context);
35
+ const updateObjValue = updater(data, context);
36
+ if (updateObjValue)
37
+ updateObj = updateObjValue;
36
38
  }
37
39
  else if (typeof updater === "object" && !Array.isArray(updater)) {
38
- updateObj = updateObject(data, updater);
40
+ updateObj = updateObjectAdvanced(data, updater);
39
41
  }
40
42
  line = await stringify(updateObj);
41
43
  updated = true;
42
44
  }
43
- appendFileSync(file, line + "\n");
45
+ await promises.appendFile(file, line + "\n");
44
46
  }
45
47
  await promises.writeFile(file + ".tmp", "");
46
48
  return updated;
@@ -48,12 +50,12 @@ async function updateWorker(file, search, updater, context = {}, one = false) {
48
50
  /**
49
51
  * Asynchronously updates entries in a file based on search criteria and an updater function or object.
50
52
  */
51
- async function update(cpath, arg, obj, context = {}, one = false) {
53
+ async function update(cpath, arg, updater, context = {}, one = false) {
52
54
  let files = readdirSync(cpath).filter(file => !/\.tmp$/.test(file));
53
55
  files.reverse();
54
56
  let update = false;
55
57
  for (const file of files) {
56
- const updated = await updateWorker(cpath + file, arg, obj, context, one);
58
+ const updated = await updateWorker(cpath + file, arg, updater, context, one);
57
59
  if (one && updated)
58
60
  return true;
59
61
  update = update || updated;
@@ -1,7 +1,13 @@
1
1
  import DataBase from "./database.js";
2
2
  import Graph from "./graph.js";
3
- import DataBaseRemote from "./remote/client/database.js";
4
- import GraphRemote from "./remote/client/graph.js";
3
+ import DataBaseRemote from "./client/database.js";
4
+ import GraphRemote from "./client/graph.js";
5
5
  import genId from "./gen.js";
6
6
  import Relation from "./relation.js";
7
7
  export { DataBase, Graph, DataBaseRemote, GraphRemote, Relation, genId };
8
+ import type Id from "./types/Id.js";
9
+ import type { Arg, Search, Updater } from "./types/arg.js";
10
+ import type { DbFindOpts, FindOpts, DbOpts } from "./types/options.js";
11
+ import type Data from "./types/data.js";
12
+ import type { SearchOptions } from "./types/searchOpts.js";
13
+ export type { Id, Arg, Search, Updater, DbFindOpts, FindOpts, DbOpts, Data, SearchOptions };
package/dist/esm/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import DataBase from "./database.js";
2
2
  import Graph from "./graph.js";
3
- import DataBaseRemote from "./remote/client/database.js";
4
- import GraphRemote from "./remote/client/graph.js";
3
+ import DataBaseRemote from "./client/database.js";
4
+ import GraphRemote from "./client/graph.js";
5
5
  import genId from "./gen.js";
6
6
  import Relation from "./relation.js";
7
7
  export { DataBase, Graph, DataBaseRemote, GraphRemote, Relation, genId };
@@ -1,5 +1,5 @@
1
1
  import DataBase from "./database.js";
2
- import { ArgOrFunc } from "./types/arg.js";
2
+ import { Search } from "./types/arg.js";
3
3
  import { DbFindOpts } from "./types/options.js";
4
4
  export interface Databases {
5
5
  [key: string]: DataBase;
@@ -36,7 +36,7 @@ declare class Relation {
36
36
  * @param relations - Relations configuration
37
37
  * @param options - Search options
38
38
  */
39
- find(path: string, search: ArgOrFunc, relations?: {}, options?: DbFindOpts): Promise<{
39
+ find(path: string, search: Search, relations?: {}, options?: DbFindOpts): Promise<{
40
40
  constructor: Function;
41
41
  toString(): string;
42
42
  toLocaleString(): string;
@@ -51,7 +51,7 @@ declare class Relation {
51
51
  * @param search - Search query or function
52
52
  * @param relations - Relations configuration
53
53
  */
54
- findOne(path: string, search: ArgOrFunc, relations?: Object): Promise<{
54
+ findOne(path: string, search: Search, relations?: Object): Promise<{
55
55
  constructor: Function;
56
56
  toString(): string;
57
57
  toLocaleString(): string;
@@ -1,6 +1,12 @@
1
1
  import Id from "./Id";
2
+ import { SearchOptions } from "./searchOpts";
3
+ import { Context } from "./types";
4
+ import { UpdaterArg } from "./updater";
2
5
  export interface Arg {
3
6
  _id?: Id;
4
7
  [key: string]: any;
5
8
  }
6
- export type ArgOrFunc = Arg | Function;
9
+ export type SearchFunc<T = any> = (data: T, context: Context) => boolean;
10
+ export type UpdaterFunc<T = any> = (data: T, context: Context) => boolean;
11
+ export type Search<T = any> = SearchOptions | SearchFunc<T>;
12
+ export type Updater<T = any> = UpdaterArg | UpdaterArg[] | UpdaterFunc<T>;
@@ -4,23 +4,24 @@
4
4
  * This module defines the types and structures for search operators used
5
5
  * to validate and query data objects.
6
6
  */
7
+ import { Arg } from "./arg";
7
8
  /** Logical Operators */
8
9
  export type LogicalOperators = {
9
10
  /**
10
11
  * Recursively applies multiple conditions, all of which must evaluate to true.
11
12
  * Can include other operators such as $gt, $exists, or nested $and/$or conditions.
12
13
  */
13
- $and?: Array<PredefinedSearchOperators>;
14
+ $and?: Array<SearchOptions>;
14
15
  /**
15
16
  * Recursively applies multiple conditions, at least one of which must evaluate to true.
16
17
  * Can include other operators such as $lt, $type, or nested $and/$or conditions.
17
18
  */
18
- $or?: Array<PredefinedSearchOperators>;
19
+ $or?: Array<SearchOptions>;
19
20
  /**
20
21
  * Negates a single condition.
21
22
  * Can include any other operator as its value.
22
23
  */
23
- $not?: PredefinedSearchOperators;
24
+ $not?: SearchOptions;
24
25
  };
25
26
  /** Comparison Operators */
26
27
  export type ComparisonOperators = {
@@ -58,4 +59,4 @@ export type PredefinedSearchOperators = LogicalOperators & ComparisonOperators &
58
59
  /**
59
60
  * SearchOptions can be either a function or an object with predefined operators.
60
61
  */
61
- export type SearchOptions = PredefinedSearchOperators | ((doc: Record<string, any>) => boolean);
62
+ export type SearchOptions = PredefinedSearchOperators & Arg;
@@ -1,4 +1,5 @@
1
1
  export interface Context {
2
+ [key: string]: any;
2
3
  }
3
4
  export interface SortedFiles {
4
5
  i: number;
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Predefined type for updating data.
3
+ */
4
+ /** Arrays */
5
+ export type ArrayUpdater = {
6
+ $push?: any;
7
+ /** Pushes items into an array and removes duplicates */
8
+ $pushset?: any;
9
+ $pull?: any;
10
+ $pullall?: any;
11
+ };
12
+ /** Objects */
13
+ export type ObjectUpdater = {
14
+ $merge?: any;
15
+ };
16
+ /** Values */
17
+ export type ValueUpdater = {
18
+ $set?: any;
19
+ $inc?: any;
20
+ $dec?: any;
21
+ $unset?: any;
22
+ $rename?: any;
23
+ };
24
+ export type UpdaterArg = ArrayUpdater & ObjectUpdater & ValueUpdater & {
25
+ [key: string]: any;
26
+ };
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Predefined type for updating data.
3
+ */
4
+ export {};
@@ -1,8 +1,7 @@
1
+ import { UpdaterArg } from "../types/updater.js";
1
2
  /**
2
3
  * Updates an object with new values.
3
- * @function
4
- * @param {Object} obj - The object to update.
5
- * @param {Object} newVal - An object containing new values to update in the target object.
6
- * @returns {Object} The updated object.
4
+ * @param obj - The object to update.
5
+ * @param fields - An object containing new values to update in the target object.
7
6
  */
8
- export default function updateObject(obj: any, newVal: any): any;
7
+ export default function updateObjectAdvanced(obj: any, fields: UpdaterArg | UpdaterArg[]): any;
@@ -1,11 +1,141 @@
1
1
  /**
2
2
  * Updates an object with new values.
3
- * @function
4
- * @param {Object} obj - The object to update.
5
- * @param {Object} newVal - An object containing new values to update in the target object.
6
- * @returns {Object} The updated object.
3
+ * @param obj - The object to update.
4
+ * @param fields - An object containing new values to update in the target object.
7
5
  */
8
- export default function updateObject(obj, newVal) {
6
+ export default function updateObjectAdvanced(obj, fields) {
7
+ if (typeof fields !== "object" || fields === null) {
8
+ throw new Error("Fields must be an object or object array");
9
+ }
10
+ const fieldsArray = Array.isArray(fields) ? fields : [fields];
11
+ for (const field of fieldsArray) {
12
+ if (typeof field !== "object" || field === null) {
13
+ throw new Error("Fields must be an object or object array");
14
+ }
15
+ updateAdvanced(obj, field);
16
+ const fieldsSubset = removeAdvancedOperators({ ...field });
17
+ updateObject(obj, fieldsSubset);
18
+ }
19
+ return obj;
20
+ }
21
+ function updateAdvanced(obj, fields) {
22
+ updateArray(obj, fields);
23
+ updateNested(obj, fields);
24
+ updateIncrement(obj, fields);
25
+ updateUnset(obj, fields);
26
+ updateRename(obj, fields);
27
+ }
28
+ function removeAdvancedOperators(fields) {
29
+ const advancedOperators = [
30
+ "push", "pushset", "pull", "pullall",
31
+ "inc", "dec",
32
+ "unset",
33
+ "merge",
34
+ "rename"
35
+ ].map(operator => "$" + operator);
36
+ advancedOperators.forEach(operator => delete fields[operator]);
37
+ return fields;
38
+ }
39
+ function updateArray(obj, fields) {
40
+ if ("$push" in fields) {
41
+ for (const [key, value] of Object.entries(fields["$push"])) {
42
+ if (Array.isArray(obj[key])) {
43
+ obj[key].push(value);
44
+ }
45
+ else {
46
+ obj[key] = [value];
47
+ }
48
+ }
49
+ }
50
+ if ("$pushset" in fields) {
51
+ for (const [key, value] of Object.entries(fields["$pushset"])) {
52
+ if (Array.isArray(obj[key])) {
53
+ obj[key].push(value);
54
+ }
55
+ else {
56
+ obj[key] = [value];
57
+ }
58
+ obj[key] = [...new Set(obj[key])];
59
+ }
60
+ }
61
+ if ("$pull" in fields) {
62
+ for (const [key, value] of Object.entries(fields["$pull"])) {
63
+ if (Array.isArray(obj[key])) {
64
+ obj[key] = obj[key].filter(item => item !== value);
65
+ }
66
+ }
67
+ }
68
+ if ("$pullall" in fields) {
69
+ for (const [key, values] of Object.entries(fields["$pullall"])) {
70
+ if (Array.isArray(obj[key])) {
71
+ obj[key] = obj[key].filter(item => !values.includes(item));
72
+ }
73
+ }
74
+ }
75
+ }
76
+ function updateNested(obj, fields) {
77
+ if ("$merge" in fields) {
78
+ for (const [key, value] of Object.entries(fields["$merge"])) {
79
+ if (typeof obj[key] === "object" && typeof value === "object") {
80
+ obj[key] = { ...obj[key], ...value };
81
+ }
82
+ else {
83
+ obj[key] = value;
84
+ }
85
+ }
86
+ }
87
+ }
88
+ function updateIncrement(obj, fields) {
89
+ if ("$inc" in fields) {
90
+ for (const [key, value] of Object.entries(fields["$inc"])) {
91
+ if (typeof obj[key] === "number" && typeof value === "number") {
92
+ obj[key] += value;
93
+ }
94
+ else if (!(key in obj)) {
95
+ obj[key] = value;
96
+ }
97
+ else {
98
+ throw new Error(`Cannot increment non-numeric value at key: ${key}`);
99
+ }
100
+ }
101
+ }
102
+ if ("$dec" in fields) {
103
+ for (const [key, value] of Object.entries(fields["$dec"])) {
104
+ if (typeof obj[key] === "number" && typeof value === "number") {
105
+ obj[key] -= value;
106
+ }
107
+ else if (!(key in obj)) {
108
+ obj[key] = value;
109
+ }
110
+ else {
111
+ throw new Error(`Cannot decrement non-numeric value at key: ${key}`);
112
+ }
113
+ }
114
+ }
115
+ }
116
+ function updateUnset(obj, fields) {
117
+ if ("$unset" in fields) {
118
+ for (const key of Object.keys(fields["$unset"])) {
119
+ delete obj[key];
120
+ }
121
+ }
122
+ }
123
+ function updateRename(obj, fields) {
124
+ if ("$rename" in fields) {
125
+ for (const [oldKey, newKey] of Object.entries(fields["$rename"])) {
126
+ if (oldKey in obj) {
127
+ obj[newKey] = obj[oldKey];
128
+ delete obj[oldKey];
129
+ }
130
+ }
131
+ }
132
+ }
133
+ /**
134
+ * Updates an object with new values.
135
+ * @param obj - The object to update.
136
+ * @param newVal - An object containing new values to update in the target object.
137
+ */
138
+ function updateObject(obj, newVal) {
9
139
  for (let key in newVal) {
10
140
  if (newVal.hasOwnProperty(key)) {
11
141
  obj[key] = newVal[key];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/db",
3
- "version": "0.0.6",
3
+ "version": "0.0.8",
4
4
  "main": "dist/esm/index.js",
5
5
  "types": "dist/esm/index.d.ts",
6
6
  "description": "A simple file-based database management system with support for CRUD operations, custom queries, and graph structures.",
@@ -25,23 +25,14 @@
25
25
  "readline": "^1.3.0"
26
26
  },
27
27
  "devDependencies": {
28
- "@types/body-parser": "^1.19.5",
29
28
  "@types/got": "^9.6.12",
30
29
  "@types/node": "^22.10.2",
31
- "ava": "^6.2.0",
32
- "body-parser": "^1.20.3",
33
- "cors": "^2.8.5",
34
- "dotenv": "^16.4.5",
35
- "express": "^4.21.1",
36
- "jwt-simple": "^0.5.6",
37
- "node-cache": "^5.1.2",
38
30
  "typescript": "^5.7.2"
39
31
  },
40
32
  "scripts": {
41
33
  "build:esm": "tsc",
42
34
  "build:cjs": "tsc --module CommonJS --outDir ./dist/cjs --declarationDir ./dist/cjs",
43
35
  "build": "npm run build:esm && npm run build:cjs",
44
- "postbuild": "node scripts/postbuild.js",
45
36
  "prepare": "npm run build",
46
37
  "postversion": "git push && git push --tags"
47
38
  },
@@ -51,7 +42,13 @@
51
42
  "exports": {
52
43
  ".": {
53
44
  "import": "./dist/esm/index.js",
54
- "require": "./dist/cjs/index.js"
45
+ "require": "./dist/cjs/index.js",
46
+ "types": "./dist/esm/index.d.ts"
47
+ },
48
+ "./*": {
49
+ "import": "./dist/esm/*",
50
+ "require": "./dist/cjs/*",
51
+ "types": "./dist/esm/*"
55
52
  }
56
53
  }
57
54
  }
@@ -1,71 +0,0 @@
1
- import CollectionManager from "../../CollectionManager.js";
2
- import { Remote, RequestData } from "./remote.js";
3
- import { Arg, ArgOrFunc } from "../../types/arg";
4
- import { DbFindOpts, FindOpts } from "../../types/options.js";
5
- import { Context } from "../../types/types";
6
- /**
7
- * Represents a database management class for performing CRUD operations.
8
- * Uses a remote database.
9
- * @class
10
- */
11
- declare class DataBaseRemote {
12
- remote: Remote;
13
- constructor(remote: Remote);
14
- /**
15
- * Make a request to the remote database.
16
- */
17
- _request(type: string, data: RequestData): Promise<any>;
18
- /**
19
- * Create a new instance of a CollectionManager class.
20
- */
21
- c(collection: string): CollectionManager;
22
- /**
23
- * Get the names of all available databases.
24
- */
25
- getCollections(): Promise<any>;
26
- /**
27
- * Check and create the specified collection if it doesn't exist.
28
- */
29
- checkCollection(collection: string): Promise<any>;
30
- /**
31
- * Check if a collection exists.
32
- */
33
- issetCollection(collection: any): Promise<any>;
34
- /**
35
- * Add data to a database.
36
- */
37
- add(collection: any, data: any, id_gen?: boolean): Promise<any>;
38
- /**
39
- * Find data in a database.
40
- */
41
- find(collection: string, search: ArgOrFunc, context?: Context, options?: DbFindOpts, findOpts?: FindOpts): Promise<any>;
42
- /**
43
- * Find one data entry in a database.
44
- */
45
- findOne(collection: string, search: ArgOrFunc, context?: Context, findOpts?: FindOpts): Promise<any>;
46
- /**
47
- * Update data in a database.
48
- */
49
- update(collection: string, search: ArgOrFunc, arg: ArgOrFunc, context?: Context): Promise<any>;
50
- /**
51
- * Update one data entry in a database.
52
- */
53
- updateOne(collection: string, search: ArgOrFunc, arg: ArgOrFunc, context?: Context): Promise<any>;
54
- /**
55
- * Remove data from a database.
56
- */
57
- remove(collection: string, search: ArgOrFunc, context?: Context): Promise<any>;
58
- /**
59
- * Remove one data entry from a database.
60
- */
61
- removeOne(collection: string, search: ArgOrFunc, context?: Context): Promise<any>;
62
- /**
63
- * Asynchronously updates one entry in a database or adds a new one if it doesn't exist.
64
- */
65
- updateOneOrAdd(collection: string, search: ArgOrFunc, arg: ArgOrFunc, add_arg?: Arg, context?: Context, id_gen?: boolean): Promise<any>;
66
- /**
67
- * Removes a database collection from the file system.
68
- */
69
- removeCollection(name: string): Promise<any>;
70
- }
71
- export default DataBaseRemote;
@@ -1,31 +0,0 @@
1
- export declare function authMiddleware(req: any, res: any, next: any): Promise<any>;
2
- export declare function loginFunction(login: any, password: any): Promise<{
3
- err: boolean;
4
- msg: string;
5
- token?: undefined;
6
- } | {
7
- err: boolean;
8
- token: string;
9
- msg?: undefined;
10
- }>;
11
- export declare function generateToken(payload: any): Promise<string>;
12
- export declare function removeToken(token: any): Promise<any>;
13
- export declare function addUserAccess(login: any, password: any): Promise<{
14
- err: boolean;
15
- msg: string;
16
- user?: undefined;
17
- } | {
18
- err: boolean;
19
- user: any;
20
- msg?: undefined;
21
- }>;
22
- export declare function checkUserAccess(login: any, password: any): Promise<{
23
- err: boolean;
24
- msg: string;
25
- user?: undefined;
26
- } | {
27
- err: boolean;
28
- user: any;
29
- msg?: undefined;
30
- }>;
31
- export declare function removeUser(idOrLogin: any): Promise<any>;