@wxn0brp/db-core 0.2.9 → 0.2.11

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,4 @@
1
- import { setDataUsingUpdateOneOrAdd } from "../helpers/updateOneOrAdd.js";
1
+ import { setDataForToggleOne, setDataForUpdateOneOrAdd } from "../helpers/assignDataPush.js";
2
2
  class ActionsBase {
3
3
  _inited = true;
4
4
  numberId = false;
@@ -49,17 +49,19 @@ class ActionsBase {
49
49
  }
50
50
  async updateOneOrAdd(config) {
51
51
  const res = await this.updateOne(config);
52
- if (!res) {
53
- setDataUsingUpdateOneOrAdd(config);
54
- await this.add(config);
55
- }
56
- return res;
52
+ if (res)
53
+ return true;
54
+ setDataForUpdateOneOrAdd(config);
55
+ await this.add(config);
56
+ return false;
57
57
  }
58
58
  async toggleOne(config) {
59
59
  const res = await this.removeOne(config);
60
- if (!res)
61
- await this.add(config);
62
- return res;
60
+ if (res)
61
+ return true;
62
+ setDataForToggleOne(config);
63
+ await this.add(config);
64
+ return false;
63
65
  }
64
66
  }
65
67
  export default ActionsBase;
@@ -77,7 +77,7 @@ declare class ValtheraClass implements ValtheraCompatible {
77
77
  /**
78
78
  * Asynchronously removes one entry in a database or adds a new one if it doesn't exist. Usage e.g. for toggling a flag.
79
79
  */
80
- toggleOne<T = Data>(collection: string, search: Search<T>, data: Arg<T>, context?: VContext): Promise<boolean>;
80
+ toggleOne<T = Data>(collection: string, search: Search<T>, data?: Arg<T>, context?: VContext): Promise<boolean>;
81
81
  /**
82
82
  * Removes a database collection from the file system.
83
83
  */
@@ -110,7 +110,7 @@ class ValtheraClass {
110
110
  /**
111
111
  * Asynchronously removes one entry in a database or adds a new one if it doesn't exist. Usage e.g. for toggling a flag.
112
112
  */
113
- async toggleOne(collection, search, data, context = {}) {
113
+ async toggleOne(collection, search, data = {}, context = {}) {
114
114
  return await this.execute("toggleOne", { collection, search, data, context, id_gen: false });
115
115
  }
116
116
  /**
@@ -0,0 +1,4 @@
1
+ import { VQuery } from "../types/query.js";
2
+ export declare function assignDataPush(data: any): {};
3
+ export declare function setDataForUpdateOneOrAdd(query: VQuery): void;
4
+ export declare function setDataForToggleOne(query: VQuery): void;
@@ -0,0 +1,26 @@
1
+ export function assignDataPush(data) {
2
+ if (!data)
3
+ return {};
4
+ if (typeof data !== "object" || Array.isArray(data))
5
+ return {};
6
+ if (Object.keys(data).length === 0)
7
+ return {};
8
+ const obj = {};
9
+ for (const key of Object.keys(data)) {
10
+ if (!key.startsWith("$")) {
11
+ obj[key] = data[key];
12
+ continue;
13
+ }
14
+ const dk = data[key];
15
+ if (Array.isArray(dk))
16
+ continue;
17
+ Object.keys(dk).forEach((k) => obj[k] = dk[k]);
18
+ }
19
+ return obj;
20
+ }
21
+ export function setDataForUpdateOneOrAdd(query) {
22
+ query.data = Object.assign({}, assignDataPush(query.search), assignDataPush(query.updater), assignDataPush(query.add_arg));
23
+ }
24
+ export function setDataForToggleOne(query) {
25
+ query.data = Object.assign({}, assignDataPush(query.search), assignDataPush(query.data));
26
+ }
@@ -18,7 +18,7 @@ export interface ValtheraCompatible {
18
18
  removeOne<T = Data>(collection: string, search: Search<T>, context?: VContext): Promise<boolean>;
19
19
  removeCollection(collection: string): Promise<boolean>;
20
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>;
21
+ toggleOne<T = Data>(collection: string, search: Search<T>, data?: Arg<T>, context?: VContext): Promise<boolean>;
22
22
  }
23
23
  export interface UpdateOneOrAdd<T> {
24
24
  add_arg?: Arg<T>;
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const version = "0.2.9";
1
+ export const version = "0.2.11";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/db-core",
3
- "version": "0.2.9",
3
+ "version": "0.2.11",
4
4
  "types": "dist/index.d.ts",
5
5
  "main": "dist/index.js",
6
6
  "author": "wxn0brP",
@@ -1,2 +0,0 @@
1
- import { VQuery } from "../types/query.js";
2
- export declare function setDataUsingUpdateOneOrAdd(query: VQuery): void;
@@ -1,18 +0,0 @@
1
- function assignDataPush(data) {
2
- if (typeof data !== "object" || Array.isArray(data))
3
- return;
4
- const obj = {};
5
- for (const key of Object.keys(data)) {
6
- if (key.startsWith("$")) {
7
- Object.keys(data[key]).forEach((k) => {
8
- obj[k] = data[key][k];
9
- });
10
- }
11
- else
12
- obj[key] = data[key];
13
- }
14
- return obj;
15
- }
16
- export function setDataUsingUpdateOneOrAdd(query) {
17
- query.data = Object.assign({}, assignDataPush(query.search), assignDataPush(query.updater), assignDataPush(query.add_arg));
18
- }