@livestore/livestore 0.5.0-dev.1 → 0.5.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/dist/.tsbuildinfo +1 -1
- package/dist/QueryCache.d.ts.map +1 -1
- package/dist/QueryCache.js +1 -2
- package/dist/QueryCache.js.map +1 -1
- package/dist/ReactiveStateSqliteDb.d.ts +9 -0
- package/dist/ReactiveStateSqliteDb.d.ts.map +1 -0
- package/dist/ReactiveStateSqliteDb.js +6 -0
- package/dist/ReactiveStateSqliteDb.js.map +1 -0
- package/dist/SqliteDbWrapper.d.ts +11 -9
- package/dist/SqliteDbWrapper.d.ts.map +1 -1
- package/dist/SqliteDbWrapper.js +65 -14
- package/dist/SqliteDbWrapper.js.map +1 -1
- package/dist/SqliteDbWrapper.test.js +18 -0
- package/dist/SqliteDbWrapper.test.js.map +1 -1
- package/dist/live-queries/db-query.test.js +4 -0
- package/dist/live-queries/db-query.test.js.map +1 -1
- package/dist/store/create-store.d.ts.map +1 -1
- package/dist/store/create-store.js +47 -17
- package/dist/store/create-store.js.map +1 -1
- package/dist/store/store-types.d.ts +2 -2
- package/dist/store/store-types.d.ts.map +1 -1
- package/dist/store/store-types.js.map +1 -1
- package/dist/store/store.d.ts +2 -2
- package/dist/store/store.d.ts.map +1 -1
- package/dist/store/store.js +32 -34
- package/dist/store/store.js.map +1 -1
- package/package.json +12 -12
- package/src/QueryCache.ts +1 -3
- package/src/ReactiveStateSqliteDb.ts +10 -0
- package/src/SqliteDbWrapper.test.ts +21 -0
- package/src/SqliteDbWrapper.ts +81 -19
- package/src/live-queries/__snapshots__/db-query.test.ts.snap +223 -278
- package/src/live-queries/db-query.test.ts +4 -0
- package/src/store/create-store.ts +68 -22
- package/src/store/store-types.ts +11 -2
- package/src/store/store.ts +101 -102
package/src/SqliteDbWrapper.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as otel from '@opentelemetry/api'
|
|
2
2
|
|
|
3
3
|
import {
|
|
4
4
|
BoundArray,
|
|
@@ -40,6 +40,11 @@ export class SqliteDbWrapper implements SqliteDb {
|
|
|
40
40
|
private otelRootSpanContext: otel.Context
|
|
41
41
|
private tablesUsedStmt
|
|
42
42
|
public debugInfo: MutableDebugInfo = emptyDebugInfo()
|
|
43
|
+
/**
|
|
44
|
+
* The same database for LiveStore's SQLite services (`StateSqliteDb`). They trace their own statements, so this view
|
|
45
|
+
* skips the wrapper's spans. Writes and rollbacks still go through the query cache so cached reads stay coherent.
|
|
46
|
+
*/
|
|
47
|
+
readonly serviceDb: SqliteDb
|
|
43
48
|
|
|
44
49
|
constructor({
|
|
45
50
|
db,
|
|
@@ -61,6 +66,29 @@ export class SqliteDbWrapper implements SqliteDb {
|
|
|
61
66
|
|
|
62
67
|
this.cachedStmts.onEvict = (_queryStr, stmt) => stmt.finalize()
|
|
63
68
|
|
|
69
|
+
this.serviceDb = {
|
|
70
|
+
_tag: 'SqliteDb',
|
|
71
|
+
get metadata() {
|
|
72
|
+
return db.metadata
|
|
73
|
+
},
|
|
74
|
+
get debug() {
|
|
75
|
+
return db.debug
|
|
76
|
+
},
|
|
77
|
+
prepare: (queryStr) => this.prepare(queryStr),
|
|
78
|
+
execute: SqliteDbHelper.makeExecute((queryStr, bindValues) =>
|
|
79
|
+
this.cachedExecute(queryStr, bindValues, { traced: false }),
|
|
80
|
+
),
|
|
81
|
+
select: SqliteDbHelper.makeSelect((queryStr, bindValues) =>
|
|
82
|
+
this.cachedSelect(queryStr, bindValues, { traced: false }),
|
|
83
|
+
),
|
|
84
|
+
export: () => this.export(),
|
|
85
|
+
import: (data) => this.import(data),
|
|
86
|
+
close: () => this.close(),
|
|
87
|
+
destroy: () => this.destroy(),
|
|
88
|
+
session: () => this.session(),
|
|
89
|
+
makeChangeset: (data) => this.makeChangeset(data),
|
|
90
|
+
}
|
|
91
|
+
|
|
64
92
|
configureSQLite(this)
|
|
65
93
|
}
|
|
66
94
|
get debug() {
|
|
@@ -85,7 +113,19 @@ export class SqliteDbWrapper implements SqliteDb {
|
|
|
85
113
|
return this.db.session()
|
|
86
114
|
}
|
|
87
115
|
makeChangeset(data: Uint8Array<ArrayBuffer>): SqliteDbChangeset {
|
|
88
|
-
|
|
116
|
+
const wrap = (changeset: SqliteDbChangeset): SqliteDbChangeset => ({
|
|
117
|
+
invert: () => wrap(changeset.invert()),
|
|
118
|
+
apply: () => {
|
|
119
|
+
try {
|
|
120
|
+
changeset.apply()
|
|
121
|
+
} finally {
|
|
122
|
+
// Journal rollback bypasses cachedExecute. SQLite doesn't report the touched tables here, so cached
|
|
123
|
+
// results must all be discarded, including when applying a changeset fails partway through.
|
|
124
|
+
this.resultCache = new QueryCache()
|
|
125
|
+
}
|
|
126
|
+
},
|
|
127
|
+
})
|
|
128
|
+
return wrap(this.db.makeChangeset(data))
|
|
89
129
|
}
|
|
90
130
|
|
|
91
131
|
txn<TRes>(callback: () => TRes): TRes {
|
|
@@ -111,7 +151,7 @@ export class SqliteDbWrapper implements SqliteDb {
|
|
|
111
151
|
|
|
112
152
|
withChangeset<TRes>(callback: () => TRes): {
|
|
113
153
|
result: TRes
|
|
114
|
-
changeset:
|
|
154
|
+
changeset: Uint8Array<ArrayBuffer> | null
|
|
115
155
|
} {
|
|
116
156
|
const session = this.db.session()
|
|
117
157
|
const result = callback()
|
|
@@ -121,16 +161,10 @@ export class SqliteDbWrapper implements SqliteDb {
|
|
|
121
161
|
|
|
122
162
|
return {
|
|
123
163
|
result,
|
|
124
|
-
changeset
|
|
125
|
-
changeset !== undefined ? { _tag: 'sessionChangeset', data: changeset, debug: null } : { _tag: 'no-op' },
|
|
164
|
+
changeset,
|
|
126
165
|
}
|
|
127
166
|
}
|
|
128
167
|
|
|
129
|
-
rollback(changeset: Uint8Array<ArrayBuffer>) {
|
|
130
|
-
const invertedChangeset = this.db.makeChangeset(changeset).invert()
|
|
131
|
-
invertedChangeset.apply()
|
|
132
|
-
}
|
|
133
|
-
|
|
134
168
|
getTablesUsed(query: string) {
|
|
135
169
|
// It seems that SQLite doesn't properly handle `DELETE FROM SOME_TABLE` queries without a WHERE clause
|
|
136
170
|
// So we need to handle these queries separately
|
|
@@ -165,13 +199,15 @@ export class SqliteDbWrapper implements SqliteDb {
|
|
|
165
199
|
options?: {
|
|
166
200
|
hasNoEffects?: boolean
|
|
167
201
|
otelContext?: otel.Context
|
|
202
|
+
traced?: boolean
|
|
168
203
|
writeTables?: ReadonlySet<string>
|
|
169
204
|
onRowsChanged?: (rowsChanged: number) => void
|
|
170
205
|
},
|
|
171
206
|
): { durationMs: number } {
|
|
172
207
|
// console.debug('in-memory-db:execute', query, bindValues)
|
|
173
208
|
|
|
174
|
-
return this.
|
|
209
|
+
return this.withSpan(
|
|
210
|
+
options?.traced !== false,
|
|
175
211
|
'livestore.in-memory-db:execute',
|
|
176
212
|
// TODO truncate query string
|
|
177
213
|
{ attributes: { 'sql.query': queryStr } },
|
|
@@ -180,15 +216,24 @@ export class SqliteDbWrapper implements SqliteDb {
|
|
|
180
216
|
const startTimePerfNow = performance.now()
|
|
181
217
|
|
|
182
218
|
try {
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
this.
|
|
219
|
+
if (isSavepointStatement(queryStr) === true) {
|
|
220
|
+
// Savepoint statements carry a unique name per use (see `SqliteDbHelper.withSavepoint`). Caching them
|
|
221
|
+
// would fill the bounded statement cache with single-use entries and evict the reusable statements.
|
|
222
|
+
this.db.execute(queryStr, bindValues)
|
|
223
|
+
} else {
|
|
224
|
+
let stmt = this.cachedStmts.get(queryStr)
|
|
225
|
+
if (stmt === undefined) {
|
|
226
|
+
stmt = this.db.prepare(queryStr)
|
|
227
|
+
this.cachedStmts.set(queryStr, stmt)
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
stmt.execute(bindValues)
|
|
187
231
|
}
|
|
188
232
|
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
233
|
+
if (/^\s*rollback\b/i.test(queryStr) === true) {
|
|
234
|
+
// A savepoint rollback can undo writes whose intermediate results were read by a materializer.
|
|
235
|
+
this.resultCache = new QueryCache()
|
|
236
|
+
} else if (options?.hasNoEffects !== true && this.resultCache.ignoreQuery(queryStr) === false) {
|
|
192
237
|
// TODO use write tables instead
|
|
193
238
|
// check what queries actually end up here.
|
|
194
239
|
this.resultCache.invalidate(options?.writeTables ?? this.getTablesUsed(queryStr))
|
|
@@ -237,13 +282,15 @@ export class SqliteDbWrapper implements SqliteDb {
|
|
|
237
282
|
queriedTables?: ReadonlySet<string>
|
|
238
283
|
skipCache?: boolean
|
|
239
284
|
otelContext?: otel.Context
|
|
285
|
+
traced?: boolean
|
|
240
286
|
},
|
|
241
287
|
): ReadonlyArray<T> {
|
|
242
288
|
const { queriedTables, skipCache = false, otelContext } = options ?? {}
|
|
243
289
|
|
|
244
290
|
// console.debug('in-memory-db:select', query, bindValues)
|
|
245
291
|
|
|
246
|
-
return this.
|
|
292
|
+
return this.withSpan(
|
|
293
|
+
options?.traced !== false,
|
|
247
294
|
'sql-in-memory-select',
|
|
248
295
|
{},
|
|
249
296
|
otelContext ?? this.otelRootSpanContext,
|
|
@@ -303,6 +350,19 @@ export class SqliteDbWrapper implements SqliteDb {
|
|
|
303
350
|
)
|
|
304
351
|
}
|
|
305
352
|
|
|
353
|
+
/** Runs `fn` in a span, or with a non-recording span when the caller traces the statement itself. */
|
|
354
|
+
private withSpan<T>(
|
|
355
|
+
traced: boolean,
|
|
356
|
+
name: string,
|
|
357
|
+
options: otel.SpanOptions,
|
|
358
|
+
context: otel.Context,
|
|
359
|
+
fn: (span: otel.Span) => T,
|
|
360
|
+
): T {
|
|
361
|
+
return traced === true
|
|
362
|
+
? this.otelTracer.startActiveSpan(name, options, context, fn)
|
|
363
|
+
: fn(otel.trace.wrapSpanContext(otel.INVALID_SPAN_CONTEXT))
|
|
364
|
+
}
|
|
365
|
+
|
|
306
366
|
export() {
|
|
307
367
|
// Clear statement cache because exporting frees statements
|
|
308
368
|
for (const key of this.cachedStmts.keys()) {
|
|
@@ -331,3 +391,5 @@ const tryGetTableNameFromPlainDeleteQuery = (query: string) => {
|
|
|
331
391
|
const [_, tableName] = query.trim().match(/^delete\s+from\s+(\w+)$/i) ?? []
|
|
332
392
|
return tableName
|
|
333
393
|
}
|
|
394
|
+
|
|
395
|
+
const isSavepointStatement = (query: string) => /^\s*(?:savepoint|release|rollback\s+to)\b/i.test(query)
|