@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.
- package/CHANGELOG.md +191 -102
- package/README.md +728 -728
- package/package.json +62 -62
- package/skills/sdk-rn-local-db/SKILL.md +0 -1
- package/src/adapter/budgets.ts +67 -67
- package/src/adapter/collection.ts +303 -298
- package/src/adapter/deletion.ts +252 -252
- package/src/adapter/index.ts +64 -68
- package/src/adapter/lifecycle.ts +517 -520
- package/src/adapter/transaction.ts +71 -71
- package/src/artifacts/availability.ts +67 -67
- package/src/artifacts/cascade.ts +114 -114
- package/src/artifacts/paths.ts +29 -29
- package/src/artifacts/reconcile.ts +63 -63
- package/src/artifacts/store.ts +334 -345
- package/src/contract/adapter.ts +76 -78
- package/src/contract/attachment.ts +75 -78
- package/src/contract/backup.ts +28 -28
- package/src/contract/config.ts +54 -54
- package/src/contract/encryption.ts +34 -34
- package/src/contract/errors.ts +76 -76
- package/src/contract/migration.ts +42 -42
- package/src/contract/query.ts +81 -81
- package/src/contract/record.ts +46 -51
- package/src/contract/schema.ts +36 -36
- package/src/contract/scope.ts +36 -36
- package/src/contract/transaction.ts +52 -54
- package/src/crypto/chunked-cipher.ts +153 -150
- package/src/crypto/derive.ts +64 -64
- package/src/crypto/key-provider.ts +78 -78
- package/src/crypto/provider.ts +75 -81
- package/src/diagnostics/dev-log.ts +132 -108
- package/src/diagnostics/errors.ts +63 -66
- package/src/diagnostics/faults.ts +58 -64
- package/src/diagnostics/map-native.ts +108 -106
- package/src/diagnostics/redact.ts +119 -141
- package/src/index.ts +80 -110
- package/src/migration/executor.ts +132 -136
- package/src/migration/ledger.ts +95 -97
- package/src/migration/validate-chain.ts +65 -65
- package/src/platform/backup-android-verify.ts +88 -88
- package/src/platform/backup-ios.ts +49 -48
- package/src/platform/bytes.ts +157 -144
- package/src/platform/fs.ts +90 -89
- package/src/platform/location.ts +135 -135
- package/src/query/budgets.ts +54 -54
- package/src/query/compare.ts +91 -91
- package/src/query/cursor.ts +100 -100
- package/src/query/filter.ts +82 -82
- package/src/query/paginate.ts +51 -51
- package/src/query/sort.ts +55 -56
- package/src/query/validate.ts +171 -171
- package/src/schema/declare.ts +42 -45
- package/src/schema/validate-config.ts +158 -158
- package/src/scope/hash.ts +131 -134
- package/src/scope/normalize.ts +83 -90
- package/src/scope/validate.ts +83 -83
- package/src/sqlite/adapter.ts +26 -26
- package/src/sqlite/batch.ts +242 -242
- package/src/sqlite/catalog.ts +105 -108
- package/src/sqlite/connection.ts +227 -227
- package/src/sqlite/index-coverage.ts +54 -56
- package/src/sqlite/native.ts +84 -83
- package/src/sqlite/row-codec.ts +217 -221
- package/src/sqlite/sql-builder.ts +244 -244
- package/src/sqlite/transaction-queue.ts +87 -87
- package/src/sqlite/transactions.ts +33 -33
- package/src/sqlite/values.ts +24 -24
- package/src/testing/conformance/attachments.ts +139 -136
- package/src/testing/conformance/encryption.ts +151 -150
- package/src/testing/conformance/index.ts +161 -161
- package/src/testing/conformance/lifecycle.ts +99 -99
- package/src/testing/conformance/migrations.ts +177 -177
- package/src/testing/conformance/queries.ts +204 -167
- package/src/testing/conformance/records.ts +169 -169
- package/src/testing/conformance/scopes.ts +86 -88
- package/src/testing/conformance/transactions.ts +149 -149
- package/src/testing/faults.ts +21 -21
- package/src/testing/index.ts +17 -21
- package/src/testing/memory-adapter/collection.ts +214 -219
- package/src/testing/memory-adapter/index.ts +511 -492
- package/src/testing/memory-adapter/migrations.ts +117 -119
- package/src/testing/memory-adapter/reset.ts +50 -50
- package/src/testing/memory-adapter/store.ts +128 -134
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The transaction primitive.
|
|
3
|
-
*
|
|
4
|
-
* A transaction never spans two scopes, and not because a check forbids it:
|
|
5
|
-
* a transaction runs on one connection, a connection is opened against one
|
|
6
|
-
* scope's file, and the public surface offers no way to reach a second one
|
|
7
|
-
* from inside. FR-038 is a type error rather than a runtime one (research.md
|
|
8
|
-
* R2).
|
|
9
|
-
*
|
|
10
|
-
* Rollback fidelity is SQLite's, not this package's. A failure at any step
|
|
11
|
-
* unwinds through the engine's own `ROLLBACK`, which is the only way SC-006's
|
|
12
|
-
* "identical to its pre-transaction state, every time" survives a process
|
|
13
|
-
* kill halfway through.
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
import type { Connection, TransactionExecutor } from './connection';
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Run `body` inside one transaction on one scope.
|
|
20
|
-
*
|
|
21
|
-
* Deliberately thin. The interesting behavior — read-your-own-writes,
|
|
22
|
-
* terminal-state rejection, what a settled transaction does when used again —
|
|
23
|
-
* belongs to `adapter/transaction.ts`, where both adapters can be held to the
|
|
24
|
-
* same rules.
|
|
25
|
-
*/
|
|
26
|
-
export function runInTransaction<R>(
|
|
27
|
-
connection: Connection,
|
|
28
|
-
body: (tx: TransactionExecutor) => Promise<R>,
|
|
29
|
-
): Promise<R> {
|
|
30
|
-
return connection.transaction(body);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export type { TransactionExecutor };
|
|
1
|
+
/**
|
|
2
|
+
* The transaction primitive.
|
|
3
|
+
*
|
|
4
|
+
* A transaction never spans two scopes, and not because a check forbids it:
|
|
5
|
+
* a transaction runs on one connection, a connection is opened against one
|
|
6
|
+
* scope's file, and the public surface offers no way to reach a second one
|
|
7
|
+
* from inside. FR-038 is a type error rather than a runtime one (research.md
|
|
8
|
+
* R2).
|
|
9
|
+
*
|
|
10
|
+
* Rollback fidelity is SQLite's, not this package's. A failure at any step
|
|
11
|
+
* unwinds through the engine's own `ROLLBACK`, which is the only way SC-006's
|
|
12
|
+
* "identical to its pre-transaction state, every time" survives a process
|
|
13
|
+
* kill halfway through.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import type { Connection, TransactionExecutor } from './connection';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Run `body` inside one transaction on one scope.
|
|
20
|
+
*
|
|
21
|
+
* Deliberately thin. The interesting behavior — read-your-own-writes,
|
|
22
|
+
* terminal-state rejection, what a settled transaction does when used again —
|
|
23
|
+
* belongs to `adapter/transaction.ts`, where both adapters can be held to the
|
|
24
|
+
* same rules.
|
|
25
|
+
*/
|
|
26
|
+
export function runInTransaction<R>(
|
|
27
|
+
connection: Connection,
|
|
28
|
+
body: (tx: TransactionExecutor) => Promise<R>,
|
|
29
|
+
): Promise<R> {
|
|
30
|
+
return connection.transaction(body);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export type { TransactionExecutor };
|
package/src/sqlite/values.ts
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Reading values back out of a result row.
|
|
3
|
-
*
|
|
4
|
-
* The engine hands back `unknown` per column, and every read site would
|
|
5
|
-
* otherwise repeat the same `String(...)` / `Number(...)` coercion with a
|
|
6
|
-
* slightly different null check. One place to do it means one place where the
|
|
7
|
-
* "NULL means absent, not empty string" rule is decided.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
export function text(value: unknown): string {
|
|
11
|
-
return typeof value === 'string' ? value : String(value as string | number | boolean);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export function num(value: unknown): number {
|
|
15
|
-
return typeof value === 'number' ? value : Number(value as string | number);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export function maybeText(value: unknown): string | null {
|
|
19
|
-
return value == null ? null : text(value);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export function maybeNum(value: unknown): number | null {
|
|
23
|
-
return value == null ? null : num(value);
|
|
24
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Reading values back out of a result row.
|
|
3
|
+
*
|
|
4
|
+
* The engine hands back `unknown` per column, and every read site would
|
|
5
|
+
* otherwise repeat the same `String(...)` / `Number(...)` coercion with a
|
|
6
|
+
* slightly different null check. One place to do it means one place where the
|
|
7
|
+
* "NULL means absent, not empty string" rule is decided.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export function text(value: unknown): string {
|
|
11
|
+
return typeof value === 'string' ? value : String(value as string | number | boolean);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function num(value: unknown): number {
|
|
15
|
+
return typeof value === 'number' ? value : Number(value as string | number);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function maybeText(value: unknown): string | null {
|
|
19
|
+
return value == null ? null : text(value);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function maybeNum(value: unknown): number | null {
|
|
23
|
+
return value == null ? null : num(value);
|
|
24
|
+
}
|
|
@@ -1,136 +1,139 @@
|
|
|
1
|
-
import { DiagnosticCodes } from '../../contract/errors';
|
|
2
|
-
import { isCode } from '../../diagnostics/errors';
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
const
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
expect(
|
|
25
|
-
expect(
|
|
26
|
-
await
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
await
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
expect((await
|
|
71
|
-
await handle.
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
await
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
await
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
expect(report.
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
expect(
|
|
90
|
-
await
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
await tx.
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
//
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
const
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
1
|
+
import { DiagnosticCodes } from '../../contract/errors';
|
|
2
|
+
import { isCode } from '../../diagnostics/errors';
|
|
3
|
+
import { base64ToBytes, bytesToBase64, utf8Bytes, utf8String } from '../../platform/bytes';
|
|
4
|
+
import type { ConformanceContext } from './index';
|
|
5
|
+
|
|
6
|
+
/** US7: artifacts the package owns, and the reconciliation that keeps them honest. */
|
|
7
|
+
export function registerAttachmentConformance(context: ConformanceContext): void {
|
|
8
|
+
describe('conformance: attachments', () => {
|
|
9
|
+
beforeEach(() => context.reset());
|
|
10
|
+
|
|
11
|
+
const scope = { kind: 'guest' } as const;
|
|
12
|
+
const content = bytesToBase64(utf8Bytes('hello attachment'));
|
|
13
|
+
|
|
14
|
+
it('stores a reference alongside the artifact', async () => {
|
|
15
|
+
const handle = await context.adapter.open(context.configFor(scope));
|
|
16
|
+
const attachments = handle.attachments();
|
|
17
|
+
const reference = await attachments.write({
|
|
18
|
+
id: 'a1',
|
|
19
|
+
owner: { collection: 'notes', id: 'n1' },
|
|
20
|
+
contentType: 'text/plain',
|
|
21
|
+
content,
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
expect(reference.size).toBe(base64ToBytes(content).length);
|
|
25
|
+
expect(await attachments.read('a1')).toBe(content);
|
|
26
|
+
expect((await attachments.availability('a1')).state).toBe('available');
|
|
27
|
+
await handle.close();
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('reads a byte range without materialising the whole artifact', async () => {
|
|
31
|
+
const handle = await context.adapter.open(context.configFor(scope));
|
|
32
|
+
const attachments = handle.attachments();
|
|
33
|
+
await attachments.write({ id: 'a1', contentType: 'text/plain', content });
|
|
34
|
+
|
|
35
|
+
const slice = await attachments.read('a1', { offset: 0, length: 5 });
|
|
36
|
+
expect(utf8String(base64ToBytes(slice))).toBe('hello');
|
|
37
|
+
await handle.close();
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('lists the attachments belonging to a record', async () => {
|
|
41
|
+
const handle = await context.adapter.open(context.configFor(scope));
|
|
42
|
+
const attachments = handle.attachments();
|
|
43
|
+
await attachments.write({
|
|
44
|
+
id: 'a1',
|
|
45
|
+
owner: { collection: 'notes', id: 'n1' },
|
|
46
|
+
contentType: 'text/plain',
|
|
47
|
+
content,
|
|
48
|
+
});
|
|
49
|
+
await attachments.write({ id: 'a2', contentType: 'text/plain', content });
|
|
50
|
+
|
|
51
|
+
const owned = await attachments.listFor({ collection: 'notes', id: 'n1' });
|
|
52
|
+
expect(owned.map(r => r.id)).toEqual(['a1']);
|
|
53
|
+
await handle.close();
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('reports a missing artifact without making the record unreadable', async () => {
|
|
57
|
+
const handle = await context.adapter.open(context.configFor(scope));
|
|
58
|
+
await handle.collection('notes').insert({ id: 'n1', data: { title: 'has media' } });
|
|
59
|
+
const attachments = handle.attachments();
|
|
60
|
+
await attachments.write({
|
|
61
|
+
id: 'a1',
|
|
62
|
+
owner: { collection: 'notes', id: 'n1' },
|
|
63
|
+
contentType: 'text/plain',
|
|
64
|
+
content,
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
// Remove the artifact behind the package's back.
|
|
68
|
+
await context.removeArtifactBehindTheScenes(handle.scopeKey, 'a1');
|
|
69
|
+
|
|
70
|
+
expect((await attachments.availability('a1')).state).toBe('missing');
|
|
71
|
+
expect((await handle.collection('notes').get('n1')).data.title).toBe('has media');
|
|
72
|
+
await handle.close();
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('finds an orphaned artifact and a dangling reference', async () => {
|
|
76
|
+
const handle = await context.adapter.open(context.configFor(scope));
|
|
77
|
+
const attachments = handle.attachments();
|
|
78
|
+
await attachments.write({ id: 'a1', contentType: 'text/plain', content });
|
|
79
|
+
await context.removeReferenceBehindTheScenes(handle.scopeKey, 'a1');
|
|
80
|
+
|
|
81
|
+
await attachments.write({ id: 'a2', contentType: 'text/plain', content });
|
|
82
|
+
await context.removeArtifactBehindTheScenes(handle.scopeKey, 'a2');
|
|
83
|
+
|
|
84
|
+
const report = await attachments.reconcile();
|
|
85
|
+
expect(report.orphanedArtifacts).toEqual(['a1']);
|
|
86
|
+
expect(report.danglingReferences).toEqual(['a2']);
|
|
87
|
+
|
|
88
|
+
const removed = await attachments.reconcile({ removeOrphans: true });
|
|
89
|
+
expect(removed.removedArtifacts).toEqual(['a1']);
|
|
90
|
+
expect((await attachments.reconcile()).orphanedArtifacts).toEqual([]);
|
|
91
|
+
await handle.close();
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it('rolls a reference back with its transaction', async () => {
|
|
95
|
+
const handle = await context.adapter.open(context.configFor(scope));
|
|
96
|
+
try {
|
|
97
|
+
await handle.transaction(async tx => {
|
|
98
|
+
await tx.collection('notes').insert({ id: 'n1', data: { title: 'x' } });
|
|
99
|
+
await tx.attachments().write({ id: 'a1', contentType: 'text/plain', content });
|
|
100
|
+
throw new Error('injected');
|
|
101
|
+
});
|
|
102
|
+
} catch {
|
|
103
|
+
// Expected.
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
expect(await handle.collection('notes').find('n1')).toBeNull();
|
|
107
|
+
// The reference is gone; the artifact on disk is not, which is
|
|
108
|
+
// exactly why reconciliation exists.
|
|
109
|
+
const report = await handle.attachments().reconcile();
|
|
110
|
+
expect(report.orphanedArtifacts).toEqual(['a1']);
|
|
111
|
+
await handle.close();
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('rejects an artifact over the size budget', async () => {
|
|
115
|
+
const handle = await context.adapter.open(context.configFor(scope));
|
|
116
|
+
const huge = bytesToBase64(new Uint8Array(101 * 1024 * 1024));
|
|
117
|
+
|
|
118
|
+
let thrown: unknown;
|
|
119
|
+
try {
|
|
120
|
+
await handle
|
|
121
|
+
.attachments()
|
|
122
|
+
.write({ id: 'big', contentType: 'application/octet-stream', content: huge });
|
|
123
|
+
} catch (error) {
|
|
124
|
+
thrown = error;
|
|
125
|
+
}
|
|
126
|
+
expect(isCode(thrown, DiagnosticCodes.ATTACHMENT_WRITE_FAILED)).toBe(true);
|
|
127
|
+
await handle.close();
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it('removes artifacts when the scope is deleted', async () => {
|
|
131
|
+
const handle = await context.adapter.open(context.configFor(scope));
|
|
132
|
+
await handle.attachments().write({ id: 'a1', contentType: 'text/plain', content });
|
|
133
|
+
await handle.close();
|
|
134
|
+
|
|
135
|
+
const outcome = await context.adapter.deleteScopeData(context.scopeTargetFor(scope));
|
|
136
|
+
expect(outcome.removedArtifacts).toContain('a1');
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
}
|