@wxn0brp/db-storage-sqlite 0.100.2 → 0.110.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/README.md CHANGED
@@ -41,7 +41,9 @@ const db = createSQLiteValthera<{
41
41
  name: string;
42
42
  email: string;
43
43
  }
44
- }>(sqliteDB);
44
+ }>(sqliteDB, {
45
+ users: "_id" // define the primary key (default: "_id")
46
+ });
45
47
 
46
48
  // 4. Ensure the collection/table exists
47
49
  await db.ensureCollection("users");
package/dist/find.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  import { Data } from "@wxn0brp/db-core/types/data";
2
- import { FindQuery } from "@wxn0brp/db-core/types/query";
2
+ import { VQueryT } from "@wxn0brp/db-core/types/query";
3
3
  import { SQLiteValthera } from "./index.js";
4
- export declare function find(slv: SQLiteValthera, config: FindQuery): Promise<Data[]>;
4
+ export declare function find(slv: SQLiteValthera, config: VQueryT.Find): Promise<Data[]>;
package/dist/find.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { findUtil } from "@wxn0brp/db-core/utils/action";
2
- import { hasFieldsAdvanced } from "@wxn0brp/db-core/utils/hasFieldsAdvanced";
2
+ import { findObj } from "@wxn0brp/db-core/utils/process";
3
3
  export async function find(slv, config) {
4
- const { collection, search, context } = config;
4
+ const { collection, search } = config;
5
5
  let sqlResult = [];
6
6
  if (typeof search === "function" || Object.keys(search).length === 0) {
7
7
  const stmt = await slv._prepare(`SELECT * FROM ${collection}`);
@@ -17,10 +17,6 @@ export async function find(slv, config) {
17
17
  const stmt = await slv._prepare(baseSql);
18
18
  sqlResult = await Promise.resolve(stmt.all(...baseValues));
19
19
  }
20
- let result = sqlResult.filter(entry => typeof search === "function" ? search(entry, context) : hasFieldsAdvanced(entry, search));
21
- return findUtil(config, {
22
- find() {
23
- return result;
24
- }
25
- }, [null]);
20
+ const result = sqlResult.filter(entry => findObj(entry, search));
21
+ return findUtil(config, result, []);
26
22
  }
package/dist/index.d.ts CHANGED
@@ -5,8 +5,9 @@ import { VQueryT } from "@wxn0brp/db-core/types/query";
5
5
  import { SupportedDB, VStatement } from "./types.js";
6
6
  export declare class SQLiteValthera extends ActionsBase {
7
7
  db: SupportedDB;
8
+ primaryKey: Record<string, string>;
8
9
  _inited: boolean;
9
- constructor(db: SupportedDB);
10
+ constructor(db: SupportedDB, primaryKey?: Record<string, string>);
10
11
  _prepare(sql: string): Promise<VStatement>;
11
12
  getCollections(): Promise<string[]>;
12
13
  add(config: VQueryT.Add): Promise<Data>;
@@ -22,8 +23,8 @@ export declare class SQLiteValthera extends ActionsBase {
22
23
  }
23
24
  export declare function createSQLiteValthera<T extends Record<string, Data> = {}>(sqlDB: SupportedDB): ValtheraClass & { [K in keyof T]: import("@wxn0brp/db-core/helpers/collection").Collection<T[K]>; };
24
25
  export declare const DYNAMIC: {
25
- sqlite(file: string, opts?: any): Promise<SQLiteValthera>;
26
- bun(file: string, opts?: any): Promise<SQLiteValthera>;
27
- node(file: string, opts?: any): Promise<SQLiteValthera>;
28
- better(file: string, opts?: any): Promise<SQLiteValthera>;
26
+ sqlite(file: string, keys?: Record<string, string>, opts?: any): Promise<SQLiteValthera>;
27
+ bun(file: string, keys?: Record<string, string>, opts?: any): Promise<SQLiteValthera>;
28
+ node(file: string, keys?: Record<string, string>, opts?: any): Promise<SQLiteValthera>;
29
+ better(file: string, keys?: Record<string, string>, opts?: any): Promise<SQLiteValthera>;
29
30
  };
package/dist/index.js CHANGED
@@ -1,14 +1,17 @@
1
- import { forgeTypedValthera, genId, ValtheraClass } from "@wxn0brp/db-core";
1
+ import { forgeTypedValthera, ValtheraClass } from "@wxn0brp/db-core";
2
2
  import { ActionsBase } from "@wxn0brp/db-core/base/actions";
3
+ import { addId } from "@wxn0brp/db-core/helpers/addId";
3
4
  import { find } from "./find.js";
4
5
  import { remove } from "./remove.js";
5
6
  import { update } from "./update.js";
6
7
  export class SQLiteValthera extends ActionsBase {
7
8
  db;
9
+ primaryKey;
8
10
  _inited = true;
9
- constructor(db) {
11
+ constructor(db, primaryKey = {}) {
10
12
  super();
11
13
  this.db = db;
14
+ this.primaryKey = primaryKey;
12
15
  }
13
16
  async _prepare(sql) {
14
17
  const db = this.db;
@@ -28,9 +31,8 @@ export class SQLiteValthera extends ActionsBase {
28
31
  return tables.map((t) => t.name);
29
32
  }
30
33
  async add(config) {
31
- const { data, id_gen = true, collection } = config;
32
- if (id_gen && !data._id)
33
- data._id = genId();
34
+ const { data, collection } = config;
35
+ await addId(config, this, true);
34
36
  const keys = Object.keys(data);
35
37
  const placeholders = keys.map(() => "?").join(", ");
36
38
  const values = keys.map(k => data[k]);
@@ -87,21 +89,27 @@ export function createSQLiteValthera(sqlDB) {
87
89
  return forgeTypedValthera(db);
88
90
  }
89
91
  export const DYNAMIC = {
90
- sqlite(file, opts) {
92
+ sqlite(file, keys = {}, opts) {
91
93
  if (typeof Bun !== "undefined")
92
- return DYNAMIC.bun(file, opts);
93
- return DYNAMIC.node(file, opts);
94
+ return DYNAMIC.bun(file, keys, opts);
95
+ return DYNAMIC.node(file, keys, opts);
94
96
  },
95
- async bun(file, opts) {
97
+ async bun(file, keys = {}, opts = undefined) {
96
98
  const { Database } = await import("bun:sqlite");
97
- return new SQLiteValthera(new Database(file, opts));
99
+ if (typeof opts === "object" && Object.keys(opts).length === 0)
100
+ opts = undefined;
101
+ return new SQLiteValthera(new Database(file, opts), keys);
98
102
  },
99
- async node(file, opts) {
103
+ async node(file, keys = {}, opts) {
100
104
  const { DatabaseSync } = await import("node:sqlite");
101
- return new SQLiteValthera(new DatabaseSync(file, opts));
105
+ if (!opts)
106
+ opts = {};
107
+ return new SQLiteValthera(new DatabaseSync(file, opts), keys);
102
108
  },
103
- async better(file, opts) {
109
+ async better(file, keys = {}, opts) {
104
110
  const { default: def } = await import("better-sqlite3");
105
- return new SQLiteValthera(new def(file, opts));
111
+ if (!opts)
112
+ opts = {};
113
+ return new SQLiteValthera(new def(file, opts), keys);
106
114
  }
107
115
  };
package/dist/remove.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  import { DataInternal } from "@wxn0brp/db-core/types/data";
2
- import { RemoveQuery } from "@wxn0brp/db-core/types/query";
2
+ import { VQueryT } from "@wxn0brp/db-core/types/query";
3
3
  import { SQLiteValthera } from "./index.js";
4
- export declare function remove(slv: SQLiteValthera, query: RemoveQuery, one: boolean): Promise<DataInternal[]>;
4
+ export declare function remove(slv: SQLiteValthera, query: VQueryT.Remove, one: boolean): Promise<DataInternal[]>;
package/dist/remove.js CHANGED
@@ -1,24 +1,16 @@
1
- import { hasFieldsAdvanced } from "@wxn0brp/db-core/utils/hasFieldsAdvanced";
1
+ import { find } from "./find.js";
2
2
  export async function remove(slv, query, one) {
3
- const { collection, search, context } = query;
4
- let stmt = await slv._prepare(`SELECT * FROM "${collection}"`);
5
- const allEntries = await Promise.resolve(stmt.all());
6
- const toDelete = [];
7
- for (const entry of allEntries) {
8
- const match = typeof search === "function"
9
- ? search(entry, context)
10
- : hasFieldsAdvanced(entry, search);
11
- if (match) {
12
- toDelete.push(entry);
13
- if (one)
14
- break;
3
+ const { collection } = query;
4
+ const toDelete = await find(slv, {
5
+ ...query,
6
+ dbFindOpts: {
7
+ limit: one ? 1 : undefined
15
8
  }
16
- }
9
+ });
17
10
  if (!toDelete.length)
18
11
  return [];
19
- stmt = await slv._prepare(`DELETE FROM "${collection}" WHERE _id = ?`);
20
- for (const entry of toDelete) {
21
- await Promise.resolve(stmt.run(entry._id));
22
- }
12
+ const key = slv.primaryKey[collection] || "_id";
13
+ const stmt = await slv._prepare(`DELETE FROM "${collection}" WHERE "${key}" IN (${toDelete.map(() => "?").join(", ")})`);
14
+ await Promise.resolve(stmt.run(...toDelete.map(d => d[key])));
23
15
  return toDelete;
24
16
  }
package/dist/update.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  import { DataInternal } from "@wxn0brp/db-core/types/data";
2
- import { UpdateQuery } from "@wxn0brp/db-core/types/query";
2
+ import { VQueryT } from "@wxn0brp/db-core/types/query";
3
3
  import { SQLiteValthera } from "./index.js";
4
- export declare function update(slv: SQLiteValthera, query: UpdateQuery, one: boolean): Promise<DataInternal[]>;
4
+ export declare function update(slv: SQLiteValthera, query: VQueryT.Update, one: boolean): Promise<DataInternal[]>;
package/dist/update.js CHANGED
@@ -1,33 +1,26 @@
1
- import { hasFieldsAdvanced } from "@wxn0brp/db-core/utils/hasFieldsAdvanced";
1
+ import { updateObj } from "@wxn0brp/db-core/utils/process";
2
+ import { find } from "./find.js";
2
3
  export async function update(slv, query, one) {
3
- const { collection, search, updater, context } = query;
4
- const stmt = await slv._prepare(`SELECT * FROM "${collection}"`);
5
- const allEntries = await Promise.resolve(stmt.all());
6
- const matched = [];
7
- for (const entry of allEntries) {
8
- const match = typeof search === "function"
9
- ? search(entry, context)
10
- : hasFieldsAdvanced(entry, search);
11
- if (match) {
12
- matched.push(entry);
13
- if (one)
14
- break;
4
+ const { collection } = query;
5
+ const matched = await find(slv, {
6
+ ...query,
7
+ dbFindOpts: {
8
+ limit: one ? 1 : undefined
15
9
  }
16
- }
10
+ });
17
11
  if (matched.length === 0)
18
12
  return [];
13
+ const key = slv.primaryKey[collection] || "_id";
19
14
  const updateOne = async (target) => {
20
- const newData = typeof updater === "function"
21
- ? updater(target, context)
22
- : { ...target, ...updater };
23
- if (newData._id !== target._id)
24
- newData._id = target._id;
25
- const keys = Object.keys(newData).filter(k => k !== "_id");
15
+ const newData = updateObj(query, target);
16
+ if (newData[key] !== target[key])
17
+ newData[key] = target[key];
18
+ const keys = Object.keys(newData).filter(k => k !== key);
26
19
  const values = keys.map(k => newData[k]);
27
- const sql = `UPDATE "${collection}" SET ${keys.map(k => `"${k}" = ?`).join(", ")} WHERE _id = ?`;
20
+ const sql = `UPDATE "${collection}" SET ${keys.map(k => `"${k}" = ?`).join(", ")} WHERE "${key}" = ?`;
28
21
  const stmt = await slv._prepare(sql);
29
- await Promise.resolve(stmt.run(...values, target._id));
30
- return await slv.findOne({ collection, search: { _id: target._id } });
22
+ await Promise.resolve(stmt.run(...values, target[key]));
23
+ return newData;
31
24
  };
32
25
  const results = [];
33
26
  for (const target of matched)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/db-storage-sqlite",
3
- "version": "0.100.2",
3
+ "version": "0.110.0",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "description": "A pure SQLite storage adapter for the ValtheraDB database library",
@@ -22,13 +22,13 @@
22
22
  "@types/better-sqlite3": "^7.6.13",
23
23
  "@types/bun": "*",
24
24
  "@types/node": "*",
25
- "@wxn0brp/db-core": "^0.10.4",
25
+ "@wxn0brp/db-core": "^0.11.0-alpha.4",
26
26
  "better-sqlite3": "^12.9.0",
27
27
  "tsc-alias": "^1",
28
28
  "typescript": "^6"
29
29
  },
30
30
  "peerDependencies": {
31
- "@wxn0brp/db-core": "^0.10.4",
31
+ "@wxn0brp/db-core": "^0.11.0-alpha.4",
32
32
  "better-sqlite3": "^12.9.0"
33
33
  },
34
34
  "peerDependenciesMeta": {