@wxn0brp/db-core 0.12.4-alpha.1 → 0.12.4-alpha.2

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.
@@ -25,6 +25,7 @@ export declare abstract class ActionsBase implements ActionsBaseInterface {
25
25
  beginTransaction(id: Id): Promise<TransactionHandle>;
26
26
  commitTransaction(handle: TransactionHandle): Promise<void>;
27
27
  rollbackTransaction(handle: TransactionHandle): Promise<void>;
28
+ createIndex(config: VQueryT.CreateIndex): Promise<void>;
28
29
  updateOneOrAdd(config: VQueryT.UpdateOneOrAdd): Promise<VQueryT.UpdateOneOrAddResult<DataInternal>>;
29
30
  toggleOne(config: VQueryT.ToggleOne): Promise<VQueryT.ToggleOneResult<DataInternal>>;
30
31
  }
@@ -19,6 +19,9 @@ export class ActionsBase {
19
19
  async rollbackTransaction(handle) {
20
20
  throw new Error("Transactions are not supported for ActionsBase");
21
21
  }
22
+ async createIndex(config) {
23
+ // No-op: adapter does not use indexes internally
24
+ }
22
25
  async updateOneOrAdd(config) {
23
26
  const res = await this.updateOne(config);
24
27
  if (res)
@@ -3,6 +3,7 @@ import { ActionsBase } from "../base/actions.js";
3
3
  import { Collection } from "../helpers/collection.js";
4
4
  import { ExecutorInterface } from "../helpers/executor.js";
5
5
  import { Data } from "../types/data.js";
6
+ import { IndexOpts } from "../types/idx.js";
6
7
  import { DbOpts } from "../types/options.js";
7
8
  import { ValtheraPlugin } from "../types/plugin.js";
8
9
  import { VQuery, VQueryT } from "../types/query.js";
@@ -86,6 +87,10 @@ export declare class ValtheraClass implements ValtheraCompatible {
86
87
  * Removes a database collection from the database.
87
88
  */
88
89
  removeCollection(collection: string): Promise<boolean>;
90
+ /**
91
+ * Create an index on a collection.
92
+ */
93
+ createIndex(collection: string, fields: string[], opts?: IndexOpts): Promise<void>;
89
94
  /**
90
95
  * @experimental
91
96
  *
@@ -195,6 +195,20 @@ export class ValtheraClass {
195
195
  this._collections.delete(collection);
196
196
  return this.execute("removeCollection", collection);
197
197
  }
198
+ /**
199
+ * Create an index on a collection.
200
+ */
201
+ async createIndex(collection, fields, opts) {
202
+ const index = {
203
+ collection,
204
+ fields,
205
+ opts,
206
+ };
207
+ await this.execute("createIndex", {
208
+ collection,
209
+ index,
210
+ });
211
+ }
198
212
  /**
199
213
  * @experimental
200
214
  *
@@ -11,7 +11,11 @@ export async function addId(query, actions, defaultGen = true) {
11
11
  data[idKey] = genId();
12
12
  return;
13
13
  }
14
- const txOpts = query.transaction ? { transaction: query.transaction } : {};
14
+ const txOpts = query.transaction
15
+ ? {
16
+ transaction: query.transaction,
17
+ }
18
+ : {};
15
19
  const find = (await actions.findOne({
16
20
  collection: "__vdb_id",
17
21
  search: {
@@ -1,6 +1,7 @@
1
1
  import { Arg, Search, Updater } from "../types/arg.js";
2
2
  import { UpdateOneOrAdd } from "../types/collection.js";
3
3
  import { Data } from "../types/data.js";
4
+ import { IndexOpts } from "../types/idx.js";
4
5
  import { DbFindOpts, FindOpts } from "../types/options.js";
5
6
  import { VQueryT } from "../types/query.js";
6
7
  import { VContext } from "../types/types.js";
@@ -48,4 +49,8 @@ export declare class Collection<D = Data> {
48
49
  * 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
50
  */
50
51
  toggleOne(search: Search<D>, data?: Arg<D>, context?: VContext): Promise<VQueryT.ToggleOneResult<D>>;
52
+ /**
53
+ * Create an index on this collection.
54
+ */
55
+ createIndex(fields: string[], opts?: IndexOpts): Promise<void>;
51
56
  }
@@ -101,4 +101,10 @@ export class Collection {
101
101
  context,
102
102
  });
103
103
  }
