@lunora/replica 0.0.0 → 1.0.0-alpha.2
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 +231 -0
- package/README.md +192 -29
- package/dist/adapters/better-sqlite3.d.mts +27 -0
- package/dist/adapters/better-sqlite3.d.ts +27 -0
- package/dist/adapters/better-sqlite3.mjs +29 -0
- package/dist/adapters/sqlite-wasm.d.mts +25 -0
- package/dist/adapters/sqlite-wasm.d.ts +25 -0
- package/dist/adapters/sqlite-wasm.mjs +56 -0
- package/dist/adapters/sqljs.d.mts +19 -0
- package/dist/adapters/sqljs.d.ts +19 -0
- package/dist/adapters/sqljs.mjs +55 -0
- package/dist/index.d.mts +865 -0
- package/dist/index.d.ts +865 -0
- package/dist/index.mjs +20 -0
- package/dist/packem_shared/EventEmitter-CMZfct03.mjs +92 -0
- package/dist/packem_shared/EventLog-zMy7AYP4.mjs +162 -0
- package/dist/packem_shared/EventLogDO-CZYUvvSr.mjs +235 -0
- package/dist/packem_shared/EventLogDOClient-DGiEdi96.mjs +86 -0
- package/dist/packem_shared/EventSource-DfV4VoRD.mjs +195 -0
- package/dist/packem_shared/EventsSync-DkVbU0WV.mjs +91 -0
- package/dist/packem_shared/InMemorySnapshotStore-BHVAD-Bp.mjs +24 -0
- package/dist/packem_shared/LocalMirror-GeJ26eNe.mjs +188 -0
- package/dist/packem_shared/MaterializerRuntime-HqNXqJxp.mjs +204 -0
- package/dist/packem_shared/SubscriptionManager-C5xbw0pg.mjs +75 -0
- package/dist/packem_shared/applyDiff-BtbIl1D3.mjs +40 -0
- package/dist/packem_shared/applyDiffToDb-DQ1xZp5J.mjs +58 -0
- package/dist/packem_shared/classifyChanges-aZmkxgVI.mjs +38 -0
- package/dist/packem_shared/defineEvents-DiBkPTh_.mjs +28 -0
- package/dist/packem_shared/eventsContext-Bk_p48hj.mjs +6 -0
- package/dist/packem_shared/isClientSeq-C46BkzqJ.mjs +5 -0
- package/dist/packem_shared/local-mirror.d-Bp19ueGy.d.mts +412 -0
- package/dist/packem_shared/local-mirror.d-GhuZAKgm.d.ts +412 -0
- package/dist/packem_shared/subscribeToMirror-CiaM-nQ7.mjs +45 -0
- package/dist/packem_shared/types.d-VfJ76cK4.d.mts +23 -0
- package/dist/packem_shared/types.d-VfJ76cK4.d.ts +23 -0
- package/dist/react.d.mts +65 -0
- package/dist/react.d.ts +65 -0
- package/dist/react.mjs +15 -0
- package/package.json +88 -7
package/LICENSE.md
ADDED
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
# Functional Source License, Version 1.1, Apache 2.0 Future License
|
|
2
|
+
|
|
3
|
+
## Abbreviation
|
|
4
|
+
|
|
5
|
+
FSL-1.1-Apache-2.0
|
|
6
|
+
|
|
7
|
+
## Notice
|
|
8
|
+
|
|
9
|
+
Copyright 2026 anolilab and contributors
|
|
10
|
+
|
|
11
|
+
## Terms and Conditions
|
|
12
|
+
|
|
13
|
+
### Licensor ("We")
|
|
14
|
+
|
|
15
|
+
The party offering the Software under these Terms and Conditions.
|
|
16
|
+
|
|
17
|
+
### The Software
|
|
18
|
+
|
|
19
|
+
The "Software" is each version of the software that we make available under
|
|
20
|
+
these Terms and Conditions, as indicated by our inclusion of these Terms and
|
|
21
|
+
Conditions with the Software.
|
|
22
|
+
|
|
23
|
+
### License Grant
|
|
24
|
+
|
|
25
|
+
Subject to your compliance with this License Grant and the Patents,
|
|
26
|
+
Redistribution and Trademark clauses below, we hereby grant you the right to
|
|
27
|
+
use, copy, modify, create derivative works, publicly perform, publicly display
|
|
28
|
+
and redistribute the Software for any Permitted Purpose identified below.
|
|
29
|
+
|
|
30
|
+
### Permitted Purpose
|
|
31
|
+
|
|
32
|
+
A Permitted Purpose is any purpose other than a Competing Use. A Competing Use
|
|
33
|
+
means making the Software available to others in a commercial product or service
|
|
34
|
+
that:
|
|
35
|
+
|
|
36
|
+
1. substitutes for the Software;
|
|
37
|
+
|
|
38
|
+
2. substitutes for any other product or service we offer using the Software that
|
|
39
|
+
exists as of the date we make the Software available; or
|
|
40
|
+
|
|
41
|
+
3. offers the same or substantially similar functionality as the Software.
|
|
42
|
+
|
|
43
|
+
Permitted Purposes specifically include using the Software:
|
|
44
|
+
|
|
45
|
+
1. for your internal use and access;
|
|
46
|
+
|
|
47
|
+
2. for non-commercial education;
|
|
48
|
+
|
|
49
|
+
3. for non-commercial research; and
|
|
50
|
+
|
|
51
|
+
4. in connection with professional services that you provide to a licensee using
|
|
52
|
+
the Software in accordance with these Terms and Conditions.
|
|
53
|
+
|
|
54
|
+
### Patents
|
|
55
|
+
|
|
56
|
+
To the extent your use for a Permitted Purpose would necessarily infringe our
|
|
57
|
+
patents, the license grant above includes a license under our patents. If you
|
|
58
|
+
make a claim against any party that the Software infringes or contributes to the
|
|
59
|
+
infringement of any patent, then your patent license to the Software ends
|
|
60
|
+
immediately.
|
|
61
|
+
|
|
62
|
+
### Redistribution
|
|
63
|
+
|
|
64
|
+
The Terms and Conditions apply to all copies, modifications and derivatives of
|
|
65
|
+
the Software.
|
|
66
|
+
|
|
67
|
+
If you redistribute any copies, modifications or derivatives of the Software,
|
|
68
|
+
you must include a copy of or a link to these Terms and Conditions and not
|
|
69
|
+
remove any copyright notices provided in or with the Software.
|
|
70
|
+
|
|
71
|
+
### Disclaimer
|
|
72
|
+
|
|
73
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EXPRESS OR
|
|
74
|
+
IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR
|
|
75
|
+
PURPOSE, MERCHANTABILITY, TITLE OR NON-INFRINGEMENT.
|
|
76
|
+
|
|
77
|
+
IN NO EVENT WILL WE HAVE ANY LIABILITY TO YOU ARISING OUT OF OR RELATED TO THE
|
|
78
|
+
SOFTWARE, INCLUDING INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES, EVEN
|
|
79
|
+
IF WE HAVE BEEN INFORMED OF THEIR POSSIBILITY IN ADVANCE.
|
|
80
|
+
|
|
81
|
+
### Trademarks
|
|
82
|
+
|
|
83
|
+
Except for displaying the License Details and identifying us as the origin of
|
|
84
|
+
the Software, you have no right under these Terms and Conditions to use our
|
|
85
|
+
trademarks, trade names, service marks or product names.
|
|
86
|
+
|
|
87
|
+
## Grant of Future License
|
|
88
|
+
|
|
89
|
+
We hereby irrevocably grant you an additional license to use the Software under
|
|
90
|
+
the Apache License, Version 2.0 that is effective on the second anniversary of
|
|
91
|
+
the date we make the Software available. On or after that date, you may use the
|
|
92
|
+
Software under the Apache License, Version 2.0, in which case the following will
|
|
93
|
+
apply:
|
|
94
|
+
|
|
95
|
+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
96
|
+
this file except in compliance with the License.
|
|
97
|
+
|
|
98
|
+
You may obtain a copy of the License at
|
|
99
|
+
|
|
100
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
101
|
+
|
|
102
|
+
Unless required by applicable law or agreed to in writing, software distributed
|
|
103
|
+
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
104
|
+
CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
105
|
+
specific language governing permissions and limitations under the License.
|
|
106
|
+
|
|
107
|
+
<!-- DEPENDENCIES -->
|
|
108
|
+
<!-- /DEPENDENCIES -->
|
|
109
|
+
|
|
110
|
+
<!-- TYPE_DEPENDENCIES -->
|
|
111
|
+
|
|
112
|
+
# Licenses of bundled types
|
|
113
|
+
The published @lunora/replica artifact additionally contains code with the following licenses:
|
|
114
|
+
FSL-1.1-Apache-2.0
|
|
115
|
+
|
|
116
|
+
# Bundled types:
|
|
117
|
+
## @lunora/server
|
|
118
|
+
License: FSL-1.1-Apache-2.0
|
|
119
|
+
By: Daniel Bannert
|
|
120
|
+
Repository: git+https://github.com/anolilab/lunora.git
|
|
121
|
+
|
|
122
|
+
> # Functional Source License, Version 1.1, Apache 2.0 Future License
|
|
123
|
+
>
|
|
124
|
+
> ## Abbreviation
|
|
125
|
+
>
|
|
126
|
+
> FSL-1.1-Apache-2.0
|
|
127
|
+
>
|
|
128
|
+
> ## Notice
|
|
129
|
+
>
|
|
130
|
+
> Copyright 2026 anolilab and contributors
|
|
131
|
+
>
|
|
132
|
+
> ## Terms and Conditions
|
|
133
|
+
>
|
|
134
|
+
> ### Licensor ("We")
|
|
135
|
+
>
|
|
136
|
+
> The party offering the Software under these Terms and Conditions.
|
|
137
|
+
>
|
|
138
|
+
> ### The Software
|
|
139
|
+
>
|
|
140
|
+
> The "Software" is each version of the software that we make available under
|
|
141
|
+
> these Terms and Conditions, as indicated by our inclusion of these Terms and
|
|
142
|
+
> Conditions with the Software.
|
|
143
|
+
>
|
|
144
|
+
> ### License Grant
|
|
145
|
+
>
|
|
146
|
+
> Subject to your compliance with this License Grant and the Patents,
|
|
147
|
+
> Redistribution and Trademark clauses below, we hereby grant you the right to
|
|
148
|
+
> use, copy, modify, create derivative works, publicly perform, publicly display
|
|
149
|
+
> and redistribute the Software for any Permitted Purpose identified below.
|
|
150
|
+
>
|
|
151
|
+
> ### Permitted Purpose
|
|
152
|
+
>
|
|
153
|
+
> A Permitted Purpose is any purpose other than a Competing Use. A Competing Use
|
|
154
|
+
> means making the Software available to others in a commercial product or service
|
|
155
|
+
> that:
|
|
156
|
+
>
|
|
157
|
+
> 1. substitutes for the Software;
|
|
158
|
+
>
|
|
159
|
+
> 2. substitutes for any other product or service we offer using the Software that
|
|
160
|
+
> exists as of the date we make the Software available; or
|
|
161
|
+
>
|
|
162
|
+
> 3. offers the same or substantially similar functionality as the Software.
|
|
163
|
+
>
|
|
164
|
+
> Permitted Purposes specifically include using the Software:
|
|
165
|
+
>
|
|
166
|
+
> 1. for your internal use and access;
|
|
167
|
+
>
|
|
168
|
+
> 2. for non-commercial education;
|
|
169
|
+
>
|
|
170
|
+
> 3. for non-commercial research; and
|
|
171
|
+
>
|
|
172
|
+
> 4. in connection with professional services that you provide to a licensee using
|
|
173
|
+
> the Software in accordance with these Terms and Conditions.
|
|
174
|
+
>
|
|
175
|
+
> ### Patents
|
|
176
|
+
>
|
|
177
|
+
> To the extent your use for a Permitted Purpose would necessarily infringe our
|
|
178
|
+
> patents, the license grant above includes a license under our patents. If you
|
|
179
|
+
> make a claim against any party that the Software infringes or contributes to the
|
|
180
|
+
> infringement of any patent, then your patent license to the Software ends
|
|
181
|
+
> immediately.
|
|
182
|
+
>
|
|
183
|
+
> ### Redistribution
|
|
184
|
+
>
|
|
185
|
+
> The Terms and Conditions apply to all copies, modifications and derivatives of
|
|
186
|
+
> the Software.
|
|
187
|
+
>
|
|
188
|
+
> If you redistribute any copies, modifications or derivatives of the Software,
|
|
189
|
+
> you must include a copy of or a link to these Terms and Conditions and not
|
|
190
|
+
> remove any copyright notices provided in or with the Software.
|
|
191
|
+
>
|
|
192
|
+
> ### Disclaimer
|
|
193
|
+
>
|
|
194
|
+
> THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EXPRESS OR
|
|
195
|
+
> IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR
|
|
196
|
+
> PURPOSE, MERCHANTABILITY, TITLE OR NON-INFRINGEMENT.
|
|
197
|
+
>
|
|
198
|
+
> IN NO EVENT WILL WE HAVE ANY LIABILITY TO YOU ARISING OUT OF OR RELATED TO THE
|
|
199
|
+
> SOFTWARE, INCLUDING INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES, EVEN
|
|
200
|
+
> IF WE HAVE BEEN INFORMED OF THEIR POSSIBILITY IN ADVANCE.
|
|
201
|
+
>
|
|
202
|
+
> ### Trademarks
|
|
203
|
+
>
|
|
204
|
+
> Except for displaying the License Details and identifying us as the origin of
|
|
205
|
+
> the Software, you have no right under these Terms and Conditions to use our
|
|
206
|
+
> trademarks, trade names, service marks or product names.
|
|
207
|
+
>
|
|
208
|
+
> ## Grant of Future License
|
|
209
|
+
>
|
|
210
|
+
> We hereby irrevocably grant you an additional license to use the Software under
|
|
211
|
+
> the Apache License, Version 2.0 that is effective on the second anniversary of
|
|
212
|
+
> the date we make the Software available. On or after that date, you may use the
|
|
213
|
+
> Software under the Apache License, Version 2.0, in which case the following will
|
|
214
|
+
> apply:
|
|
215
|
+
>
|
|
216
|
+
> Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
217
|
+
> this file except in compliance with the License.
|
|
218
|
+
>
|
|
219
|
+
> You may obtain a copy of the License at
|
|
220
|
+
>
|
|
221
|
+
> http://www.apache.org/licenses/LICENSE-2.0
|
|
222
|
+
>
|
|
223
|
+
> Unless required by applicable law or agreed to in writing, software distributed
|
|
224
|
+
> under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
225
|
+
> CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
226
|
+
> specific language governing permissions and limitations under the License.
|
|
227
|
+
>
|
|
228
|
+
> <!-- DEPENDENCIES -->
|
|
229
|
+
> <!-- /DEPENDENCIES -->
|
|
230
|
+
|
|
231
|
+
<!-- /TYPE_DEPENDENCIES -->
|
package/README.md
CHANGED
|
@@ -1,45 +1,208 @@
|
|
|
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
|
+
<br />
|
|
17
14
|
|
|
18
|
-
|
|
15
|
+
<div align="center">
|
|
19
16
|
|
|
20
|
-
|
|
17
|
+
[![typescript-image][typescript-badge]][typescript-url]
|
|
18
|
+
[![FSL-1.1-Apache-2.0 licence][license-badge]][license]
|
|
19
|
+
[![npm version][npm-version-badge]][npm-version]
|
|
20
|
+
[![npm downloads][npm-downloads-badge]][npm-downloads]
|
|
21
|
+
[![PRs Welcome][prs-welcome-badge]][prs-welcome]
|
|
21
22
|
|
|
22
|
-
|
|
23
|
+
</div>
|
|
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
|
+
---
|
|
28
26
|
|
|
29
|
-
|
|
27
|
+
<div align="center">
|
|
28
|
+
<p>
|
|
29
|
+
<sup>
|
|
30
|
+
Daniel Bannert's open source work is supported by the community on <a href="https://github.com/sponsors/prisis">GitHub Sponsors</a>
|
|
31
|
+
</sup>
|
|
32
|
+
</p>
|
|
33
|
+
</div>
|
|
30
34
|
|
|
31
|
-
|
|
32
|
-
- Contains no executable code
|
|
33
|
-
- Provides no functionality
|
|
34
|
-
- Should not be installed as a dependency
|
|
35
|
-
- Exists only for administrative purposes
|
|
35
|
+
---
|
|
36
36
|
|
|
37
|
-
|
|
37
|
+
Local-first replica runtime and local SQLite mirror for [Lunora](https://lunora.sh).
|
|
38
38
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
- [GitHub Actions OIDC Documentation](https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/about-security-hardening-with-openid-connect)
|
|
39
|
+
**Events layer** — derive state from an append-only event log via reducers, persist
|
|
40
|
+
events through a Durable Object, and subscribe to typed events.
|
|
42
41
|
|
|
43
|
-
|
|
42
|
+
**Local mirror** — a client-side SQLite mirror that maintains a replica
|
|
43
|
+
of server tables by applying typed row-level diffs. Choose the SQLite backend
|
|
44
|
+
that fits your runtime:
|
|
45
|
+
|
|
46
|
+
| Backend | Entry point | Runtime |
|
|
47
|
+
| ----------------------- | ----------------------------------------- | ------------------------------ |
|
|
48
|
+
| sql.js (WASM) | `@lunora/replica/adapters/sqljs` | Browser, Node, Service Workers |
|
|
49
|
+
| better-sqlite3 (native) | `@lunora/replica/adapters/better-sqlite3` | Node.js |
|
|
50
|
+
| @sqlite.org/sqlite-wasm | `@lunora/replica/adapters/sqlite-wasm` | Browser, Node (official WASM) |
|
|
51
|
+
| Custom | Write your own `SqliteAdapter` | Any |
|
|
52
|
+
|
|
53
|
+
## Install
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
pnpm add @lunora/replica
|
|
57
|
+
# Choose one SQLite backend:
|
|
58
|
+
pnpm add sql.js # browser + Node (WASM)
|
|
59
|
+
pnpm add better-sqlite3 # Node.js only (native)
|
|
60
|
+
pnpm add @sqlite.org/sqlite-wasm # browser (official WASM)
|
|
61
|
+
# @lunora/server is optional — needed only for the EventLogDO:
|
|
62
|
+
pnpm add @lunora/server
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Exports
|
|
66
|
+
|
|
67
|
+
| Entry point | Contents |
|
|
68
|
+
| ----------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
69
|
+
| `@lunora/replica` | `EventSource`, `EventEmitter`, `defineEvents`, `defineMaterializer`, `EventLogDO`, `EventLogDOClient`, `SubscriptionManager`, `InMemorySnapshotStore`, `eventsContext`, `LocalMirror`, `EventLog`, `EventsSync`, `subscribeToMirror`, `applyDiff`, `TableDiff` helpers, `createSqlJsAdapter`, `createBetterSqlite3Adapter`, `createSqliteWasmAdapter` |
|
|
70
|
+
| `@lunora/replica/react` | `useLocalQuery` React hook |
|
|
71
|
+
| `@lunora/replica/adapters/sqljs` | `createSqlJsAdapter` (sql.js) |
|
|
72
|
+
| `@lunora/replica/adapters/better-sqlite3` | `createBetterSqlite3Adapter` (better-sqlite3) |
|
|
73
|
+
| `@lunora/replica/adapters/sqlite-wasm` | `createSqliteWasmAdapter` (@sqlite.org/sqlite-wasm) |
|
|
74
|
+
|
|
75
|
+
## EventSource
|
|
76
|
+
|
|
77
|
+
The core runtime — a typed reducer that derives state from an append-only log:
|
|
78
|
+
|
|
79
|
+
```ts
|
|
80
|
+
import { EventSource } from "@lunora/replica";
|
|
81
|
+
|
|
82
|
+
interface MyState {
|
|
83
|
+
count: number;
|
|
84
|
+
items: string[];
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const source = new EventSource<MyState>({ count: 0, items: [] }, (state, entry) => {
|
|
88
|
+
switch (entry.type) {
|
|
89
|
+
case "item:added":
|
|
90
|
+
return { count: state.count + 1, items: [...state.items, entry.payload as string] };
|
|
91
|
+
default:
|
|
92
|
+
return state;
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
source.applyEvent("item:added", "hello");
|
|
97
|
+
console.log(source.state.count); // 1
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## LocalMirror
|
|
101
|
+
|
|
102
|
+
An SQLite mirror that applies typed row-level diffs. Create it with your
|
|
103
|
+
chosen backend:
|
|
104
|
+
|
|
105
|
+
```ts
|
|
106
|
+
import initSqlJs from "sql.js";
|
|
107
|
+
import { createSqlJsAdapter, LocalMirror } from "@lunora/replica";
|
|
108
|
+
|
|
109
|
+
const SQL = await initSqlJs();
|
|
110
|
+
const adapter = createSqlJsAdapter(new SQL.Database());
|
|
111
|
+
const mirror = new LocalMirror({ db: adapter });
|
|
112
|
+
|
|
113
|
+
mirror.applyDiff({
|
|
114
|
+
table: "todos",
|
|
115
|
+
schema: "1.0",
|
|
116
|
+
changes: [{ type: "insert", data: { id: "1", title: "hello", done: false } }],
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
const todos = mirror.query<{ id: string; title: string }>("SELECT id, title FROM todos");
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
For `better-sqlite3`:
|
|
123
|
+
|
|
124
|
+
```ts
|
|
125
|
+
import Database from "better-sqlite3";
|
|
126
|
+
import { createBetterSqlite3Adapter, LocalMirror } from "@lunora/replica";
|
|
127
|
+
|
|
128
|
+
const adapter = createBetterSqlite3Adapter(new Database(":memory:"));
|
|
129
|
+
const mirror = new LocalMirror({ db: adapter });
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### EventsSync
|
|
133
|
+
|
|
134
|
+
Periodically polls a server-side event log, replays events through a state
|
|
135
|
+
machine, and pushes the resulting diffs to the mirror:
|
|
136
|
+
|
|
137
|
+
```ts
|
|
138
|
+
import { EventsSync } from "@lunora/replica";
|
|
139
|
+
|
|
140
|
+
const sync = new EventsSync({
|
|
141
|
+
fetchEventsSince: (seq) => eventLogClient.getSince(seq),
|
|
142
|
+
applyEvents: (events) => {
|
|
143
|
+
/* feed events into your state machine */
|
|
144
|
+
},
|
|
145
|
+
getTableDiffs: () => {
|
|
146
|
+
/* compare state before/after → TableDiff[] */
|
|
147
|
+
},
|
|
148
|
+
mirror,
|
|
149
|
+
pollInterval: 3000,
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
sync.start(); // begin polling
|
|
153
|
+
// sync.sync(); // one-shot sync
|
|
154
|
+
// sync.stop(); // halt polling
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
See the [EventsSync JSDoc](src/sync-events.ts) for full API details.
|
|
158
|
+
|
|
159
|
+
### useLocalQuery (React)
|
|
160
|
+
|
|
161
|
+
Live-updating hook that re-queries the mirror whenever a diff is applied.
|
|
162
|
+
Returns `undefined` when the query fails (e.g. table doesn't exist yet),
|
|
163
|
+
and the result rows otherwise.
|
|
164
|
+
|
|
165
|
+
```tsx
|
|
166
|
+
import { useLocalQuery } from "@lunora/replica/react";
|
|
167
|
+
|
|
168
|
+
function TodoList() {
|
|
169
|
+
const todos = useLocalQuery<{ id: string; title: string; done: boolean }>(mirror, "SELECT id, title, done FROM todos WHERE done = ?", [false]);
|
|
170
|
+
|
|
171
|
+
if (todos === undefined) {
|
|
172
|
+
return <p>Waiting for data…</p>;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return (
|
|
176
|
+
<ul>
|
|
177
|
+
{todos.map((t) => (
|
|
178
|
+
<li key={t.id}>{t.title}</li>
|
|
179
|
+
))}
|
|
180
|
+
</ul>
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
The hook uses `useSyncExternalStore` under the hood, so it integrates with
|
|
186
|
+
React 18+ concurrent features and Suspense-based frameworks (Next.js, Remix).
|
|
187
|
+
|
|
188
|
+
## EventLogDO
|
|
189
|
+
|
|
190
|
+
A Durable Object that persists the event log to DO SQLite storage:
|
|
191
|
+
|
|
192
|
+
```ts
|
|
193
|
+
export { EventLogDO } from "@lunora/replica";
|
|
194
|
+
|
|
195
|
+
const client = new EventLogDOClient({ namespace: "my-app" });
|
|
196
|
+
await client.append({ type: "order:placed", payload: { orderId: "123" } });
|
|
197
|
+
const events = await client.query({ type: "order:placed", limit: 10 });
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
## Custom adapters
|
|
201
|
+
|
|
202
|
+
See the [custom adapter guide](docs/guides/custom-adapter.mdx) for writing
|
|
203
|
+
`SqliteAdapter` implementations for `expo-sqlite`, `bun:sqlite`, or any other
|
|
204
|
+
SQLite runtime.
|
|
205
|
+
|
|
206
|
+
## License
|
|
44
207
|
|
|
45
|
-
|
|
208
|
+
[FSL-1.1-Apache-2.0](./LICENSE.md)
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { S as SqliteAdapter } from "../packem_shared/types.d-VfJ76cK4.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
|
+
*/
|
|
15
|
+
declare const createBetterSqlite3Adapter: (database: {
|
|
16
|
+
close: () => void;
|
|
17
|
+
exec: (sql: string) => void;
|
|
18
|
+
prepare: (sql: string) => {
|
|
19
|
+
all: (params?: unknown[]) => unknown[];
|
|
20
|
+
get: (params?: unknown[]) => unknown;
|
|
21
|
+
run: (params?: unknown[]) => {
|
|
22
|
+
lastInsertRowid: number | bigint;
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
transaction: (function_: () => void) => () => void;
|
|
26
|
+
}) => SqliteAdapter;
|
|
27
|
+
export { createBetterSqlite3Adapter };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { S as SqliteAdapter } from "../packem_shared/types.d-VfJ76cK4.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
|
+
*/
|
|
15
|
+
declare const createBetterSqlite3Adapter: (database: {
|
|
16
|
+
close: () => void;
|
|
17
|
+
exec: (sql: string) => void;
|
|
18
|
+
prepare: (sql: string) => {
|
|
19
|
+
all: (params?: unknown[]) => unknown[];
|
|
20
|
+
get: (params?: unknown[]) => unknown;
|
|
21
|
+
run: (params?: unknown[]) => {
|
|
22
|
+
lastInsertRowid: number | bigint;
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
transaction: (function_: () => void) => () => void;
|
|
26
|
+
}) => SqliteAdapter;
|
|
27
|
+
export { createBetterSqlite3Adapter };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
const createBetterSqlite3Adapter = (database) => {
|
|
2
|
+
return {
|
|
3
|
+
exec(sql, params) {
|
|
4
|
+
if (params && params.length > 0) {
|
|
5
|
+
database.prepare(sql).run([...params]);
|
|
6
|
+
} else {
|
|
7
|
+
database.exec(sql);
|
|
8
|
+
}
|
|
9
|
+
},
|
|
10
|
+
query(sql, params) {
|
|
11
|
+
const stmt = database.prepare(sql);
|
|
12
|
+
const rows = params && params.length > 0 ? stmt.all([...params]) : stmt.all();
|
|
13
|
+
return rows;
|
|
14
|
+
},
|
|
15
|
+
transaction(function_) {
|
|
16
|
+
const wrapped = database.transaction(function_);
|
|
17
|
+
wrapped();
|
|
18
|
+
},
|
|
19
|
+
lastInsertRowId() {
|
|
20
|
+
const row = database.prepare("SELECT last_insert_rowid() AS id").get();
|
|
21
|
+
return Number(row?.id ?? -1);
|
|
22
|
+
},
|
|
23
|
+
close() {
|
|
24
|
+
database.close();
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export { createBetterSqlite3Adapter };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { S as SqliteAdapter } from "../packem_shared/types.d-VfJ76cK4.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
|
+
* @param database An already-initialised `sqlite3.oo1.DB` instance.
|
|
10
|
+
* @param database.close Tear down the database connection.
|
|
11
|
+
* @param database.exec Execute SQL with optional bind params and return rows
|
|
12
|
+
* as `{ columns, values }` result objects.
|
|
13
|
+
*/
|
|
14
|
+
declare const createSqliteWasmAdapter: (database: {
|
|
15
|
+
close: () => void;
|
|
16
|
+
exec: (sql: string, options?: {
|
|
17
|
+
bind?: unknown[];
|
|
18
|
+
returnValue?: "resultRows" | "simple";
|
|
19
|
+
rowMode?: "array" | "object";
|
|
20
|
+
}) => {
|
|
21
|
+
columns: string[];
|
|
22
|
+
values: unknown[][];
|
|
23
|
+
}[];
|
|
24
|
+
}) => SqliteAdapter;
|
|
25
|
+
export { createSqliteWasmAdapter };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { S as SqliteAdapter } from "../packem_shared/types.d-VfJ76cK4.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
|
+
* @param database An already-initialised `sqlite3.oo1.DB` instance.
|
|
10
|
+
* @param database.close Tear down the database connection.
|
|
11
|
+
* @param database.exec Execute SQL with optional bind params and return rows
|
|
12
|
+
* as `{ columns, values }` result objects.
|
|
13
|
+
*/
|
|
14
|
+
declare const createSqliteWasmAdapter: (database: {
|
|
15
|
+
close: () => void;
|
|
16
|
+
exec: (sql: string, options?: {
|
|
17
|
+
bind?: unknown[];
|
|
18
|
+
returnValue?: "resultRows" | "simple";
|
|
19
|
+
rowMode?: "array" | "object";
|
|
20
|
+
}) => {
|
|
21
|
+
columns: string[];
|
|
22
|
+
values: unknown[][];
|
|
23
|
+
}[];
|
|
24
|
+
}) => SqliteAdapter;
|
|
25
|
+
export { createSqliteWasmAdapter };
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
const createSqliteWasmAdapter = (database) => {
|
|
2
|
+
return {
|
|
3
|
+
exec(sql, params) {
|
|
4
|
+
if (params && params.length > 0) {
|
|
5
|
+
database.exec(sql, { bind: [...params] });
|
|
6
|
+
} else {
|
|
7
|
+
database.exec(sql);
|
|
8
|
+
}
|
|
9
|
+
},
|
|
10
|
+
query(sql, params) {
|
|
11
|
+
const options = params && params.length > 0 ? { bind: [...params], returnValue: "resultRows" } : { returnValue: "resultRows" };
|
|
12
|
+
const result = database.exec(sql, options);
|
|
13
|
+
const first = result[0];
|
|
14
|
+
if (!first) {
|
|
15
|
+
return [];
|
|
16
|
+
}
|
|
17
|
+
const colNames = first.columns;
|
|
18
|
+
const rows = [];
|
|
19
|
+
for (const row of first.values) {
|
|
20
|
+
const object = {};
|
|
21
|
+
for (const [i, column] of colNames.entries()) {
|
|
22
|
+
object[column] = row[i];
|
|
23
|
+
}
|
|
24
|
+
rows.push(object);
|
|
25
|
+
}
|
|
26
|
+
return rows;
|
|
27
|
+
},
|
|
28
|
+
transaction(function_) {
|
|
29
|
+
database.exec("BEGIN");
|
|
30
|
+
try {
|
|
31
|
+
function_();
|
|
32
|
+
database.exec("COMMIT");
|
|
33
|
+
} catch (error) {
|
|
34
|
+
database.exec("ROLLBACK");
|
|
35
|
+
throw error;
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
lastInsertRowId() {
|
|
39
|
+
const result = database.exec("SELECT last_insert_rowid() AS id", { returnValue: "resultRows" });
|
|
40
|
+
const firstRow = result[0];
|
|
41
|
+
if (result.length === 0 || !firstRow || firstRow.values.length === 0) {
|
|
42
|
+
return -1;
|
|
43
|
+
}
|
|
44
|
+
const value = firstRow.values[0];
|
|
45
|
+
if (!value || value.length === 0) {
|
|
46
|
+
return -1;
|
|
47
|
+
}
|
|
48
|
+
return Number(value[0]);
|
|
49
|
+
},
|
|
50
|
+
close() {
|
|
51
|
+
database.close();
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
export { createSqliteWasmAdapter };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { S as SqliteAdapter } from "../packem_shared/types.d-VfJ76cK4.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
|
+
*/
|
|
11
|
+
declare const createSqlJsAdapter: (database: {
|
|
12
|
+
close: () => void;
|
|
13
|
+
exec: (sql: string, params?: unknown[]) => {
|
|
14
|
+
columns: string[];
|
|
15
|
+
values: unknown[][];
|
|
16
|
+
}[];
|
|
17
|
+
run: (sql: string, params?: unknown[]) => void;
|
|
18
|
+
}) => SqliteAdapter;
|
|
19
|
+
export { createSqlJsAdapter };
|