@sixb/sqlite 0.0.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.
Files changed (152) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +57 -0
  3. package/dist/action-run-storage.d.ts +29 -0
  4. package/dist/action-run-storage.d.ts.map +1 -0
  5. package/dist/agents/agent-storage.d.ts +25 -0
  6. package/dist/agents/agent-storage.d.ts.map +1 -0
  7. package/dist/agents/index.d.ts +6 -0
  8. package/dist/agents/index.d.ts.map +1 -0
  9. package/dist/agents/messages.d.ts +14 -0
  10. package/dist/agents/messages.d.ts.map +1 -0
  11. package/dist/agents/rows.d.ts +77 -0
  12. package/dist/agents/rows.d.ts.map +1 -0
  13. package/dist/agents/runs.d.ts +26 -0
  14. package/dist/agents/runs.d.ts.map +1 -0
  15. package/dist/agents/threads.d.ts +14 -0
  16. package/dist/agents/threads.d.ts.map +1 -0
  17. package/dist/auth-storage/access-tokens.d.ts +33 -0
  18. package/dist/auth-storage/access-tokens.d.ts.map +1 -0
  19. package/dist/auth-storage/auth-storage.d.ts +46 -0
  20. package/dist/auth-storage/auth-storage.d.ts.map +1 -0
  21. package/dist/auth-storage/group-memberships.d.ts +21 -0
  22. package/dist/auth-storage/group-memberships.d.ts.map +1 -0
  23. package/dist/auth-storage/identities.d.ts +17 -0
  24. package/dist/auth-storage/identities.d.ts.map +1 -0
  25. package/dist/auth-storage/index.d.ts +3 -0
  26. package/dist/auth-storage/index.d.ts.map +1 -0
  27. package/dist/auth-storage/invitations.d.ts +28 -0
  28. package/dist/auth-storage/invitations.d.ts.map +1 -0
  29. package/dist/auth-storage/magic-links.d.ts +28 -0
  30. package/dist/auth-storage/magic-links.d.ts.map +1 -0
  31. package/dist/auth-storage/oidc-attempts.d.ts +18 -0
  32. package/dist/auth-storage/oidc-attempts.d.ts.map +1 -0
  33. package/dist/auth-storage/rows.d.ts +134 -0
  34. package/dist/auth-storage/rows.d.ts.map +1 -0
  35. package/dist/auth-storage/service-account-group-memberships.d.ts +17 -0
  36. package/dist/auth-storage/service-account-group-memberships.d.ts.map +1 -0
  37. package/dist/auth-storage/service-accounts.d.ts +14 -0
  38. package/dist/auth-storage/service-accounts.d.ts.map +1 -0
  39. package/dist/auth-storage/sessions.d.ts +55 -0
  40. package/dist/auth-storage/sessions.d.ts.map +1 -0
  41. package/dist/auth-storage/shared.d.ts +131 -0
  42. package/dist/auth-storage/shared.d.ts.map +1 -0
  43. package/dist/auth-storage/users.d.ts +19 -0
  44. package/dist/auth-storage/users.d.ts.map +1 -0
  45. package/dist/index.d.ts +71 -0
  46. package/dist/index.d.ts.map +1 -0
  47. package/dist/index.js +10204 -0
  48. package/dist/index.js.map +54 -0
  49. package/dist/latest-run-query.d.ts +22 -0
  50. package/dist/latest-run-query.d.ts.map +1 -0
  51. package/dist/migrations.d.ts +18 -0
  52. package/dist/migrations.d.ts.map +1 -0
  53. package/dist/object-query-compiler.d.ts +44 -0
  54. package/dist/object-query-compiler.d.ts.map +1 -0
  55. package/dist/object-storage.d.ts +93 -0
  56. package/dist/object-storage.d.ts.map +1 -0
  57. package/dist/ontology-storage/commits.d.ts +14 -0
  58. package/dist/ontology-storage/commits.d.ts.map +1 -0
  59. package/dist/ontology-storage/index.d.ts +22 -0
  60. package/dist/ontology-storage/index.d.ts.map +1 -0
  61. package/dist/ontology-storage/materialization-session.d.ts +49 -0
  62. package/dist/ontology-storage/materialization-session.d.ts.map +1 -0
  63. package/dist/ontology-storage/materialization-state.d.ts +63 -0
  64. package/dist/ontology-storage/materialization-state.d.ts.map +1 -0
  65. package/dist/ontology-storage/materialization-writer.d.ts +15 -0
  66. package/dist/ontology-storage/materialization-writer.d.ts.map +1 -0
  67. package/dist/ontology-storage/materializations.d.ts +32 -0
  68. package/dist/ontology-storage/materializations.d.ts.map +1 -0
  69. package/dist/ontology-storage/outbox.d.ts +14 -0
  70. package/dist/ontology-storage/outbox.d.ts.map +1 -0
  71. package/dist/ontology-storage/shared.d.ts +91 -0
  72. package/dist/ontology-storage/shared.d.ts.map +1 -0
  73. package/dist/ontology-storage/sources.d.ts +26 -0
  74. package/dist/ontology-storage/sources.d.ts.map +1 -0
  75. package/dist/pipeline-run-storage.d.ts +26 -0
  76. package/dist/pipeline-run-storage.d.ts.map +1 -0
  77. package/dist/projection-run-storage.d.ts +32 -0
  78. package/dist/projection-run-storage.d.ts.map +1 -0
  79. package/dist/rules-storage.d.ts +33 -0
  80. package/dist/rules-storage.d.ts.map +1 -0
  81. package/dist/run-list-query.d.ts +27 -0
  82. package/dist/run-list-query.d.ts.map +1 -0
  83. package/dist/storage-errors.d.ts +2 -0
  84. package/dist/storage-errors.d.ts.map +1 -0
  85. package/dist/sync-run-storage.d.ts +23 -0
  86. package/dist/sync-run-storage.d.ts.map +1 -0
  87. package/dist/timeseries-storage.d.ts +41 -0
  88. package/dist/timeseries-storage.d.ts.map +1 -0
  89. package/dist/transactions.d.ts +15 -0
  90. package/dist/transactions.d.ts.map +1 -0
  91. package/dist/tsconfig.json +5 -0
  92. package/dist/validation.d.ts +5 -0
  93. package/dist/validation.d.ts.map +1 -0
  94. package/dist/webhook-delivery-storage.d.ts +27 -0
  95. package/dist/webhook-delivery-storage.d.ts.map +1 -0
  96. package/dist/webhook-run-storage.d.ts +22 -0
  97. package/dist/webhook-run-storage.d.ts.map +1 -0
  98. package/dist/workflow-intervention-storage.d.ts +26 -0
  99. package/dist/workflow-intervention-storage.d.ts.map +1 -0
  100. package/dist/workflow-run-storage.d.ts +63 -0
  101. package/dist/workflow-run-storage.d.ts.map +1 -0
  102. package/package.json +58 -0
  103. package/src/action-run-storage.ts +761 -0
  104. package/src/agents/agent-storage.ts +46 -0
  105. package/src/agents/index.ts +5 -0
  106. package/src/agents/messages.ts +162 -0
  107. package/src/agents/rows.ts +205 -0
  108. package/src/agents/runs.ts +389 -0
  109. package/src/agents/threads.ts +133 -0
  110. package/src/auth-storage/access-tokens.ts +289 -0
  111. package/src/auth-storage/auth-storage.ts +784 -0
  112. package/src/auth-storage/group-memberships.ts +52 -0
  113. package/src/auth-storage/identities.ts +98 -0
  114. package/src/auth-storage/index.ts +2 -0
  115. package/src/auth-storage/invitations.ts +258 -0
  116. package/src/auth-storage/magic-links.ts +149 -0
  117. package/src/auth-storage/oidc-attempts.ts +109 -0
  118. package/src/auth-storage/rows.ts +338 -0
  119. package/src/auth-storage/service-account-group-memberships.ts +161 -0
  120. package/src/auth-storage/service-accounts.ts +168 -0
  121. package/src/auth-storage/sessions.ts +230 -0
  122. package/src/auth-storage/shared.ts +707 -0
  123. package/src/auth-storage/users.ts +226 -0
  124. package/src/index.ts +292 -0
  125. package/src/latest-run-query.ts +49 -0
  126. package/src/migrations/001-initial-schema.sql +1068 -0
  127. package/src/migrations.ts +245 -0
  128. package/src/object-query-compiler.ts +1198 -0
  129. package/src/object-storage.ts +605 -0
  130. package/src/ontology-storage/commits.ts +127 -0
  131. package/src/ontology-storage/index.ts +35 -0
  132. package/src/ontology-storage/materialization-session.ts +393 -0
  133. package/src/ontology-storage/materialization-state.ts +901 -0
  134. package/src/ontology-storage/materialization-writer.ts +394 -0
  135. package/src/ontology-storage/materializations.ts +838 -0
  136. package/src/ontology-storage/outbox.ts +237 -0
  137. package/src/ontology-storage/shared.ts +269 -0
  138. package/src/ontology-storage/sources.ts +653 -0
  139. package/src/pipeline-run-storage.ts +506 -0
  140. package/src/projection-run-storage.ts +592 -0
  141. package/src/rules-storage.ts +375 -0
  142. package/src/run-list-query.ts +98 -0
  143. package/src/sql.d.ts +4 -0
  144. package/src/storage-errors.ts +3 -0
  145. package/src/sync-run-storage.ts +299 -0
  146. package/src/timeseries-storage.ts +212 -0
  147. package/src/transactions.ts +96 -0
  148. package/src/validation.ts +30 -0
  149. package/src/webhook-delivery-storage.ts +272 -0
  150. package/src/webhook-run-storage.ts +240 -0
  151. package/src/workflow-intervention-storage.ts +398 -0
  152. package/src/workflow-run-storage.ts +1148 -0
