@chipmobilesdk/rn-local-db 0.2.0 → 0.3.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.
Files changed (84) hide show
  1. package/CHANGELOG.md +191 -102
  2. package/README.md +728 -728
  3. package/package.json +62 -62
  4. package/skills/sdk-rn-local-db/SKILL.md +0 -1
  5. package/src/adapter/budgets.ts +67 -67
  6. package/src/adapter/collection.ts +303 -298
  7. package/src/adapter/deletion.ts +252 -252
  8. package/src/adapter/index.ts +64 -68
  9. package/src/adapter/lifecycle.ts +517 -520
  10. package/src/adapter/transaction.ts +71 -71
  11. package/src/artifacts/availability.ts +67 -67
  12. package/src/artifacts/cascade.ts +114 -114
  13. package/src/artifacts/paths.ts +29 -29
  14. package/src/artifacts/reconcile.ts +63 -63
  15. package/src/artifacts/store.ts +334 -345
  16. package/src/contract/adapter.ts +76 -78
  17. package/src/contract/attachment.ts +75 -78
  18. package/src/contract/backup.ts +28 -28
  19. package/src/contract/config.ts +54 -54
  20. package/src/contract/encryption.ts +34 -34
  21. package/src/contract/errors.ts +76 -76
  22. package/src/contract/migration.ts +42 -42
  23. package/src/contract/query.ts +81 -81
  24. package/src/contract/record.ts +46 -51
  25. package/src/contract/schema.ts +36 -36
  26. package/src/contract/scope.ts +36 -36
  27. package/src/contract/transaction.ts +52 -54
  28. package/src/crypto/chunked-cipher.ts +153 -150
  29. package/src/crypto/derive.ts +64 -64
  30. package/src/crypto/key-provider.ts +78 -78
  31. package/src/crypto/provider.ts +75 -81
  32. package/src/diagnostics/dev-log.ts +132 -108
  33. package/src/diagnostics/errors.ts +63 -66
  34. package/src/diagnostics/faults.ts +58 -64
  35. package/src/diagnostics/map-native.ts +108 -106
  36. package/src/diagnostics/redact.ts +119 -141
  37. package/src/index.ts +80 -110
  38. package/src/migration/executor.ts +132 -136
  39. package/src/migration/ledger.ts +95 -97
  40. package/src/migration/validate-chain.ts +65 -65
  41. package/src/platform/backup-android-verify.ts +88 -88
  42. package/src/platform/backup-ios.ts +49 -48
  43. package/src/platform/bytes.ts +157 -144
  44. package/src/platform/fs.ts +90 -89
  45. package/src/platform/location.ts +135 -135
  46. package/src/query/budgets.ts +54 -54
  47. package/src/query/compare.ts +91 -91
  48. package/src/query/cursor.ts +100 -100
  49. package/src/query/filter.ts +82 -82
  50. package/src/query/paginate.ts +51 -51
  51. package/src/query/sort.ts +55 -56
  52. package/src/query/validate.ts +171 -171
  53. package/src/schema/declare.ts +42 -45
  54. package/src/schema/validate-config.ts +158 -158
  55. package/src/scope/hash.ts +131 -134
  56. package/src/scope/normalize.ts +83 -90
  57. package/src/scope/validate.ts +83 -83
  58. package/src/sqlite/adapter.ts +26 -26
  59. package/src/sqlite/batch.ts +242 -242
  60. package/src/sqlite/catalog.ts +105 -108
  61. package/src/sqlite/connection.ts +227 -227
  62. package/src/sqlite/index-coverage.ts +54 -56
  63. package/src/sqlite/native.ts +84 -83
  64. package/src/sqlite/row-codec.ts +217 -221
  65. package/src/sqlite/sql-builder.ts +244 -244
  66. package/src/sqlite/transaction-queue.ts +87 -87
  67. package/src/sqlite/transactions.ts +33 -33
  68. package/src/sqlite/values.ts +24 -24
  69. package/src/testing/conformance/attachments.ts +139 -136
  70. package/src/testing/conformance/encryption.ts +151 -150
  71. package/src/testing/conformance/index.ts +161 -161
  72. package/src/testing/conformance/lifecycle.ts +99 -99
  73. package/src/testing/conformance/migrations.ts +177 -177
  74. package/src/testing/conformance/queries.ts +204 -167
  75. package/src/testing/conformance/records.ts +169 -169
  76. package/src/testing/conformance/scopes.ts +86 -88
  77. package/src/testing/conformance/transactions.ts +149 -149
  78. package/src/testing/faults.ts +21 -21
  79. package/src/testing/index.ts +17 -21
  80. package/src/testing/memory-adapter/collection.ts +214 -219
  81. package/src/testing/memory-adapter/index.ts +511 -492
  82. package/src/testing/memory-adapter/migrations.ts +117 -119
  83. package/src/testing/memory-adapter/reset.ts +50 -50
  84. package/src/testing/memory-adapter/store.ts +128 -134
