@wxn0brp/db-core 0.11.0-alpha.1 → 0.11.0-alpha.3

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.
@@ -20,17 +20,17 @@ export declare abstract class CustomActionsBase extends ActionsBase {
20
20
  /**
21
21
  * Update entries in the specified database based on search criteria and an updater function or object.
22
22
  */
23
- update(query: VQueryT.Update): Promise<Data[]>;
23
+ update(query: VQueryT.Update): Promise<import("../types/data.js").DataInternal[]>;
24
24
  /**
25
25
  * Update the first matching entry in the specified database based on search criteria and an updater function or object.
26
26
  */
27
- updateOne(query: VQueryT.Update): Promise<Data>;
27
+ updateOne(query: VQueryT.Update): Promise<import("../types/data.js").DataInternal>;
28
28
  /**
29
29
  * Remove entries from the specified database based on search criteria.
30
30
  */
31
- remove(query: VQueryT.Remove): Promise<Data[]>;
31
+ remove(query: VQueryT.Remove): Promise<import("../types/data.js").DataInternal[]>;
32
32
  /**
33
33
  * Remove the first matching entry from the specified database based on search criteria.
34
34
  */
35
- removeOne(query: VQueryT.Remove): Promise<Data>;
35
+ removeOne(query: VQueryT.Remove): Promise<import("../types/data.js").DataInternal>;
36
36
  }
@@ -1,4 +1,4 @@
1
- import { Data } from "./types/data.js";
1
+ import { Data, DataInternal } from "./types/data.js";
2
2
  import { FileCpu } from "./types/fileCpu.js";
3
3
  import { VQueryT } from "./types/query.js";
4
4
  export type WriteFile = (file: string, data: any[]) => Promise<void>;
@@ -10,7 +10,7 @@ export declare class CustomFileCpu implements FileCpu {
10
10
  constructor(readFile: ReadFile, writeFile: WriteFile);
11
11
  add(file: string, config: VQueryT.Add): Promise<void>;
12
12
  find(file: string, config: VQueryT.Find): Promise<Data[]>;
13
- findOne(file: string, config: VQueryT.FindOne): Promise<Data | false>;
14
- remove(file: string, config: VQueryT.Remove, one: boolean): Promise<Data[]>;
15
- update(file: string, config: VQueryT.Update, one: boolean): Promise<Data[]>;
13
+ findOne(file: string, config: VQueryT.FindOne): Promise<DataInternal | false>;
14
+ remove(file: string, config: VQueryT.Remove, one: boolean): Promise<DataInternal[]>;
15
+ update(file: string, config: VQueryT.Update, one: boolean): Promise<DataInternal[]>;
16
16
  }
@@ -1,6 +1,4 @@
1
- import { hasFieldsAdvanced } from "./utils/hasFieldsAdvanced.js";
2
- import { updateFindObject } from "./utils/updateFindObject.js";
3
- import { updateObjectAdvanced } from "./utils/updateObject.js";
1
+ import { matchObj, findObj, updateObj } from "./utils/process.js";
4
2
  export function pathRepair(path) {
5
3
  return path.replaceAll("//", "/");
6
4
  }
