@wxn0brp/db-core 0.2.10 → 0.2.12

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 { setDataUsingToggleOne, 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,19 +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
- setDataUsingToggleOne(config);
62
- await this.add(config);
63
- }
64
- return res;
60
+ if (res)
61
+ return true;
62
+ setDataForToggleOne(config);
63
+ await this.add(config);
64
+ return false;
65
65
  }
66
66
  }
67
67
  export default ActionsBase;
@@ -39,5 +39,9 @@ declare class CollectionManager<D = Data> {
39
39
  * Asynchronously updates one entry in a database or adds a new one if it doesn't exist.
40
40
  */
41
41
  updateOneOrAdd<T = Data>(search: Search<T & Data>, updater: Updater<T & Data>, { add_arg, context, id_gen }: UpdateOneOrAdd<T & Data>): 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 & Data>, data?: Arg<T & Data>, context?: VContext): Promise<boolean>;
42
46
  }
43
47
  export default CollectionManager;
@@ -53,5 +53,11 @@ class CollectionManager {
53
53
  async updateOneOrAdd(search, updater, { add_arg = {}, context = {}, id_gen = true }) {
54
54
  return await this.db.updateOneOrAdd(this.collection, search, updater, { add_arg, context, id_gen });
55
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
+ }
56
62
  }
57
63
  export default CollectionManager;
@@ -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;
@@ -1,21 +1,26 @@
1
1
  export function assignDataPush(data) {
2
+ if (!data)
3
+ return {};
2
4
  if (typeof data !== "object" || Array.isArray(data))
3
- return;
5
+ return {};
6
+ if (Object.keys(data).length === 0)
7
+ return {};
4
8
  const obj = {};
5
9
  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
10
+ if (!key.startsWith("$")) {
12
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]);
13
18
  }
14
19
  return obj;
15
20
  }
16
- export function setDataUsingUpdateOneOrAdd(query) {
21
+ export function setDataForUpdateOneOrAdd(query) {
17
22
  query.data = Object.assign({}, assignDataPush(query.search), assignDataPush(query.updater), assignDataPush(query.add_arg));
18
23
  }
19
- export function setDataUsingToggleOne(query) {
24
+ export function setDataForToggleOne(query) {
20
25
  query.data = Object.assign({}, assignDataPush(query.search), assignDataPush(query.data));
21
26
  }
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const version = "0.2.10";
1
+ export const version = "0.2.12";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/db-core",
3
- "version": "0.2.10",
3
+ "version": "0.2.12",
4
4
  "types": "dist/index.d.ts",
5
5
  "main": "dist/index.js",
6
6
  "author": "wxn0brP",
@@ -1,4 +0,0 @@
1
- import { VQuery } from "../types/query.js";
2
- export declare function assignDataPush(data: any): {};
3
- export declare function setDataUsingUpdateOneOrAdd(query: VQuery): void;
4
- export declare function setDataUsingToggleOne(query: VQuery): void;