@wxn0brp/db-storage-sqlite 0.0.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 +21 -0
- package/README.md +73 -0
- package/dist/find.d.ts +4 -0
- package/dist/find.js +39 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.js +80 -0
- package/dist/remove.d.ts +4 -0
- package/dist/remove.js +25 -0
- package/dist/types.d.ts +7 -0
- package/dist/types.js +1 -0
- package/dist/update.d.ts +4 -0
- package/dist/update.js +33 -0
- package/package.json +54 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 wxn0brP
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# @wxn0brp/db-storage-sqlite
|
|
2
|
+
|
|
3
|
+
A **pure SQLite storage adapter** for the ValtheraDB database library.
|
|
4
|
+
This package allows you to use SQLite as a backend while leveraging the **full power of ValtheraDB** (CRUD operations, relations, queries, `_id` generation, sorting, pagination, etc.).
|
|
5
|
+
|
|
6
|
+
> ⚠️ **Note:** Unlike the original ValtheraDB, SQLite requires tables to exist before inserting data. This adapter provides `ensureCollection` to check for table existence, but it **does not create empty tables automatically**.
|
|
7
|
+
|
|
8
|
+
## Features
|
|
9
|
+
|
|
10
|
+
* **Full CRUD** – Create, Read, Update, Delete operations fully supported.
|
|
11
|
+
* **Collection-based** – Each collection corresponds to a SQLite table.
|
|
12
|
+
* **Flexible Search** – Supports function-based or object-based searches.
|
|
13
|
+
* **Automatic ID Generation** – `_id` is generated automatically if missing.
|
|
14
|
+
* **Sorting & Pagination** – Built-in support for sorting, limiting, and offsetting results.
|
|
15
|
+
* **TypeScript Support** – Fully typed for safety and autocompletion.
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
yarn add github:wxn0brP/ValtheraDB-storage-sqlite#dist
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
import { SQLiteValthera } from "@wxn0brp/db-storage-sqlite";
|
|
27
|
+
import { ValtheraClass } from "@wxn0brp/db-core";
|
|
28
|
+
import Database from "better-sqlite3";
|
|
29
|
+
|
|
30
|
+
// 1. Initialize SQLite database connection
|
|
31
|
+
const sqliteDB = new Database("path/to/database.sqlite");
|
|
32
|
+
|
|
33
|
+
// 2. Create the SQLiteValthera instance
|
|
34
|
+
const sqliteDbAction = new SQLiteValthera(sqliteDB);
|
|
35
|
+
|
|
36
|
+
// 3. Ensure the collection/table exists
|
|
37
|
+
await sqliteDbAction.ensureCollection("users");
|
|
38
|
+
|
|
39
|
+
// 4. Create ValtheraClass instance using the SQLite adapter
|
|
40
|
+
const db = new ValtheraClass({
|
|
41
|
+
dbAction: sqliteDbAction
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// 5. Add a new entry (_id generated automatically, _id is type `TEXT`)
|
|
45
|
+
await db.add("users", { name: "John Doe", email: "john@example.com" });
|
|
46
|
+
|
|
47
|
+
// 6. Find entries
|
|
48
|
+
const users = await db.find("users", { name: "John Doe" });
|
|
49
|
+
|
|
50
|
+
// 7. Update entries
|
|
51
|
+
await db.update("users", { name: "John Doe" }, { email: "newemail@example.com" });
|
|
52
|
+
|
|
53
|
+
// 8. Remove entries
|
|
54
|
+
await db.remove("users", { name: "John Doe" });
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## API Notes
|
|
58
|
+
|
|
59
|
+
* **`ensureCollection(collection: string)`**
|
|
60
|
+
Checks if the collection (table) exists.
|
|
61
|
+
Throws an error if the table does not exist.
|
|
62
|
+
|
|
63
|
+
> This is necessary because SQLite cannot create a table without defining at least one column.
|
|
64
|
+
|
|
65
|
+
* 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.
|
|
66
|
+
|
|
67
|
+
## License
|
|
68
|
+
|
|
69
|
+
MIT
|
|
70
|
+
|
|
71
|
+
## Contributing
|
|
72
|
+
|
|
73
|
+
Contributions and bug requests are welcome!
|
package/dist/find.d.ts
ADDED
package/dist/find.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { compareSafe } from "@wxn0brp/db-core/utils/compare";
|
|
2
|
+
import hasFieldsAdvanced from "@wxn0brp/db-core/utils/hasFieldsAdvanced";
|
|
3
|
+
import updateFindObject from "@wxn0brp/db-core/utils/updateFindObject";
|
|
4
|
+
export async function find(slv, config) {
|
|
5
|
+
const { collection, search, findOpts, dbFindOpts, context } = config;
|
|
6
|
+
let sqlResult = [];
|
|
7
|
+
if (typeof search === "function" || Object.keys(search).length === 0) {
|
|
8
|
+
const stmt = await slv._prepare(`SELECT * FROM ${collection}`);
|
|
9
|
+
sqlResult = await Promise.resolve(stmt.all());
|
|
10
|
+
}
|
|
11
|
+
else {
|
|
12
|
+
const baseKeys = Object.keys(search)
|
|
13
|
+
.filter(k => search[k] !== undefined)
|
|
14
|
+
.filter(k => !k.startsWith("$"))
|
|
15
|
+
.filter(k => typeof search[k] !== "object");
|
|
16
|
+
const baseSql = `SELECT * FROM ${collection} WHERE ${baseKeys.map(k => `${k} = ?`).join(" AND ")}`;
|
|
17
|
+
const baseValues = baseKeys.map(k => search[k]);
|
|
18
|
+
const stmt = await slv._prepare(baseSql);
|
|
19
|
+
sqlResult = await Promise.resolve(stmt.all(...baseValues));
|
|
20
|
+
}
|
|
21
|
+
let result = sqlResult.filter(entry => typeof search === "function" ? search(entry, context) : hasFieldsAdvanced(entry, search));
|
|
22
|
+
const { reverse = false, limit = -1, offset = 0, sortBy, sortAsc = true } = dbFindOpts;
|
|
23
|
+
if (reverse)
|
|
24
|
+
result.reverse();
|
|
25
|
+
if (sortBy) {
|
|
26
|
+
const dir = sortAsc ? 1 : -1;
|
|
27
|
+
result.sort((a, b) => compareSafe(a[sortBy], b[sortBy]) * dir);
|
|
28
|
+
const start = offset;
|
|
29
|
+
const end = limit !== -1 ? offset + limit : undefined;
|
|
30
|
+
result = result.slice(start, end);
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
if (offset > 0)
|
|
34
|
+
result.splice(0, offset);
|
|
35
|
+
if (limit > 0)
|
|
36
|
+
result.splice(limit);
|
|
37
|
+
}
|
|
38
|
+
return result.length ? result.map(res => updateFindObject(res, findOpts)) : [];
|
|
39
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import ActionsBase from "@wxn0brp/db-core/base/actions";
|
|
2
|
+
import Data from "@wxn0brp/db-core/types/data";
|
|
3
|
+
import { VQuery } from "@wxn0brp/db-core/types/query";
|
|
4
|
+
import { Statement } from "bun:sqlite";
|
|
5
|
+
import { SupportedDB } from "./types.js";
|
|
6
|
+
export declare class SQLiteValthera extends ActionsBase {
|
|
7
|
+
db: SupportedDB;
|
|
8
|
+
_inited: boolean;
|
|
9
|
+
constructor(db: SupportedDB);
|
|
10
|
+
_prepare(sql: string): Promise<Statement>;
|
|
11
|
+
add(config: VQuery): Promise<Data>;
|
|
12
|
+
find(config: VQuery): Promise<Data[]>;
|
|
13
|
+
findOne(config: VQuery): Promise<Data | null>;
|
|
14
|
+
update(config: VQuery): Promise<boolean>;
|
|
15
|
+
updateOne(config: VQuery): Promise<boolean>;
|
|
16
|
+
remove(config: VQuery): Promise<boolean>;
|
|
17
|
+
removeOne(config: VQuery): Promise<boolean>;
|
|
18
|
+
removeCollection(config: VQuery): Promise<boolean>;
|
|
19
|
+
issetCollection(config: VQuery): Promise<boolean>;
|
|
20
|
+
ensureCollection(config: VQuery): Promise<boolean>;
|
|
21
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { genId } from "@wxn0brp/db-core";
|
|
2
|
+
import ActionsBase from "@wxn0brp/db-core/base/actions";
|
|
3
|
+
import { find } from "./find.js";
|
|
4
|
+
import { remove } from "./remove.js";
|
|
5
|
+
import { update } from "./update.js";
|
|
6
|
+
export class SQLiteValthera extends ActionsBase {
|
|
7
|
+
db;
|
|
8
|
+
_inited = true;
|
|
9
|
+
constructor(db) {
|
|
10
|
+
super();
|
|
11
|
+
this.db = db;
|
|
12
|
+
}
|
|
13
|
+
async _prepare(sql) {
|
|
14
|
+
const db = this.db;
|
|
15
|
+
if (typeof db.prepare !== "undefined")
|
|
16
|
+
return await db.prepare(sql);
|
|
17
|
+
if (typeof db.prepareSync !== "undefined")
|
|
18
|
+
return await db.prepareSync(sql);
|
|
19
|
+
if (typeof db.query === "function") {
|
|
20
|
+
const q = await db.query(sql);
|
|
21
|
+
if (q && (q.all || q.get || q.run))
|
|
22
|
+
return q;
|
|
23
|
+
}
|
|
24
|
+
throw new Error("Unsupported database");
|
|
25
|
+
}
|
|
26
|
+
async add(config) {
|
|
27
|
+
const { data, id_gen = true, collection } = config;
|
|
28
|
+
if (id_gen && !data._id)
|
|
29
|
+
data._id = genId();
|
|
30
|
+
const keys = Object.keys(data);
|
|
31
|
+
const placeholders = keys.map(() => "?").join(", ");
|
|
32
|
+
const values = keys.map(k => data[k]);
|
|
33
|
+
const sql = `INSERT INTO ${collection} (${keys.join(", ")}) VALUES (${placeholders})`;
|
|
34
|
+
const stmt = await this._prepare(sql);
|
|
35
|
+
await Promise.resolve(stmt.run(...values));
|
|
36
|
+
return data;
|
|
37
|
+
}
|
|
38
|
+
find(config) {
|
|
39
|
+
return find(this, config);
|
|
40
|
+
}
|
|
41
|
+
async findOne(config) {
|
|
42
|
+
config.dbFindOpts = { limit: 1 };
|
|
43
|
+
const result = await this.find(config);
|
|
44
|
+
return result.length ? result[0] : null;
|
|
45
|
+
}
|
|
46
|
+
update(config) {
|
|
47
|
+
return update(this, config.collection, false, config.search, config.updater, config.context);
|
|
48
|
+
}
|
|
49
|
+
updateOne(config) {
|
|
50
|
+
return update(this, config.collection, true, config.search, config.updater, config.context);
|
|
51
|
+
}
|
|
52
|
+
remove(config) {
|
|
53
|
+
return remove(this, config.collection, false, config.search, config.context);
|
|
54
|
+
}
|
|
55
|
+
removeOne(config) {
|
|
56
|
+
return remove(this, config.collection, true, config.search, config.context);
|
|
57
|
+
}
|
|
58
|
+
async removeCollection(config) {
|
|
59
|
+
const { collection } = config;
|
|
60
|
+
const sql = `DROP TABLE IF EXISTS ${collection}`;
|
|
61
|
+
const stmt = await this._prepare(sql);
|
|
62
|
+
await Promise.resolve(stmt.run());
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
async issetCollection(config) {
|
|
66
|
+
const { collection } = config;
|
|
67
|
+
const sql = `SELECT name FROM sqlite_master WHERE type='table' AND name=?`;
|
|
68
|
+
const stmt = await this._prepare(sql);
|
|
69
|
+
const result = await Promise.resolve(stmt.all(collection));
|
|
70
|
+
return result.length > 0;
|
|
71
|
+
}
|
|
72
|
+
async ensureCollection(config) {
|
|
73
|
+
const { collection } = config;
|
|
74
|
+
const issetCollection = await this.issetCollection({ collection });
|
|
75
|
+
if (!issetCollection) {
|
|
76
|
+
throw new Error(`Collection "${collection}" not found. Please create it first.`);
|
|
77
|
+
}
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
}
|
package/dist/remove.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { Search } from "@wxn0brp/db-core/types/arg";
|
|
2
|
+
import { VContext } from "@wxn0brp/db-core/types/types";
|
|
3
|
+
import { SQLiteValthera } from "./index.js";
|
|
4
|
+
export declare function remove(slv: SQLiteValthera, collection: string, one: boolean, search: Search, context?: VContext): Promise<boolean>;
|
package/dist/remove.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import hasFieldsAdvanced from "@wxn0brp/db-core/utils/hasFieldsAdvanced";
|
|
2
|
+
export async function remove(slv, collection, one, search, context = {}) {
|
|
3
|
+
let stmt = await slv._prepare(`SELECT * FROM "${collection}"`);
|
|
4
|
+
const allEntries = await Promise.resolve(stmt.all());
|
|
5
|
+
const toDelete = [];
|
|
6
|
+
let removed = false;
|
|
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
|
+
removed = true;
|
|
14
|
+
if (one)
|
|
15
|
+
break;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
if (!removed)
|
|
19
|
+
return false;
|
|
20
|
+
stmt = await slv._prepare(`DELETE FROM "${collection}" WHERE _id = ?`);
|
|
21
|
+
for (const entry of toDelete) {
|
|
22
|
+
await Promise.resolve(stmt.run(entry._id));
|
|
23
|
+
}
|
|
24
|
+
return true;
|
|
25
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Database as BetterSqliteDB } from "better-sqlite3";
|
|
2
|
+
import type { Database as BunSqliteDB } from "bun:sqlite";
|
|
3
|
+
import type { DatabaseSync as NodeSqliteDB } from "node:sqlite";
|
|
4
|
+
import type BetterSqlite3 from "better-sqlite3";
|
|
5
|
+
import type NodeSqlite from "node:sqlite";
|
|
6
|
+
export type SupportedDB = BetterSqliteDB | NodeSqliteDB | BunSqliteDB;
|
|
7
|
+
export type Statement = BetterSqlite3.Statement | NodeSqlite.StatementSync;
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/update.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { Search, Updater } from "@wxn0brp/db-core/types/arg";
|
|
2
|
+
import { VContext } from "@wxn0brp/db-core/types/types";
|
|
3
|
+
import { SQLiteValthera } from "./index.js";
|
|
4
|
+
export declare function update(slv: SQLiteValthera, collection: string, one: boolean, search: Search, updater: Updater, context?: VContext): Promise<boolean>;
|
package/dist/update.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import hasFieldsAdvanced from "@wxn0brp/db-core/utils/hasFieldsAdvanced";
|
|
2
|
+
export async function update(slv, collection, one, search, updater, context = {}) {
|
|
3
|
+
const stmt = await slv._prepare(`SELECT * FROM "${collection}"`);
|
|
4
|
+
const allEntries = await Promise.resolve(stmt.all());
|
|
5
|
+
const matched = [];
|
|
6
|
+
for (const entry of allEntries) {
|
|
7
|
+
const match = typeof search === "function"
|
|
8
|
+
? search(entry, context)
|
|
9
|
+
: hasFieldsAdvanced(entry, search);
|
|
10
|
+
if (match) {
|
|
11
|
+
matched.push(entry);
|
|
12
|
+
if (one)
|
|
13
|
+
break;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
if (matched.length === 0)
|
|
17
|
+
return false;
|
|
18
|
+
const updateOne = async (target) => {
|
|
19
|
+
const newData = typeof updater === "function"
|
|
20
|
+
? updater(target, context)
|
|
21
|
+
: { ...target, ...updater };
|
|
22
|
+
if (newData._id !== target._id)
|
|
23
|
+
newData._id = target._id;
|
|
24
|
+
const keys = Object.keys(newData).filter(k => k !== "_id");
|
|
25
|
+
const values = keys.map(k => newData[k]);
|
|
26
|
+
const sql = `UPDATE "${collection}" SET ${keys.map(k => `"${k}" = ?`).join(", ")} WHERE _id = ?`;
|
|
27
|
+
const stmt = await slv._prepare(sql);
|
|
28
|
+
await Promise.resolve(stmt.run(...values, target._id));
|
|
29
|
+
};
|
|
30
|
+
for (const target of matched)
|
|
31
|
+
await updateOne(target);
|
|
32
|
+
return true;
|
|
33
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@wxn0brp/db-storage-sqlite",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"types": "dist/index.d.ts",
|
|
6
|
+
"description": "",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/wxn0brP/ValtheraDB-storage-sqlite.git"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/wxn0brP/ValtheraDB-storage-sqlite",
|
|
12
|
+
"author": "wxn0brP",
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"type": "module",
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "tsc && tsc-alias"
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@types/better-sqlite3": "^7.6.13",
|
|
20
|
+
"@types/bun": "*",
|
|
21
|
+
"@types/node": "*",
|
|
22
|
+
"@wxn0brp/db-core": "^0.3.0",
|
|
23
|
+
"better-sqlite3": "^12.5.0",
|
|
24
|
+
"tsc-alias": "*",
|
|
25
|
+
"typescript": "*"
|
|
26
|
+
},
|
|
27
|
+
"peerDependencies": {
|
|
28
|
+
"@wxn0brp/db-core": ">=0.3.0",
|
|
29
|
+
"better-sqlite3": "^12.5.0"
|
|
30
|
+
},
|
|
31
|
+
"peerDependenciesMeta": {
|
|
32
|
+
"@wxn0brp/db-core": {
|
|
33
|
+
"optional": false
|
|
34
|
+
},
|
|
35
|
+
"better-sqlite3": {
|
|
36
|
+
"optional": true
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"files": [
|
|
40
|
+
"dist"
|
|
41
|
+
],
|
|
42
|
+
"exports": {
|
|
43
|
+
".": {
|
|
44
|
+
"types": "./dist/index.d.ts",
|
|
45
|
+
"import": "./dist/index.js",
|
|
46
|
+
"default": "./dist/index.js"
|
|
47
|
+
},
|
|
48
|
+
"./*": {
|
|
49
|
+
"types": "./dist/*.d.ts",
|
|
50
|
+
"import": "./dist/*.js",
|
|
51
|
+
"default": "./dist/*.js"
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|