@wxn0brp/db-core 0.6.1 → 0.8.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.
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2025 wxn0brP
3
+ Copyright (c) 2026 wxn0brP
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -1,6 +1,6 @@
1
1
  import { ActionsBaseInterface } from "../types/action.js";
2
2
  import { DataInternal } from "../types/data.js";
3
- import { VQuery } from "../types/query.js";
3
+ import { ToggleOneResult, UpdateOneOrAddResult, VQuery } from "../types/query.js";
4
4
  export declare abstract class ActionsBase implements ActionsBaseInterface {
5
5
  _inited: boolean;
6
6
  numberId: boolean;
@@ -16,6 +16,6 @@ export declare abstract class ActionsBase implements ActionsBaseInterface {
16
16
  abstract remove(config: VQuery): Promise<DataInternal[] | null>;
17
17
  abstract removeOne(config: VQuery): Promise<DataInternal | null>;
18
18
  abstract removeCollection(config: VQuery): Promise<boolean>;
19
- updateOneOrAdd(config: VQuery): Promise<DataInternal>;
20
- toggleOne(config: VQuery): Promise<DataInternal | null>;
19
+ updateOneOrAdd(config: VQuery): Promise<UpdateOneOrAddResult<DataInternal>>;
20
+ toggleOne(config: VQuery): Promise<ToggleOneResult<DataInternal>>;
21
21
  }
@@ -5,24 +5,28 @@ export class ActionsBase {
5
5
  async init(...args) { }
6
6
  async updateOneOrAdd(config) {
7
7
  const res = await this.updateOne(config);
8
- const controlData = { updated: true };
9
- config.control ||= {};
10
- config.control.updateOneOrAdd = controlData;
11
8
  if (res)
12
- return res;
9
+ return {
10
+ data: res,
11
+ type: "updated"
12
+ };
13
13
  setDataForUpdateOneOrAdd(config);
14
- config.control.updateOneOrAdd.updated = false;
15
- return await this.add(config);
14
+ return {
15
+ data: await this.add(config),
16
+ type: "added"
17
+ };
16
18
  }
17
19
  async toggleOne(config) {
18
20
  const res = await this.removeOne(config);
19
- const controlData = { created: false };
20
- config.control ||= {};
21
- config.control.toggleOne = controlData;
22
21
  if (res)
23
- return res;
22
+ return {
23
+ data: res,
24
+ type: "removed"
25
+ };
24
26
  setDataForToggleOne(config);
25
- config.control.toggleOne.created = true;
26
- return await this.add(config);
27
+ return {
28
+ data: await this.add(config),
29
+ type: "added"
30
+ };
27
31
  }
28
32
  }
@@ -4,8 +4,8 @@ import { Collection } from "../helpers/collection.js";
4
4
  import { Executor } from "../helpers/executor.js";
5
5
  import { Data } from "../types/data.js";
6
6
  import { DbOpts } from "../types/options.js";
7
- import { VQuery } from "../types/query.js";
8
- import { AddQuery, FindOneQuery, FindQuery, RemoveQuery, ToggleOneQuery, UpdateOneOrAddQuery, UpdateQuery, ValtheraCompatible } from "../types/valthera.js";
7
+ import { AddQuery, FindOneQuery, FindQuery, RemoveQuery, ToggleOneQuery, UpdateOneOrAddQuery, UpdateQuery, VQuery } from "../types/query.js";
8
+ import { ValtheraCompatible } from "../types/valthera.js";
9
9
  /**
10
10
  * Represents a database management class for performing CRUD operations.
11
11
  * @class
@@ -72,6 +72,9 @@ export declare class ValtheraClass implements ValtheraCompatible {
72
72
  updateOneOrAdd<T = Data>(query: UpdateOneOrAddQuery): Promise<T>;
73
73
  /**
74
74
  * Asynchronously removes one entry in a database or adds a new one if it doesn't exist. Usage e.g. for toggling a flag.
75
+ * Returns a promise resolving to `false` if the entry was found and removed,
76
+ * or `true` if the entry was added. The returned value reflects the state of the database
77
+ * after the operation.
75
78
  */
76
79
  toggleOne<T = Data>(query: ToggleOneQuery): Promise<T>;
77
80
  /**
@@ -129,6 +129,9 @@ export class ValtheraClass {
129
129
  }
130
130
  /**
131
131
  * Asynchronously removes one entry in a database or adds a new one if it doesn't exist. Usage e.g. for toggling a flag.
132
+ * Returns a promise resolving to `false` if the entry was found and removed,
133
+ * or `true` if the entry was added. The returned value reflects the state of the database
134
+ * after the operation.
132
135
  */
133
136
  toggleOne(query) {
134
137
  query.data ||= {};
@@ -1,8 +1,10 @@
1
1
  import { Arg, Search, Updater } from "../types/arg.js";
2
+ import { UpdateOneOrAdd } from "../types/collection.js";
2
3
  import { Data } from "../types/data.js";
3
4
  import { DbFindOpts, FindOpts } from "../types/options.js";
5
+ import { ToggleOneResult, UpdateOneOrAddResult } from "../types/query.js";
4
6
  import { VContext } from "../types/types.js";
5
- import { UpdateOneOrAdd, ValtheraCompatible } from "../types/valthera.js";
7
+ import { ValtheraCompatible } from "../types/valthera.js";
6
8
  export declare class Collection<D = Data> {
7
9
  db: ValtheraCompatible;
8
10
  collection: string;
@@ -41,9 +43,12 @@ export declare class Collection<D = Data> {
41
43
  /**
42
44
  * Asynchronously updates one entry in a database or adds a new one if it doesn't exist.
43
45
  */
44
- updateOneOrAdd(search: Search<D>, updater: Updater<D>, { add_arg, context, id_gen }?: UpdateOneOrAdd<D>): Promise<D>;
46
+ updateOneOrAdd(search: Search<D>, updater: Updater<D>, { add_arg, context, id_gen }?: UpdateOneOrAdd<D>): Promise<UpdateOneOrAddResult<D>>;
45
47
  /**
46
48
  * Asynchronously removes one entry in a database or adds a new one if it doesn't exist. Usage e.g. for toggling a flag.
49
+ * Returns a promise resolving to `false` if the entry was found and removed,
50
+ * or `true` if the entry was added. The returned value reflects the state of the database
51
+ * after the operation.
47
52
  */
48
- toggleOne(search: Search<D>, data?: Arg<D>, context?: VContext): Promise<D>;
53
+ toggleOne(search: Search<D>, data?: Arg<D>, context?: VContext): Promise<ToggleOneResult<D>>;
49
54
  }
@@ -52,6 +52,9 @@ export class Collection {
52
52
  }
53
53
  /**
54
54
  * Asynchronously removes one entry in a database or adds a new one if it doesn't exist. Usage e.g. for toggling a flag.
55
+ * Returns a promise resolving to `false` if the entry was found and removed,
56
+ * or `true` if the entry was added. The returned value reflects the state of the database
57
+ * after the operation.
55
58
  */
56
59
  toggleOne(search, data = {}, context = {}) {
57
60
  return this.db.toggleOne({ collection: this.collection, search, data, context });
@@ -1,5 +1,5 @@
1
1
  import { DataInternal } from "./data.js";
2
- import { VQuery } from "./query.js";
2
+ import { ToggleOneResult, UpdateOneOrAddResult, VQuery } from "./query.js";
3
3
  export interface ActionsBaseInterface {
4
4
  _inited: boolean;
5
5
  numberId: boolean;
@@ -15,6 +15,6 @@ export interface ActionsBaseInterface {
15
15
  remove(config: VQuery): Promise<DataInternal[] | null>;
16
16
  removeOne(config: VQuery): Promise<DataInternal | null>;
17
17
  removeCollection(config: VQuery): Promise<boolean>;
18
- updateOneOrAdd(config: VQuery): Promise<DataInternal>;
19
- toggleOne(config: VQuery): Promise<DataInternal | null>;
18
+ updateOneOrAdd(config: VQuery): Promise<UpdateOneOrAddResult<DataInternal>>;
19
+ toggleOne(config: VQuery): Promise<ToggleOneResult<DataInternal>>;
20
20
  }
@@ -0,0 +1,7 @@
1
+ import { Arg } from "./arg.js";
2
+ import { VContext } from "./types.js";
3
+ export interface UpdateOneOrAdd<T> {
4
+ add_arg?: Arg<T>;
5
+ id_gen?: boolean;
6
+ context?: VContext;
7
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -1,6 +1,6 @@
1
- import { VContext } from "./types.js";
2
1
  import { Arg, Search, Updater } from "./arg.js";
3
2
  import { DbFindOpts, FindOpts } from "./options.js";
3
+ import { VContext } from "./types.js";
4
4
  /**
5
5
  * To extend via adapters
6
6
  * @example
@@ -11,12 +11,6 @@ import { DbFindOpts, FindOpts } from "./options.js";
11
11
  * }
12
12
  */
13
13
  export interface VQuery_Control {
14
- updateOneOrAdd?: {
15
- updated?: boolean;
16
- };
17
- toggleOne?: {
18
- created?: boolean;
19
- };
20
14
  }
21
15
  export interface VQuery {
22
16
  collection?: string;
@@ -30,3 +24,19 @@ export interface VQuery {
30
24
  updater?: Updater;
31
25
  control?: VQuery_Control;
32
26
  }
27
+ export type QueryBase = Required<Pick<VQuery, "collection" | "search">> & Pick<VQuery, "control">;
28
+ export type AddQuery = Required<Pick<VQuery, "data" | "collection">> & Pick<VQuery, "id_gen" | "control">;
29
+ export type FindQuery = Omit<QueryBase, "search"> & Pick<VQuery, "search"> & Pick<VQuery, "findOpts" | "dbFindOpts" | "context">;
30
+ export type FindOneQuery = QueryBase & Pick<VQuery, "findOpts" | "context">;
31
+ export type UpdateQuery = QueryBase & Required<Pick<VQuery, "updater">> & Pick<VQuery, "context">;
32
+ export type RemoveQuery = QueryBase & Pick<VQuery, "context">;
33
+ export type UpdateOneOrAddQuery = QueryBase & UpdateQuery & Pick<VQuery, "add_arg" | "id_gen">;
34
+ export type ToggleOneQuery = QueryBase & Pick<VQuery, "data" | "context">;
35
+ export interface UpdateOneOrAddResult<T> {
36
+ data: T;
37
+ type: "added" | "updated";
38
+ }
39
+ export interface ToggleOneResult<T> {
40
+ data: T;
41
+ type: "added" | "removed";
42
+ }
@@ -1 +1,3 @@
1
+ ;
2
+ ;
1
3
  export {};
@@ -1,16 +1,6 @@
1
1
  import { Collection } from "../helpers/collection.js";
2
- import { Arg } from "./arg.js";
3
2
  import { Data } from "./data.js";
4
- import { VQuery } from "./query.js";
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 = Omit<QueryBase, "search"> & Pick<VQuery, "search"> & 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">;
3
+ import { AddQuery, FindOneQuery, FindQuery, RemoveQuery, ToggleOneQuery, ToggleOneResult, UpdateOneOrAddQuery, UpdateOneOrAddResult, UpdateQuery } from "./query.js";
14
4
  export interface ValtheraCompatible {
15
5
  c(collection: string): Collection;
16
6
  getCollections(): Promise<string[]>;
@@ -24,11 +14,6 @@ export interface ValtheraCompatible {
24
14
  remove<T = Data>(query: RemoveQuery): Promise<T[] | null>;
25
15
  removeOne<T = Data>(query: RemoveQuery): Promise<T | null>;
26
16
  removeCollection(collection: string): Promise<boolean>;
27
- updateOneOrAdd<T = Data>(query: UpdateOneOrAddQuery): Promise<T>;
28
- toggleOne<T = Data>(query: ToggleOneQuery): Promise<T | null>;
29
- }
30
- export interface UpdateOneOrAdd<T> {
31
- add_arg?: Arg<T>;
32
- id_gen?: boolean;
33
- context?: VContext;
17
+ updateOneOrAdd<T = Data>(query: UpdateOneOrAddQuery): Promise<UpdateOneOrAddResult<T>>;
18
+ toggleOne<T = Data>(query: ToggleOneQuery): Promise<ToggleOneResult<T>>;
34
19
  }
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const version = "0.6.1";
1
+ export const version = "0.8.0";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/db-core",
3
- "version": "0.6.1",
3
+ "version": "0.8.0",
4
4
  "types": "dist/index.d.ts",
5
5
  "main": "dist/index.js",
6
6
  "author": "wxn0brP",