@sixb/pg 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 +73 -0
- package/dist/agents/agent-storage.d.ts +20 -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 +13 -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 +25 -0
- package/dist/agents/runs.d.ts.map +1 -0
- package/dist/agents/threads.d.ts +13 -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 +41 -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 +29 -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 +138 -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 +159 -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 +127 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +11957 -0
- package/dist/index.js.map +55 -0
- package/dist/latest-run-query.d.ts +24 -0
- package/dist/latest-run-query.d.ts.map +1 -0
- package/dist/migrations.d.ts +20 -0
- package/dist/migrations.d.ts.map +1 -0
- package/dist/ontology-storage/commits.d.ts +11 -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 +63 -0
- package/dist/ontology-storage/materialization-session.d.ts.map +1 -0
- package/dist/ontology-storage/materialization-state.d.ts +69 -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 +33 -0
- package/dist/ontology-storage/materializations.d.ts.map +1 -0
- package/dist/ontology-storage/outbox.d.ts +12 -0
- package/dist/ontology-storage/outbox.d.ts.map +1 -0
- package/dist/ontology-storage/shared.d.ts +95 -0
- package/dist/ontology-storage/shared.d.ts.map +1 -0
- package/dist/ontology-storage/sources.d.ts +25 -0
- package/dist/ontology-storage/sources.d.ts.map +1 -0
- package/dist/pg-action-run-storage.d.ts +21 -0
- package/dist/pg-action-run-storage.d.ts.map +1 -0
- package/dist/pg-client.d.ts +58 -0
- package/dist/pg-client.d.ts.map +1 -0
- package/dist/pg-object-query-compiler.d.ts +55 -0
- package/dist/pg-object-query-compiler.d.ts.map +1 -0
- package/dist/pg-object-storage.d.ts +90 -0
- package/dist/pg-object-storage.d.ts.map +1 -0
- package/dist/pg-pipeline-run-storage.d.ts +18 -0
- package/dist/pg-pipeline-run-storage.d.ts.map +1 -0
- package/dist/pg-projection-run-storage.d.ts +19 -0
- package/dist/pg-projection-run-storage.d.ts.map +1 -0
- package/dist/pg-rules-storage.d.ts +25 -0
- package/dist/pg-rules-storage.d.ts.map +1 -0
- package/dist/pg-sync-run-storage.d.ts +15 -0
- package/dist/pg-sync-run-storage.d.ts.map +1 -0
- package/dist/pg-timeseries-storage.d.ts +32 -0
- package/dist/pg-timeseries-storage.d.ts.map +1 -0
- package/dist/pg-webhook-delivery-storage.d.ts +17 -0
- package/dist/pg-webhook-delivery-storage.d.ts.map +1 -0
- package/dist/pg-webhook-run-storage.d.ts +14 -0
- package/dist/pg-webhook-run-storage.d.ts.map +1 -0
- package/dist/pg-workflow-intervention-storage.d.ts +16 -0
- package/dist/pg-workflow-intervention-storage.d.ts.map +1 -0
- package/dist/pg-workflow-run-storage.d.ts +49 -0
- package/dist/pg-workflow-run-storage.d.ts.map +1 -0
- package/dist/run-list-query.d.ts +30 -0
- package/dist/run-list-query.d.ts.map +1 -0
- package/dist/storage-errors.d.ts +16 -0
- package/dist/storage-errors.d.ts.map +1 -0
- package/dist/transactions.d.ts +10 -0
- package/dist/transactions.d.ts.map +1 -0
- package/dist/tsconfig.json +5 -0
- package/package.json +60 -0
- package/src/agents/agent-storage.ts +26 -0
- package/src/agents/index.ts +5 -0
- package/src/agents/messages.ts +144 -0
- package/src/agents/rows.ts +227 -0
- package/src/agents/runs.ts +330 -0
- package/src/agents/threads.ts +116 -0
- package/src/auth-storage/access-tokens.ts +254 -0
- package/src/auth-storage/auth-storage.ts +798 -0
- package/src/auth-storage/group-memberships.ts +47 -0
- package/src/auth-storage/identities.ts +85 -0
- package/src/auth-storage/index.ts +2 -0
- package/src/auth-storage/invitations.ts +280 -0
- package/src/auth-storage/magic-links.ts +140 -0
- package/src/auth-storage/oidc-attempts.ts +93 -0
- package/src/auth-storage/rows.ts +352 -0
- package/src/auth-storage/service-account-group-memberships.ts +150 -0
- package/src/auth-storage/service-accounts.ts +154 -0
- package/src/auth-storage/sessions.ts +193 -0
- package/src/auth-storage/shared.ts +799 -0
- package/src/auth-storage/users.ts +196 -0
- package/src/index.ts +367 -0
- package/src/latest-run-query.ts +42 -0
- package/src/migrations/001-initial-schema.sql +1195 -0
- package/src/migrations.ts +280 -0
- package/src/ontology-storage/commits.ts +115 -0
- package/src/ontology-storage/index.ts +35 -0
- package/src/ontology-storage/materialization-session.ts +426 -0
- package/src/ontology-storage/materialization-state.ts +1109 -0
- package/src/ontology-storage/materialization-writer.ts +509 -0
- package/src/ontology-storage/materializations.ts +939 -0
- package/src/ontology-storage/outbox.ts +216 -0
- package/src/ontology-storage/shared.ts +293 -0
- package/src/ontology-storage/sources.ts +658 -0
- package/src/pg-action-run-storage.ts +657 -0
- package/src/pg-client.ts +124 -0
- package/src/pg-object-query-compiler.ts +1545 -0
- package/src/pg-object-storage.ts +638 -0
- package/src/pg-pipeline-run-storage.ts +457 -0
- package/src/pg-projection-run-storage.ts +526 -0
- package/src/pg-rules-storage.ts +308 -0
- package/src/pg-sync-run-storage.ts +263 -0
- package/src/pg-timeseries-storage.ts +180 -0
- package/src/pg-webhook-delivery-storage.ts +180 -0
- package/src/pg-webhook-run-storage.ts +185 -0
- package/src/pg-workflow-intervention-storage.ts +339 -0
- package/src/pg-workflow-run-storage.ts +1065 -0
- package/src/run-list-query.ts +108 -0
- package/src/sql.d.ts +4 -0
- package/src/storage-errors.ts +72 -0
- package/src/transactions.ts +58 -0
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import type { AuthSessionAudience } from "@sixb/core"
|
|
2
|
+
import type { AuthSessionStore, CreateAuthSessionInput, SessionRecord } from "@sixb/core/storage"
|
|
3
|
+
import { AuthStorageError } from "@sixb/core/storage"
|
|
4
|
+
import {
|
|
5
|
+
authLockKey,
|
|
6
|
+
lockAdvisoryKeys,
|
|
7
|
+
type PgStoreClient,
|
|
8
|
+
runPgTransaction,
|
|
9
|
+
} from "../transactions"
|
|
10
|
+
import type { PgAuthSessionRow } from "./rows"
|
|
11
|
+
import { rowToSessionRecord } from "./rows"
|
|
12
|
+
import {
|
|
13
|
+
createSession,
|
|
14
|
+
getSessionById,
|
|
15
|
+
getSessionRowById,
|
|
16
|
+
requireSessionById,
|
|
17
|
+
revokeActiveSessionsForUser,
|
|
18
|
+
} from "./shared"
|
|
19
|
+
|
|
20
|
+
export class PgAuthSessionStore implements AuthSessionStore {
|
|
21
|
+
constructor(private readonly sql: PgStoreClient) {}
|
|
22
|
+
|
|
23
|
+
async create(input: CreateAuthSessionInput): Promise<SessionRecord> {
|
|
24
|
+
// Session ids are random UUIDs and the table has a primary-key guard, so
|
|
25
|
+
// concurrent inserts cannot collide; no advisory lock is needed here.
|
|
26
|
+
return runPgTransaction(this.sql, (tx) => createSession(tx, input))
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async getById(params: {
|
|
30
|
+
readonly projectId: string
|
|
31
|
+
readonly id: string
|
|
32
|
+
}): Promise<SessionRecord | null> {
|
|
33
|
+
return getSessionById(this.sql, params)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async getActiveByUserId(params: {
|
|
37
|
+
readonly projectId: string
|
|
38
|
+
readonly userId: string
|
|
39
|
+
readonly audience: AuthSessionAudience
|
|
40
|
+
readonly now: Date
|
|
41
|
+
}): Promise<SessionRecord | null> {
|
|
42
|
+
const [row] = await this.sql<PgAuthSessionRow[]>`
|
|
43
|
+
SELECT *
|
|
44
|
+
FROM auth_sessions
|
|
45
|
+
WHERE project_id = ${params.projectId}
|
|
46
|
+
AND user_id = ${params.userId}
|
|
47
|
+
AND audience = ${params.audience}
|
|
48
|
+
AND revoked_at IS NULL
|
|
49
|
+
AND expires_at > ${params.now}
|
|
50
|
+
AND (absolute_expires_at IS NULL OR absolute_expires_at > ${params.now})
|
|
51
|
+
ORDER BY created_at DESC, id DESC
|
|
52
|
+
LIMIT 1
|
|
53
|
+
`
|
|
54
|
+
|
|
55
|
+
return row ? rowToSessionRecord(row) : null
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async listActiveByUserId(params: {
|
|
59
|
+
readonly projectId: string
|
|
60
|
+
readonly userId: string
|
|
61
|
+
readonly now: Date
|
|
62
|
+
}): Promise<readonly SessionRecord[]> {
|
|
63
|
+
const rows = (await this.sql`
|
|
64
|
+
SELECT *
|
|
65
|
+
FROM auth_sessions
|
|
66
|
+
WHERE project_id = ${params.projectId}
|
|
67
|
+
AND user_id = ${params.userId}
|
|
68
|
+
AND revoked_at IS NULL
|
|
69
|
+
AND expires_at > ${params.now}
|
|
70
|
+
AND (absolute_expires_at IS NULL OR absolute_expires_at > ${params.now})
|
|
71
|
+
ORDER BY COALESCE(last_seen_at, created_at) DESC, created_at DESC, id DESC
|
|
72
|
+
`) as PgAuthSessionRow[]
|
|
73
|
+
|
|
74
|
+
return rows.map(rowToSessionRecord)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async findValidByTokenHash(params: {
|
|
78
|
+
readonly projectId: string
|
|
79
|
+
readonly id: string
|
|
80
|
+
readonly audience: AuthSessionAudience
|
|
81
|
+
readonly tokenHash: string
|
|
82
|
+
readonly now: Date
|
|
83
|
+
}): Promise<SessionRecord | null> {
|
|
84
|
+
const row = await getSessionRowById(this.sql, params)
|
|
85
|
+
|
|
86
|
+
if (
|
|
87
|
+
!row ||
|
|
88
|
+
row.audience !== params.audience ||
|
|
89
|
+
row.token_hash !== params.tokenHash ||
|
|
90
|
+
row.revoked_at ||
|
|
91
|
+
new Date(row.expires_at) <= params.now ||
|
|
92
|
+
(row.absolute_expires_at !== null && new Date(row.absolute_expires_at) <= params.now)
|
|
93
|
+
) {
|
|
94
|
+
return null
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return rowToSessionRecord(row)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async renewIfValid(params: {
|
|
101
|
+
readonly projectId: string
|
|
102
|
+
readonly id: string
|
|
103
|
+
readonly audience: AuthSessionAudience
|
|
104
|
+
readonly tokenHash: string
|
|
105
|
+
readonly now: Date
|
|
106
|
+
readonly expiresAt: Date
|
|
107
|
+
}): Promise<SessionRecord | null> {
|
|
108
|
+
const [row] = await this.sql<PgAuthSessionRow[]>`
|
|
109
|
+
UPDATE auth_sessions
|
|
110
|
+
SET expires_at = GREATEST(
|
|
111
|
+
expires_at,
|
|
112
|
+
LEAST(
|
|
113
|
+
${params.expiresAt},
|
|
114
|
+
COALESCE(absolute_expires_at, ${params.expiresAt})
|
|
115
|
+
)
|
|
116
|
+
),
|
|
117
|
+
last_seen_at = ${params.now}
|
|
118
|
+
WHERE project_id = ${params.projectId}
|
|
119
|
+
AND id = ${params.id}
|
|
120
|
+
AND audience = ${params.audience}
|
|
121
|
+
AND token_hash = ${params.tokenHash}
|
|
122
|
+
AND revoked_at IS NULL
|
|
123
|
+
AND expires_at > ${params.now}
|
|
124
|
+
AND (absolute_expires_at IS NULL OR absolute_expires_at > ${params.now})
|
|
125
|
+
RETURNING *
|
|
126
|
+
`
|
|
127
|
+
|
|
128
|
+
return row ? rowToSessionRecord(row) : null
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
async revoke(params: {
|
|
132
|
+
readonly projectId: string
|
|
133
|
+
readonly id: string
|
|
134
|
+
readonly revokedAt: Date
|
|
135
|
+
}): Promise<SessionRecord> {
|
|
136
|
+
return runPgTransaction(this.sql, async (tx) => {
|
|
137
|
+
const existing = await getSessionRowById(tx, params, { forUpdate: true })
|
|
138
|
+
|
|
139
|
+
if (!existing) {
|
|
140
|
+
throw new AuthStorageError(
|
|
141
|
+
"missing_session",
|
|
142
|
+
`[Sixb] Session '${params.id}' not found for project '${params.projectId}'.`
|
|
143
|
+
)
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const [row] = await tx<PgAuthSessionRow[]>`
|
|
147
|
+
UPDATE auth_sessions
|
|
148
|
+
SET revoked_at = ${params.revokedAt}
|
|
149
|
+
WHERE project_id = ${params.projectId}
|
|
150
|
+
AND id = ${params.id}
|
|
151
|
+
RETURNING *
|
|
152
|
+
`
|
|
153
|
+
|
|
154
|
+
return rowToSessionRecord(row)
|
|
155
|
+
})
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
async revokeActiveForUser(params: {
|
|
159
|
+
readonly projectId: string
|
|
160
|
+
readonly userId: string
|
|
161
|
+
readonly audience?: AuthSessionAudience
|
|
162
|
+
readonly revokedAt: Date
|
|
163
|
+
}): Promise<readonly SessionRecord[]> {
|
|
164
|
+
return runPgTransaction(this.sql, async (tx) => {
|
|
165
|
+
const locks = [authLockKey("sessions", params.projectId, params.userId)]
|
|
166
|
+
if (params.audience) {
|
|
167
|
+
locks.push(authLockKey("sessions", params.projectId, params.userId, params.audience))
|
|
168
|
+
}
|
|
169
|
+
await lockAdvisoryKeys(tx, locks)
|
|
170
|
+
return revokeActiveSessionsForUser(tx, params)
|
|
171
|
+
})
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
async touch(params: {
|
|
175
|
+
readonly projectId: string
|
|
176
|
+
readonly id: string
|
|
177
|
+
readonly lastSeenAt: Date
|
|
178
|
+
}): Promise<SessionRecord> {
|
|
179
|
+
return runPgTransaction(this.sql, async (tx) => {
|
|
180
|
+
await requireSessionById(tx, params)
|
|
181
|
+
|
|
182
|
+
const [row] = await tx<PgAuthSessionRow[]>`
|
|
183
|
+
UPDATE auth_sessions
|
|
184
|
+
SET last_seen_at = ${params.lastSeenAt}
|
|
185
|
+
WHERE project_id = ${params.projectId}
|
|
186
|
+
AND id = ${params.id}
|
|
187
|
+
RETURNING *
|
|
188
|
+
`
|
|
189
|
+
|
|
190
|
+
return rowToSessionRecord(row)
|
|
191
|
+
})
|
|
192
|
+
}
|
|
193
|
+
}
|