104
+ /**
105
+ * Create an index on this collection.
106
+ */
107
+ createIndex(fields, opts) {
108
+ return this.db.createIndex(this.collection, fields, opts);
109
+ }
104
110
  }
@@ -25,4 +25,5 @@ export interface ActionsBaseInterface {
25
25
  beginTransaction(id: Id): Promise<TransactionHandle>;
26
26
  commitTransaction(handle: TransactionHandle): Promise<void>;
27
27
  rollbackTransaction(handle: TransactionHandle): Promise<void>;
28
+ createIndex(config: VQueryT.CreateIndex): Promise<void>;
28
29
  }
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Index options - adapter-dependent, should not be relied upon
3
+ */
4
+ export interface IndexOpts {
5
+ unique?: boolean;
6
+ name?: string;
7
+ }
8
+ export interface IndexDefinition {
9
+ collection: string;
10
+ fields: string[];
11
+ opts?: IndexOpts;
12
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -1,5 +1,6 @@
1
1
  import { Arg, Search, Updater } from "./arg.js";
2
2
  import { Data } from "./data.js";
3
+ import { IndexDefinition } from "./idx.js";
3
4
  import { DbFindOpts, FindOpts } from "./options.js";
4
5
  import { TransactionHandle } from "./transaction.js";
5
6
  import { VContext } from "./types.js";
@@ -27,6 +28,7 @@ export interface VQuery<T = Data, AllowFn extends boolean = true> {
27
28
  updater?: Updater<T, AllowFn>;
28
29
  control?: VQuery_Control;
29
30
  transaction?: TransactionHandle;
31
+ index?: IndexDefinition;
30
32
  }
31
33
  export declare namespace VQueryT {
32
34
  type QueryBase<T = Data> = {
@@ -78,4 +80,7 @@ export declare namespace VQueryT {
78
80
  data: T;
79
81
  type: "added" | "removed";
80
82
  }
83
+ type CreateIndex<T = Data> = QueryBase<T> & {
84
+ index: IndexDefinition;
85
+ };
81
86
  }
@@ -1,5 +1,6 @@
1
1
  import { Collection } from "../helpers/collection.js";
2
2
  import { Data } from "./data.js";
3
+ import { IndexOpts } from "./idx.js";
3
4
  import { VQueryT } from "./query.js";
4
5
  export interface ValtheraCompatible {
5
6
  c<T = Data>(collection: string): Collection<T>;
@@ -16,4 +17,5 @@ export interface ValtheraCompatible {
16
17
  removeCollection(collection: string): Promise<boolean>;
17
18
  updateOneOrAdd<T = Data>(query: VQueryT.UpdateOneOrAdd<T>): Promise<VQueryT.UpdateOneOrAddResult<T>>;
18
19
  toggleOne<T = Data>(query: VQueryT.ToggleOne<T>): Promise<VQueryT.ToggleOneResult<T>>;
20
+ createIndex(collection: string, fields: string[], opts?: IndexOpts): Promise<void>;
19
21
  }
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const version = "0.12.4-alpha.1";
1
+ export declare const version = "0.12.4-alpha.2";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const version = "0.12.4-alpha.1";
1
+ export const version = "0.12.4-alpha.2";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/db-core",
3
- "version": "0.12.4-alpha.1",
3
+ "version": "0.12.4-alpha.2",
4
4
  "types": "dist/index.d.ts",
5
5
  "main": "dist/index.js",
6
6
  "author": "wxn0brP",
@@ -13,7 +13,7 @@
13
13
  },
14
14
  "homepage": "https://github.com/wxn0brP/ValtheraDB",
15
15
  "dependencies": {
16
- "@wxn0brp/event-emitter": "0.0.6"
16
+ "@wxn0brp/event-emitter": "0.0.7"
17
17
  },
18
18
  "devDependencies": {
19
19
  "@types/bun": "*",