@@ -20,31 +18,28 @@ export class CustomFileCpu {
20
18
  async find(file, config) {
21
19
  file = pathRepair(file);
22
20
  const entries = await this._readFile(file);
23
- const { search, context = {}, findOpts = {} } = config;
24
- const results = entries.filter(entry => typeof search === "function" ? search(entry, context) : hasFieldsAdvanced(entry, search));
25
- return results.length ?
26
- results.map(res => updateFindObject(res, findOpts)) :
27
- [];
21
+ return entries
22
+ .map((entry) => findObj(config, entry))
23
+ .filter(Boolean);
28
24
  }
29
25
  async findOne(file, config) {
30
26
  file = pathRepair(file);
31
27
  const entries = await this._readFile(file);
32
- const { search, context = {}, findOpts = {} } = config;
33
- const result = entries.find(entry => typeof search === "function" ? search(entry, context) : hasFieldsAdvanced(entry, search));
34
- return result ?
35
- updateFindObject(result, findOpts) :
36
- false;
28
+ for (const entry of entries) {
29
+ const result = findObj(config, entry);
30
+ if (result)
31
+ return result;
32
+ }
33
+ return false;
37
34
  }
38
35
  async remove(file, config, one) {
39
36
  file = pathRepair(file);
40
37
  let entries = await this._readFile(file);
41
38
  const removed = [];
42
- const { search, context = {} } = config;
43
- entries = entries.filter(entry => {
39
+ entries = entries.filter((entry) => {
44
40
  if (removed.length && one)
45
41
  return true;
46
- let match = typeof search === "function" ? search(entry, context) : hasFieldsAdvanced(entry, search);
47
- if (match) {
42
+ if (matchObj(config, entry)) {
48
43
  removed.push(entry);
49
44
  return false;
50
45
  }
@@ -58,13 +53,11 @@ export class CustomFileCpu {
58
53
  file = pathRepair(file);
59
54
  let entries = await this._readFile(file);
60
55
  const updated = [];
61
- const { search, updater, context = {} } = config;
62
- entries = entries.map(entry => {
56
+ entries = entries.map((entry) => {
63
57
  if (updated.length && one)
64
58
  return entry;
65
- let match = typeof search === "function" ? search(entry, context) : hasFieldsAdvanced(entry, search);
66
- if (match) {
67
- const updatedEntry = typeof updater === "function" ? updater(entry, context) : updateObjectAdvanced(entry, updater);
59
+ if (matchObj(config, entry)) {
60
+ const updatedEntry = updateObj(config, entry);
68
61
  updated.push(updatedEntry);
69
62
  return updatedEntry;
70
63
  }
@@ -1,3 +1,4 @@
1
+ import { Data } from "./data.js";
1
2
  import { SearchOptions } from "./searchOpts.js";
2
3
  import { VContext } from "./types.js";
3
4
  import { UpdaterArg } from "./updater.js";
@@ -5,6 +6,6 @@ export type Arg<T = any> = {
5
6
  [K in keyof T]?: any;
6
7
  } & Record<string, any>;
7
8
  export type SearchFunc<T = any> = (data: T, context: VContext) => boolean;
8
- export type UpdaterFunc<T = any> = (data: T, context: VContext) => boolean;
9
+ export type UpdaterFunc<T = any> = (data: T, context: VContext) => Data | void;
9
10
  export type Search<T = any> = SearchOptions<T> | SearchFunc<T>;
10
- export type Updater<T = any> = UpdaterArg<T> | UpdaterArg<T>[] | UpdaterFunc<T>;
11
+ export type Updater<T = any> = UpdaterArg<T> | UpdaterFunc<T>;
@@ -0,0 +1,5 @@
1
+ import { Data, DataInternal } from "../types/data.js";
2
+ import { VQuery, VQueryT } from "../types/query.js";
3
+ export declare function findObj(config: VQueryT.Find | VQueryT.FindOne, obj: Data): DataInternal | null;
4
+ export declare function matchObj(config: VQuery, obj: Data): boolean;
5
+ export declare function updateObj(config: VQueryT.Update, obj: Data): DataInternal;
@@ -0,0 +1,30 @@
1
+ import { hasFieldsAdvanced } from "./hasFieldsAdvanced.js";
2
+ import { updateFindObject } from "./updateFindObject.js";
3
+ import { updateObjectAdvanced } from "./updateObject.js";
4
+ export function findObj(config, obj) {
5
+ if (matchObj(config, obj))
6
+ return updateFindObject(obj, config.findOpts || {});
7
+ return null;
8
+ }
9
+ export function matchObj(config, obj) {
10
+ const { search, context } = config;
11
+ if (search === undefined || search === null)
12
+ return true;
13
+ if (typeof search === "function" && search(obj, context))
14
+ return true;
15
+ if (typeof search === "object" && !Array.isArray(search) && hasFieldsAdvanced(obj, search))
16
+ return true;
17
+ return false;
18
+ }
19
+ export function updateObj(config, obj) {
20
+ const { updater, context } = config;
21
+ if (typeof updater === "object" && !Array.isArray(updater))
22
+ return updateObjectAdvanced(obj, updater);
23
+ if (typeof updater === "function") {
24
+ const updateObjValue = updater(obj, context);
25
+ if (updateObjValue)
26
+ return updateObjValue;
27
+ return obj;
28
+ }
29
+ return obj;
30
+ }
@@ -5,12 +5,10 @@ import { _deepMerge } from "./merge.js";
5
5
  * @param field - An object containing new values to update in the target object.
6
6
  */
7
7
  export function updateObjectAdvanced(obj, field) {
8
- if (typeof field !== "object" || field === null) {
9
- throw new Error("Fields must be an object or object array");
10
- }
11
- if (Object.keys(field).length === 0) {
8
+ if (typeof field !== "object" || Array.isArray(field) || field === null)
9
+ throw new Error("Fields must be an object");
10
+ if (Object.keys(field).length === 0)
12
11
  return obj;
13
- }
14
12
  const newObj = structuredClone(obj);
15
13
  const $fields = {};
16
14
  const subsetFields = {};
@@ -106,11 +104,9 @@ function mainUpdate(obj, fields) {
106
104
  * @param newVal - An object containing new values to update in the target object.
107
105
  */
108
106
  function updateObject(obj, newVal) {
109
- for (let key in newVal) {
110
- if (newVal.hasOwnProperty(key)) {
107
+ for (let key in newVal)
108
+ if (newVal.hasOwnProperty(key))
111
109
  obj[key] = newVal[key];
112
- }
113
- }
114
110
  return obj;
115
111
  }
116
112
  function _for(fields, obj, opts) {
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const version = "0.11.0-alpha.1";
1
+ export const version = "0.11.0-alpha.3";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/db-core",
3
- "version": "0.11.0-alpha.1",
3
+ "version": "0.11.0-alpha.3",
4
4
  "types": "dist/index.d.ts",
5
5
  "main": "dist/index.js",
6
6
  "author": "wxn0brP",