@wxn0brp/db-core 0.7.0 → 0.8.1

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<boolean>;
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 = { data: res };
20
- config.control ||= {};
21
- config.control.toggleOne = controlData;
22
21
  if (res)
23
- return false;
22
+ return {
23
+ data: res,
24
+ type: "removed"
25
+ };
24
26
  setDataForToggleOne(config);
25
- config.control.toggleOne.data = await this.add(config);
26
- return true;
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
@@ -63,6 +63,7 @@ export class ValtheraClass {
63
63
  */
64
64
  add(query) {
65
65
  query.control ||= {};
66
+ query.id_gen ??= true;
66
67
  return this.execute("add", query);
67
68
  }
68
69
  /**
@@ -124,7 +125,7 @@ export class ValtheraClass {
124
125
  query.context ||= {};
125
126
  query.add_arg ||= {};
126
127
  query.control ||= {};
127
- query.id_gen ||= true;
128
+ query.id_gen ??= true;
128
129
  return this.execute("updateOneOrAdd", query);
129
130
  }
130
131
  /**
@@ -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,12 +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.
47
49
  * Returns a promise resolving to `false` if the entry was found and removed,
48
50
  * or `true` if the entry was added. The returned value reflects the state of the database
49
51
  * after the operation.
50
52
  */
51
- toggleOne(search: Search<D>, data?: Arg<D>, context?: VContext): Promise<boolean>;
53
+ toggleOne(search: Search<D>, data?: Arg<D>, context?: VContext): Promise<ToggleOneResult<D>>;
52
54
  }
@@ -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<boolean>;
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,5 +1,4 @@
1
1
  import { Arg, Search, Updater } from "./arg.js";
2
- import { Data } from "./data.js";
3
2
  import { DbFindOpts, FindOpts } from "./options.js";
4
3
  import { VContext } from "./types.js";
5
4
  /**
@@ -12,12 +11,6 @@ import { VContext } from "./types.js";
12
11
  * }
13
12
  */
14
13
  export interface VQuery_Control {
15
- updateOneOrAdd?: {
16
- updated?: boolean;
17
- };
18
- toggleOne?: {
19
- data?: Data;
20
- };
21
14
  }
22
15
  export interface VQuery {
23
16
  collection?: string;
@@ -31,3 +24,19 @@ export interface VQuery {
31
24
  updater?: Updater;
32
25
  control?: VQuery_Control;
33
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(query: ToggleOneQuery): Promise<boolean>;
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.7.0";
1
+ export const version = "0.8.1";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/db-core",
3
- "version": "0.7.0",
3
+ "version": "0.8.1",
4
4
  "types": "dist/index.d.ts",
5
5
  "main": "dist/index.js",
6
6
  "author": "wxn0brP",