@yorozu/db-sqlite 0.5.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/LICENSE +21 -0
- package/README.md +37 -0
- package/driver.d.ts +8 -0
- package/handle.d.ts +14 -0
- package/index.d.ts +3 -0
- package/index.js +2365 -0
- package/package.json +26 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ivan
|
|
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,37 @@
|
|
|
1
|
+
# @yorozu/db-sqlite
|
|
2
|
+
|
|
3
|
+
better-sqlite3 thin wrapper + driver for `@yorozu/db`. Node-only.
|
|
4
|
+
|
|
5
|
+
## Setup
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { createSqliteDriver, wrapBetterSqlite3 } from "@yorozu/db-sqlite"
|
|
9
|
+
import type { Logger } from "@yorozu/log"
|
|
10
|
+
|
|
11
|
+
let driver = createSqliteDriver({
|
|
12
|
+
filename: "app.sqlite",
|
|
13
|
+
// native, // inject better-sqlite3 ctor (tests). Default: better-sqlite3
|
|
14
|
+
log, // optional Logger; silent default
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
let db = await driver.open(schema)
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Logger is optional. Internally: `makeLog(opts.log ?? makeSilentLog(), "yorozu-db-sqlite")`.
|
|
21
|
+
|
|
22
|
+
`wrapBetterSqlite3(db)` is the thin handle: `exec`, `prepare` → `{ run, all, get }` with array bind `stmt.run(...params)`, `transaction(fn) => db.transaction(fn)()`, `close`.
|
|
23
|
+
|
|
24
|
+
## Musts
|
|
25
|
+
|
|
26
|
+
- `new Native(filename)` then `PRAGMA journal_mode = WAL` (ignore failure on `:memory:`) then wrap, then apply schema.
|
|
27
|
+
- One table per collection: `pk TEXT PRIMARY KEY`, `payload TEXT` (JSON minus blob fields), plus `"<index>__<i>"` columns (TEXT/REAL). Sidecar `"<collection>__blobs"` `(pk, field, data BLOB)`.
|
|
28
|
+
- `get` rehydrates `Blob` when `Blob` exists, otherwise `Uint8Array`.
|
|
29
|
+
- Reserved index `"__pk"`: primary-key walk, even if not in `CollectionDef.indexes`.
|
|
30
|
+
- `scan(..., { keysOnly: true })` is `SELECT pk, index columns` only. Never `payload`, never join `__blobs`. Omit `ScanHit.value`.
|
|
31
|
+
- Prefix TTL: `scan("by-evict", { lt: [cutoff], keysOnly: true })` matches `[storedAt, bytes]` with `storedAt < cutoff` (`[cutoff] < [cutoff, 0]`).
|
|
32
|
+
- Default `put` flush is `"now"`. `"batch"` buffers until `db.flush()` or the next `"rw"` transact commit, not `"r"`. `flush()` coalesces by `(collection, pk)`. Sync put batches use better-sqlite3 `db.transaction`.
|
|
33
|
+
- Async `transact`: mutex + `BEGIN IMMEDIATE` / `COMMIT` / `ROLLBACK`. Not `db.transaction(fn)` (that API is sync-only). Nested `transact` throws via a facade. Concurrent `transact` serializes. Prefer the callback's `db.collection()`. A collection obtained before `transact` is reentrant while the SQL tx is open and joins that tx (rolls back with it). Concurrent ops from another task still queue on the mutex.
|
|
34
|
+
- Scan `limit` is applied after `inRange` + `compareIndexKey` sort (no SQL `LIMIT`). String / mixed index keys are not filtered by SQL `WHERE` (SQLite TEXT is UTF-8; `IndexKey` strings compare as UTF-16). Select covering columns, then `inRange` / `compareIndexKey`.
|
|
35
|
+
- Call `await db.flush()` before `close()`. Do not leave `{ flush: "batch" }` puts outstanding if another process may take the file.
|
|
36
|
+
- Collection / index names must match `/^[A-Za-z0-9_-]+$/` and are quoted as `"name"`. Illegal names throw.
|
|
37
|
+
- `drop(schema)` closes tracked connections and `fs.unlink`s the file unless `filename === ":memory:"`.
|
package/driver.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { default as Database } from 'better-sqlite3';
|
|
2
|
+
import { DbDriver } from '@yorozu/db';
|
|
3
|
+
import { Logger } from '@yorozu/log';
|
|
4
|
+
export declare function createSqliteDriver(opts: {
|
|
5
|
+
filename: string;
|
|
6
|
+
native?: typeof Database;
|
|
7
|
+
log?: Logger;
|
|
8
|
+
}): DbDriver;
|
package/handle.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { default as Database } from 'better-sqlite3';
|
|
2
|
+
export type SqliteStatement = {
|
|
3
|
+
run(params?: readonly unknown[]): void;
|
|
4
|
+
all<T = Record<string, unknown>>(params?: readonly unknown[]): T[];
|
|
5
|
+
get<T = Record<string, unknown>>(params?: readonly unknown[]): T | undefined;
|
|
6
|
+
};
|
|
7
|
+
export type SqliteHandle = {
|
|
8
|
+
exec(sql: string): void;
|
|
9
|
+
prepare(sql: string): SqliteStatement;
|
|
10
|
+
transaction<T>(fn: () => T): T;
|
|
11
|
+
close(): void;
|
|
12
|
+
};
|
|
13
|
+
/** Wrap a better-sqlite3 Database. Array bind: `stmt.run(...params)`. */
|
|
14
|
+
export declare function wrapBetterSqlite3(db: Database.Database): SqliteHandle;
|
package/index.d.ts
ADDED