@@ -0,0 +1,245 @@
1
+ import { Database } from "bun:sqlite"
2
+ import { createHash } from "node:crypto"
3
+ import { existsSync, mkdirSync } from "node:fs"
4
+ import { dirname } from "node:path"
5
+ import type { StorageMigrator } from "@sixb/core"
6
+ import type {
7
+ MigrationHistoryStore,
8
+ MigrationRecord,
9
+ MigrationSet,
10
+ MigrationStep,
11
+ MigrationStepOptions,
12
+ } from "@sixb/core/storage"
13
+ import {
14
+ defineMigrations,
15
+ describeMigrationHistory,
16
+ runMigrationSet,
17
+ step,
18
+ } from "@sixb/core/storage"
19
+ import initialSchemaSql from "./migrations/001-initial-schema.sql" with { type: "text" }
20
+
21
+ const MIGRATIONS_TABLE_SQL = `
22
+ CREATE TABLE IF NOT EXISTS sixb_migrations (
23
+ adapter_id TEXT NOT NULL,
24
+ version INTEGER NOT NULL,
25
+ id TEXT NOT NULL,
26
+ checksum TEXT,
27
+ status TEXT NOT NULL,
28
+ started_at TEXT NOT NULL,
29
+ finished_at TEXT,
30
+ PRIMARY KEY (adapter_id, version)
31
+ );
32
+ `
33
+
34
+ export const SQLITE_STORAGE_ADAPTER_ID = "SixbSqliteStorage"
35
+ export const SQLITE_STORAGE_FILE = "storage.sqlite"
36
+
37
+ export const sqliteStorageMigrations = defineMigrations({
38
+ adapterId: SQLITE_STORAGE_ADAPTER_ID,
39
+ steps: [sqliteSql("001-initial-schema", initialSchemaSql)],
40
+ })
41
+
42
+ export function sqliteStoragePath(basePath: string): string {
43
+ return `${basePath}/${SQLITE_STORAGE_FILE}`
44
+ }
45
+
46
+ export function sqliteSql(id: string, sqlText: string): MigrationStep<Database> {
47
+ return sqliteStep(
48
+ id,
49
+ (db) => {
50
+ db.run(sqlText)
51
+ },
52
+ { checksum: checksum(sqlText) }
53
+ )
54
+ }
55
+
56
+ export function sqliteStep(
57
+ id: string,
58
+ up: (db: Database) => void | Promise<void>,
59
+ options: MigrationStepOptions = {}
60
+ ): MigrationStep<Database> {
61
+ return step<Database>(id, up, options)
62
+ }
63
+
64
+ export function installFreshSqliteSchema(db: Database): void {
65
+ for (const migration of sqliteStorageMigrations.steps) {
66
+ const installed = migration.up(db)
67
+ if (installed instanceof Promise) {
68
+ throw new Error(
69
+ `[${sqliteStorageMigrations.adapterId}] Fresh SQLite schema installation must be synchronous`
70
+ )
71
+ }
72
+ }
73
+ }
74
+
75
+ export function createSqliteStorageMigrators(basePath: string): readonly StorageMigrator[] {
76
+ return [
77
+ createSqliteMigrator({
78
+ path: sqliteStoragePath(basePath),
79
+ migrations: sqliteStorageMigrations,
80
+ }),
81
+ ]
82
+ }
83
+
84
+ export function createSqliteMigrator(params: {
85
+ readonly path: string
86
+ readonly migrations: MigrationSet<Database>
87
+ }): StorageMigrator {
88
+ return {
89
+ adapterId: params.migrations.adapterId,
90
+ latestVersion: params.migrations.latestVersion,
91
+ async status() {
92
+ return describeMigrationHistory({
93
+ migrations: params.migrations,
94
+ rows: await readSqliteHistory(params.path, params.migrations.adapterId),
95
+ })
96
+ },
97
+ async migrate() {
98
+ return withSqliteDatabase(params.path, (db) =>
99
+ runMigrationSet({
100
+ context: db,
101
+ migrations: params.migrations,
102
+ state: sqliteMigrationHistoryStore(db),
103
+ })
104
+ )
105
+ },
106
+ }
107
+ }
108
+
109
+ export async function migrateSqliteStorage(basePath: string): Promise<void> {
110
+ mkdirSync(basePath, { recursive: true })
111
+
112
+ for (const migrator of createSqliteStorageMigrators(basePath)) {
113
+ await migrator.migrate()
114
+ }
115
+ }
116
+
117
+ export async function migrateSqliteDatabase(path: string): Promise<void> {
118
+ await createSqliteMigrator({ path, migrations: sqliteStorageMigrations }).migrate()
119
+ }
120
+
121
+ function sqliteMigrationHistoryStore(db: Database): MigrationHistoryStore {
122
+ return {
123
+ ensure() {
124
+ db.run(MIGRATIONS_TABLE_SQL)
125
+ },
126
+ readHistory(adapterId) {
127
+ return db
128
+ .query("SELECT * FROM sixb_migrations WHERE adapter_id = ? ORDER BY version")
129
+ .all(adapterId)
130
+ .map(rowToMigrationRecord)
131
+ },
132
+ markStarted(adapterId, migration, at) {
133
+ db.query(`
134
+ INSERT INTO sixb_migrations (
135
+ adapter_id, version, id, checksum, status, started_at, finished_at
136
+ ) VALUES (?, ?, ?, ?, 'started', ?, NULL)
137
+ ON CONFLICT(adapter_id, version) DO UPDATE SET
138
+ id = excluded.id,
139
+ checksum = excluded.checksum,
140
+ status = excluded.status,
141
+ started_at = excluded.started_at,
142
+ finished_at = NULL
143
+ `).run(adapterId, migration.version, migration.id, migration.checksum ?? null, at)
144
+ },
145
+ markApplied(adapterId, migration, at) {
146
+ db.query(`
147
+ UPDATE sixb_migrations
148
+ SET status = 'applied', finished_at = ?
149
+ WHERE adapter_id = ? AND version = ?
150
+ `).run(at, adapterId, migration.version)
151
+ },
152
+ async transaction(run) {
153
+ db.run("BEGIN")
154
+
155
+ try {
156
+ const result = await run()
157
+ db.run("COMMIT")
158
+ return result
159
+ } catch (error) {
160
+ rollbackQuietly(db)
161
+ throw error
162
+ }
163
+ },
164
+ }
165
+ }
166
+
167
+ function rowToMigrationRecord(row: unknown): MigrationRecord {
168
+ const migration = row as {
169
+ adapter_id: string
170
+ version: number
171
+ id: string
172
+ checksum: string | null
173
+ status: "started" | "applied"
174
+ started_at: string
175
+ finished_at: string | null
176
+ }
177
+
178
+ return {
179
+ adapterId: migration.adapter_id,
180
+ version: migration.version,
181
+ id: migration.id,
182
+ checksum: migration.checksum ?? undefined,
183
+ status: migration.status,
184
+ startedAt: migration.started_at,
185
+ finishedAt: migration.finished_at ?? undefined,
186
+ }
187
+ }
188
+
189
+ function checksum(value: string): string {
190
+ return createHash("sha256").update(value).digest("hex")
191
+ }
192
+
193
+ /**
194
+ * Reads migration history without creating anything. `null` means there is no history
195
+ * to read, which is a state and not a failure.
196
+ *
197
+ * `new Database(path)` creates the file, and `withSqliteDatabase` additionally mkdirs
198
+ * its parent — fine on the way to a migration, wrong for a probe. So: `existsSync`
199
+ * first, then open `readonly` so even a mistake below cannot write. `:memory:` is
200
+ * always empty on a fresh connection, so there is nothing to report.
201
+ */
202
+ async function readSqliteHistory(
203
+ path: string,
204
+ adapterId: string
205
+ ): Promise<readonly MigrationRecord[] | null> {
206
+ if (path === ":memory:" || !existsSync(path)) {
207
+ return null
208
+ }
209
+
210
+ const db = new Database(path, { readonly: true })
211
+ try {
212
+ const table = db
213
+ .query("SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'sixb_migrations'")
214
+ .get()
215
+ if (!table) {
216
+ return null
217
+ }
218
+
219
+ return db
220
+ .query("SELECT * FROM sixb_migrations WHERE adapter_id = ? ORDER BY version")
221
+ .all(adapterId)
222
+ .map(rowToMigrationRecord)
223
+ } finally {
224
+ db.close()
225
+ }
226
+ }
227
+
228
+ async function withSqliteDatabase<T>(path: string, run: (db: Database) => Promise<T>): Promise<T> {
229
+ if (path !== ":memory:") mkdirSync(dirname(path), { recursive: true })
230
+ const db = new Database(path)
231
+
232
+ try {
233
+ return await run(db)
234
+ } finally {
235
+ db.close()
236
+ }
237
+ }
238
+
239
+ function rollbackQuietly(db: Database): void {
240
+ try {
241
+ db.run("ROLLBACK")
242
+ } catch {
243
+ // Ignore rollback errors so the original migration failure is preserved.
244
+ }
245
+ }