@wxn0brp/db-storage-sqlite 0.101.0 → 0.110.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/README.md CHANGED
@@ -69,6 +69,8 @@ await db.users.remove({ name: "John Doe" });
69
69
 
70
70
  > This is necessary because SQLite cannot create a table without defining at least one column.
71
71
 
72
+ - **Boolean values** are automatically mapped to `0` (false) / `1` (true) when stored in or queried against SQLite, since SQLite has no native boolean type.
73
+
72
74
  - All other methods (`add`, `find`, `update`, `remove`) are **fully compatible with ValtheraDB**, preserving all features like `_id` generation, complex filters (`hasFieldsAdvanced`), sorting, pagination, and function-based search.
73
75
 
74
76
  ## License
package/dist/find.js CHANGED
@@ -1,7 +1,8 @@
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
+ import { toSqlValue } from "./index.js";
3
4
  export async function find(slv, config) {
4
- const { collection, search, context } = config;
5
+ const { collection, search } = config;
5
6
  let sqlResult = [];
6
7
  if (typeof search === "function" || Object.keys(search).length === 0) {
7
8
  const stmt = await slv._prepare(`SELECT * FROM ${collection}`);
@@ -13,14 +14,10 @@ export async function find(slv, config) {
13
14
  .filter(k => !k.startsWith("$"))
14
15
  .filter(k => typeof search[k] !== "object");
15
16
  const baseSql = `SELECT * FROM ${collection} WHERE ${baseKeys.map(k => `${k} = ?`).join(" AND ")}`;
16
- const baseValues = baseKeys.map(k => search[k]);
17
+ const baseValues = baseKeys.map(k => toSqlValue(search[k]));
17
18
  const stmt = await slv._prepare(baseSql);
18
19
  sqlResult = await Promise.resolve(stmt.all(...baseValues));
19
20
  }
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]);
21
+ const result = sqlResult.filter(entry => findObj(entry, search));
22
+ return findUtil(config, result, []);
26
23
  }
package/dist/index.d.ts CHANGED
@@ -3,6 +3,7 @@ import { ActionsBase } from "@wxn0brp/db-core/base/actions";
3
3
  import { Data } from "@wxn0brp/db-core/types/data";
4
4
  import { VQueryT } from "@wxn0brp/db-core/types/query";
5
5
  import { SupportedDB, VStatement } from "./types.js";
6
+ export declare function toSqlValue(v: any): any;
6
7
  export declare class SQLiteValthera extends ActionsBase {
7
8
  db: SupportedDB;
8
9
  primaryKey: Record<string, string>;
package/dist/index.js CHANGED
@@ -1,8 +1,14 @@
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";
7
+ export function toSqlValue(v) {
8
+ if (typeof v === "boolean")
9
+ return v ? 1 : 0;
10
+ return v;
11
+ }
6
12
  export class SQLiteValthera extends ActionsBase {
7
13
  db;
8
14
  primaryKey;
@@ -30,12 +36,11 @@ export class SQLiteValthera extends ActionsBase {
30
36
  return tables.map((t) => t.name);
31
37
  }
32
38
  async add(config) {
33
- const { data, id_gen = true, collection } = config;
34
- if (id_gen && !data._id)
35
- data._id = genId();
39
+ const { data, collection } = config;
40
+ await addId(config, this, true);
36
41
  const keys = Object.keys(data);
37
42
  const placeholders = keys.map(() => "?").join(", ");
38
- const values = keys.map(k => data[k]);
43
+ const values = keys.map(k => toSqlValue(data[k]));
39
44
  const sql = `INSERT INTO ${collection} (${keys.join(", ")}) VALUES (${placeholders})`;
40
45
  const stmt = await this._prepare(sql);
41
46
  await Promise.resolve(stmt.run(...values));
@@ -94,16 +99,22 @@ export const DYNAMIC = {
94
99
  return DYNAMIC.bun(file, keys, opts);
95
100
  return DYNAMIC.node(file, keys, opts);
96
101
  },
97
- async bun(file, keys = {}, opts) {
102
+ async bun(file, keys = {}, opts = undefined) {
98
103
  const { Database } = await import("bun:sqlite");
104
+ if (typeof opts === "object" && Object.keys(opts).length === 0)
105
+ opts = undefined;
99
106
  return new SQLiteValthera(new Database(file, opts), keys);
100
107
  },
101
108
  async node(file, keys = {}, opts) {
102
109
  const { DatabaseSync } = await import("node:sqlite");
110
+ if (!opts)
111
+ opts = {};
103
112
  return new SQLiteValthera(new DatabaseSync(file, opts), keys);
104
113
  },
105
114
  async better(file, keys = {}, opts) {
106
115
  const { default: def } = await import("better-sqlite3");
116
+ if (!opts)
117
+ opts = {};
107
118
  return new SQLiteValthera(new def(file, opts), keys);
108
119
  }
109
120
  };
package/dist/update.js CHANGED
@@ -1,7 +1,8 @@
1
- import { updateObjectAdvanced } from "@wxn0brp/db-core/utils/updateObject";
1
+ import { updateObj } from "@wxn0brp/db-core/utils/process";
2
+ import { toSqlValue } from "./index.js";
2
3
  import { find } from "./find.js";
3
4
  export async function update(slv, query, one) {
4
- const { collection, updater, context } = query;
5
+ const { collection } = query;
5
6
  const matched = await find(slv, {
6
7
  ...query,
7
8
  dbFindOpts: {
@@ -12,13 +13,11 @@ export async function update(slv, query, one) {
12
13
  return [];
13
14
  const key = slv.primaryKey[collection] || "_id";
14
15
  const updateOne = async (target) => {
15
- const newData = typeof updater === "function"
16
- ? updater(target, context)
17
- : updateObjectAdvanced(target, updater);
16
+ const newData = updateObj(query, target);
18
17
  if (newData[key] !== target[key])
19
18
  newData[key] = target[key];
20
19
  const keys = Object.keys(newData).filter(k => k !== key);
21
- const values = keys.map(k => newData[k]);
20
+ const values = keys.map(k => toSqlValue(newData[k]));
22
21
  const sql = `UPDATE "${collection}" SET ${keys.map(k => `"${k}" = ?`).join(", ")} WHERE "${key}" = ?`;
23
22
  const stmt = await slv._prepare(sql);
24
23
  await Promise.resolve(stmt.run(...values, target[key]));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/db-storage-sqlite",
3
- "version": "0.101.0",
3
+ "version": "0.110.1",
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": {