@hulla/db-sqlite 0.3.1 → 0.3.3
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 +2 -0
- package/dist/browser.d.ts +1 -2
- package/dist/browser.js +2 -8
- package/dist/descriptor.d.ts +3 -1
- package/dist/descriptor.js +34 -2
- package/dist/engine.d.ts +1 -1
- package/dist/engine.js +12 -10
- package/dist/expo.d.ts +1 -1
- package/dist/expo.js +15 -1
- package/dist/index.js +1 -1
- package/dist/portable.d.ts +2 -0
- package/dist/portable.js +13 -0
- package/dist/runtime.js +5 -7
- package/package.json +8 -2
package/README.md
CHANGED
|
@@ -19,6 +19,8 @@ an application glob compiled by [`@hulla/db/build`](https://github.com/hulladev/
|
|
|
19
19
|
combine shared/app handlers using ordinary exports.
|
|
20
20
|
|
|
21
21
|
The browser and Expo entrypoints accept `open`, which supplies a dedicated driver.
|
|
22
|
+
Expo can use the optional `expo-crypto` peer for secure UUID generation when
|
|
23
|
+
Web Crypto is unavailable.
|
|
22
24
|
Readonly replicas require explicit source URL bindings. Offline mutations queue
|
|
23
25
|
server calls with mandatory device-side conflict callbacks and optional optimistic
|
|
24
26
|
feedback. Native Rust SQLite storage and SQLite managed HTTP hosting remain work.
|
package/dist/browser.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import type { BrowserSQLiteConnection } from "./descriptor.ts";
|
|
2
1
|
export { wasmSQLiteDriver } from "./driver.ts";
|
|
3
2
|
export type { SQLiteWasmConnection, SQLiteDriver } from "./driver.ts";
|
|
4
3
|
export type { BrowserSQLiteConnection as SQLiteConnection } from "./descriptor.ts";
|
|
5
|
-
export declare const sqlite: import("./descriptor.ts").SQLiteFactory<BrowserSQLiteConnection>;
|
|
4
|
+
export declare const sqlite: import("./descriptor.ts").SQLiteFactory<import("./descriptor.ts").BrowserSQLiteConnection>;
|
package/dist/browser.js
CHANGED
|
@@ -1,9 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { openSQLiteDatabase } from "./runtime.js";
|
|
1
|
+
import { portableSQLite } from "./portable.js";
|
|
3
2
|
export { wasmSQLiteDriver } from "./driver.js";
|
|
4
|
-
export const sqlite =
|
|
5
|
-
if (!settings || typeof settings !== "object" || Reflect.ownKeys(settings).some(key => !["open", "randomBytes", "transactionTimeoutMs"].includes(key))
|
|
6
|
-
|| settings.filename !== undefined || typeof settings.open !== "function")
|
|
7
|
-
throw new TypeError("Browser SQLite requires open: () => a dedicated SQLite driver");
|
|
8
|
-
return openSQLiteDatabase(schema, await settings.open(), settings, initialize);
|
|
9
|
-
});
|
|
3
|
+
export const sqlite = portableSQLite();
|
package/dist/descriptor.d.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import type { Database, DatabaseEngine, SchemaDefinition, ConnectedDatabase, SourceConnection } from "@hulla/db";
|
|
2
2
|
import { type DatabaseOptions } from "@hulla/db/engine";
|
|
3
3
|
import type { FunctionSource } from "@hulla/db";
|
|
4
|
-
import type
|
|
4
|
+
import { type SourcesOf } from "@hulla/db/internal/engine";
|
|
5
5
|
import type { SQLiteDriver } from "./driver.ts";
|
|
6
6
|
interface SQLiteSettings {
|
|
7
7
|
readonly randomBytes?: (length: number) => Uint8Array;
|
|
8
8
|
readonly transactionTimeoutMs?: number;
|
|
9
|
+
/** Override which driver errors get one startup retry, or disable it. */
|
|
10
|
+
readonly startupRetry?: false | ((error: unknown) => boolean);
|
|
9
11
|
}
|
|
10
12
|
/** Each instance owns a fresh dedicated driver connection. */
|
|
11
13
|
export type BrowserSQLiteConnection = SQLiteSettings & {
|
package/dist/descriptor.js
CHANGED
|
@@ -1,9 +1,41 @@
|
|
|
1
1
|
import { createDatabase, registerDatabaseFactory } from "@hulla/db/engine";
|
|
2
|
+
import { DatabaseError } from "@hulla/db/internal/engine";
|
|
3
|
+
function isLockResult(code) {
|
|
4
|
+
if (typeof code !== "number" || !Number.isInteger(code) || code < 0 || code > 0x7fff_ffff)
|
|
5
|
+
return false;
|
|
6
|
+
const primary = code & 0xff;
|
|
7
|
+
return primary === 5 || primary === 6;
|
|
8
|
+
}
|
|
9
|
+
function isLockError(error) {
|
|
10
|
+
if (!(error instanceof Error))
|
|
11
|
+
return false;
|
|
12
|
+
const { code, errcode, errno, resultCode } = error;
|
|
13
|
+
if (typeof code === "string" && /^SQLITE_(?:BUSY|LOCKED)(?:_|$)/.test(code))
|
|
14
|
+
return true;
|
|
15
|
+
if (isLockResult(errcode ?? errno ?? resultCode))
|
|
16
|
+
return true;
|
|
17
|
+
// Expo reports SQLite's result code in the message of its native error.
|
|
18
|
+
if (code !== "ERR_INTERNAL_SQLITE_ERROR")
|
|
19
|
+
return false;
|
|
20
|
+
const result = /\bError code (\d+)\b/.exec(error.message);
|
|
21
|
+
return result !== null && isLockResult(Number(result[1]));
|
|
22
|
+
}
|
|
2
23
|
export function sqliteDescriptor(connect) {
|
|
24
|
+
const open = (schema, settings, initialize) => {
|
|
25
|
+
const policy = settings?.startupRetry;
|
|
26
|
+
if (policy !== undefined && policy !== false && typeof policy !== "function")
|
|
27
|
+
throw new TypeError("SQLite startupRetry must be false or a function");
|
|
28
|
+
return connect(schema, settings, initialize);
|
|
29
|
+
};
|
|
3
30
|
return Object.freeze({
|
|
4
31
|
name: "sqlite", replicaStorage: true, serialTransactions: true,
|
|
5
|
-
connect: (schema, settings) =>
|
|
6
|
-
initialize: (schema, settings) =>
|
|
32
|
+
connect: (schema, settings) => open(schema, settings, false),
|
|
33
|
+
initialize: (schema, settings) => open(schema, settings, true),
|
|
34
|
+
retryStartup(error, settings) {
|
|
35
|
+
if (error instanceof DatabaseError || settings?.startupRetry === false)
|
|
36
|
+
return false;
|
|
37
|
+
return typeof settings?.startupRetry === "function" ? settings.startupRetry(error) === true : isLockError(error);
|
|
38
|
+
},
|
|
7
39
|
});
|
|
8
40
|
}
|
|
9
41
|
export function sqliteFactory(connect) {
|
package/dist/engine.d.ts
CHANGED
|
@@ -21,7 +21,7 @@ export declare class SQLiteEngine {
|
|
|
21
21
|
physical(table: CompiledTable): string;
|
|
22
22
|
column(table: CompiledTable, name: string): string;
|
|
23
23
|
bind(table: CompiledTable, name: string, value: unknown, parameters: SQLiteValue[]): string;
|
|
24
|
-
fields(table: CompiledTable
|
|
24
|
+
fields(table: CompiledTable): string;
|
|
25
25
|
decode(table: CompiledTable, value: SQLiteRecord): StoredRow;
|
|
26
26
|
envelope(row: StoredRow, projection?: string[]): {
|
|
27
27
|
row: StoredRow;
|
package/dist/engine.js
CHANGED
|
@@ -20,10 +20,15 @@ export class SQLiteEngine {
|
|
|
20
20
|
this.driver = driver;
|
|
21
21
|
this.randomBytes = randomBytes;
|
|
22
22
|
this.tables = compileSchema(schema);
|
|
23
|
-
this.#layout = new Map(this.tables.map((table, index) =>
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
23
|
+
this.#layout = new Map(this.tables.map((table, index) => {
|
|
24
|
+
const fields = Object.entries(table.fields);
|
|
25
|
+
const columns = new Map(fields.map(([name], index) => [name, quote(`c${index}`)]));
|
|
26
|
+
const selection = fields.map(([name, field]) => {
|
|
27
|
+
const column = columns.get(name);
|
|
28
|
+
return `${field.scalar === "int64" ? `CAST(${column} AS TEXT)` : column} AS ${quote(name)}`;
|
|
29
|
+
}).join(", ");
|
|
30
|
+
return [table.name, { table, physical: quote(`_hulla_t${index}`), columns, fields, selection }];
|
|
31
|
+
}));
|
|
27
32
|
this.contract = JSON.stringify(schemaDescriptor(schema));
|
|
28
33
|
if (this.tables.some(table => typeof table.keyGeneration === "object"))
|
|
29
34
|
throw new TypeError("SQLite custom key handlers are not supported; supply explicit keys with keyGeneration: 'none'");
|
|
@@ -56,14 +61,11 @@ export class SQLiteEngine {
|
|
|
56
61
|
throw new TypeError("Invalid SQLite wire value");
|
|
57
62
|
return field.scalar === "int64" ? "CAST(? AS INTEGER)" : "?";
|
|
58
63
|
}
|
|
59
|
-
fields(table
|
|
60
|
-
return
|
|
61
|
-
const column = prefix + physical;
|
|
62
|
-
return `${table.fields[name].scalar === "int64" ? `CAST(${column} AS TEXT)` : column} AS ${quote(name)}`;
|
|
63
|
-
}).join(", ");
|
|
64
|
+
fields(table) {
|
|
65
|
+
return this.#layout.get(table.name).selection;
|
|
64
66
|
}
|
|
65
67
|
decode(table, value) {
|
|
66
|
-
return Object.fromEntries(
|
|
68
|
+
return Object.fromEntries(this.#layout.get(table.name).fields.map(([name, field]) => {
|
|
67
69
|
let item = value[name];
|
|
68
70
|
if (item !== null && field.scalar === "json")
|
|
69
71
|
item = JSON.parse(item);
|
package/dist/expo.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { sqlite } from "./browser.ts";
|
|
2
1
|
export { expoSQLiteDriver } from "./driver.ts";
|
|
3
2
|
export type { ExpoSQLiteConnection, SQLiteDriver } from "./driver.ts";
|
|
4
3
|
export type { BrowserSQLiteConnection as SQLiteConnection } from "./descriptor.ts";
|
|
4
|
+
export declare const sqlite: import("./descriptor.ts").SQLiteFactory<import("./descriptor.ts").BrowserSQLiteConnection>;
|
package/dist/expo.js
CHANGED
|
@@ -1,2 +1,16 @@
|
|
|
1
|
-
|
|
1
|
+
/// <reference path="./expo-crypto.d.ts" />
|
|
2
|
+
import { portableSQLite } from "./portable.js";
|
|
2
3
|
export { expoSQLiteDriver } from "./driver.js";
|
|
4
|
+
export const sqlite = portableSQLite(async () => {
|
|
5
|
+
try {
|
|
6
|
+
const { getRandomValues } = await import("expo-crypto");
|
|
7
|
+
if (typeof getRandomValues !== "function")
|
|
8
|
+
throw new TypeError("expo-crypto requires getRandomValues");
|
|
9
|
+
return length => getRandomValues(new Uint8Array(length));
|
|
10
|
+
}
|
|
11
|
+
catch (cause) {
|
|
12
|
+
return () => {
|
|
13
|
+
throw new TypeError("Install expo-crypto for secure SQLite UUID generation, or supply randomBytes", { cause });
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
});
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { sqliteFactory } from "./descriptor.js";
|
|
2
2
|
export const sqlite = sqliteFactory(async (schema, settings, initialize) => {
|
|
3
|
-
if (!settings || typeof settings !== "object" || Reflect.ownKeys(settings).some(key => !["filename", "open", "randomBytes", "transactionTimeoutMs"].includes(key))
|
|
3
|
+
if (!settings || typeof settings !== "object" || Reflect.ownKeys(settings).some(key => !["filename", "open", "randomBytes", "transactionTimeoutMs", "startupRetry"].includes(key))
|
|
4
4
|
|| (settings.filename === undefined) === (settings.open === undefined))
|
|
5
5
|
throw new TypeError("SQLite requires exactly one of filename or open");
|
|
6
6
|
let driver;
|
package/dist/portable.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { sqliteFactory } from "./descriptor.js";
|
|
2
|
+
import { openSQLiteDatabase } from "./runtime.js";
|
|
3
|
+
export function portableSQLite(resolveRandomBytes) {
|
|
4
|
+
return sqliteFactory(async (schema, settings, initialize) => {
|
|
5
|
+
if (!settings || typeof settings !== "object" || Reflect.ownKeys(settings).some(key => !["open", "randomBytes", "transactionTimeoutMs", "startupRetry"].includes(key))
|
|
6
|
+
|| settings.filename !== undefined || typeof settings.open !== "function")
|
|
7
|
+
throw new TypeError("Browser SQLite requires open: () => a dedicated SQLite driver");
|
|
8
|
+
const options = resolveRandomBytes && settings.randomBytes === undefined && typeof globalThis.crypto?.getRandomValues !== "function"
|
|
9
|
+
? { ...settings, randomBytes: await resolveRandomBytes() }
|
|
10
|
+
: settings;
|
|
11
|
+
return openSQLiteDatabase(schema, await settings.open(), options, initialize);
|
|
12
|
+
});
|
|
13
|
+
}
|
package/dist/runtime.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { registerClientStorage, registerStorageExecution, registerStorageChanges, transactionDatabase } from "@hulla/db/internal/engine";
|
|
1
|
+
import { failStorageStartup, registerClientStorage, registerStorageExecution, registerStorageChanges, transactionDatabase } from "@hulla/db/internal/engine";
|
|
2
2
|
import { SQLiteEngine, sqliteFailure } from "./engine.js";
|
|
3
3
|
import { sqliteClientStorage } from "./client-storage.js";
|
|
4
4
|
/** Open schema-typed local storage. The driver belongs exclusively to the returned handle. */
|
|
5
5
|
export async function openSQLiteDatabase(schema, driver, options = {}, initialize = false) {
|
|
6
|
+
const failOpen = (error) => failStorageStartup(error, () => driver.close());
|
|
6
7
|
let engine;
|
|
7
8
|
const timeoutMs = options.transactionTimeoutMs ?? 5000;
|
|
8
9
|
try {
|
|
@@ -11,8 +12,7 @@ export async function openSQLiteDatabase(schema, driver, options = {}, initializ
|
|
|
11
12
|
engine = new SQLiteEngine(driver, schema, options.randomBytes);
|
|
12
13
|
}
|
|
13
14
|
catch (error) {
|
|
14
|
-
|
|
15
|
-
throw error;
|
|
15
|
+
return failOpen(error);
|
|
16
16
|
}
|
|
17
17
|
let closed = false, userTransaction = false, queued = 0;
|
|
18
18
|
let tail = Promise.resolve();
|
|
@@ -62,8 +62,7 @@ export async function openSQLiteDatabase(schema, driver, options = {}, initializ
|
|
|
62
62
|
storage = sqliteClientStorage(engine, schema, handler => enqueue(handler, true), notify);
|
|
63
63
|
}
|
|
64
64
|
catch (error) {
|
|
65
|
-
|
|
66
|
-
throw error;
|
|
65
|
+
return failOpen(error);
|
|
67
66
|
}
|
|
68
67
|
try {
|
|
69
68
|
if (initialize)
|
|
@@ -72,8 +71,7 @@ export async function openSQLiteDatabase(schema, driver, options = {}, initializ
|
|
|
72
71
|
await engine.verify();
|
|
73
72
|
}
|
|
74
73
|
catch (error) {
|
|
75
|
-
|
|
76
|
-
throw error;
|
|
74
|
+
return failOpen(error);
|
|
77
75
|
}
|
|
78
76
|
function guard(operation) {
|
|
79
77
|
if (typeof operation.table === "string" && storage.isReplica(operation.table)) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hulla/db-sqlite",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "SQLite application instances and platform adapters for Hulla schemas",
|
|
6
6
|
"exports": {
|
|
@@ -55,7 +55,13 @@
|
|
|
55
55
|
"LICENSE"
|
|
56
56
|
],
|
|
57
57
|
"peerDependencies": {
|
|
58
|
-
"@hulla/db": "^0.3.
|
|
58
|
+
"@hulla/db": "^0.3.3",
|
|
59
|
+
"expo-crypto": "*"
|
|
60
|
+
},
|
|
61
|
+
"peerDependenciesMeta": {
|
|
62
|
+
"expo-crypto": {
|
|
63
|
+
"optional": true
|
|
64
|
+
}
|
|
59
65
|
},
|
|
60
66
|
"engines": {
|
|
61
67
|
"node": ">=22.18.0"
|