@@ -1,492 +1,511 @@
1
- /**
2
- * The in-memory adapter.
3
- *
4
- * Not a convenience: op-sqlite is native and unreachable from Jest, so this
5
- * is the only way the package's own guarantees are testable in CI
6
- * (research.md R8). Documented divergences are listed in the package README.
7
- */
8
-
9
- import { createTransactionQueue } from '../../sqlite/transaction-queue';
10
- import type {
11
- AttachmentInput,
12
- AttachmentReference,
13
- AttachmentOwner,
14
- ByteRange,
15
- DeletionOutcome,
16
- ReconciliationReport,
17
- } from '../../contract/attachment';
18
- import type { EffectivePosture } from '../../contract/backup';
19
- import type { DatabaseConfig, DatabaseTargetConfig, ScopeTargetConfig } from '../../contract/config';
20
- import { DiagnosticCodes } from '../../contract/errors';
21
- import type { IndexCoverageReport, Query } from '../../contract/query';
22
- import type { StorableRecordData } from '../../contract/record';
23
- import type { CollectionSchema } from '../../contract/schema';
24
- import type { BatchOperation, BatchOutcome, CollectionOps, Transaction } from '../../contract/transaction';
25
- import type { Attachments, DatabaseHandle, StorageAdapter } from '../../contract/adapter';
26
- import { acquireKey } from '../../crypto/key-provider';
27
- import { instrument } from '../../diagnostics/dev-log';
28
- import { raise } from '../../diagnostics/errors';
29
- import { assertValidAttachmentId } from '../../platform/location';
30
- import { assertBatchWithinBudget, assertArtifactWithinBudget } from '../../query/budgets';
31
- import { validateConfig } from '../../schema/validate-config';
32
- import { resolveScope } from '../../scope/validate';
33
- import { runMigrations } from './migrations';
34
- import { createMemoryCollection } from './collection';
35
- import {
36
- collectionMap,
37
- dropDatabase,
38
- dropScope,
39
- existingScopeStore,
40
- scopeStoreFor,
41
- type MemoryScopeStore,
42
- } from './store';
43
- import { throwIfFaulted } from '../faults';
44
-
45
- export type MemoryAdapterOptions = {
46
- /** Fixed clock, so timestamp assertions are deterministic. */
47
- now?: () => number;
48
- };
49
-
50
- function schemaIndex(collections: CollectionSchema[]): Map<string, CollectionSchema> {
51
- return new Map(collections.map(collection => [collection.name, collection]));
52
- }
53
-
54
- function cloneCollections(
55
- store: MemoryScopeStore,
56
- ): Map<string, Map<string, { id: string; payload: string; createdAt?: number; updatedAt?: number; deletedAt?: number | null }>> {
57
- const copy = new Map<string, Map<string, never>>();
58
- for (const [name, rows] of store.collections) {
59
- copy.set(name, new Map([...rows].map(([id, row]) => [id, { ...row } as never])));
60
- }
61
- return copy as never;
62
- }
63
-
64
- export function createMemoryAdapter(options: MemoryAdapterOptions = {}): StorageAdapter {
65
- const now = options.now ?? (() => Date.now());
66
-
67
- async function open(config: DatabaseConfig): Promise<DatabaseHandle> {
68
- validateConfig(config);
69
- throwIfFaulted('open');
70
-
71
- const scope = resolveScope(config.scope);
72
- const existing = existingScopeStore(config.name, scope.key);
73
- if (existing?.deletionState === 'deleting') {
74
- // A deletion that was interrupted must never look complete.
75
- raise(DiagnosticCodes.SCOPE_DELETED, {
76
- message: 'scope deletion was interrupted',
77
- detail: { scopeKey: scope.key },
78
- });
79
- }
80
-
81
- const store = scopeStoreFor(config.name, scope.key);
82
- const encryptionRequested = config.encryption?.enabled === true;
83
-
84
- if (store.schemaVersion > 0 && store.encrypted !== encryptionRequested) {
85
- raise(DiagnosticCodes.ENCRYPTION_MISMATCH, {
86
- message: 'encryption posture does not match the stored database',
87
- });
88
- }
89
- if (encryptionRequested) {
90
- // The same acquisition path the on-device adapter uses, so a locked
91
- // keychain and a provider returning nothing stay as distinguishable
92
- // here as they are on a phone (SC-020b). Released immediately: the
93
- // in-memory store has nothing to encrypt with it.
94
- const key = await acquireKey(config.encryption!, scope);
95
- key.release();
96
- }
97
- store.encrypted = encryptionRequested;
98
- store.backupIncluded = config.backup?.includeInPlatformBackup === true;
99
-
100
- const schemas = schemaIndex(config.collections);
101
- for (const collection of config.collections) {
102
- collectionMap(store, collection.name);
103
- }
104
-
105
- await runMigrations(store, config, now);
106
-
107
- let closed = false;
108
- const assertOpen = (): void => {
109
- if (closed) {
110
- raise(DiagnosticCodes.CLOSED_HANDLE, { message: 'handle is closed' });
111
- }
112
- if (store.deletionState === 'deleting') {
113
- raise(DiagnosticCodes.SCOPE_DELETED, {
114
- message: 'scope was deleted beneath this handle',
115
- detail: { scopeKey: scope.key },
116
- });
117
- }
118
- };
119
-
120
- /**
121
- * The same isolation the SQLite connection gives: a transaction holds the
122
- * handle until it settles, and work through the handle meanwhile waits
123
- * rather than landing inside the transaction's snapshot and vanishing
124
- * with its rollback. Only what the transaction hands out runs inside it.
125
- */
126
- const queue = createTransactionQueue();
127
- const queued = <O extends object>(ops: O): O =>
128
- Object.fromEntries(
129
- Object.entries(ops).map(([key, value]) => [
130
- key,
131
- typeof value === 'function'
132
- ? (...args: unknown[]) =>
133
- queue.run(() => (value as (...a: unknown[]) => Promise<unknown>)(...args))
134
- : value,
135
- ]),
136
- ) as O;
137
-
138
- /** Artifact deletions held back until the transaction that caused them commits. */
139
- type Deferred = { add(id: string): void };
140
-
141
- /** FR-070 for the memory adapter, with the SQLite adapter's ordering and reporting. */
142
- const cascadeFor = (collection: string, id: string, deferred?: Deferred): DeletionOutcome => {
143
- const removedArtifacts: string[] = [];
144
- for (const reference of [...store.attachments.values()]) {
145
- if (reference.owner?.collection !== collection || reference.owner?.id !== id) continue;
146
- store.attachments.delete(reference.id);
147
- if (deferred) {
148
- deferred.add(reference.id);
149
- } else {
150
- store.artifacts.delete(reference.id);
151
- }
152
- removedArtifacts.push(reference.id);
153
- }
154
- return { removedRecords: 0, removedArtifacts, orphanedReferences: [], failedRemovals: [] };
155
- };
156
-
157
- const rawCollectionFor = <T extends StorableRecordData>(
158
- name: string,
159
- deferred?: Deferred,
160
- ): CollectionOps<T> => {
161
- assertOpen();
162
- const schema = schemas.get(name);
163
- if (!schema) {
164
- raise(DiagnosticCodes.CONFIG_INVALID, {
165
- message: 'collection is not declared',
166
- detail: { collection: name },
167
- });
168
- }
169
- const ops = instrument(
170
- name,
171
- 'memory',
172
- createMemoryCollection<T>({ store, schema, now, assertOpen }) as unknown as Record<
173
- string,
174
- (...args: never[]) => unknown
175
- >,
176
- ) as unknown as CollectionOps<T>;
177
- if (config.cascadeAttachments !== true) {
178
- return ops;
179
- }
180
- return {
181
- ...ops,
182
- async delete(id: string) {
183
- const outcome = await ops.delete(id);
184
- return { ...outcome, cascade: cascadeFor(name, id, deferred) };
185
- },
186
- };
187
- };
188
-
189
- const collectionFor = <T extends StorableRecordData>(name: string): CollectionOps<T> =>
190
- queued(rawCollectionFor<T>(name));
191
-
192
- const backupPosture: EffectivePosture = {
193
- requested: store.backupIncluded ? 'included' : 'excluded',
194
- observed: store.backupIncluded ? 'included' : 'excluded',
195
- enforcedBy: 'package',
196
- };
197
-
198
- const rawAttachments = (deferred?: Deferred): Attachments => ({
199
- async write(input: AttachmentInput): Promise<AttachmentReference> {
200
- assertOpen();
201
- throwIfFaulted('attachmentWrite');
202
- assertValidAttachmentId(input.id);
203
- const bytes = Buffer.from(input.content, 'base64');
204
- assertArtifactWithinBudget(input.id, bytes.length);
205
- const reference: AttachmentReference = {
206
- id: input.id,
207
- owner: input.owner,
208
- contentType: input.contentType,
209
- size: bytes.length,
210
- createdAt: now(),
211
- };
212
- store.artifacts.set(input.id, input.content);
213
- store.attachments.set(input.id, reference);
214
- return reference;
215
- },
216
- async read(id: string, range?: ByteRange): Promise<string> {
217
- assertOpen();
218
- // No paths here to escape, but an id the device build would refuse
219
- // has to be refused here too, or a consumer's passing tests stop
220
- // predicting the device (FR-096).
221
- assertValidAttachmentId(id);
222
- const content = store.artifacts.get(id);
223
- if (content === undefined) {
224
- raise(DiagnosticCodes.ATTACHMENT_MISSING, {
225
- message: 'artifact is missing',
226
- detail: { attachmentId: id },
227
- });
228
- }
229
- if (!range) {
230
- return content;
231
- }
232
- const bytes = Buffer.from(content, 'base64');
233
- return bytes.subarray(range.offset, range.offset + range.length).toString('base64');
234
- },
235
- async availability(id) {
236
- assertOpen();
237
- const reference = store.attachments.get(id);
238
- if (!reference) {
239
- return { id, state: 'missing' };
240
- }
241
- return store.artifacts.has(id)
242
- ? { id, state: 'available', size: reference.size }
243
- : { id, state: 'missing' };
244
- },
245
- async remove(id: string): Promise<DeletionOutcome> {
246
- assertOpen();
247
- assertValidAttachmentId(id);
248
- if (deferred) {
249
- const hadReference = store.attachments.delete(id);
250
- if (hadReference) deferred.add(id);
251
- return {
252
- removedRecords: 0,
253
- removedArtifacts: hadReference ? [id] : [],
254
- orphanedReferences: [],
255
- failedRemovals: [],
256
- };
257
- }
258
- const removed = store.artifacts.delete(id);
259
- store.attachments.delete(id);
260
- return {
261
- removedRecords: 0,
262
- removedArtifacts: removed ? [id] : [],
263
- orphanedReferences: [],
264
- failedRemovals: [],
265
- };
266
- },
267
- async listFor(owner: AttachmentOwner): Promise<AttachmentReference[]> {
268
- assertOpen();
269
- return [...store.attachments.values()].filter(
270
- reference =>
271
- reference.owner?.collection === owner.collection &&
272
- reference.owner?.id === owner.id,
273
- );
274
- },
275
- async reconcile(reconcileOptions): Promise<ReconciliationReport> {
276
- assertOpen();
277
- const orphanedArtifacts = [...store.artifacts.keys()].filter(
278
- id => !store.attachments.has(id),
279
- );
280
- const danglingReferences = [...store.attachments.keys()].filter(
281
- id => !store.artifacts.has(id),
282
- );
283
- const removedArtifacts: string[] = [];
284
- if (reconcileOptions?.removeOrphans) {
285
- for (const id of orphanedArtifacts) {
286
- store.artifacts.delete(id);
287
- removedArtifacts.push(id);
288
- }
289
- }
290
- return { orphanedArtifacts, danglingReferences, removedArtifacts };
291
- },
292
- });
293
- const attachments = queued(rawAttachments());
294
-
295
- /**
296
- * One transaction against the store. Snapshot-and-restore rather than a
297
- * write log: simpler to reason about, and rollback fidelity is what US4
298
- * tests. Runs only inside `exclusive`, so the snapshot never captures,
299
- * nor its restore erases, a write made from outside.
300
- */
301
- const runTransaction = async <R>(fn: (tx: Transaction) => Promise<R>): Promise<R> => {
302
- const snapshot = cloneCollections(store);
303
- const attachmentSnapshot = new Map(store.attachments);
304
- const pendingArtifactRemovals: string[] = [];
305
- const deferred: Deferred = { add: id => pendingArtifactRemovals.push(id) };
306
- let settled = false;
307
-
308
- const assertLive = (): void => {
309
- if (settled) {
310
- raise(DiagnosticCodes.TRANSACTION_STATE, {
311
- message: 'transaction already settled',
312
- });
313
- }
314
- };
315
- const rollback = (): void => {
316
- store.collections = snapshot as never;
317
- store.attachments = attachmentSnapshot;
318
- };
319
-
320
- // A collection taken from the transaction keeps refusing once it
321
- // settles, as the SQLite executor does; otherwise a write after abort
322
- // would land straight in the store.
323
- const live = <O extends object>(ops: O): O =>
324
- Object.fromEntries(
325
- Object.entries(ops).map(([key, value]) => [
326
- key,
327
- typeof value === 'function'
328
- ? // Async so a refusal rejects, as the SQLite executor's does.
329
- async (...args: unknown[]) => {
330
- assertLive();
331
- return (value as (...a: unknown[]) => unknown)(...args);
332
- }
333
- : value,
334
- ]),
335
- ) as O;
336
-
337
- const tx: Transaction = {
338
- collection: <T extends StorableRecordData>(name: string) => {
339
- assertLive();
340
- return live(rawCollectionFor<T>(name, deferred));
341
- },
342
- attachments: () => {
343
- assertLive();
344
- return rawAttachments(deferred);
345
- },
346
- async abort() {
347
- assertLive();
348
- settled = true;
349
- rollback();
350
- },
351
- };
352
-
353
- try {
354
- const result = await fn(tx);
355
- if (!settled) {
356
- throwIfFaulted('commit');
357
- settled = true;
358
- // Committed: the artifacts whose references went may go too.
359
- for (const id of pendingArtifactRemovals) {
360
- store.artifacts.delete(id);
361
- }
362
- }
363
- return result;
364
- } catch (error) {
365
- if (!settled) {
366
- settled = true;
367
- rollback();
368
- }
369
- throw error;
370
- }
371
- };
372
-
373
- const handle: DatabaseHandle = {
374
- scopeKey: scope.key,
375
- schemaVersion: store.schemaVersion,
376
- backupPosture,
377
- isEncrypted: store.encrypted,
378
- collection: collectionFor,
379
-
380
- transaction<R>(fn: (tx: Transaction) => Promise<R>): Promise<R> {
381
- assertOpen();
382
- return queue.runTransaction(() => runTransaction(fn));
383
- },
384
-
385
- batch(operations: BatchOperation[]): Promise<BatchOutcome> {
386
- assertOpen();
387
- assertBatchWithinBudget(operations.length);
388
- return queue.runTransaction(() =>
389
- runTransaction(async (tx: Transaction) => {
390
- const outcomes = [];
391
- for (const operation of operations) {
392
- const ops = tx.collection(operation.collection);
393
- switch (operation.type) {
394
- case 'insert':
395
- outcomes.push(await ops.insert(operation.record));
396
- break;
397
- case 'update':
398
- outcomes.push(await ops.update(operation.id, { data: operation.data }));
399
- break;
400
- case 'upsert':
401
- outcomes.push(await ops.upsert(operation.record));
402
- break;
403
- case 'delete':
404
- outcomes.push(await ops.delete(operation.id));
405
- break;
406
- case 'softDelete':
407
- outcomes.push(await ops.softDelete(operation.id));
408
- break;
409
- default:
410
- break;
411
- }
412
- }
413
- return { applied: outcomes.length, outcomes };
414
- }),
415
- );
416
- },
417
-
418
- attachments: () => attachments,
419
-
420
- async explain(collection: string, _query?: Query): Promise<IndexCoverageReport> {
421
- assertOpen();
422
- // Documented divergence: index declarations are validated but
423
- // have no performance effect in memory (FR-096).
424
- return {
425
- collection,
426
- usesIndex: false,
427
- detail: 'memory adapter does not use indexes',
428
- };
429
- },
430
-
431
- async close(): Promise<void> {
432
- closed = true;
433
- },
434
- };
435
-
436
- return handle;
437
- }
438
-
439
- /**
440
- * Backs both `deleteScopeData` and `resetScope`. They differ in intent and
441
- * in what they let a consumer conclude, not in what they do to the store —
442
- * keeping them as separate exported names is what makes the destructive one
443
- * impossible to call by accident.
444
- */
445
- async function removeScope(config: ScopeTargetConfig): Promise<DeletionOutcome> {
446
- const scope = resolveScope(config.scope);
447
- const store = existingScopeStore(config.name, scope.key);
448
- if (!store) {
449
- // Deleting nothing is success, not an error (FR-062).
450
- return {
451
- scopeKey: scope.key,
452
- removedRecords: 0,
453
- removedArtifacts: [],
454
- orphanedReferences: [],
455
- failedRemovals: [],
456
- };
457
- }
458
- store.deletionState = 'deleting';
459
- let removedRecords = 0;
460
- for (const rows of store.collections.values()) {
461
- removedRecords += rows.size;
462
- }
463
- const removedArtifacts = [...store.artifacts.keys()];
464
- dropScope(config.name, scope.key);
465
- return {
466
- scopeKey: scope.key,
467
- removedRecords,
468
- removedArtifacts,
469
- orphanedReferences: [],
470
- failedRemovals: [],
471
- };
472
- }
473
-
474
- return {
475
- name: 'memory',
476
- open,
477
- deleteScopeData: config => removeScope(config),
478
- resetScope: config => removeScope(config),
479
- async deleteDatabase(config: DatabaseTargetConfig): Promise<DeletionOutcome> {
480
- dropDatabase(config.name);
481
- return {
482
- removedRecords: 0,
483
- removedArtifacts: [],
484
- orphanedReferences: [],
485
- failedRemovals: [],
486
- };
487
- },
488
- };
489
- }
490
-
491
- export { Budgets } from '../../contract/config';
492
- export { activeFaults } from '../faults';
1
+ /**
2
+ * The in-memory adapter.
3
+ *
4
+ * Not a convenience: op-sqlite is native and unreachable from Jest, so this
5
+ * is the only way the package's own guarantees are testable in CI
6
+ * (research.md R8). Documented divergences are listed in the package README.
7
+ */
8
+
9
+ import { base64ToBytes, bytesToBase64 } from '../../platform/bytes';
10
+ import { createTransactionQueue } from '../../sqlite/transaction-queue';
11
+ import type {
12
+ AttachmentInput,
13
+ AttachmentReference,
14
+ AttachmentOwner,
15
+ ByteRange,
16
+ DeletionOutcome,
17
+ ReconciliationReport,
18
+ } from '../../contract/attachment';
19
+ import type { EffectivePosture } from '../../contract/backup';
20
+ import type {
21
+ DatabaseConfig,
22
+ DatabaseTargetConfig,
23
+ ScopeTargetConfig,
24
+ } from '../../contract/config';
25
+ import { DiagnosticCodes } from '../../contract/errors';
26
+ import type { IndexCoverageReport, Query } from '../../contract/query';
27
+ import type { StorableRecordData } from '../../contract/record';
28
+ import type { CollectionSchema } from '../../contract/schema';
29
+ import type {
30
+ BatchOperation,
31
+ BatchOutcome,
32
+ CollectionOps,
33
+ Transaction,
34
+ } from '../../contract/transaction';
35
+ import type { Attachments, DatabaseHandle, StorageAdapter } from '../../contract/adapter';
36
+ import { acquireKey } from '../../crypto/key-provider';
37
+ import { instrument } from '../../diagnostics/dev-log';
38
+ import { raise } from '../../diagnostics/errors';
39
+ import { assertValidAttachmentId } from '../../platform/location';
40
+ import { assertBatchWithinBudget, assertArtifactWithinBudget } from '../../query/budgets';
41
+ import { validateConfig } from '../../schema/validate-config';
42
+ import { resolveScope } from '../../scope/validate';
43
+ import { runMigrations } from './migrations';
44
+ import { createMemoryCollection } from './collection';
45
+ import {
46
+ collectionMap,
47
+ dropDatabase,
48
+ dropScope,
49
+ existingScopeStore,
50
+ scopeStoreFor,
51
+ type MemoryScopeStore,
52
+ } from './store';
53
+ import { throwIfFaulted } from '../faults';
54
+
55
+ export type MemoryAdapterOptions = {
56
+ /** Fixed clock, so timestamp assertions are deterministic. */
57
+ now?: () => number;
58
+ };
59
+
60
+ function schemaIndex(collections: CollectionSchema[]): Map<string, CollectionSchema> {
61
+ return new Map(collections.map(collection => [collection.name, collection]));
62
+ }
63
+
64
+ function cloneCollections(store: MemoryScopeStore): Map<
65
+ string,
66
+ Map<
67
+ string,
68
+ {
69
+ id: string;
70
+ payload: string;
71
+ createdAt?: number;
72
+ updatedAt?: number;
73
+ deletedAt?: number | null;
74
+ }
75
+ >
76
+ > {
77
+ const copy = new Map<string, Map<string, never>>();
78
+ for (const [name, rows] of store.collections) {
79
+ copy.set(name, new Map([...rows].map(([id, row]) => [id, { ...row } as never])));
80
+ }
81
+ return copy as never;
82
+ }
83
+
84
+ export function createMemoryAdapter(options: MemoryAdapterOptions = {}): StorageAdapter {
85
+ const now = options.now ?? (() => Date.now());
86
+
87
+ async function open(config: DatabaseConfig): Promise<DatabaseHandle> {
88
+ validateConfig(config);
89
+ throwIfFaulted('open');
90
+
91
+ const scope = resolveScope(config.scope);
92
+ const existing = existingScopeStore(config.name, scope.key);
93
+ if (existing?.deletionState === 'deleting') {
94
+ // A deletion that was interrupted must never look complete.
95
+ raise(DiagnosticCodes.SCOPE_DELETED, {
96
+ message: 'scope deletion was interrupted',
97
+ detail: { scopeKey: scope.key },
98
+ });
99
+ }
100
+
101
+ const store = scopeStoreFor(config.name, scope.key);
102
+ const encryptionRequested = config.encryption?.enabled === true;
103
+
104
+ if (store.schemaVersion > 0 && store.encrypted !== encryptionRequested) {
105
+ raise(DiagnosticCodes.ENCRYPTION_MISMATCH, {
106
+ message: 'encryption posture does not match the stored database',
107
+ });
108
+ }
109
+ if (encryptionRequested) {
110
+ // The same acquisition path the on-device adapter uses, so a locked
111
+ // keychain and a provider returning nothing stay as distinguishable
112
+ // here as they are on a phone (SC-020b). Released immediately: the
113
+ // in-memory store has nothing to encrypt with it.
114
+ const key = await acquireKey(config.encryption!, scope);
115
+ key.release();
116
+ }
117
+ store.encrypted = encryptionRequested;
118
+ store.backupIncluded = config.backup?.includeInPlatformBackup === true;
119
+
120
+ const schemas = schemaIndex(config.collections);
121
+ for (const collection of config.collections) {
122
+ collectionMap(store, collection.name);
123
+ }
124
+
125
+ await runMigrations(store, config, now);
126
+
127
+ let closed = false;
128
+ const assertOpen = (): void => {
129
+ if (closed) {
130
+ raise(DiagnosticCodes.CLOSED_HANDLE, { message: 'handle is closed' });
131
+ }
132
+ if (store.deletionState === 'deleting') {
133
+ raise(DiagnosticCodes.SCOPE_DELETED, {
134
+ message: 'scope was deleted beneath this handle',
135
+ detail: { scopeKey: scope.key },
136
+ });
137
+ }
138
+ };
139
+
140
+ /**
141
+ * The same isolation the SQLite connection gives: a transaction holds the
142
+ * handle until it settles, and work through the handle meanwhile waits
143
+ * rather than landing inside the transaction's snapshot and vanishing
144
+ * with its rollback. Only what the transaction hands out runs inside it.
145
+ */
146
+ const queue = createTransactionQueue();
147
+ const queued = <O extends object>(ops: O): O =>
148
+ Object.fromEntries(
149
+ Object.entries(ops).map(([key, value]) => [
150
+ key,
151
+ typeof value === 'function'
152
+ ? (...args: unknown[]) =>
153
+ queue.run(() => (value as (...a: unknown[]) => Promise<unknown>)(...args))
154
+ : value,
155
+ ]),
156
+ ) as O;
157
+
158
+ /** Artifact deletions held back until the transaction that caused them commits. */
159
+ type Deferred = { add(id: string): void };
160
+
161
+ /** FR-070 for the memory adapter, with the SQLite adapter's ordering and reporting. */
162
+ const cascadeFor = (collection: string, id: string, deferred?: Deferred): DeletionOutcome => {
163
+ const removedArtifacts: string[] = [];
164
+ for (const reference of [...store.attachments.values()]) {
165
+ if (reference.owner?.collection !== collection || reference.owner?.id !== id) continue;
166
+ store.attachments.delete(reference.id);
167
+ if (deferred) {
168
+ deferred.add(reference.id);
169
+ } else {
170
+ store.artifacts.delete(reference.id);
171
+ }
172
+ removedArtifacts.push(reference.id);
173
+ }
174
+ return { removedRecords: 0, removedArtifacts, orphanedReferences: [], failedRemovals: [] };
175
+ };
176
+
177
+ const rawCollectionFor = <T extends StorableRecordData>(
178
+ name: string,
179
+ deferred?: Deferred,
180
+ ): CollectionOps<T> => {
181
+ assertOpen();
182
+ const schema = schemas.get(name);
183
+ if (!schema) {
184
+ raise(DiagnosticCodes.CONFIG_INVALID, {
185
+ message: 'collection is not declared',
186
+ detail: { collection: name },
187
+ });
188
+ }
189
+ const ops = instrument(
190
+ name,
191
+ 'memory',
192
+ createMemoryCollection<T>({ store, schema, now, assertOpen }) as unknown as Record<
193
+ string,
194
+ (...args: never[]) => unknown
195
+ >,
196
+ ) as unknown as CollectionOps<T>;
197
+ if (config.cascadeAttachments !== true) {
198
+ return ops;
199
+ }
200
+ return {
201
+ ...ops,
202
+ async delete(id: string) {
203
+ const outcome = await ops.delete(id);
204
+ return { ...outcome, cascade: cascadeFor(name, id, deferred) };
205
+ },
206
+ };
207
+ };
208
+
209
+ const collectionFor = <T extends StorableRecordData>(name: string): CollectionOps<T> =>
210
+ queued(rawCollectionFor<T>(name));
211
+
212
+ const backupPosture: EffectivePosture = {
213
+ requested: store.backupIncluded ? 'included' : 'excluded',
214
+ observed: store.backupIncluded ? 'included' : 'excluded',
215
+ enforcedBy: 'package',
216
+ };
217
+
218
+ const rawAttachments = (deferred?: Deferred): Attachments => ({
219
+ async write(input: AttachmentInput): Promise<AttachmentReference> {
220
+ assertOpen();
221
+ throwIfFaulted('attachmentWrite');
222
+ assertValidAttachmentId(input.id);
223
+ const bytes = base64ToBytes(input.content);
224
+ assertArtifactWithinBudget(input.id, bytes.length);
225
+ const reference: AttachmentReference = {
226
+ id: input.id,
227
+ owner: input.owner,
228
+ contentType: input.contentType,
229
+ size: bytes.length,
230
+ createdAt: now(),
231
+ };
232
+ store.artifacts.set(input.id, input.content);
233
+ store.attachments.set(input.id, reference);
234
+ return reference;
235
+ },
236
+ async read(id: string, range?: ByteRange): Promise<string> {
237
+ assertOpen();
238
+ // No paths here to escape, but an id the device build would refuse
239
+ // has to be refused here too, or a consumer's passing tests stop
240
+ // predicting the device (FR-096).
241
+ assertValidAttachmentId(id);
242
+ const content = store.artifacts.get(id);
243
+ if (content === undefined) {
244
+ raise(DiagnosticCodes.ATTACHMENT_MISSING, {
245
+ message: 'artifact is missing',
246
+ detail: { attachmentId: id },
247
+ });
248
+ }
249
+ if (!range) {
250
+ return content;
251
+ }
252
+ const bytes = base64ToBytes(content);
253
+ return bytesToBase64(bytes.subarray(range.offset, range.offset + range.length));
254
+ },
255
+ async availability(id) {
256
+ assertOpen();
257
+ const reference = store.attachments.get(id);
258
+ if (!reference) {
259
+ return { id, state: 'missing' };
260
+ }
261
+ return store.artifacts.has(id)
262
+ ? { id, state: 'available', size: reference.size }
263
+ : { id, state: 'missing' };
264
+ },
265
+ async remove(id: string): Promise<DeletionOutcome> {
266
+ assertOpen();
267
+ assertValidAttachmentId(id);
268
+ if (deferred) {
269
+ const hadReference = store.attachments.delete(id);
270
+ if (hadReference) deferred.add(id);
271
+ return {
272
+ removedRecords: 0,
273
+ removedArtifacts: hadReference ? [id] : [],
274
+ orphanedReferences: [],
275
+ failedRemovals: [],
276
+ };
277
+ }
278
+ const removed = store.artifacts.delete(id);
279
+ store.attachments.delete(id);
280
+ return {
281
+ removedRecords: 0,
282
+ removedArtifacts: removed ? [id] : [],
283
+ orphanedReferences: [],
284
+ failedRemovals: [],
285
+ };
286
+ },
287
+ async listFor(owner: AttachmentOwner): Promise<AttachmentReference[]> {
288
+ assertOpen();
289
+ return [...store.attachments.values()].filter(
290
+ reference =>
291
+ reference.owner?.collection === owner.collection && reference.owner?.id === owner.id,
292
+ );
293
+ },
294
+ async reconcile(reconcileOptions): Promise<ReconciliationReport> {
295
+ assertOpen();
296
+ const orphanedArtifacts = [...store.artifacts.keys()].filter(
297
+ id => !store.attachments.has(id),
298
+ );
299
+ const danglingReferences = [...store.attachments.keys()].filter(
300
+ id => !store.artifacts.has(id),
301
+ );
302
+ const removedArtifacts: string[] = [];
303
+ if (reconcileOptions?.removeOrphans) {
304
+ for (const id of orphanedArtifacts) {
305
+ store.artifacts.delete(id);
306
+ removedArtifacts.push(id);
307
+ }
308
+ }
309
+ return { orphanedArtifacts, danglingReferences, removedArtifacts };
310
+ },
311
+ });
312
+ const attachments = queued(rawAttachments());
313
+
314
+ /**
315
+ * One transaction against the store. Snapshot-and-restore rather than a
316
+ * write log: simpler to reason about, and rollback fidelity is what US4
317
+ * tests. Runs only inside `exclusive`, so the snapshot never captures,
318
+ * nor its restore erases, a write made from outside.
319
+ */
320
+ const runTransaction = async <R>(fn: (tx: Transaction) => Promise<R>): Promise<R> => {
321
+ const snapshot = cloneCollections(store);
322
+ const attachmentSnapshot = new Map(store.attachments);
323
+ const pendingArtifactRemovals: string[] = [];
324
+ const deferred: Deferred = { add: id => pendingArtifactRemovals.push(id) };
325
+ let settled = false;
326
+
327
+ const assertLive = (): void => {
328
+ if (settled) {
329
+ raise(DiagnosticCodes.TRANSACTION_STATE, {
330
+ message: 'transaction already settled',
331
+ });
332
+ }
333
+ };
334
+ const rollback = (): void => {
335
+ store.collections = snapshot as never;
336
+ store.attachments = attachmentSnapshot;
337
+ };
338
+
339
+ // A collection taken from the transaction keeps refusing once it
340
+ // settles, as the SQLite executor does; otherwise a write after abort
341
+ // would land straight in the store.
342
+ const live = <O extends object>(ops: O): O =>
343
+ Object.fromEntries(
344
+ Object.entries(ops).map(([key, value]) => [
345
+ key,
346
+ typeof value === 'function'
347
+ ? // Async so a refusal rejects, as the SQLite executor's does.
348
+ async (...args: unknown[]) => {
349
+ assertLive();
350
+ return (value as (...a: unknown[]) => unknown)(...args);
351
+ }
352
+ : value,
353
+ ]),
354
+ ) as O;
355
+
356
+ const tx: Transaction = {
357
+ collection: <T extends StorableRecordData>(name: string) => {
358
+ assertLive();
359
+ return live(rawCollectionFor<T>(name, deferred));
360
+ },
361
+ attachments: () => {
362
+ assertLive();
363
+ return rawAttachments(deferred);
364
+ },
365
+ async abort() {
366
+ assertLive();
367
+ settled = true;
368
+ rollback();
369
+ },
370
+ };
371
+
372
+ try {
373
+ const result = await fn(tx);
374
+ if (!settled) {
375
+ throwIfFaulted('commit');
376
+ settled = true;
377
+ // Committed: the artifacts whose references went may go too.
378
+ for (const id of pendingArtifactRemovals) {
379
+ store.artifacts.delete(id);
380
+ }
381
+ }
382
+ return result;
383
+ } catch (error) {
384
+ if (!settled) {
385
+ settled = true;
386
+ rollback();
387
+ }
388
+ throw error;
389
+ }
390
+ };
391
+
392
+ const handle: DatabaseHandle = {
393
+ scopeKey: scope.key,
394
+ schemaVersion: store.schemaVersion,
395
+ backupPosture,
396
+ isEncrypted: store.encrypted,
397
+ collection: collectionFor,
398
+
399
+ transaction<R>(fn: (tx: Transaction) => Promise<R>): Promise<R> {
400
+ assertOpen();
401
+ return queue.runTransaction(() => runTransaction(fn));
402
+ },
403
+
404
+ batch(operations: BatchOperation[]): Promise<BatchOutcome> {
405
+ assertOpen();
406
+ assertBatchWithinBudget(operations.length);
407
+ return queue.runTransaction(() =>
408
+ runTransaction(async (tx: Transaction) => {
409
+ const outcomes = [];
410
+ for (const operation of operations) {
411
+ const ops = tx.collection(operation.collection);
412
+ switch (operation.type) {
413
+ case 'insert':
414
+ outcomes.push(await ops.insert(operation.record));
415
+ break;
416
+ case 'update':
417
+ outcomes.push(await ops.update(operation.id, { data: operation.data }));
418
+ break;
419
+ case 'upsert':
420
+ outcomes.push(await ops.upsert(operation.record));
421
+ break;
422
+ case 'delete':
423
+ outcomes.push(await ops.delete(operation.id));
424
+ break;
425
+ case 'softDelete':
426
+ outcomes.push(await ops.softDelete(operation.id));
427
+ break;
428
+ default:
429
+ break;
430
+ }
431
+ }
432
+ return { applied: outcomes.length, outcomes };
433
+ }),
434
+ );
435
+ },
436
+
437
+ attachments: () => attachments,
438
+
439
+ async explain(collection: string, _query?: Query): Promise<IndexCoverageReport> {
440
+ assertOpen();
441
+ // Documented divergence: index declarations are validated but
442
+ // have no performance effect in memory (FR-096).
443
+ return {
444
+ collection,
445
+ usesIndex: false,
446
+ detail: 'memory adapter does not use indexes',
447
+ };
448
+ },
449
+
450
+ async close(): Promise<void> {
451
+ closed = true;
452
+ },
453
+ };
454
+
455
+ return handle;
456
+ }
457
+
458
+ /**
459
+ * Backs both `deleteScopeData` and `resetScope`. They differ in intent and
460
+ * in what they let a consumer conclude, not in what they do to the store —
461
+ * keeping them as separate exported names is what makes the destructive one
462
+ * impossible to call by accident.
463
+ */
464
+ async function removeScope(config: ScopeTargetConfig): Promise<DeletionOutcome> {
465
+ const scope = resolveScope(config.scope);
466
+ const store = existingScopeStore(config.name, scope.key);
467
+ if (!store) {
468
+ // Deleting nothing is success, not an error (FR-062).
469
+ return {
470
+ scopeKey: scope.key,
471
+ removedRecords: 0,
472
+ removedArtifacts: [],
473
+ orphanedReferences: [],
474
+ failedRemovals: [],
475
+ };
476
+ }
477
+ store.deletionState = 'deleting';
478
+ let removedRecords = 0;
479
+ for (const rows of store.collections.values()) {
480
+ removedRecords += rows.size;
481
+ }
482
+ const removedArtifacts = [...store.artifacts.keys()];
483
+ dropScope(config.name, scope.key);
484
+ return {
485
+ scopeKey: scope.key,
486
+ removedRecords,
487
+ removedArtifacts,
488
+ orphanedReferences: [],
489
+ failedRemovals: [],
490
+ };
491
+ }
492
+
493
+ return {
494
+ name: 'memory',
495
+ open,
496
+ deleteScopeData: config => removeScope(config),
497
+ resetScope: config => removeScope(config),
498
+ async deleteDatabase(config: DatabaseTargetConfig): Promise<DeletionOutcome> {
499
+ dropDatabase(config.name);
500
+ return {
501
+ removedRecords: 0,
502
+ removedArtifacts: [],
503
+ orphanedReferences: [],
504
+ failedRemovals: [],
505
+ };
506
+ },
507
+ };
508
+ }
509
+
510
+ export { Budgets } from '../../contract/config';
511
+ export { activeFaults } from '../faults';