@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,230 @@
1
+ import type { Database } from "bun:sqlite"
2
+ import type { AuthSessionAudience } from "@sixb/core"
3
+ import type { AuthSessionStore, CreateAuthSessionInput, SessionRecord } from "@sixb/core/storage"
4
+ import { AuthStorageError } from "@sixb/core/storage"
5
+ import { runImmediateTransaction } from "../transactions"
6
+ import type { SqliteAuthSessionRow } from "./rows"
7
+ import { rowToSessionRecord } from "./rows"
8
+ import {
9
+ createSession,
10
+ getSessionById,
11
+ getSessionRowById,
12
+ requireSessionById,
13
+ revokeActiveSessionsForUser,
14
+ toIso,
15
+ } from "./shared"
16
+
17
+ export class SqliteAuthSessionStore implements AuthSessionStore {
18
+ constructor(private readonly db: Database) {}
19
+
20
+ async create(input: CreateAuthSessionInput): Promise<SessionRecord> {
21
+ return runImmediateTransaction(this.db, () => createSession(this.db, input))
22
+ }
23
+
24
+ async getById(params: {
25
+ readonly projectId: string
26
+ readonly id: string
27
+ }): Promise<SessionRecord | null> {
28
+ return getSessionById(this.db, params)
29
+ }
30
+
31
+ async getActiveByUserId(params: {
32
+ readonly projectId: string
33
+ readonly userId: string
34
+ readonly audience: AuthSessionAudience
35
+ readonly now: Date
36
+ }): Promise<SessionRecord | null> {
37
+ const row = this.db
38
+ .query(
39
+ `
40
+ SELECT *
41
+ FROM auth_sessions
42
+ WHERE project_id = ?
43
+ AND user_id = ?
44
+ AND audience = ?
45
+ AND revoked_at IS NULL
46
+ AND expires_at > ?
47
+ AND (absolute_expires_at IS NULL OR absolute_expires_at > ?)
48
+ ORDER BY created_at DESC, id DESC
49
+ LIMIT 1
50
+ `
51
+ )
52
+ .get(
53
+ params.projectId,
54
+ params.userId,
55
+ params.audience,
56
+ toIso(params.now),
57
+ toIso(params.now)
58
+ ) as SqliteAuthSessionRow | null
59
+
60
+ return row ? rowToSessionRecord(row) : null
61
+ }
62
+
63
+ async listActiveByUserId(params: {
64
+ readonly projectId: string
65
+ readonly userId: string
66
+ readonly now: Date
67
+ }): Promise<readonly SessionRecord[]> {
68
+ const rows = this.db
69
+ .query(
70
+ `
71
+ SELECT *
72
+ FROM auth_sessions
73
+ WHERE project_id = ?
74
+ AND user_id = ?
75
+ AND revoked_at IS NULL
76
+ AND expires_at > ?
77
+ AND (absolute_expires_at IS NULL OR absolute_expires_at > ?)
78
+ ORDER BY COALESCE(last_seen_at, created_at) DESC, created_at DESC, id DESC
79
+ `
80
+ )
81
+ .all(
82
+ params.projectId,
83
+ params.userId,
84
+ toIso(params.now),
85
+ toIso(params.now)
86
+ ) as SqliteAuthSessionRow[]
87
+
88
+ return rows.map(rowToSessionRecord)
89
+ }
90
+
91
+ async findValidByTokenHash(params: {
92
+ readonly projectId: string
93
+ readonly id: string
94
+ readonly audience: AuthSessionAudience
95
+ readonly tokenHash: string
96
+ readonly now: Date
97
+ }): Promise<SessionRecord | null> {
98
+ const row = getSessionRowById(this.db, params)
99
+
100
+ if (
101
+ !row ||
102
+ row.audience !== params.audience ||
103
+ row.token_hash !== params.tokenHash ||
104
+ row.revoked_at ||
105
+ new Date(row.expires_at) <= params.now ||
106
+ (row.absolute_expires_at !== null && new Date(row.absolute_expires_at) <= params.now)
107
+ ) {
108
+ return null
109
+ }
110
+
111
+ return rowToSessionRecord(row)
112
+ }
113
+
114
+ async renewIfValid(params: {
115
+ readonly projectId: string
116
+ readonly id: string
117
+ readonly audience: AuthSessionAudience
118
+ readonly tokenHash: string
119
+ readonly now: Date
120
+ readonly expiresAt: Date
121
+ }): Promise<SessionRecord | null> {
122
+ return runImmediateTransaction(this.db, () => {
123
+ const now = toIso(params.now)
124
+ const expiresAt = toIso(params.expiresAt)
125
+ const result = this.db
126
+ .query(
127
+ `
128
+ UPDATE auth_sessions
129
+ SET expires_at = MAX(
130
+ expires_at,
131
+ MIN(?, COALESCE(absolute_expires_at, ?))
132
+ ),
133
+ last_seen_at = ?
134
+ WHERE project_id = ?
135
+ AND id = ?
136
+ AND audience = ?
137
+ AND token_hash = ?
138
+ AND revoked_at IS NULL
139
+ AND expires_at > ?
140
+ AND (absolute_expires_at IS NULL OR absolute_expires_at > ?)
141
+ `
142
+ )
143
+ .run(
144
+ expiresAt,
145
+ expiresAt,
146
+ now,
147
+ params.projectId,
148
+ params.id,
149
+ params.audience,
150
+ params.tokenHash,
151
+ now,
152
+ now
153
+ )
154
+
155
+ return result.changes === 0 ? null : getSessionById(this.db, params)
156
+ })
157
+ }
158
+
159
+ async revoke(params: {
160
+ readonly projectId: string
161
+ readonly id: string
162
+ readonly revokedAt: Date
163
+ }): Promise<SessionRecord> {
164
+ return runImmediateTransaction(this.db, () => {
165
+ const existing = getSessionRowById(this.db, params)
166
+
167
+ if (!existing) {
168
+ throw new AuthStorageError(
169
+ "missing_session",
170
+ `[Sixb] Session '${params.id}' not found for project '${params.projectId}'.`
171
+ )
172
+ }
173
+
174
+ this.db
175
+ .query(
176
+ `
177
+ UPDATE auth_sessions
178
+ SET revoked_at = ?
179
+ WHERE project_id = ?
180
+ AND id = ?
181
+ `
182
+ )
183
+ .run(toIso(params.revokedAt), params.projectId, params.id)
184
+
185
+ return rowToSessionRecord({
186
+ ...existing,
187
+ revoked_at: toIso(params.revokedAt),
188
+ })
189
+ })
190
+ }
191
+
192
+ async revokeActiveForUser(params: {
193
+ readonly projectId: string
194
+ readonly userId: string
195
+ readonly audience?: AuthSessionAudience
196
+ readonly revokedAt: Date
197
+ }): Promise<readonly SessionRecord[]> {
198
+ return runImmediateTransaction(this.db, () => revokeActiveSessionsForUser(this.db, params))
199
+ }
200
+
201
+ async touch(params: {
202
+ readonly projectId: string
203
+ readonly id: string
204
+ readonly lastSeenAt: Date
205
+ }): Promise<SessionRecord> {
206
+ return runImmediateTransaction(this.db, () => {
207
+ requireSessionById(this.db, params)
208
+
209
+ this.db
210
+ .query(
211
+ `
212
+ UPDATE auth_sessions
213
+ SET last_seen_at = ?
214
+ WHERE project_id = ?
215
+ AND id = ?
216
+ `
217
+ )
218
+ .run(toIso(params.lastSeenAt), params.projectId, params.id)
219
+
220
+ const updated = getSessionById(this.db, params)
221
+ if (!updated) {
222
+ throw new AuthStorageError(
223
+ "missing_session",
224
+ `[Sixb] Session '${params.id}' not found for project '${params.projectId}'.`
225
+ )
226
+ }
227
+ return updated
228
+ })
229
+ }
230
+ }