@sixb/pg 0.1.7 → 0.1.8
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/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7548 -6387
- package/dist/index.js.map +16 -10
- package/dist/migrations.d.ts.map +1 -1
- package/dist/objects/index.d.ts +2 -0
- package/dist/objects/index.d.ts.map +1 -0
- package/dist/{pg-object-query-compiler.d.ts → objects/query-compiler.d.ts} +25 -4
- package/dist/objects/query-compiler.d.ts.map +1 -0
- package/dist/objects/read-scope.d.ts +17 -0
- package/dist/objects/read-scope.d.ts.map +1 -0
- package/dist/objects/reader.d.ts +50 -0
- package/dist/objects/reader.d.ts.map +1 -0
- package/dist/objects/rows.d.ts +47 -0
- package/dist/objects/rows.d.ts.map +1 -0
- package/dist/objects/selected-reader.d.ts +8 -0
- package/dist/objects/selected-reader.d.ts.map +1 -0
- package/dist/{pg-object-storage.d.ts → objects/storage.d.ts} +12 -20
- package/dist/objects/storage.d.ts.map +1 -0
- package/dist/pg-execution-storage.d.ts +3 -2
- package/dist/pg-execution-storage.d.ts.map +1 -1
- package/dist/share-grant-storage.d.ts +11 -0
- package/dist/share-grant-storage.d.ts.map +1 -0
- package/dist/share-session-storage.d.ts +11 -0
- package/dist/share-session-storage.d.ts.map +1 -0
- package/dist/storage-errors.d.ts +2 -0
- package/dist/storage-errors.d.ts.map +1 -1
- package/dist/transactions.d.ts +9 -2
- package/dist/transactions.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/index.ts +16 -3
- package/src/migrations/035-share-grants.sql +57 -0
- package/src/migrations/036-share-sessions.sql +173 -0
- package/src/migrations.ts +4 -0
- package/src/objects/index.ts +1 -0
- package/src/{pg-object-query-compiler.ts → objects/query-compiler.ts} +185 -100
- package/src/objects/read-scope.ts +324 -0
- package/src/objects/reader.ts +604 -0
- package/src/objects/rows.ts +146 -0
- package/src/objects/selected-reader.ts +118 -0
- package/src/objects/storage.ts +305 -0
- package/src/pg-execution-storage.ts +21 -2
- package/src/share-grant-storage.ts +236 -0
- package/src/share-session-storage.ts +127 -0
- package/src/storage-errors.ts +6 -0
- package/src/transactions.ts +46 -5
- package/dist/pg-object-query-compiler.d.ts.map +0 -1
- package/dist/pg-object-storage.d.ts.map +0 -1
- package/src/pg-object-storage.ts +0 -734
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
-- Shared sessions and their execution provenance.
|
|
2
|
+
CREATE TABLE share_sessions (
|
|
3
|
+
project_id TEXT NOT NULL CHECK (length(trim(project_id)) > 0),
|
|
4
|
+
id TEXT NOT NULL CHECK (length(trim(id)) > 0),
|
|
5
|
+
grant_id TEXT NOT NULL CHECK (length(trim(grant_id)) > 0),
|
|
6
|
+
token_hash TEXT NOT NULL CHECK (token_hash ~ '^[0-9a-f]{64}$'),
|
|
7
|
+
created_at TIMESTAMPTZ NOT NULL,
|
|
8
|
+
expires_at TIMESTAMPTZ NOT NULL,
|
|
9
|
+
absolute_expires_at TIMESTAMPTZ NOT NULL,
|
|
10
|
+
revoked_at TIMESTAMPTZ,
|
|
11
|
+
PRIMARY KEY (project_id, id),
|
|
12
|
+
UNIQUE (project_id, id, grant_id),
|
|
13
|
+
UNIQUE (project_id, token_hash),
|
|
14
|
+
FOREIGN KEY (project_id, grant_id)
|
|
15
|
+
REFERENCES share_grants (project_id, id),
|
|
16
|
+
CHECK (expires_at > created_at),
|
|
17
|
+
CHECK (absolute_expires_at > created_at),
|
|
18
|
+
CHECK (expires_at <= absolute_expires_at),
|
|
19
|
+
CHECK (revoked_at IS NULL OR revoked_at >= created_at)
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
CREATE INDEX idx_share_sessions_grant
|
|
23
|
+
ON share_sessions (project_id, grant_id, created_at DESC, id DESC);
|
|
24
|
+
|
|
25
|
+
CREATE INDEX idx_share_sessions_expiry
|
|
26
|
+
ON share_sessions (project_id, expires_at)
|
|
27
|
+
WHERE revoked_at IS NULL;
|
|
28
|
+
|
|
29
|
+
ALTER TABLE executions
|
|
30
|
+
ADD COLUMN authority_delegation_kind TEXT,
|
|
31
|
+
ADD COLUMN authority_delegation_id TEXT,
|
|
32
|
+
ADD COLUMN authority_delegation_session_id TEXT;
|
|
33
|
+
|
|
34
|
+
-- The original authority checks were unnamed. Drop exactly the checks that mention
|
|
35
|
+
-- authority_kind, then replace them with stable names and the delegated variant below.
|
|
36
|
+
DO $$
|
|
37
|
+
DECLARE authority_constraint RECORD;
|
|
38
|
+
BEGIN
|
|
39
|
+
FOR authority_constraint IN
|
|
40
|
+
SELECT conname
|
|
41
|
+
FROM pg_constraint
|
|
42
|
+
WHERE conrelid = 'executions'::regclass
|
|
43
|
+
AND contype = 'c'
|
|
44
|
+
AND pg_get_constraintdef(oid) LIKE '%authority_kind%'
|
|
45
|
+
LOOP
|
|
46
|
+
EXECUTE format('ALTER TABLE executions DROP CONSTRAINT %I', authority_constraint.conname);
|
|
47
|
+
END LOOP;
|
|
48
|
+
END $$;
|
|
49
|
+
|
|
50
|
+
ALTER TABLE executions
|
|
51
|
+
ADD CONSTRAINT executions_authority_kind_check CHECK (
|
|
52
|
+
authority_kind IN ('principal', 'trustedPrimitive', 'delegated', 'kernel', 'disabled')
|
|
53
|
+
),
|
|
54
|
+
ADD CONSTRAINT executions_authority_delegation_kind_check CHECK (
|
|
55
|
+
authority_delegation_kind IS NULL OR authority_delegation_kind = 'share'
|
|
56
|
+
),
|
|
57
|
+
ADD CONSTRAINT executions_authority_shape_check CHECK (
|
|
58
|
+
(
|
|
59
|
+
authority_kind = 'principal'
|
|
60
|
+
AND num_nonnulls(authority_user_id, authority_service_account_id) = 1
|
|
61
|
+
AND num_nonnulls(authority_session_id, authority_access_token_id) <= 1
|
|
62
|
+
AND (authority_session_id IS NULL OR authority_user_id IS NOT NULL)
|
|
63
|
+
AND authority_primitive_kind IS NULL
|
|
64
|
+
AND authority_primitive_id IS NULL
|
|
65
|
+
AND authority_kernel_operation IS NULL
|
|
66
|
+
AND authority_delegation_kind IS NULL
|
|
67
|
+
AND authority_delegation_id IS NULL
|
|
68
|
+
AND authority_delegation_session_id IS NULL
|
|
69
|
+
)
|
|
70
|
+
OR (
|
|
71
|
+
authority_kind = 'trustedPrimitive'
|
|
72
|
+
AND authority_user_id IS NULL
|
|
73
|
+
AND authority_service_account_id IS NULL
|
|
74
|
+
AND authority_session_id IS NULL
|
|
75
|
+
AND authority_access_token_id IS NULL
|
|
76
|
+
AND authority_primitive_kind IS NOT NULL
|
|
77
|
+
AND authority_primitive_id IS NOT NULL
|
|
78
|
+
AND length(trim(authority_primitive_id)) > 0
|
|
79
|
+
AND authority_kernel_operation IS NULL
|
|
80
|
+
AND authority_delegation_kind IS NULL
|
|
81
|
+
AND authority_delegation_id IS NULL
|
|
82
|
+
AND authority_delegation_session_id IS NULL
|
|
83
|
+
)
|
|
84
|
+
OR (
|
|
85
|
+
authority_kind = 'delegated'
|
|
86
|
+
AND authority_user_id IS NULL
|
|
87
|
+
AND authority_service_account_id IS NULL
|
|
88
|
+
AND authority_session_id IS NULL
|
|
89
|
+
AND authority_access_token_id IS NULL
|
|
90
|
+
AND authority_primitive_kind IS NULL
|
|
91
|
+
AND authority_primitive_id IS NULL
|
|
92
|
+
AND authority_kernel_operation IS NULL
|
|
93
|
+
AND authority_delegation_kind IS NOT NULL
|
|
94
|
+
AND authority_delegation_kind = 'share'
|
|
95
|
+
AND authority_delegation_id IS NOT NULL
|
|
96
|
+
AND length(trim(authority_delegation_id)) > 0
|
|
97
|
+
AND authority_delegation_session_id IS NOT NULL
|
|
98
|
+
AND length(trim(authority_delegation_session_id)) > 0
|
|
99
|
+
)
|
|
100
|
+
OR (
|
|
101
|
+
authority_kind = 'kernel'
|
|
102
|
+
AND authority_user_id IS NULL
|
|
103
|
+
AND authority_service_account_id IS NULL
|
|
104
|
+
AND authority_session_id IS NULL
|
|
105
|
+
AND authority_access_token_id IS NULL
|
|
106
|
+
AND authority_primitive_kind IS NULL
|
|
107
|
+
AND authority_primitive_id IS NULL
|
|
108
|
+
AND authority_kernel_operation IS NOT NULL
|
|
109
|
+
AND authority_delegation_kind IS NULL
|
|
110
|
+
AND authority_delegation_id IS NULL
|
|
111
|
+
AND authority_delegation_session_id IS NULL
|
|
112
|
+
)
|
|
113
|
+
OR (
|
|
114
|
+
authority_kind = 'disabled'
|
|
115
|
+
AND authority_user_id IS NULL
|
|
116
|
+
AND authority_service_account_id IS NULL
|
|
117
|
+
AND authority_session_id IS NULL
|
|
118
|
+
AND authority_access_token_id IS NULL
|
|
119
|
+
AND authority_primitive_kind IS NULL
|
|
120
|
+
AND authority_primitive_id IS NULL
|
|
121
|
+
AND authority_kernel_operation IS NULL
|
|
122
|
+
AND authority_delegation_kind IS NULL
|
|
123
|
+
AND authority_delegation_id IS NULL
|
|
124
|
+
AND authority_delegation_session_id IS NULL
|
|
125
|
+
)
|
|
126
|
+
),
|
|
127
|
+
ADD CONSTRAINT executions_executor_authority_check CHECK (
|
|
128
|
+
(
|
|
129
|
+
executor_kind = 'request'
|
|
130
|
+
AND source_kind = 'http'
|
|
131
|
+
AND executor_id = source_id
|
|
132
|
+
AND (
|
|
133
|
+
(
|
|
134
|
+
authority_kind = 'principal'
|
|
135
|
+
AND requested_by_user_id IS NOT DISTINCT FROM authority_user_id
|
|
136
|
+
AND requested_by_service_account_id IS NOT DISTINCT FROM authority_service_account_id
|
|
137
|
+
)
|
|
138
|
+
OR (
|
|
139
|
+
authority_kind IN ('delegated', 'disabled')
|
|
140
|
+
AND requested_by_user_id IS NULL
|
|
141
|
+
AND requested_by_service_account_id IS NULL
|
|
142
|
+
)
|
|
143
|
+
)
|
|
144
|
+
)
|
|
145
|
+
OR (
|
|
146
|
+
executor_kind IN ('action', 'pipeline', 'projection', 'rule', 'sync', 'webhook', 'workflow')
|
|
147
|
+
AND authority_kind = 'trustedPrimitive'
|
|
148
|
+
AND executor_kind = authority_primitive_kind
|
|
149
|
+
)
|
|
150
|
+
OR (
|
|
151
|
+
executor_kind = 'agent'
|
|
152
|
+
AND source_kind = 'execution'
|
|
153
|
+
AND (
|
|
154
|
+
(
|
|
155
|
+
authority_kind = 'principal'
|
|
156
|
+
AND authority_service_account_id IS NOT NULL
|
|
157
|
+
AND authority_session_id IS NULL
|
|
158
|
+
AND authority_access_token_id IS NULL
|
|
159
|
+
)
|
|
160
|
+
OR (
|
|
161
|
+
authority_kind = 'principal'
|
|
162
|
+
AND authority_user_id IS NOT NULL
|
|
163
|
+
AND num_nonnulls(authority_session_id, authority_access_token_id) = 1
|
|
164
|
+
)
|
|
165
|
+
OR authority_kind = 'disabled'
|
|
166
|
+
)
|
|
167
|
+
)
|
|
168
|
+
OR (executor_kind = 'kernel' AND authority_kind = 'kernel')
|
|
169
|
+
),
|
|
170
|
+
ADD CONSTRAINT fk_executions_share_session
|
|
171
|
+
FOREIGN KEY (project_id, authority_delegation_session_id, authority_delegation_id)
|
|
172
|
+
REFERENCES share_sessions (project_id, id, grant_id)
|
|
173
|
+
ON DELETE RESTRICT;
|
package/src/migrations.ts
CHANGED
|
@@ -85,6 +85,8 @@ import retireAgentDefinitionsSql from "./migrations/033-retire-agent-definitions
|
|
|
85
85
|
import deviceAuthorizationsSql from "./migrations/034-device-authorizations.sql" with {
|
|
86
86
|
type: "text",
|
|
87
87
|
}
|
|
88
|
+
import shareGrantsSql from "./migrations/035-share-grants.sql" with { type: "text" }
|
|
89
|
+
import shareSessionsSql from "./migrations/036-share-sessions.sql" with { type: "text" }
|
|
88
90
|
import type { SQL, SQLClient } from "./pg-client"
|
|
89
91
|
|
|
90
92
|
export interface PostgresMigrationContext {
|
|
@@ -372,6 +374,8 @@ export const postgresStorageMigrations = defineMigrations<PostgresMigrationConte
|
|
|
372
374
|
pgSql("032-conversation-run-spec", conversationRunSpecSql),
|
|
373
375
|
pgSql("033-retire-agent-definitions", retireAgentDefinitionsSql),
|
|
374
376
|
pgSql("034-device-authorizations", deviceAuthorizationsSql),
|
|
377
|
+
pgSql("035-share-grants", shareGrantsSql),
|
|
378
|
+
pgSql("036-share-sessions", shareSessionsSql),
|
|
375
379
|
],
|
|
376
380
|
})
|
|
377
381
|
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { PgObjectStorage } from "./storage"
|