@aletheia-labs/store-sqlite 0.1.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/LICENSE ADDED
@@ -0,0 +1,176 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
package/README.md ADDED
@@ -0,0 +1,119 @@
1
+ # @aletheia-labs/store-sqlite
2
+
3
+ SQLite-backed implementations of the storage interfaces declared in `@aletheia-labs/core`.
4
+
5
+ > **Status**: `0.1.0` public baseline. Event, memory, conflict, migrations, and audit-history paths are live.
6
+
7
+ ## Quickstart
8
+
9
+ ```bash
10
+ pnpm add @aletheia-labs/core @aletheia-labs/store-sqlite
11
+ ```
12
+
13
+ ```ts
14
+ import { AletheiaAuthority, staticVisibilityPolicy } from '@aletheia-labs/core';
15
+ import { openSqliteStores } from '@aletheia-labs/store-sqlite';
16
+
17
+ const stores = openSqliteStores('./aletheia.sqlite');
18
+ const authority = new AletheiaAuthority({
19
+ eventLedger: stores.eventLedger,
20
+ memoryStore: stores.memoryStore,
21
+ conflictRegistry: stores.conflictRegistry,
22
+ visibilityPolicy: staticVisibilityPolicy([{ kind: 'private:user' }]),
23
+ });
24
+ ```
25
+
26
+ For an in-memory database in tests or demos:
27
+
28
+ ```ts
29
+ const stores = openSqliteStores(':memory:');
30
+ ```
31
+
32
+ Always call `stores.close()` when the host is done.
33
+
34
+ ## What this package provides
35
+
36
+ - `SqliteEventLedger` — append-only event log, with permission-filtered queries.
37
+ - `SqliteMemoryStore` — atom storage with strict status-transition enforcement and audit history.
38
+ - `SqliteConflictRegistry` — conflicts as first-class records with resolution history.
39
+ - `openSqliteStores(path)` — convenience wrapper that opens one database connection, runs migrations, and returns all three stores with a shared `close()`.
40
+ - `openConnection(options)` — lower-level connection helper for custom wiring.
41
+ - `MIGRATIONS` / `applyMigrations` — embedded, idempotent SQL migrations.
42
+
43
+ ## Storage contract
44
+
45
+ - Events are append-only.
46
+ - Memory atoms are immutable by ID. Duplicate `memoryId` insertions throw.
47
+ - Content, scope, visibility, source links, and lineage do not have UPDATE paths.
48
+ - Status changes go through `transitionStatus()` and are recorded in `memory_status_history`.
49
+ - Conflict resolution goes through `resolve()` and is recorded in `conflict_resolution_history`.
50
+ - Visibility filtering happens in SQL before rows are decoded.
51
+ - Reads decode through zod-backed codecs; corrupt persisted data becomes a parse error instead of a silent value.
52
+
53
+ ## Example
54
+
55
+ ```ts
56
+ import { EventSchema } from '@aletheia-labs/core';
57
+ import { openSqliteStores } from '@aletheia-labs/store-sqlite';
58
+
59
+ const stores = openSqliteStores(':memory:');
60
+
61
+ await stores.eventLedger.append(
62
+ EventSchema.parse({
63
+ eventId: 'evt-1',
64
+ kind: 'observation',
65
+ agentId: 'agent-1',
66
+ occurredAt: '2026-05-17T12:00:00Z',
67
+ payload: { note: 'source event' },
68
+ scope: { kind: 'project', projectId: 'demo' },
69
+ visibility: { kind: 'private:user' },
70
+ }),
71
+ );
72
+
73
+ const events = await stores.eventLedger.query({
74
+ permittedVisibilities: [{ kind: 'private:user' }],
75
+ scope: { kind: 'project', projectId: 'demo' },
76
+ limit: 10,
77
+ });
78
+
79
+ console.log(events.length);
80
+ stores.close();
81
+ ```
82
+
83
+ ## What this package does NOT do
84
+
85
+ - No LLM calls.
86
+ - No semantic retrieval, embeddings, vector index, or ranking.
87
+ - No authorization service or OAuth.
88
+ - No background daemon, watcher, or scheduler.
89
+ - No automatic memory promotion beyond the status transition requested by the host.
90
+
91
+ ## Stability
92
+
93
+ Public surface for the initial library cycle:
94
+
95
+ - `openSqliteStores(path)`
96
+ - `openConnection(options)`
97
+ - `SqliteEventLedger`
98
+ - `SqliteMemoryStore`
99
+ - `SqliteConflictRegistry`
100
+ - migration exports
101
+
102
+ The SQL schema is versioned through `schema_migrations`. Future changes that alter persisted rows or zod codecs need both SQL migration and data compatibility notes, because existing SQLite files may fail to decode if the TypeScript schema changes without a migration path.
103
+
104
+ ## Development
105
+
106
+ From the repo root:
107
+
108
+ ```bash
109
+ pnpm install
110
+ pnpm -F @aletheia-labs/store-sqlite typecheck
111
+ pnpm -F @aletheia-labs/store-sqlite test
112
+ pnpm -F @aletheia-labs/store-sqlite build
113
+ ```
114
+
115
+ Publish dry-run:
116
+
117
+ ```bash
118
+ pnpm -F @aletheia-labs/store-sqlite publish --dry-run --no-git-checks
119
+ ```
@@ -0,0 +1,68 @@
1
+ /**
2
+ * Encoders / decoders between domain types and SQLite row shapes.
3
+ *
4
+ * Every "row → domain" path goes through a zod schema so corruption in the
5
+ * database surfaces as a parse error, not a silent bad value downstream.
6
+ */
7
+ import { type ConflictRecord, type Event, type MemoryAtom } from '@aletheia-labs/core';
8
+ export interface EventRow {
9
+ event_id: string;
10
+ kind: string;
11
+ agent_id: string | null;
12
+ occurred_at: string;
13
+ payload_json: string;
14
+ scope_json: string;
15
+ visibility_json: string;
16
+ scope_key: string;
17
+ visibility_key: string;
18
+ inserted_at: string;
19
+ }
20
+ export declare function eventToRow(event: Event, insertedAt: string): EventRow;
21
+ export declare function rowToEvent(row: EventRow): Event;
22
+ export interface AtomRow {
23
+ memory_id: string;
24
+ memory_type: string;
25
+ content: string;
26
+ source_agent_id: string;
27
+ source_event_ids_json: string;
28
+ source_memory_ids_json: string;
29
+ scope_json: string;
30
+ visibility_json: string;
31
+ status: string;
32
+ scores_json: string;
33
+ valid_from: string;
34
+ valid_until: string | null;
35
+ last_confirmed_at: string | null;
36
+ links_json: string;
37
+ scope_key: string;
38
+ visibility_key: string;
39
+ inserted_at: string;
40
+ }
41
+ export declare function atomToRow(atom: MemoryAtom, insertedAt: string): AtomRow;
42
+ export declare function rowToAtom(row: AtomRow): MemoryAtom;
43
+ export interface ConflictRow {
44
+ conflict_id: string;
45
+ topic: string;
46
+ scope_json: string;
47
+ scope_key: string;
48
+ claims_json: string;
49
+ status: string;
50
+ decision_policy: string;
51
+ recorded_at: string;
52
+ resolved_at: string | null;
53
+ }
54
+ export declare function conflictToRow(conflict: ConflictRecord): ConflictRow;
55
+ export declare function rowToConflict(row: ConflictRow): ConflictRecord;
56
+ import type { Visibility } from '@aletheia-labs/core';
57
+ /**
58
+ * Build the `visibility_key IN (?, ?, ...)` clause and its parameter array.
59
+ * Returns `{ clause, params }`. Use as: `WHERE ${clause}` then spread params.
60
+ *
61
+ * If the permitted set is empty, returns a clause that matches no rows —
62
+ * a permission-empty caller sees nothing, which is the fail-closed default.
63
+ */
64
+ export declare function permittedClause(permitted: readonly Visibility[]): {
65
+ clause: string;
66
+ params: readonly string[];
67
+ };
68
+ //# sourceMappingURL=codec.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"codec.d.ts","sourceRoot":"","sources":["../src/codec.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EACL,KAAK,cAAc,EAEnB,KAAK,KAAK,EAEV,KAAK,UAAU,EAIhB,MAAM,qBAAqB,CA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AAM,WAAW,WAAW;IAC1B,WAAW,EAAE,MAAM,CAAC;IACpB,KAAK,EAAE,MAAM,CAAC;IACd,UAAU,EAAE,MAAM,CAAC;IACnB,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,MAAM,CAAC;IACpB,MAAM,EAAE,MAAM,CAAC;IACf,eAAe,EAAE,MAAM,CAAC;IACxB,WAAW,EAAE,MAAM,CAAC;IACpB,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;CAC5B;AAED,wBAAgB,aAAa,CAAC,QAAQ,EAAE,cAAc,GAAG,WAAW,CAYnE;AAED,wBAAgB,aAAa,CAAC,GAAG,EAAE,WAAW,GAAG,cAAc,CAW9D;AAID,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AAEtD;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,SAAS,EAAE,SAAS,UAAU,EAAE,GAAG;IACjE,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,SAAS,MAAM,EAAE,CAAC;CAC3B,CAOA"}
package/dist/codec.js ADDED
@@ -0,0 +1,112 @@
1
+ /**
2
+ * Encoders / decoders between domain types and SQLite row shapes.
3
+ *
4
+ * Every "row → domain" path goes through a zod schema so corruption in the
5
+ * database surfaces as a parse error, not a silent bad value downstream.
6
+ */
7
+ import { ConflictRecordSchema, EventSchema, MemoryAtomSchema, scopeKey, visibilityKey, } from '@aletheia-labs/core';
8
+ export function eventToRow(event, insertedAt) {
9
+ return {
10
+ event_id: event.eventId,
11
+ kind: event.kind,
12
+ agent_id: event.agentId,
13
+ occurred_at: event.occurredAt,
14
+ payload_json: JSON.stringify(event.payload),
15
+ scope_json: JSON.stringify(event.scope),
16
+ visibility_json: JSON.stringify(event.visibility),
17
+ scope_key: scopeKey(event.scope),
18
+ visibility_key: visibilityKey(event.visibility),
19
+ inserted_at: insertedAt,
20
+ };
21
+ }
22
+ export function rowToEvent(row) {
23
+ return EventSchema.parse({
24
+ eventId: row.event_id,
25
+ kind: row.kind,
26
+ agentId: row.agent_id,
27
+ occurredAt: row.occurred_at,
28
+ payload: JSON.parse(row.payload_json),
29
+ scope: JSON.parse(row.scope_json),
30
+ visibility: JSON.parse(row.visibility_json),
31
+ });
32
+ }
33
+ export function atomToRow(atom, insertedAt) {
34
+ return {
35
+ memory_id: atom.memoryId,
36
+ memory_type: atom.memoryType,
37
+ content: atom.content,
38
+ source_agent_id: atom.sourceAgentId,
39
+ source_event_ids_json: JSON.stringify(atom.sourceEventIds),
40
+ source_memory_ids_json: JSON.stringify(atom.sourceMemoryIds),
41
+ scope_json: JSON.stringify(atom.scope),
42
+ visibility_json: JSON.stringify(atom.visibility),
43
+ status: atom.status,
44
+ scores_json: JSON.stringify(atom.scores),
45
+ valid_from: atom.validFrom,
46
+ valid_until: atom.validUntil,
47
+ last_confirmed_at: atom.lastConfirmedAt,
48
+ links_json: JSON.stringify(atom.links),
49
+ scope_key: scopeKey(atom.scope),
50
+ visibility_key: visibilityKey(atom.visibility),
51
+ inserted_at: insertedAt,
52
+ };
53
+ }
54
+ export function rowToAtom(row) {
55
+ return MemoryAtomSchema.parse({
56
+ memoryId: row.memory_id,
57
+ memoryType: row.memory_type,
58
+ content: row.content,
59
+ sourceAgentId: row.source_agent_id,
60
+ sourceEventIds: JSON.parse(row.source_event_ids_json),
61
+ sourceMemoryIds: JSON.parse(row.source_memory_ids_json),
62
+ scope: JSON.parse(row.scope_json),
63
+ visibility: JSON.parse(row.visibility_json),
64
+ status: row.status,
65
+ scores: JSON.parse(row.scores_json),
66
+ validFrom: row.valid_from,
67
+ validUntil: row.valid_until,
68
+ lastConfirmedAt: row.last_confirmed_at,
69
+ links: JSON.parse(row.links_json),
70
+ });
71
+ }
72
+ export function conflictToRow(conflict) {
73
+ return {
74
+ conflict_id: conflict.conflictId,
75
+ topic: conflict.topic,
76
+ scope_json: JSON.stringify(conflict.scope),
77
+ scope_key: scopeKey(conflict.scope),
78
+ claims_json: JSON.stringify(conflict.claims),
79
+ status: conflict.status,
80
+ decision_policy: conflict.decisionPolicy,
81
+ recorded_at: conflict.recordedAt,
82
+ resolved_at: conflict.resolvedAt,
83
+ };
84
+ }
85
+ export function rowToConflict(row) {
86
+ return ConflictRecordSchema.parse({
87
+ conflictId: row.conflict_id,
88
+ topic: row.topic,
89
+ scope: JSON.parse(row.scope_json),
90
+ claims: JSON.parse(row.claims_json),
91
+ status: row.status,
92
+ decisionPolicy: row.decision_policy,
93
+ recordedAt: row.recorded_at,
94
+ resolvedAt: row.resolved_at,
95
+ });
96
+ }
97
+ /**
98
+ * Build the `visibility_key IN (?, ?, ...)` clause and its parameter array.
99
+ * Returns `{ clause, params }`. Use as: `WHERE ${clause}` then spread params.
100
+ *
101
+ * If the permitted set is empty, returns a clause that matches no rows —
102
+ * a permission-empty caller sees nothing, which is the fail-closed default.
103
+ */
104
+ export function permittedClause(permitted) {
105
+ if (permitted.length === 0) {
106
+ return { clause: '1 = 0', params: [] };
107
+ }
108
+ const keys = permitted.map(visibilityKey);
109
+ const placeholders = keys.map(() => '?').join(', ');
110
+ return { clause: `visibility_key IN (${placeholders})`, params: keys };
111
+ }
112
+ //# sourceMappingURL=codec.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"codec.js","sourceRoot":"","sources":["../src/codec.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAEL,oBAAoB,EAEpB,WAAW,EAEX,gBAAgB,EAChB,QAAQ,EACR,aAAa,GACd,MAAM,qBAAqB,CAAC;AAiB7B,MAAM,UAAU,UAAU,CAAC,KAAY,EAAE,UAAkB;IACzD,OAAO;QACL,QAAQ,EAAE,KAAK,CAAC,OAAO;QACvB,IAAI,EAAE,KAAK,CAAC,IAAI;QAChB,QAAQ,EAAE,KAAK,CAAC,OAAO;QACvB,WAAW,EAAE,KAAK,CAAC,UAAU;QAC7B,YAAY,EAAE,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,OAAO,CAAC;QAC3C,UAAU,EAAE,IAAI,CA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AG,EAAE,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACpD,OAAO,EAAE,MAAM,EAAE,sBAAsB,YAAY,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC;AACzE,CAAC"}
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Database connection lifecycle.
3
+ *
4
+ * Opens a SQLite file (or in-memory database), enables sane pragmas,
5
+ * applies migrations, and returns the connection.
6
+ */
7
+ import Database from 'better-sqlite3';
8
+ export interface OpenOptions {
9
+ /**
10
+ * Filesystem path to the database file, or `:memory:` for an ephemeral
11
+ * in-process database (useful for tests).
12
+ */
13
+ readonly path: string;
14
+ /** Open in read-only mode. Migrations are skipped. */
15
+ readonly readOnly?: boolean;
16
+ }
17
+ export interface AletheiaConnection {
18
+ readonly db: Database.Database;
19
+ /**
20
+ * Close the SQLite connection.
21
+ *
22
+ * @remarks
23
+ * Subsequent calls are no-ops so hosts can safely call `close()` from
24
+ * `finally` blocks without tracking ownership twice.
25
+ */
26
+ close(): void;
27
+ }
28
+ /**
29
+ * Open a connection, enable WAL + foreign keys, and apply migrations.
30
+ *
31
+ * @remarks
32
+ * In write mode, migrations are applied immediately and idempotently. In
33
+ * read-only mode, migrations are skipped because SQLite cannot write the
34
+ * `schema_migrations` table.
35
+ */
36
+ export declare function openConnection(options: OpenOptions): AletheiaConnection;
37
+ //# sourceMappingURL=connection.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"connection.d.ts","sourceRoot":"","sources":["../src/connection.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,QAAQ,MAAM,gBAAgB,CAAC;AAGtC,MAAM,WAAW,WAAW;IAC1B;;;OAGG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,sDAAsD;IACtD,QAAQ,CAAC,QAAQ,CAAC,EAAE,OAAO,CAAC;CAC7B;AAED,MAAM,WAAW,kBAAkB;IACjC,QAAQ,CAAC,EAAE,EAAE,QAAQ,CAAC,QAAQ,CAAC;IAC/B;;;;;;OAMG;IACH,KAAK,IAAI,IAAI,CAAC;CACf;AAED;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,WAAW,GAAG,kBAAkB,CA0BvE"}
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Database connection lifecycle.
3
+ *
4
+ * Opens a SQLite file (or in-memory database), enables sane pragmas,
5
+ * applies migrations, and returns the connection.
6
+ */
7
+ import Database from 'better-sqlite3';
8
+ import { applyMigrations } from './migrations.js';
9
+ /**
10
+ * Open a connection, enable WAL + foreign keys, and apply migrations.
11
+ *
12
+ * @remarks
13
+ * In write mode, migrations are applied immediately and idempotently. In
14
+ * read-only mode, migrations are skipped because SQLite cannot write the
15
+ * `schema_migrations` table.
16
+ */
17
+ export function openConnection(options) {
18
+ const db = new Database(options.path, {
19
+ readonly: options.readOnly ?? false,
20
+ fileMustExist: false,
21
+ });
22
+ // Pragmas: WAL for concurrent readers, foreign keys ON (off by default in SQLite),
23
+ // synchronous=NORMAL for a reasonable durability/perf trade-off in WAL mode.
24
+ if (!options.readOnly) {
25
+ db.pragma('journal_mode = WAL');
26
+ db.pragma('synchronous = NORMAL');
27
+ db.pragma('foreign_keys = ON');
28
+ applyMigrations(db);
29
+ }
30
+ else {
31
+ db.pragma('foreign_keys = ON');
32
+ }
33
+ let closed = false;
34
+ return {
35
+ db,
36
+ close() {
37
+ if (closed)
38
+ return;
39
+ closed = true;
40
+ db.close();
41
+ },
42
+ };
43
+ }
44
+ //# sourceMappingURL=connection.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"connection.js","sourceRoot":"","sources":["../src/connection.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,QAAQ,MAAM,gBAAgB,CAAC;AACtC,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAwBlD;;;;;;;GAOG;AACH,MAAM,UAAU,cAAc,CAAC,OAAoB;IACjD,MAAM,EAAE,GAAG,IAAI,QAAQ,CAAC,OAAO,CAAC,IAAI,EAAE;QACpC,QAAQ,EAAE,OAAO,CAAC,QAAQ,IAAI,KAAK;QACnC,aAAa,EAAE,KAAK;KACrB,CAAC,CAAC;IAEH,mFAAmF;IACnF,6EAA6E;IAC7E,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC;QACtB,EAAE,CAAC,MAAM,CAAC,oBAAoB,CAAC,CAAC;QAChC,EAAE,CAAC,MAAM,CAAC,sBAAsB,CAAC,CAAC;QAClC,EAAE,CAAC,MAAM,CAAC,mBAAmB,CAAC,CAAC;QAC/B,eAAe,CAAC,EAAE,CAAC,CAAC;IACtB,CAAC;SAAM,CAAC;QACN,EAAE,CAAC,MAAM,CAAC,mBAAmB,CAAC,CAAC;IACjC,CAAC;IAED,IAAI,MAAM,GAAG,KAAK,CAAC;IACnB,OAAO;QACL,EAAE;QACF,KAAK;YACH,IAAI,MAAM;gBAAE,OAAO;YACnB,MAAM,GAAG,IAAI,CAAC;YACd,EAAE,CAAC,KAAK,EAAE,CAAC;QACb,CAAC;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,34 @@
1
+ /**
2
+ * @aletheia-labs/store-sqlite
3
+ *
4
+ * SQLite-backed implementations of the @aletheia-labs/core storage interfaces.
5
+ */
6
+ import { type AletheiaConnection, type OpenOptions, openConnection } from './connection.js';
7
+ import { SqliteConflictRegistry } from './sqlite-conflict-registry.js';
8
+ import { SqliteEventLedger } from './sqlite-event-ledger.js';
9
+ import { SqliteMemoryStore } from './sqlite-memory-store.js';
10
+ export { openConnection, type AletheiaConnection, type OpenOptions };
11
+ export { SqliteEventLedger };
12
+ export { SqliteMemoryStore };
13
+ export { SqliteConflictRegistry };
14
+ export { MIGRATIONS, applyMigrations, type Migration } from './migrations.js';
15
+ export interface SqliteStores {
16
+ readonly eventLedger: SqliteEventLedger;
17
+ readonly memoryStore: SqliteMemoryStore;
18
+ readonly conflictRegistry: SqliteConflictRegistry;
19
+ /** Close the shared SQLite connection backing all returned stores. */
20
+ readonly close: () => void;
21
+ }
22
+ /**
23
+ * Open a SQLite database and return the three storage implementations sharing
24
+ * the same connection. Convenience wrapper for the common case.
25
+ *
26
+ * For tests, pass `:memory:`.
27
+ *
28
+ * @remarks
29
+ * Use this as the default composition helper for examples and small hosts.
30
+ * It applies migrations, enables SQLite pragmas through `openConnection()`,
31
+ * and returns one shared `close()` hook for all three stores.
32
+ */
33
+ export declare function openSqliteStores(path: string): SqliteStores;
34
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,KAAK,kBAAkB,EAAE,KAAK,WAAW,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAC5F,OAAO,EAAE,sBAAsB,EAAE,MAAM,+BAA+B,CAAC;AACvE,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAC7D,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAE7D,OAAO,EAAE,cAAc,EAAE,KAAK,kBAAkB,EAAE,KAAK,WAAW,EAAE,CAAC;AACrE,OAAO,EAAE,iBAAiB,EAAE,CAAC;AAC7B,OAAO,EAAE,iBAAiB,EAAE,CAAC;AAC7B,OAAO,EAAE,sBAAsB,EAAE,CAAC;AAClC,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,KAAK,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE9E,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,WAAW,EAAE,iBAAiB,CAAC;IACxC,QAAQ,CAAC,WAAW,EAAE,iBAAiB,CAAC;IACxC,QAAQ,CAAC,gBAAgB,EAAE,sBAAsB,CAAC;IAClD,sEAAsE;IACtE,QAAQ,CAAC,KAAK,EAAE,MAAM,IAAI,CAAC;CAC5B;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,YAAY,CAQ3D"}
package/dist/index.js ADDED
@@ -0,0 +1,35 @@
1
+ /**
2
+ * @aletheia-labs/store-sqlite
3
+ *
4
+ * SQLite-backed implementations of the @aletheia-labs/core storage interfaces.
5
+ */
6
+ import { openConnection } from './connection.js';
7
+ import { SqliteConflictRegistry } from './sqlite-conflict-registry.js';
8
+ import { SqliteEventLedger } from './sqlite-event-ledger.js';
9
+ import { SqliteMemoryStore } from './sqlite-memory-store.js';
10
+ export { openConnection };
11
+ export { SqliteEventLedger };
12
+ export { SqliteMemoryStore };
13
+ export { SqliteConflictRegistry };
14
+ export { MIGRATIONS, applyMigrations } from './migrations.js';
15
+ /**
16
+ * Open a SQLite database and return the three storage implementations sharing
17
+ * the same connection. Convenience wrapper for the common case.
18
+ *
19
+ * For tests, pass `:memory:`.
20
+ *
21
+ * @remarks
22
+ * Use this as the default composition helper for examples and small hosts.
23
+ * It applies migrations, enables SQLite pragmas through `openConnection()`,
24
+ * and returns one shared `close()` hook for all three stores.
25
+ */
26
+ export function openSqliteStores(path) {
27
+ const conn = openConnection({ path });
28
+ return {
29
+ eventLedger: new SqliteEventLedger(conn.db),
30
+ memoryStore: new SqliteMemoryStore(conn.db),
31
+ conflictRegistry: new SqliteConflictRegistry(conn.db),
32
+ close: conn.close,
33
+ };
34
+ }
35
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAA6C,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAC5F,OAAO,EAAE,sBAAsB,EAAE,MAAM,+BAA+B,CAAC;AACvE,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAC7D,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAE7D,OAAO,EAAE,cAAc,EAA6C,CAAC;AACrE,OAAO,EAAE,iBAAiB,EAAE,CAAC;AAC7B,OAAO,EAAE,iBAAiB,EAAE,CAAC;AAC7B,OAAO,EAAE,sBAAsB,EAAE,CAAC;AAClC,OAAO,EAAE,UAAU,EAAE,eAAe,EAAkB,MAAM,iBAAiB,CAAC;AAU9E;;;;;;;;;;GAUG;AACH,MAAM,UAAU,gBAAgB,CAAC,IAAY;IAC3C,MAAM,IAAI,GAAuB,cAAc,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC;IAC1D,OAAO;QACL,WAAW,EAAE,IAAI,iBAAiB,CAAC,IAAI,CAAC,EAAE,CAAC;QAC3C,WAAW,EAAE,IAAI,iBAAiB,CAAC,IAAI,CAAC,EAAE,CAAC;QAC3C,gBAAgB,EAAE,IAAI,sBAAsB,CAAC,IAAI,CAAC,EAAE,CAAC;QACrD,KAAK,EAAE,IAAI,CAAC,KAAK;KAClB,CAAC;AACJ,CAAC"}