@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.
- package/LICENSE +21 -0
- package/README.md +57 -0
- package/dist/action-run-storage.d.ts +29 -0
- package/dist/action-run-storage.d.ts.map +1 -0
- package/dist/agents/agent-storage.d.ts +25 -0
- package/dist/agents/agent-storage.d.ts.map +1 -0
- package/dist/agents/index.d.ts +6 -0
- package/dist/agents/index.d.ts.map +1 -0
- package/dist/agents/messages.d.ts +14 -0
- package/dist/agents/messages.d.ts.map +1 -0
- package/dist/agents/rows.d.ts +77 -0
- package/dist/agents/rows.d.ts.map +1 -0
- package/dist/agents/runs.d.ts +26 -0
- package/dist/agents/runs.d.ts.map +1 -0
- package/dist/agents/threads.d.ts +14 -0
- package/dist/agents/threads.d.ts.map +1 -0
- package/dist/auth-storage/access-tokens.d.ts +33 -0
- package/dist/auth-storage/access-tokens.d.ts.map +1 -0
- package/dist/auth-storage/auth-storage.d.ts +46 -0
- package/dist/auth-storage/auth-storage.d.ts.map +1 -0
- package/dist/auth-storage/group-memberships.d.ts +21 -0
- package/dist/auth-storage/group-memberships.d.ts.map +1 -0
- package/dist/auth-storage/identities.d.ts +17 -0
- package/dist/auth-storage/identities.d.ts.map +1 -0
- package/dist/auth-storage/index.d.ts +3 -0
- package/dist/auth-storage/index.d.ts.map +1 -0
- package/dist/auth-storage/invitations.d.ts +28 -0
- package/dist/auth-storage/invitations.d.ts.map +1 -0
- package/dist/auth-storage/magic-links.d.ts +28 -0
- package/dist/auth-storage/magic-links.d.ts.map +1 -0
- package/dist/auth-storage/oidc-attempts.d.ts +18 -0
- package/dist/auth-storage/oidc-attempts.d.ts.map +1 -0
- package/dist/auth-storage/rows.d.ts +134 -0
- package/dist/auth-storage/rows.d.ts.map +1 -0
- package/dist/auth-storage/service-account-group-memberships.d.ts +17 -0
- package/dist/auth-storage/service-account-group-memberships.d.ts.map +1 -0
- package/dist/auth-storage/service-accounts.d.ts +14 -0
- package/dist/auth-storage/service-accounts.d.ts.map +1 -0
- package/dist/auth-storage/sessions.d.ts +55 -0
- package/dist/auth-storage/sessions.d.ts.map +1 -0
- package/dist/auth-storage/shared.d.ts +131 -0
- package/dist/auth-storage/shared.d.ts.map +1 -0
- package/dist/auth-storage/users.d.ts +19 -0
- package/dist/auth-storage/users.d.ts.map +1 -0
- package/dist/index.d.ts +71 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +10204 -0
- package/dist/index.js.map +54 -0
- package/dist/latest-run-query.d.ts +22 -0
- package/dist/latest-run-query.d.ts.map +1 -0
- package/dist/migrations.d.ts +18 -0
- package/dist/migrations.d.ts.map +1 -0
- package/dist/object-query-compiler.d.ts +44 -0
- package/dist/object-query-compiler.d.ts.map +1 -0
- package/dist/object-storage.d.ts +93 -0
- package/dist/object-storage.d.ts.map +1 -0
- package/dist/ontology-storage/commits.d.ts +14 -0
- package/dist/ontology-storage/commits.d.ts.map +1 -0
- package/dist/ontology-storage/index.d.ts +22 -0
- package/dist/ontology-storage/index.d.ts.map +1 -0
- package/dist/ontology-storage/materialization-session.d.ts +49 -0
- package/dist/ontology-storage/materialization-session.d.ts.map +1 -0
- package/dist/ontology-storage/materialization-state.d.ts +63 -0
- package/dist/ontology-storage/materialization-state.d.ts.map +1 -0
- package/dist/ontology-storage/materialization-writer.d.ts +15 -0
- package/dist/ontology-storage/materialization-writer.d.ts.map +1 -0
- package/dist/ontology-storage/materializations.d.ts +32 -0
- package/dist/ontology-storage/materializations.d.ts.map +1 -0
- package/dist/ontology-storage/outbox.d.ts +14 -0
- package/dist/ontology-storage/outbox.d.ts.map +1 -0
- package/dist/ontology-storage/shared.d.ts +91 -0
- package/dist/ontology-storage/shared.d.ts.map +1 -0
- package/dist/ontology-storage/sources.d.ts +26 -0
- package/dist/ontology-storage/sources.d.ts.map +1 -0
- package/dist/pipeline-run-storage.d.ts +26 -0
- package/dist/pipeline-run-storage.d.ts.map +1 -0
- package/dist/projection-run-storage.d.ts +32 -0
- package/dist/projection-run-storage.d.ts.map +1 -0
- package/dist/rules-storage.d.ts +33 -0
- package/dist/rules-storage.d.ts.map +1 -0
- package/dist/run-list-query.d.ts +27 -0
- package/dist/run-list-query.d.ts.map +1 -0
- package/dist/storage-errors.d.ts +2 -0
- package/dist/storage-errors.d.ts.map +1 -0
- package/dist/sync-run-storage.d.ts +23 -0
- package/dist/sync-run-storage.d.ts.map +1 -0
- package/dist/timeseries-storage.d.ts +41 -0
- package/dist/timeseries-storage.d.ts.map +1 -0
- package/dist/transactions.d.ts +15 -0
- package/dist/transactions.d.ts.map +1 -0
- package/dist/tsconfig.json +5 -0
- package/dist/validation.d.ts +5 -0
- package/dist/validation.d.ts.map +1 -0
- package/dist/webhook-delivery-storage.d.ts +27 -0
- package/dist/webhook-delivery-storage.d.ts.map +1 -0
- package/dist/webhook-run-storage.d.ts +22 -0
- package/dist/webhook-run-storage.d.ts.map +1 -0
- package/dist/workflow-intervention-storage.d.ts +26 -0
- package/dist/workflow-intervention-storage.d.ts.map +1 -0
- package/dist/workflow-run-storage.d.ts +63 -0
- package/dist/workflow-run-storage.d.ts.map +1 -0
- package/package.json +58 -0
- package/src/action-run-storage.ts +761 -0
- package/src/agents/agent-storage.ts +46 -0
- package/src/agents/index.ts +5 -0
- package/src/agents/messages.ts +162 -0
- package/src/agents/rows.ts +205 -0
- package/src/agents/runs.ts +389 -0
- package/src/agents/threads.ts +133 -0
- package/src/auth-storage/access-tokens.ts +289 -0
- package/src/auth-storage/auth-storage.ts +784 -0
- package/src/auth-storage/group-memberships.ts +52 -0
- package/src/auth-storage/identities.ts +98 -0
- package/src/auth-storage/index.ts +2 -0
- package/src/auth-storage/invitations.ts +258 -0
- package/src/auth-storage/magic-links.ts +149 -0
- package/src/auth-storage/oidc-attempts.ts +109 -0
- package/src/auth-storage/rows.ts +338 -0
- package/src/auth-storage/service-account-group-memberships.ts +161 -0
- package/src/auth-storage/service-accounts.ts +168 -0
- package/src/auth-storage/sessions.ts +230 -0
- package/src/auth-storage/shared.ts +707 -0
- package/src/auth-storage/users.ts +226 -0
- package/src/index.ts +292 -0
- package/src/latest-run-query.ts +49 -0
- package/src/migrations/001-initial-schema.sql +1068 -0
- package/src/migrations.ts +245 -0
- package/src/object-query-compiler.ts +1198 -0
- package/src/object-storage.ts +605 -0
- package/src/ontology-storage/commits.ts +127 -0
- package/src/ontology-storage/index.ts +35 -0
- package/src/ontology-storage/materialization-session.ts +393 -0
- package/src/ontology-storage/materialization-state.ts +901 -0
- package/src/ontology-storage/materialization-writer.ts +394 -0
- package/src/ontology-storage/materializations.ts +838 -0
- package/src/ontology-storage/outbox.ts +237 -0
- package/src/ontology-storage/shared.ts +269 -0
- package/src/ontology-storage/sources.ts +653 -0
- package/src/pipeline-run-storage.ts +506 -0
- package/src/projection-run-storage.ts +592 -0
- package/src/rules-storage.ts +375 -0
- package/src/run-list-query.ts +98 -0
- package/src/sql.d.ts +4 -0
- package/src/storage-errors.ts +3 -0
- package/src/sync-run-storage.ts +299 -0
- package/src/timeseries-storage.ts +212 -0
- package/src/transactions.ts +96 -0
- package/src/validation.ts +30 -0
- package/src/webhook-delivery-storage.ts +272 -0
- package/src/webhook-run-storage.ts +240 -0
- package/src/workflow-intervention-storage.ts +398 -0
- 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
|
+
}
|