@wxn0brp/db-core 0.12.10 → 0.12.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.
@@ -23,7 +23,7 @@ export declare abstract class ActionsBase implements ActionsBaseInterface {
23
23
  abstract updateOne(config: VQueryT.Update): Promise<DataInternal | null>;
24
24
  abstract remove(config: VQueryT.Remove): Promise<DataInternal[]>;
25
25
  abstract removeOne(config: VQueryT.Remove): Promise<DataInternal | null>;
26
- beginTransaction(id: Id): Promise<TransactionHandle>;
26
+ beginTransaction(id: Id, collections: string[]): Promise<TransactionHandle>;
27
27
  commitTransaction(handle: TransactionHandle): Promise<void>;
28
28
  rollbackTransaction(handle: TransactionHandle): Promise<void>;
29
29
  createIndex(config: VQueryT.CreateIndex): Promise<void>;
@@ -16,7 +16,7 @@ export class ActionsBase {
16
16
  version = "ActionsBase-core-" + version;
17
17
  async init(...args) { }
18
18
  async close(...args) { }
19
- async beginTransaction(id) {
19
+ async beginTransaction(id, collections) {
20
20
  throw new UnsupportedOperationError("beginTransaction");
21
21
  }
22
22
  async commitTransaction(handle) {
@@ -23,10 +23,10 @@ export declare class Transaction {
23
23
  updateOneOrAdd<T = Data>(q: VQueryT.UpdateOneOrAdd<T>): Promise<VQueryT.UpdateOneOrAddResult<T>>;
24
24
  toggleOne<T = Data>(q: VQueryT.ToggleOne<T>): Promise<VQueryT.ToggleOneResult<T>>;
25
25
  count<T = Data>(q: VQueryT.Count<T>): Promise<number>;
26
- ensureCollection(collection: string): Promise<boolean>;
27
- issetCollection(collection: string): Promise<boolean>;
26
+ ensureCollection(collection: string | VQueryT.Collection): Promise<boolean>;
27
+ issetCollection(collection: string | VQueryT.Collection): Promise<boolean>;
28
28
  getCollections(): Promise<string[]>;
29
- removeCollection(collection: string): Promise<boolean>;
29
+ removeCollection(target: string | VQueryT.Collection): Promise<boolean>;
30
30
  commit(): Promise<void>;
31
31
  rollback(): Promise<void>;
32
32
  }
@@ -1,5 +1,5 @@
1
1
  import { wrapAdapterError } from "../errors.js";
2
- import { Collection } from "../helpers/collection.js";
2
+ import { Collection, resolveCollection } from "../helpers/collection.js";
3
3
  import { applyAddDefaults, applyBulkAddDefaults, applyCountDefaults, applyFindDefaults, applyFindOneDefaults, applyRemoveDefaults, applyToggleOneDefaults, applyUpdateDefaults, applyUpdateOneOrAddDefaults, } from "../helpers/queryDefaults.js";
4
4
  /**
5
5
  * Transaction context. Operations are executed directly through the parent
@@ -60,16 +60,16 @@ export class Transaction {
60
60
  return this._parent.execute("count", q, this.handle);
61
61
  }
62
62
  ensureCollection(collection) {
63
- return this._parent.execute("ensureCollection", collection, this.handle);
63
+ return this._parent.execute("ensureCollection", resolveCollection(collection), this.handle);
64
64
  }
65
65
  issetCollection(collection) {
66
- return this._parent.execute("issetCollection", collection, this.handle);
66
+ return this._parent.execute("issetCollection", resolveCollection(collection), this.handle);
67
67
  }
68
68
  getCollections() {
69
69
  return this._parent.execute("getCollections", {}, this.handle);
70
70
  }
71
- removeCollection(collection) {
72
- return this._parent.execute("removeCollection", collection, this.handle);
71
+ removeCollection(target) {
72
+ return this._parent.execute("removeCollection", resolveCollection(target), this.handle);
73
73
  }
74
74
  async commit() {
75
75
  try {
@@ -20,9 +20,9 @@ export declare class ValtheraClass implements ValtheraCompatible {
20
20
  adapter: ActionsBase;
21
21
  executor: ExecutorInterface;
22
22
  emitter: VEE<{
23
- [K in keyof Omit<ValtheraCompatible, "c">]: (query: VQuery, result: Awaited<ReturnType<ValtheraCompatible[K]>>) => void;
23
+ [K in keyof Omit<ValtheraCompatible, "c">]: (query: VQuery, result: Awaited<ReturnType<ValtheraCompatible[K]>>, collection?: string) => void;
24
24
  } & {
25
- "*": (name: keyof Omit<ValtheraCompatible, "c">, query: VQuery, result: any) => void;
25
+ "*": (name: keyof Omit<ValtheraCompatible, "c">, query: VQuery, result: any, collection?: string) => void;
26
26
  }>;
27
27
  version: string;
28
28
  _plugins: ValtheraPlugin[];
@@ -43,11 +43,11 @@ export declare class ValtheraClass implements ValtheraCompatible {
43
43
  /**
44
44
  * Check and create the specified collection if it doesn't exist.
45
45
  */
46
- ensureCollection(collection: string): Promise<boolean>;
46
+ ensureCollection(collection: string | VQueryT.Collection): Promise<boolean>;
47
47
  /**
48
48
  * Check if a collection exists.
49
49
  */
50
- issetCollection(collection: string): Promise<boolean>;
50
+ issetCollection(collection: string | VQueryT.Collection): Promise<boolean>;
51
51
  /**
52
52
  * Add data to a database.
53
53
  */
@@ -88,7 +88,7 @@ export declare class ValtheraClass implements ValtheraCompatible {
88
88
  /**
89
89
  * Removes a database collection from the database.
90
90
  */
91
- removeCollection(collection: string): Promise<boolean>;
91
+ removeCollection(target: string | VQueryT.Collection): Promise<boolean>;
92
92
  /**
93
93
  * Create an index on a collection.
94
94
  */
@@ -1,9 +1,11 @@
1
1
  import { VEE } from "@wxn0brp/event-emitter";
2
2
  import { AdapterCloseError, AdapterInitError, ErrorCodes, isValtheraError, QueryValidationError, TransactionError, wrapAdapterError, } from "../errors.js";
3
- import { Collection } from "../helpers/collection.js";
3
+ import { Collection, resolveCollection } from "../helpers/collection.js";
4
4
  import { SmartExecutor } from "../helpers/executor.js";
5
5
  import { genId } from "../helpers/gen.js";
6
+ import { rewriteOp } from "../helpers/plugin.js";
6
7
  import { applyAddDefaults, applyBulkAddDefaults, applyCountDefaults, applyFindDefaults, applyFindOneDefaults, applyRemoveDefaults, applyToggleOneDefaults, applyUpdateDefaults, applyUpdateOneOrAddDefaults, } from "../helpers/queryDefaults.js";
8
+ import { getOpKind } from "../utils/opKind.js";
7
9
  import { version } from "../version.js";
8
10
  import { Transaction } from "./transaction.js";
9
11
  /**
@@ -122,26 +124,37 @@ export class ValtheraClass {
122
124
  let idx = 0;
123
125
  const ctx = {
124
126
  op: name,
127
+ kind: getOpKind(name),
128
+ collection,
125
129
  query,
126
130
  next: async () => {
127
131
  if (idx < plugins.length)
128
132
  return plugins[idx++].execute(ctx);
133
+ if (txHandle && ctx.query && typeof ctx.query === "object")
134
+ ctx.query.transaction = txHandle;
129
135
  try {
130
136
  if (txHandle)
131
- return await self.adapter[ctx.op](query, txHandle);
137
+ return await self.adapter[ctx.op](ctx.query, txHandle);
132
138
  return await self.executor.addOp(self.adapter[ctx.op].bind(self.adapter), ctx.query, typeof ctx.query === "string" ? ctx.query : ctx.query.collection);
133
139
  }
134
140
  catch (err) {
135
141
  throw wrapAdapterError(err, {
136
142
  op: ctx.op,
137
- collection,
143
+ collection: typeof ctx.query === "string" ? ctx.query : ctx.query?.collection,
138
144
  adapter: self.adapter?.version,
139
145
  });
140
146
  }
141
147
  },
148
+ setOp(op, q) {
149
+ rewriteOp(ctx, op, q);
150
+ },
151
+ setQuery(q) {
152
+ rewriteOp(ctx, ctx.op, q);
153
+ },
142
154
  };
143
155
  const result = await ctx.next();
144
- this.emitter.emit(ctx.op, ctx.query, result);
156
+ const emittedCollection = typeof ctx.query === "string" ? ctx.query : ctx.query?.collection;
157
+ this.emitter.emit(ctx.op, ctx.query, result, emittedCollection);
145
158
  return result;
146
159
  }
147
160
  /**
@@ -164,13 +177,13 @@ export class ValtheraClass {
164
177
  * Check and create the specified collection if it doesn't exist.
165
178
  */
166
179
  async ensureCollection(collection) {
167
- return await this.execute("ensureCollection", collection);
180
+ return await this.execute("ensureCollection", resolveCollection(collection));
168
181
  }
169
182
  /**
170
183
  * Check if a collection exists.
171
184
  */
172
185
  async issetCollection(collection) {
173
- return await this.execute("issetCollection", collection);
186
+ return await this.execute("issetCollection", resolveCollection(collection));
174
187
  }
175
188
  /**
176
189
  * Add data to a database.
@@ -242,7 +255,8 @@ export class ValtheraClass {
242
255
  /**
243
256
  * Removes a database collection from the database.
244
257
  */
245
- removeCollection(collection) {
258
+ removeCollection(target) {
259
+ const collection = resolveCollection(target);
246
260
  this._collections.delete(collection);
247
261
  return this.execute("removeCollection", collection);
248
262
  }
@@ -316,7 +330,7 @@ export class ValtheraClass {
316
330
  try {
317
331
  let handle;
318
332
  try {
319
- handle = await this.adapter.beginTransaction(genId());
333
+ handle = await this.adapter.beginTransaction(genId(), collections);
320
334
  }
321
335
  catch (err) {
322
336
  throw wrapAdapterError(err, {
@@ -6,6 +6,7 @@ import { DbFindOpts, FindOpts } from "../types/options.js";
6
6
  import { VQueryT } from "../types/query.js";
7
7
  import { VContext } from "../types/types.js";
8
8
  import { ValtheraCompatible } from "../types/valthera.js";
9
+ export declare function resolveCollection(target: string | VQueryT.Collection): string;
9
10
  export declare class Collection<D = Data> {
10
11
  db: ValtheraCompatible;
11
12
  collection: string;
@@ -1,3 +1,6 @@
1
+ export function resolveCollection(target) {
2
+ return typeof target === "string" ? target : target.collection;
3
+ }
1
4
  export class Collection {
2
5
  db;
3
6
  collection;
@@ -0,0 +1,2 @@
1
+ import { PluginContext } from "../types/plugin.js";
2
+ export declare function rewriteOp(ctx: PluginContext, op: string, query?: any): void;
@@ -0,0 +1,9 @@
1
+ import { getOpKind } from "../utils/opKind.js";
2
+ export function rewriteOp(ctx, op, query) {
3
+ ctx.op = op;
4
+ if (query !== undefined)
5
+ ctx.query = query;
6
+ ctx.kind = getOpKind(op);
7
+ const q = ctx.query;
8
+ ctx.collection = typeof q === "string" ? q : q?.collection;
9
+ }
@@ -30,7 +30,7 @@ export interface ActionsBaseInterface {
30
30
  toggleOne(query: VQueryT.ToggleOne): Promise<VQueryT.ToggleOneResult<DataInternal>>;
31
31
  count(query: VQueryT.Count): Promise<number>;
32
32
  bulkAdd(query: VQueryT.BulkAdd): Promise<DataInternal[]>;
33
- beginTransaction(id: Id): Promise<TransactionHandle>;
33
+ beginTransaction(id: Id, collections: string[]): Promise<TransactionHandle>;
34
34
  commitTransaction(handle: TransactionHandle): Promise<void>;
35
35
  rollbackTransaction(handle: TransactionHandle): Promise<void>;
36
36
  createIndex(config: VQueryT.CreateIndex): Promise<void>;
@@ -1,8 +1,13 @@
1
1
  import type { ValtheraClass } from "../db/valthera.js";
2
+ import type { PluginOpKind } from "../utils/opKind.js";
2
3
  export interface PluginContext {
3
4
  op: string;
5
+ kind: PluginOpKind;
6
+ collection?: string;
4
7
  query: any;
5
8
  next: () => Promise<any>;
9
+ setOp(op: string, query?: any): void;
10
+ setQuery(query: any): void;
6
11
  }
7
12
  export interface ValtheraPlugin {
8
13
  name: string;
@@ -94,4 +94,7 @@ export declare namespace VQueryT {
94
94
  type Count<T = Data> = QueryBase<T> & {
95
95
  search: Search<T, false>;
96
96
  };
97
+ interface Collection {
98
+ collection: string;
99
+ }
97
100
  }
@@ -6,8 +6,8 @@ import { VQueryT } from "./query.js";
6
6
  export interface ValtheraCompatible {
7
7
  c<T = Data>(collection: string): Collection<T>;
8
8
  getCollections(): Promise<string[]>;
9
- ensureCollection(collection: string): Promise<boolean>;
10
- issetCollection(collection: string): Promise<boolean>;
9
+ ensureCollection(collection: string | VQueryT.Collection): Promise<boolean>;
10
+ issetCollection(collection: string | VQueryT.Collection): Promise<boolean>;
11
11
  add<T = Data>(query: VQueryT.Add<T>): Promise<T>;
12
12
  find<T = Data>(query: VQueryT.Find<T>): Promise<T[]>;
13
13
  findOne<T = Data>(query: VQueryT.FindOne<T>): Promise<T | null>;
@@ -15,7 +15,7 @@ export interface ValtheraCompatible {
15
15
  updateOne<T = Data>(query: VQueryT.Update<T>): Promise<T | null>;
16
16
  remove<T = Data>(query: VQueryT.Remove<T>): Promise<T[]>;
17
17
  removeOne<T = Data>(query: VQueryT.Remove<T>): Promise<T | null>;
18
- removeCollection(collection: string): Promise<boolean>;
18
+ removeCollection(collection: string | VQueryT.Collection): Promise<boolean>;
19
19
  updateOneOrAdd<T = Data>(query: VQueryT.UpdateOneOrAdd<T>): Promise<VQueryT.UpdateOneOrAddResult<T>>;
20
20
  toggleOne<T = Data>(query: VQueryT.ToggleOne<T>): Promise<VQueryT.ToggleOneResult<T>>;
21
21
  createIndex(collection: string, fields: string[], opts?: IndexOpts): Promise<void>;
@@ -0,0 +1,8 @@
1
+ export type PluginOpKind = "read" | "write" | "delete" | "collection" | "index" | "meta" | "control";
2
+ export declare function getOpKind(op: string): PluginOpKind;
3
+ export declare function isMutation(kind: PluginOpKind): kind is "delete" | "write";
4
+ export declare function isCollectionOp(op: string): boolean;
5
+ export declare function isIndexOp(op: string): boolean;
6
+ export declare function isMetaOp(op: string): boolean;
7
+ export declare function isControlOp(op: string): boolean;
8
+ export declare function isNoQueryOp(op: string): boolean;
@@ -0,0 +1,45 @@
1
+ const OP_KINDS = {
2
+ find: "read",
3
+ findOne: "read",
4
+ count: "read",
5
+ add: "write",
6
+ bulkAdd: "write",
7
+ update: "write",
8
+ updateOne: "write",
9
+ updateOneOrAdd: "write",
10
+ toggleOne: "write",
11
+ remove: "delete",
12
+ removeOne: "delete",
13
+ ensureCollection: "collection",
14
+ issetCollection: "collection",
15
+ removeCollection: "collection",
16
+ createIndex: "index",
17
+ removeIndex: "index",
18
+ getCollections: "meta",
19
+ capabilities: "meta",
20
+ beginTransaction: "control",
21
+ commitTransaction: "control",
22
+ rollbackTransaction: "control",
23
+ };
24
+ export function getOpKind(op) {
25
+ return OP_KINDS[op] ?? "write";
26
+ }
27
+ export function isMutation(kind) {
28
+ return kind === "write" || kind === "delete";
29
+ }
30
+ export function isCollectionOp(op) {
31
+ return getOpKind(op) === "collection";
32
+ }
33
+ export function isIndexOp(op) {
34
+ return getOpKind(op) === "index";
35
+ }
36
+ export function isMetaOp(op) {
37
+ return getOpKind(op) === "meta";
38
+ }
39
+ export function isControlOp(op) {
40
+ return getOpKind(op) === "control";
41
+ }
42
+ export function isNoQueryOp(op) {
43
+ const kind = getOpKind(op);
44
+ return kind === "collection" || kind === "meta";
45
+ }
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const version = "0.12.10";
1
+ export declare const version = "0.12.12";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const version = "0.12.10";
1
+ export const version = "0.12.12";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/db-core",
3
- "version": "0.12.10",
3
+ "version": "0.12.12",
4
4
  "types": "dist/index.d.ts",
5
5
  "main": "dist/index.js",
6
6
  "author": "wxn0brP",