@lunora/replica 0.0.0 → 1.0.0-alpha.11
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.md +445 -0
- package/README.md +194 -29
- package/dist/adapters/better-sqlite3.d.mts +28 -0
- package/dist/adapters/better-sqlite3.d.ts +28 -0
- package/dist/adapters/better-sqlite3.mjs +1 -0
- package/dist/adapters/sqlite-wasm.d.mts +36 -0
- package/dist/adapters/sqlite-wasm.d.ts +36 -0
- package/dist/adapters/sqlite-wasm.mjs +1 -0
- package/dist/adapters/sqljs.d.mts +20 -0
- package/dist/adapters/sqljs.d.ts +20 -0
- package/dist/adapters/sqljs.mjs +1 -0
- package/dist/index.d.mts +971 -0
- package/dist/index.d.ts +971 -0
- package/dist/index.mjs +1 -0
- package/dist/packem_shared/EventEmitter-ovTsLeAj.mjs +1 -0
- package/dist/packem_shared/EventLog-DmlRY_4Z.mjs +1 -0
- package/dist/packem_shared/EventLogDO-Cb-7iN9w.mjs +1 -0
- package/dist/packem_shared/EventLogDOClient-C6gA3b3u.mjs +1 -0
- package/dist/packem_shared/EventSource-B5UTlkl9.mjs +1 -0
- package/dist/packem_shared/EventsSync-pK_hg9KP.mjs +1 -0
- package/dist/packem_shared/InMemorySnapshotStore-C4taIG5K.mjs +1 -0
- package/dist/packem_shared/LocalMirror-Q7cPH_rX.mjs +4 -0
- package/dist/packem_shared/MaterializerRuntime-B-I9jKDe.mjs +1 -0
- package/dist/packem_shared/SubscriptionManager-CbSjG_GA.mjs +1 -0
- package/dist/packem_shared/applyDiff-BUzddc6r.mjs +1 -0
- package/dist/packem_shared/applyDiffToDb-1M2I3BHi.mjs +1 -0
- package/dist/packem_shared/classifyChanges-BnOMcDGj.mjs +1 -0
- package/dist/packem_shared/defineEvents-CJZV8Bgi.mjs +1 -0
- package/dist/packem_shared/eventsContext-Dxow9Y7S.mjs +1 -0
- package/dist/packem_shared/isClientSeq-DSXBJskD.mjs +1 -0
- package/dist/packem_shared/local-mirror.d-CyGOpUES.d.ts +530 -0
- package/dist/packem_shared/local-mirror.d-DTavX_y0.d.mts +530 -0
- package/dist/packem_shared/subscribeToMirror-Dru_AYBu.mjs +1 -0
- package/dist/packem_shared/types.d-CkMkSwLJ.d.mts +24 -0
- package/dist/packem_shared/types.d-CkMkSwLJ.d.ts +24 -0
- package/dist/react.d.mts +67 -0
- package/dist/react.d.ts +67 -0
- package/dist/react.mjs +1 -0
- package/package.json +88 -7
package/README.md
CHANGED
|
@@ -1,45 +1,210 @@
|
|
|
1
|
-
|
|
1
|
+
<!-- START_PACKAGE_OG_IMAGE_PLACEHOLDER -->
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
<a href="https://www.anolilab.com/open-source" align="center">
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
<img src="__assets__/package-og.svg" alt="replica" />
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
</a>
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
<h3 align="center">Local-first replica runtime + local SQLite mirror for Lunora</h3>
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
1. Configure OIDC trusted publishing for the package name `@lunora/replica`
|
|
13
|
-
2. Enable secure, token-less publishing from CI/CD workflows
|
|
14
|
-
3. Establish provenance for packages published under this name
|
|
11
|
+
<!-- END_PACKAGE_OG_IMAGE_PLACEHOLDER -->
|
|
15
12
|
|
|
16
|
-
|
|
13
|
+
> **Experimental** — this package is outside the Lunora 1.0 stability promise: its API may change in any release, without a major version bump.
|
|
17
14
|
|
|
18
|
-
|
|
15
|
+
<br />
|
|
19
16
|
|
|
20
|
-
|
|
17
|
+
<div align="center">
|
|
21
18
|
|
|
22
|
-
|
|
19
|
+
[![typescript-image][typescript-badge]][typescript-url]
|
|
20
|
+
[![FSL-1.1-Apache-2.0 licence][license-badge]][license]
|
|
21
|
+
[![npm version][npm-version-badge]][npm-version]
|
|
22
|
+
[![npm downloads][npm-downloads-badge]][npm-downloads]
|
|
23
|
+
[![PRs Welcome][prs-welcome-badge]][prs-welcome]
|
|
23
24
|
|
|
24
|
-
|
|
25
|
-
2. Configure the trusted publisher (e.g., GitHub Actions)
|
|
26
|
-
3. Specify the repository and workflow that should be allowed to publish
|
|
27
|
-
4. Use the configured workflow to publish your actual package
|
|
25
|
+
</div>
|
|
28
26
|
|
|
29
|
-
|
|
27
|
+
---
|
|
30
28
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
29
|
+
<div align="center">
|
|
30
|
+
<p>
|
|
31
|
+
<sup>
|
|
32
|
+
Daniel Bannert's open source work is supported by the community on <a href="https://github.com/sponsors/prisis">GitHub Sponsors</a>
|
|
33
|
+
</sup>
|
|
34
|
+
</p>
|
|
35
|
+
</div>
|
|
36
36
|
|
|
37
|
-
|
|
37
|
+
---
|
|
38
38
|
|
|
39
|
-
|
|
40
|
-
- [npm Trusted Publishing Documentation](https://docs.npmjs.com/generating-provenance-statements)
|
|
41
|
-
- [GitHub Actions OIDC Documentation](https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/about-security-hardening-with-openid-connect)
|
|
39
|
+
Local-first replica runtime and local SQLite mirror for [Lunora](https://lunora.sh).
|
|
42
40
|
|
|
43
|
-
|
|
41
|
+
**Events layer** — derive state from an append-only event log via reducers, persist
|
|
42
|
+
events through a Durable Object, and subscribe to typed events.
|
|
43
|
+
|
|
44
|
+
**Local mirror** — a client-side SQLite mirror that maintains a replica
|
|
45
|
+
of server tables by applying typed row-level diffs. Choose the SQLite backend
|
|
46
|
+
that fits your runtime:
|
|
47
|
+
|
|
48
|
+
| Backend | Entry point | Runtime |
|
|
49
|
+
| ----------------------- | ----------------------------------------- | ------------------------------ |
|
|
50
|
+
| sql.js (WASM) | `@lunora/replica/adapters/sqljs` | Browser, Node, Service Workers |
|
|
51
|
+
| better-sqlite3 (native) | `@lunora/replica/adapters/better-sqlite3` | Node.js |
|
|
52
|
+
| @sqlite.org/sqlite-wasm | `@lunora/replica/adapters/sqlite-wasm` | Browser, Node (official WASM) |
|
|
53
|
+
| Custom | Write your own `SqliteAdapter` | Any |
|
|
54
|
+
|
|
55
|
+
## Install
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
pnpm add @lunora/replica
|
|
59
|
+
# Choose one SQLite backend:
|
|
60
|
+
pnpm add sql.js # browser + Node (WASM)
|
|
61
|
+
pnpm add better-sqlite3 # Node.js only (native)
|
|
62
|
+
pnpm add @sqlite.org/sqlite-wasm # browser (official WASM)
|
|
63
|
+
# @lunora/server is optional — needed only for the EventLogDO:
|
|
64
|
+
pnpm add @lunora/server
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Exports
|
|
68
|
+
|
|
69
|
+
| Entry point | Contents |
|
|
70
|
+
| ----------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
71
|
+
| `@lunora/replica` | `EventSource`, `EventEmitter`, `defineEvents`, `defineMaterializer`, `EventLogDO`, `EventLogDOClient`, `SubscriptionManager`, `InMemorySnapshotStore`, `eventsContext`, `LocalMirror`, `EventLog`, `EventsSync`, `subscribeToMirror`, `applyDiff`, `TableDiff` helpers, `createSqlJsAdapter`, `createBetterSqlite3Adapter`, `createSqliteWasmAdapter` |
|
|
72
|
+
| `@lunora/replica/react` | `useLocalQuery` React hook |
|
|
73
|
+
| `@lunora/replica/adapters/sqljs` | `createSqlJsAdapter` (sql.js) |
|
|
74
|
+
| `@lunora/replica/adapters/better-sqlite3` | `createBetterSqlite3Adapter` (better-sqlite3) |
|
|
75
|
+
| `@lunora/replica/adapters/sqlite-wasm` | `createSqliteWasmAdapter` (@sqlite.org/sqlite-wasm) |
|
|
76
|
+
|
|
77
|
+
## EventSource
|
|
78
|
+
|
|
79
|
+
The core runtime — a typed reducer that derives state from an append-only log:
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
import { EventSource } from "@lunora/replica";
|
|
83
|
+
|
|
84
|
+
interface MyState {
|
|
85
|
+
count: number;
|
|
86
|
+
items: string[];
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const source = new EventSource<MyState>({ count: 0, items: [] }, (state, entry) => {
|
|
90
|
+
switch (entry.type) {
|
|
91
|
+
case "item:added":
|
|
92
|
+
return { count: state.count + 1, items: [...state.items, entry.payload as string] };
|
|
93
|
+
default:
|
|
94
|
+
return state;
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
source.applyEvent("item:added", "hello");
|
|
99
|
+
console.log(source.state.count); // 1
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## LocalMirror
|
|
103
|
+
|
|
104
|
+
An SQLite mirror that applies typed row-level diffs. Create it with your
|
|
105
|
+
chosen backend:
|
|
106
|
+
|
|
107
|
+
```ts
|
|
108
|
+
import initSqlJs from "sql.js";
|
|
109
|
+
import { createSqlJsAdapter, LocalMirror } from "@lunora/replica";
|
|
110
|
+
|
|
111
|
+
const SQL = await initSqlJs();
|
|
112
|
+
const adapter = createSqlJsAdapter(new SQL.Database());
|
|
113
|
+
const mirror = new LocalMirror({ db: adapter });
|
|
114
|
+
|
|
115
|
+
mirror.applyDiff({
|
|
116
|
+
table: "todos",
|
|
117
|
+
schema: "1.0",
|
|
118
|
+
changes: [{ type: "insert", data: { id: "1", title: "hello", done: false } }],
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
const todos = mirror.query<{ id: string; title: string }>("SELECT id, title FROM todos");
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
For `better-sqlite3`:
|
|
125
|
+
|
|
126
|
+
```ts
|
|
127
|
+
import Database from "better-sqlite3";
|
|
128
|
+
import { createBetterSqlite3Adapter, LocalMirror } from "@lunora/replica";
|
|
129
|
+
|
|
130
|
+
const adapter = createBetterSqlite3Adapter(new Database(":memory:"));
|
|
131
|
+
const mirror = new LocalMirror({ db: adapter });
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### EventsSync
|
|
135
|
+
|
|
136
|
+
Periodically polls a server-side event log, replays events through a state
|
|
137
|
+
machine, and pushes the resulting diffs to the mirror:
|
|
138
|
+
|
|
139
|
+
```ts
|
|
140
|
+
import { EventsSync } from "@lunora/replica";
|
|
141
|
+
|
|
142
|
+
const sync = new EventsSync({
|
|
143
|
+
fetchEventsSince: (seq) => eventLogClient.getSince(seq),
|
|
144
|
+
applyEvents: (events) => {
|
|
145
|
+
/* feed events into your state machine */
|
|
146
|
+
},
|
|
147
|
+
getTableDiffs: () => {
|
|
148
|
+
/* compare state before/after → TableDiff[] */
|
|
149
|
+
},
|
|
150
|
+
mirror,
|
|
151
|
+
pollInterval: 3000,
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
sync.start(); // begin polling
|
|
155
|
+
// sync.sync(); // one-shot sync
|
|
156
|
+
// sync.stop(); // halt polling
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
See the [EventsSync JSDoc](src/sync-events.ts) for full API details.
|
|
160
|
+
|
|
161
|
+
### useLocalQuery (React)
|
|
162
|
+
|
|
163
|
+
Live-updating hook that re-queries the mirror whenever a diff is applied.
|
|
164
|
+
Returns `undefined` when the query fails (e.g. table doesn't exist yet),
|
|
165
|
+
and the result rows otherwise.
|
|
166
|
+
|
|
167
|
+
```tsx
|
|
168
|
+
import { useLocalQuery } from "@lunora/replica/react";
|
|
169
|
+
|
|
170
|
+
function TodoList() {
|
|
171
|
+
const todos = useLocalQuery<{ id: string; title: string; done: boolean }>(mirror, "SELECT id, title, done FROM todos WHERE done = ?", [false]);
|
|
172
|
+
|
|
173
|
+
if (todos === undefined) {
|
|
174
|
+
return <p>Waiting for data…</p>;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return (
|
|
178
|
+
<ul>
|
|
179
|
+
{todos.map((t) => (
|
|
180
|
+
<li key={t.id}>{t.title}</li>
|
|
181
|
+
))}
|
|
182
|
+
</ul>
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
The hook uses `useSyncExternalStore` under the hood, so it integrates with
|
|
188
|
+
React 18+ concurrent features and Suspense-based frameworks (Next.js, Remix).
|
|
189
|
+
|
|
190
|
+
## EventLogDO
|
|
191
|
+
|
|
192
|
+
A Durable Object that persists the event log to DO SQLite storage:
|
|
193
|
+
|
|
194
|
+
```ts
|
|
195
|
+
export { EventLogDO } from "@lunora/replica";
|
|
196
|
+
|
|
197
|
+
const client = new EventLogDOClient({ namespace: "my-app" });
|
|
198
|
+
await client.append({ type: "order:placed", payload: { orderId: "123" } });
|
|
199
|
+
const events = await client.query({ type: "order:placed", limit: 10 });
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
## Custom adapters
|
|
203
|
+
|
|
204
|
+
See the [custom adapter guide](docs/guides/custom-adapter.mdx) for writing
|
|
205
|
+
`SqliteAdapter` implementations for `expo-sqlite`, `bun:sqlite`, or any other
|
|
206
|
+
SQLite runtime.
|
|
207
|
+
|
|
208
|
+
## License
|
|
44
209
|
|
|
45
|
-
|
|
210
|
+
[FSL-1.1-Apache-2.0](./LICENSE.md)
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { S as SqliteAdapter } from "../packem_shared/types.d-CkMkSwLJ.mjs";
|
|
2
|
+
/**
|
|
3
|
+
* Create a {@link SqliteAdapter} backed by [better-sqlite3](https://github.com/WiseLibs/better-sqlite3)
|
|
4
|
+
* (a synchronous SQLite3 binding for Node.js).
|
|
5
|
+
*
|
|
6
|
+
* The adapter takes a structural projection of better-sqlite3's `Database` so
|
|
7
|
+
* the package never imports from `better-sqlite3` directly — consumers install
|
|
8
|
+
* it themselves and pass in their database instance.
|
|
9
|
+
* @param database An already-initialised better-sqlite3 `Database` instance.
|
|
10
|
+
* @param database.close Tear down the database connection.
|
|
11
|
+
* @param database.exec Execute one or more SQL statements (no params, no results).
|
|
12
|
+
* @param database.prepare Prepare a SQL statement for repeated execution.
|
|
13
|
+
* @param database.transaction Wrap a function so its statements run in a transaction.
|
|
14
|
+
* @experimental
|
|
15
|
+
*/
|
|
16
|
+
declare const createBetterSqlite3Adapter: (database: {
|
|
17
|
+
close: () => void;
|
|
18
|
+
exec: (sql: string) => void;
|
|
19
|
+
prepare: (sql: string) => {
|
|
20
|
+
all: (params?: unknown[]) => unknown[];
|
|
21
|
+
get: (params?: unknown[]) => unknown;
|
|
22
|
+
run: (params?: unknown[]) => {
|
|
23
|
+
lastInsertRowid: number | bigint;
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
transaction: (function_: () => void) => () => void;
|
|
27
|
+
}) => SqliteAdapter;
|
|
28
|
+
export { createBetterSqlite3Adapter };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { S as SqliteAdapter } from "../packem_shared/types.d-CkMkSwLJ.js";
|
|
2
|
+
/**
|
|
3
|
+
* Create a {@link SqliteAdapter} backed by [better-sqlite3](https://github.com/WiseLibs/better-sqlite3)
|
|
4
|
+
* (a synchronous SQLite3 binding for Node.js).
|
|
5
|
+
*
|
|
6
|
+
* The adapter takes a structural projection of better-sqlite3's `Database` so
|
|
7
|
+
* the package never imports from `better-sqlite3` directly — consumers install
|
|
8
|
+
* it themselves and pass in their database instance.
|
|
9
|
+
* @param database An already-initialised better-sqlite3 `Database` instance.
|
|
10
|
+
* @param database.close Tear down the database connection.
|
|
11
|
+
* @param database.exec Execute one or more SQL statements (no params, no results).
|
|
12
|
+
* @param database.prepare Prepare a SQL statement for repeated execution.
|
|
13
|
+
* @param database.transaction Wrap a function so its statements run in a transaction.
|
|
14
|
+
* @experimental
|
|
15
|
+
*/
|
|
16
|
+
declare const createBetterSqlite3Adapter: (database: {
|
|
17
|
+
close: () => void;
|
|
18
|
+
exec: (sql: string) => void;
|
|
19
|
+
prepare: (sql: string) => {
|
|
20
|
+
all: (params?: unknown[]) => unknown[];
|
|
21
|
+
get: (params?: unknown[]) => unknown;
|
|
22
|
+
run: (params?: unknown[]) => {
|
|
23
|
+
lastInsertRowid: number | bigint;
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
transaction: (function_: () => void) => () => void;
|
|
27
|
+
}) => SqliteAdapter;
|
|
28
|
+
export { createBetterSqlite3Adapter };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const a=r=>({exec(e,t){t&&t.length>0?r.prepare(e).run([...t]):r.exec(e)},query(e,t){const n=r.prepare(e);return t&&t.length>0?n.all([...t]):n.all()},transaction(e){r.transaction(e)()},lastInsertRowId(){const e=r.prepare("SELECT last_insert_rowid() AS id").get();return Number(e?.id??-1)},close(){r.close()}});export{a as createBetterSqlite3Adapter};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { S as SqliteAdapter } from "../packem_shared/types.d-CkMkSwLJ.mjs";
|
|
2
|
+
/**
|
|
3
|
+
* Create a {@link SqliteAdapter} backed by the [official SQLite Wasm](https://www.npmjs.com/package/@sqlite.org/sqlite-wasm)
|
|
4
|
+
* (a WebAssembly build of SQLite that runs in browsers and Node.js).
|
|
5
|
+
*
|
|
6
|
+
* The adapter takes a structural projection of the OO API (`sqlite3.oo1.DB`) so
|
|
7
|
+
* the package never imports from `@sqlite.org/sqlite-wasm` directly — consumers
|
|
8
|
+
* install it themselves and pass in their database instance.
|
|
9
|
+
*
|
|
10
|
+
* IMPORTANT (REPLICA-01): the real `oo1.DB.exec()` does NOT return sql.js's
|
|
11
|
+
* `{ columns, values }[]` result shape — with `rowMode: "object"` and
|
|
12
|
+
* `returnValue: "resultRows"` it returns the rows directly, as
|
|
13
|
+
* `Record<string, unknown>[]`. This adapter is written against that real
|
|
14
|
+
* shape; `lastInsertRowId` uses the driver's `selectValue()` convenience
|
|
15
|
+
* method (a single-scalar query helper) rather than parsing a result-row
|
|
16
|
+
* array.
|
|
17
|
+
* @param database An already-initialised `sqlite3.oo1.DB` instance.
|
|
18
|
+
* @param database.close Tear down the database connection.
|
|
19
|
+
* @param database.exec Execute SQL with optional bind params. With
|
|
20
|
+
* `{ returnValue: "resultRows", rowMode: "object" }` it returns the matched
|
|
21
|
+
* rows directly (`Record<string, unknown>[]`); otherwise (DDL/DML/BEGIN/
|
|
22
|
+
* COMMIT/ROLLBACK) its return value is unused here.
|
|
23
|
+
* @param database.selectValue Run a query and return the first column of the
|
|
24
|
+
* first row as a single scalar — used for `SELECT last_insert_rowid()`.
|
|
25
|
+
* @experimental
|
|
26
|
+
*/
|
|
27
|
+
declare const createSqliteWasmAdapter: (database: {
|
|
28
|
+
close: () => void;
|
|
29
|
+
exec: (sql: string, options?: {
|
|
30
|
+
bind?: unknown[];
|
|
31
|
+
returnValue?: "resultRows";
|
|
32
|
+
rowMode?: "object";
|
|
33
|
+
}) => Record<string, unknown>[] | undefined;
|
|
34
|
+
selectValue: (sql: string, bind?: unknown[]) => unknown;
|
|
35
|
+
}) => SqliteAdapter;
|
|
36
|
+
export { createSqliteWasmAdapter };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { S as SqliteAdapter } from "../packem_shared/types.d-CkMkSwLJ.js";
|
|
2
|
+
/**
|
|
3
|
+
* Create a {@link SqliteAdapter} backed by the [official SQLite Wasm](https://www.npmjs.com/package/@sqlite.org/sqlite-wasm)
|
|
4
|
+
* (a WebAssembly build of SQLite that runs in browsers and Node.js).
|
|
5
|
+
*
|
|
6
|
+
* The adapter takes a structural projection of the OO API (`sqlite3.oo1.DB`) so
|
|
7
|
+
* the package never imports from `@sqlite.org/sqlite-wasm` directly — consumers
|
|
8
|
+
* install it themselves and pass in their database instance.
|
|
9
|
+
*
|
|
10
|
+
* IMPORTANT (REPLICA-01): the real `oo1.DB.exec()` does NOT return sql.js's
|
|
11
|
+
* `{ columns, values }[]` result shape — with `rowMode: "object"` and
|
|
12
|
+
* `returnValue: "resultRows"` it returns the rows directly, as
|
|
13
|
+
* `Record<string, unknown>[]`. This adapter is written against that real
|
|
14
|
+
* shape; `lastInsertRowId` uses the driver's `selectValue()` convenience
|
|
15
|
+
* method (a single-scalar query helper) rather than parsing a result-row
|
|
16
|
+
* array.
|
|
17
|
+
* @param database An already-initialised `sqlite3.oo1.DB` instance.
|
|
18
|
+
* @param database.close Tear down the database connection.
|
|
19
|
+
* @param database.exec Execute SQL with optional bind params. With
|
|
20
|
+
* `{ returnValue: "resultRows", rowMode: "object" }` it returns the matched
|
|
21
|
+
* rows directly (`Record<string, unknown>[]`); otherwise (DDL/DML/BEGIN/
|
|
22
|
+
* COMMIT/ROLLBACK) its return value is unused here.
|
|
23
|
+
* @param database.selectValue Run a query and return the first column of the
|
|
24
|
+
* first row as a single scalar — used for `SELECT last_insert_rowid()`.
|
|
25
|
+
* @experimental
|
|
26
|
+
*/
|
|
27
|
+
declare const createSqliteWasmAdapter: (database: {
|
|
28
|
+
close: () => void;
|
|
29
|
+
exec: (sql: string, options?: {
|
|
30
|
+
bind?: unknown[];
|
|
31
|
+
returnValue?: "resultRows";
|
|
32
|
+
rowMode?: "object";
|
|
33
|
+
}) => Record<string, unknown>[] | undefined;
|
|
34
|
+
selectValue: (sql: string, bind?: unknown[]) => unknown;
|
|
35
|
+
}) => SqliteAdapter;
|
|
36
|
+
export { createSqliteWasmAdapter };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const o=c=>({exec(e,t){t&&t.length>0?c.exec(e,{bind:[...t]}):c.exec(e)},query(e,t){return c.exec(e,{bind:t&&t.length>0?[...t]:void 0,returnValue:"resultRows",rowMode:"object"})??[]},transaction(e){c.exec("BEGIN");try{e(),c.exec("COMMIT")}catch(t){throw c.exec("ROLLBACK"),t}},lastInsertRowId(){const e=c.selectValue("SELECT last_insert_rowid()");return typeof e=="number"?e:typeof e=="bigint"?Number(e):-1},close(){c.close()}});export{o as createSqliteWasmAdapter};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { S as SqliteAdapter } from "../packem_shared/types.d-CkMkSwLJ.mjs";
|
|
2
|
+
/**
|
|
3
|
+
* Create a {@link SqliteAdapter} backed by [sql.js](https://sql.js.org)
|
|
4
|
+
* (a WebAssembly build of SQLite that runs in browsers, Node, and
|
|
5
|
+
* React Native).
|
|
6
|
+
* @param database An already-initialised sql.js database instance.
|
|
7
|
+
* @param database.run Execute a parameterised SQL statement (no results).
|
|
8
|
+
* @param database.exec Execute SQL and return result rows.
|
|
9
|
+
* @param database.close Tear down the database connection.
|
|
10
|
+
* @experimental
|
|
11
|
+
*/
|
|
12
|
+
declare const createSqlJsAdapter: (database: {
|
|
13
|
+
close: () => void;
|
|
14
|
+
exec: (sql: string, params?: unknown[]) => {
|
|
15
|
+
columns: string[];
|
|
16
|
+
values: unknown[][];
|
|
17
|
+
}[];
|
|
18
|
+
run: (sql: string, params?: unknown[]) => void;
|
|
19
|
+
}) => SqliteAdapter;
|
|
20
|
+
export { createSqlJsAdapter };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { S as SqliteAdapter } from "../packem_shared/types.d-CkMkSwLJ.js";
|
|
2
|
+
/**
|
|
3
|
+
* Create a {@link SqliteAdapter} backed by [sql.js](https://sql.js.org)
|
|
4
|
+
* (a WebAssembly build of SQLite that runs in browsers, Node, and
|
|
5
|
+
* React Native).
|
|
6
|
+
* @param database An already-initialised sql.js database instance.
|
|
7
|
+
* @param database.run Execute a parameterised SQL statement (no results).
|
|
8
|
+
* @param database.exec Execute SQL and return result rows.
|
|
9
|
+
* @param database.close Tear down the database connection.
|
|
10
|
+
* @experimental
|
|
11
|
+
*/
|
|
12
|
+
declare const createSqlJsAdapter: (database: {
|
|
13
|
+
close: () => void;
|
|
14
|
+
exec: (sql: string, params?: unknown[]) => {
|
|
15
|
+
columns: string[];
|
|
16
|
+
values: unknown[][];
|
|
17
|
+
}[];
|
|
18
|
+
run: (sql: string, params?: unknown[]) => void;
|
|
19
|
+
}) => SqliteAdapter;
|
|
20
|
+
export { createSqlJsAdapter };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const h=n=>({exec(e,t){t&&t.length>0?n.run(e,[...t]):n.exec(e)},query(e,t){const r=(t&&t.length>0?n.exec(e,[...t]):n.exec(e))[0];if(!r)return[];const o=r.columns,s=[];for(const u of r.values){const c={};for(const[l,a]of o.entries())c[a]=u[l];s.push(c)}return s},transaction(e){n.run("BEGIN");try{e(),n.run("COMMIT")}catch(t){throw n.run("ROLLBACK"),t}},lastInsertRowId(){const e=n.exec("SELECT last_insert_rowid() AS id"),t=e[0];if(e.length===0||!t||t.values.length===0)return-1;const r=t.values[0];return!r||r.length===0?-1:Number(r[0])},close(){n.close()}});export{h as createSqlJsAdapter};
|