@rebasepro/server-postgres 0.12.0 → 0.12.1-canary.g35be8cb
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/auth/services.d.ts +16 -0
- package/dist/backup-service-DH9kPg-E.js +8866 -0
- package/dist/backup-service-DH9kPg-E.js.map +1 -0
- package/dist/connection-B5Wndbr1.js +196 -0
- package/dist/connection-B5Wndbr1.js.map +1 -0
- package/dist/ensure-collection-policies-Vl3Cv1Q9.js +57 -0
- package/dist/ensure-collection-policies-Vl3Cv1Q9.js.map +1 -0
- package/dist/ensure-collection-tables-DsDsNl6o.js +590 -0
- package/dist/ensure-collection-tables-DsDsNl6o.js.map +1 -0
- package/dist/index.es.js +452 -9609
- package/dist/index.es.js.map +1 -1
- package/dist/schema/auth-schema.d.ts +83 -144
- package/dist/schema/ensure-collection-policies.d.ts +60 -0
- package/dist/schema/ensure-collection-tables.d.ts +24 -2
- package/dist/schema/generate-postgres-ddl-logic.d.ts +116 -1
- package/dist/{src-BbFOPJ1S.js → src-DihrDFuP.js} +160 -150
- package/dist/src-DihrDFuP.js.map +1 -0
- package/dist/{src-Zqwaw3P5.js → src-DoU9yPqq.js} +3 -159
- package/dist/src-DoU9yPqq.js.map +1 -0
- package/dist/utils/pg-error-utils.d.ts +19 -0
- package/dist/websocket-BKcGvILX.js +528 -0
- package/dist/websocket-BKcGvILX.js.map +1 -0
- package/package.json +14 -14
- package/src/PostgresAdapter.ts +14 -0
- package/src/PostgresBootstrapper.ts +111 -13
- package/src/auth/ensure-tables.ts +164 -9
- package/src/auth/services.ts +21 -2
- package/src/schema/auth-schema.ts +30 -19
- package/src/schema/ensure-collection-policies.ts +105 -0
- package/src/schema/ensure-collection-tables.test.ts +105 -9
- package/src/schema/ensure-collection-tables.ts +142 -25
- package/src/schema/generate-drizzle-schema-logic.ts +7 -3
- package/src/schema/generate-postgres-ddl-logic.ts +335 -16
- package/src/schema/introspect-runtime.test.ts +56 -8
- package/src/schema/introspect-runtime.ts +31 -9
- package/src/utils/pg-error-utils.ts +46 -0
- package/dist/chunk-DSJWtz9O.js +0 -40
- package/dist/ensure-collection-tables-CNTcZGvn.js +0 -304
- package/dist/ensure-collection-tables-CNTcZGvn.js.map +0 -1
- package/dist/src-BbFOPJ1S.js.map +0 -1
- package/dist/src-Zqwaw3P5.js.map +0 -1
|
@@ -0,0 +1,590 @@
|
|
|
1
|
+
import { createRequire as __createRequire } from "module";
|
|
2
|
+
import "process";
|
|
3
|
+
__createRequire(import.meta.url);
|
|
4
|
+
import { O as toSnakeCase, T as getPolicyNamesForRule, a as getJunctionSecurityRules, c as policyToPostgres, d as findRelation, g as resolveCollectionRelations, i as getJunctionCollectionConfig, l as securityRuleToConditions, m as getTableName, o as resolveJunctionSpecs, r as resolveStringColumnLength, s as getEffectiveSecurityRules } from "./src-DihrDFuP.js";
|
|
5
|
+
import { n as isPostgresCollectionConfig } from "./src-DoU9yPqq.js";
|
|
6
|
+
//#region src/schema/generate-postgres-ddl-logic.ts
|
|
7
|
+
var resolveColumnName = (propName, prop) => {
|
|
8
|
+
if (prop && "columnName" in prop && typeof prop.columnName === "string") return prop.columnName;
|
|
9
|
+
return toSnakeCase(propName);
|
|
10
|
+
};
|
|
11
|
+
var getPrimaryKeyProp = (collection) => {
|
|
12
|
+
if (collection.properties) {
|
|
13
|
+
const idPropEntry = Object.entries(collection.properties).find(([_, prop]) => "isId" in prop && Boolean(prop.isId));
|
|
14
|
+
if (idPropEntry) {
|
|
15
|
+
const prop = idPropEntry[1];
|
|
16
|
+
const isUuid = prop.type === "string" && "isId" in prop && prop.isId === "uuid";
|
|
17
|
+
return {
|
|
18
|
+
name: idPropEntry[0],
|
|
19
|
+
type: prop.type === "number" ? "number" : "string",
|
|
20
|
+
isUuid
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
const idProp = collection.properties?.["id"];
|
|
25
|
+
if (idProp?.type === "number") return {
|
|
26
|
+
name: "id",
|
|
27
|
+
type: "number",
|
|
28
|
+
isUuid: false
|
|
29
|
+
};
|
|
30
|
+
return {
|
|
31
|
+
name: "id",
|
|
32
|
+
type: "string",
|
|
33
|
+
isUuid: idProp?.type === "string" && "isId" in idProp && idProp.isId === "uuid"
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
var isNumericId = (collection) => {
|
|
37
|
+
return getPrimaryKeyProp(collection).type === "number";
|
|
38
|
+
};
|
|
39
|
+
var getPrimaryKeyName = (collection) => {
|
|
40
|
+
return getPrimaryKeyProp(collection).name;
|
|
41
|
+
};
|
|
42
|
+
/** The column type a junction holds for one endpoint's primary key. */
|
|
43
|
+
var junctionKeyType = (collection) => isNumericId(collection) ? "INTEGER" : getPrimaryKeyProp(collection).isUuid ? "UUID" : "TEXT";
|
|
44
|
+
var isIdProperty = (propName, prop, collection) => {
|
|
45
|
+
if ("isId" in prop && Boolean(prop.isId)) return true;
|
|
46
|
+
return !Object.values(collection.properties ?? {}).some((p) => "isId" in p && Boolean(p.isId)) && propName === "id";
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* The individual SQL statements a single security rule compiles to: a
|
|
50
|
+
* `DROP POLICY IF EXISTS` / `CREATE POLICY` pair per operation, each a complete
|
|
51
|
+
* statement (terminated by `;`, no trailing newline).
|
|
52
|
+
*
|
|
53
|
+
* This is the primitive the boot-time RLS applier runs one statement at a time
|
|
54
|
+
* (the runtime's DB handle speaks the extended query protocol, which forbids
|
|
55
|
+
* multiple commands in one execute), while `db push` writes the joined string.
|
|
56
|
+
*/
|
|
57
|
+
var generatePolicyStatements = (collection, rule, resolveCollection) => {
|
|
58
|
+
const tableName = getTableName(collection);
|
|
59
|
+
const ops = rule.operations && rule.operations.length > 0 ? rule.operations : [rule.operation ?? "all"];
|
|
60
|
+
const policyNames = getPolicyNamesForRule(rule, tableName);
|
|
61
|
+
return ops.flatMap((op, opIdx) => {
|
|
62
|
+
return generateSinglePolicyStatements(collection, rule, op, policyNames[opIdx], resolveCollection);
|
|
63
|
+
});
|
|
64
|
+
};
|
|
65
|
+
var generateSinglePolicyStatements = (collection, rule, operation, policyName, resolveCollection) => {
|
|
66
|
+
const schema = isPostgresCollectionConfig(collection) && collection.schema ? collection.schema : "public";
|
|
67
|
+
const tableName = getTableName(collection);
|
|
68
|
+
const mode = (rule.mode ?? "permissive").toUpperCase();
|
|
69
|
+
const operationUpper = operation.toUpperCase();
|
|
70
|
+
const pgRoles = rule.pgRoles ? [...rule.pgRoles].sort() : ["public"];
|
|
71
|
+
const needsUsing = operation !== "insert";
|
|
72
|
+
const needsWithCheck = operation !== "select" && operation !== "delete";
|
|
73
|
+
const { usingExpr, withCheckExpr } = securityRuleToConditions(rule);
|
|
74
|
+
let usingClause = needsUsing && usingExpr ? policyToPostgres(usingExpr, collection, { resolveCollection }) : null;
|
|
75
|
+
let withCheckClause = needsWithCheck && withCheckExpr ? policyToPostgres(withCheckExpr, collection, { resolveCollection }) : null;
|
|
76
|
+
if (!usingClause && needsUsing) usingClause = "false";
|
|
77
|
+
if (!withCheckClause && needsWithCheck) withCheckClause = "false";
|
|
78
|
+
const drop = `DROP POLICY IF EXISTS "${policyName}" ON "${schema}"."${tableName}";`;
|
|
79
|
+
let create = `CREATE POLICY "${policyName}" ON "${schema}"."${tableName}" AS ${mode} FOR ${operationUpper} TO ${pgRoles.map((r) => `"${r}"`).join(", ")}`;
|
|
80
|
+
if (usingClause) create += ` USING (${usingClause})`;
|
|
81
|
+
if (withCheckClause) create += ` WITH CHECK (${withCheckClause})`;
|
|
82
|
+
create += ";";
|
|
83
|
+
return [drop, create];
|
|
84
|
+
};
|
|
85
|
+
var getSqlColumnType = (propName, prop, collection, collections) => {
|
|
86
|
+
switch (prop.type) {
|
|
87
|
+
case "string": {
|
|
88
|
+
const stringProp = prop;
|
|
89
|
+
if (stringProp.enum) {
|
|
90
|
+
const tableName = getTableName(collection);
|
|
91
|
+
const colName = resolveColumnName(propName, prop);
|
|
92
|
+
return `"${isPostgresCollectionConfig(collection) && collection.schema ? collection.schema : "public"}"."${tableName}_${colName}"`;
|
|
93
|
+
}
|
|
94
|
+
if (stringProp.isId === "uuid" || stringProp.columnType === "uuid") return "UUID";
|
|
95
|
+
if (stringProp.columnType === "char") return `CHAR(${resolveStringColumnLength(stringProp)})`;
|
|
96
|
+
if (stringProp.columnType === "varchar") return `VARCHAR(${resolveStringColumnLength(stringProp)})`;
|
|
97
|
+
return "TEXT";
|
|
98
|
+
}
|
|
99
|
+
case "number": {
|
|
100
|
+
const numProp = prop;
|
|
101
|
+
const isId = isIdProperty(propName, prop, collection);
|
|
102
|
+
if ("isId" in numProp && numProp.isId === "increment") return "INTEGER GENERATED BY DEFAULT AS IDENTITY";
|
|
103
|
+
if (numProp.columnType) {
|
|
104
|
+
if (numProp.columnType === "double precision") return "DOUBLE PRECISION";
|
|
105
|
+
return numProp.columnType.toUpperCase();
|
|
106
|
+
}
|
|
107
|
+
return numProp.validation?.integer || isId ? "INTEGER" : "NUMERIC";
|
|
108
|
+
}
|
|
109
|
+
case "boolean": return "BOOLEAN";
|
|
110
|
+
case "date": {
|
|
111
|
+
const dateProp = prop;
|
|
112
|
+
if (dateProp.columnType === "date") return "DATE";
|
|
113
|
+
if (dateProp.columnType === "time") return "TIME";
|
|
114
|
+
return "TIMESTAMP WITH TIME ZONE";
|
|
115
|
+
}
|
|
116
|
+
case "map": return prop.columnType === "json" ? "JSON" : "JSONB";
|
|
117
|
+
case "array": {
|
|
118
|
+
const arrayProp = prop;
|
|
119
|
+
let colType = arrayProp.columnType;
|
|
120
|
+
if (!colType && arrayProp.of && !Array.isArray(arrayProp.of)) {
|
|
121
|
+
const ofProp = arrayProp.of;
|
|
122
|
+
if (ofProp.type === "string") colType = "text[]";
|
|
123
|
+
else if (ofProp.type === "number") colType = ofProp.validation?.integer ? "integer[]" : "numeric[]";
|
|
124
|
+
else if (ofProp.type === "boolean") colType = "boolean[]";
|
|
125
|
+
}
|
|
126
|
+
if (colType === "json") return "JSON";
|
|
127
|
+
if (colType === "text[]") return "TEXT[]";
|
|
128
|
+
if (colType === "integer[]") return "INTEGER[]";
|
|
129
|
+
if (colType === "boolean[]") return "BOOLEAN[]";
|
|
130
|
+
if (colType === "numeric[]") return "NUMERIC[]";
|
|
131
|
+
return "JSONB";
|
|
132
|
+
}
|
|
133
|
+
case "vector": return `VECTOR(${prop.dimensions})`;
|
|
134
|
+
case "binary": return "BYTEA";
|
|
135
|
+
case "relation": {
|
|
136
|
+
const refProp = prop;
|
|
137
|
+
const relation = findRelation(resolveCollectionRelations(collection), refProp.relation?.relationName ?? propName);
|
|
138
|
+
if (relation?.kind !== "belongsTo") throw new Error(`Relation ${propName} does not put a column on this table (only \`belongsTo\` does)`);
|
|
139
|
+
let targetCollection;
|
|
140
|
+
try {
|
|
141
|
+
targetCollection = relation.target();
|
|
142
|
+
} catch {
|
|
143
|
+
return "TEXT";
|
|
144
|
+
}
|
|
145
|
+
const pkProp = getPrimaryKeyProp(targetCollection);
|
|
146
|
+
return pkProp.type === "number" ? "INTEGER" : pkProp.isUuid ? "UUID" : "TEXT";
|
|
147
|
+
}
|
|
148
|
+
case "reference": {
|
|
149
|
+
const refProp = prop;
|
|
150
|
+
const targetCollection = collections.find((c) => c.slug === refProp.path || getTableName(c) === refProp.path);
|
|
151
|
+
if (!targetCollection) return "TEXT";
|
|
152
|
+
const pkProp = getPrimaryKeyProp(targetCollection);
|
|
153
|
+
return pkProp.type === "number" ? "INTEGER" : pkProp.isUuid ? "UUID" : "TEXT";
|
|
154
|
+
}
|
|
155
|
+
default: return "TEXT";
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
var schemaOfCollection = (collection) => isPostgresCollectionConfig(collection) && collection.schema ? collection.schema : "public";
|
|
159
|
+
var bareTableName = (name) => name.includes(".") ? name.split(".").pop() : name;
|
|
160
|
+
var foreignKeyPlan = (args) => {
|
|
161
|
+
const constraintName = `${args.table}_${args.column}_fkey`;
|
|
162
|
+
const onUpdate = args.onUpdate ? ` ON UPDATE ${args.onUpdate.toUpperCase()}` : "";
|
|
163
|
+
return {
|
|
164
|
+
constraintName,
|
|
165
|
+
schema: args.schema,
|
|
166
|
+
table: args.table,
|
|
167
|
+
column: args.column,
|
|
168
|
+
targetSchema: args.targetSchema,
|
|
169
|
+
targetTable: args.targetTable,
|
|
170
|
+
targetColumn: args.targetColumn,
|
|
171
|
+
sql: `ALTER TABLE "${args.schema}"."${args.table}" ADD CONSTRAINT "${constraintName}" FOREIGN KEY ("${args.column}") REFERENCES "${args.targetSchema}"."${args.targetTable}" ("${args.targetColumn}") ON DELETE ${args.onDelete.toUpperCase()}${onUpdate};`
|
|
172
|
+
};
|
|
173
|
+
};
|
|
174
|
+
/**
|
|
175
|
+
* The FK columns the declared collections own — one entry per `relation`
|
|
176
|
+
* (`belongsTo` side) or `reference` property.
|
|
177
|
+
*
|
|
178
|
+
* Split out of {@link generatePostgresDdl} so the boot-time schema ensure can
|
|
179
|
+
* create the same columns with the same names, types and constraints. Before
|
|
180
|
+
* this it skipped them outright, which was survivable only because `db push`
|
|
181
|
+
* always followed; on a managed tenant nothing follows, so a table arrived
|
|
182
|
+
* without the column its own collection reads and wrote 400 on every insert.
|
|
183
|
+
*
|
|
184
|
+
* A relation whose target is not in the bundle yields no column at all (the
|
|
185
|
+
* generator returns early on an unresolvable target); a `reference` whose target
|
|
186
|
+
* is unknown yields the column without a constraint. Both mirror the generator
|
|
187
|
+
* exactly — a divergence here is a schema fork between boot and `db push`.
|
|
188
|
+
*/
|
|
189
|
+
var planRelationalColumns = (collections) => {
|
|
190
|
+
const plans = [];
|
|
191
|
+
for (const collection of collections) {
|
|
192
|
+
const tableName = getTableName(collection);
|
|
193
|
+
if (!tableName) continue;
|
|
194
|
+
const schema = schemaOfCollection(collection);
|
|
195
|
+
const table = bareTableName(tableName);
|
|
196
|
+
for (const [propName, rawProp] of Object.entries(collection.properties ?? {})) {
|
|
197
|
+
const prop = rawProp;
|
|
198
|
+
if (prop.type === "relation") {
|
|
199
|
+
const refProp = prop;
|
|
200
|
+
const relInfo = findRelation(resolveCollectionRelations(collection), refProp.relation?.relationName ?? propName);
|
|
201
|
+
if (relInfo?.kind !== "belongsTo") continue;
|
|
202
|
+
if (collection.properties[relInfo.localKey] && propName !== relInfo.localKey) continue;
|
|
203
|
+
let targetCollection;
|
|
204
|
+
try {
|
|
205
|
+
targetCollection = relInfo.target();
|
|
206
|
+
} catch {
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
if (!targetCollection) continue;
|
|
210
|
+
const required = prop.validation?.required;
|
|
211
|
+
plans.push({
|
|
212
|
+
schema,
|
|
213
|
+
table,
|
|
214
|
+
column: relInfo.localKey,
|
|
215
|
+
type: getSqlColumnType(propName, prop, collection, collections),
|
|
216
|
+
foreignKey: foreignKeyPlan({
|
|
217
|
+
schema,
|
|
218
|
+
table,
|
|
219
|
+
column: relInfo.localKey,
|
|
220
|
+
targetSchema: schemaOfCollection(targetCollection),
|
|
221
|
+
targetTable: bareTableName(getTableName(targetCollection)),
|
|
222
|
+
targetColumn: getPrimaryKeyName(targetCollection),
|
|
223
|
+
onDelete: relInfo.onDelete ?? (required ? "CASCADE" : "SET NULL"),
|
|
224
|
+
onUpdate: relInfo.onUpdate
|
|
225
|
+
})
|
|
226
|
+
});
|
|
227
|
+
} else if (prop.type === "reference") {
|
|
228
|
+
const refProp = prop;
|
|
229
|
+
const targetCollection = collections.find((c) => c.slug === refProp.path || getTableName(c) === refProp.path);
|
|
230
|
+
const column = resolveColumnName(propName, prop);
|
|
231
|
+
const type = getSqlColumnType(propName, prop, collection, collections);
|
|
232
|
+
const required = prop.validation?.required;
|
|
233
|
+
plans.push({
|
|
234
|
+
schema,
|
|
235
|
+
table,
|
|
236
|
+
column,
|
|
237
|
+
type,
|
|
238
|
+
foreignKey: targetCollection ? foreignKeyPlan({
|
|
239
|
+
schema,
|
|
240
|
+
table,
|
|
241
|
+
column,
|
|
242
|
+
targetSchema: schemaOfCollection(targetCollection),
|
|
243
|
+
targetTable: bareTableName(getTableName(targetCollection)),
|
|
244
|
+
targetColumn: getPrimaryKeyName(targetCollection),
|
|
245
|
+
onDelete: required ? "CASCADE" : "SET NULL"
|
|
246
|
+
}) : void 0
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
return plans;
|
|
252
|
+
};
|
|
253
|
+
/**
|
|
254
|
+
* The junction tables a bundle's many-to-many relations imply.
|
|
255
|
+
*
|
|
256
|
+
* Derived from {@link resolveJunctionSpecs}, the same source the junction RLS
|
|
257
|
+
* comes from, so a table created here always has policies planned for it — a
|
|
258
|
+
* junction with row-level security left off is readable and writable by every
|
|
259
|
+
* signed-in user, which is why the two must ship together.
|
|
260
|
+
*/
|
|
261
|
+
var planJunctionTables = (collections) => {
|
|
262
|
+
const plans = [];
|
|
263
|
+
for (const spec of resolveJunctionSpecs(collections).values()) {
|
|
264
|
+
const [source, target] = spec.endpoints;
|
|
265
|
+
const columns = [{
|
|
266
|
+
name: source.junctionColumn,
|
|
267
|
+
type: junctionKeyType(source.collection)
|
|
268
|
+
}, {
|
|
269
|
+
name: target.junctionColumn,
|
|
270
|
+
type: junctionKeyType(target.collection)
|
|
271
|
+
}];
|
|
272
|
+
const onDelete = spec.declaringSides[0]?.relation.onDelete ?? "CASCADE";
|
|
273
|
+
plans.push({
|
|
274
|
+
schema: spec.schema,
|
|
275
|
+
table: spec.table,
|
|
276
|
+
columns,
|
|
277
|
+
createTable: `CREATE TABLE IF NOT EXISTS "${spec.schema}"."${spec.table}" (` + columns.map((c) => `"${c.name}" ${c.type} NOT NULL`).join(", ") + `, PRIMARY KEY (${columns.map((c) => `"${c.name}"`).join(", ")}));`,
|
|
278
|
+
foreignKeys: [source, target].map((endpoint, i) => foreignKeyPlan({
|
|
279
|
+
schema: spec.schema,
|
|
280
|
+
table: spec.table,
|
|
281
|
+
column: columns[i].name,
|
|
282
|
+
targetSchema: schemaOfCollection(endpoint.collection),
|
|
283
|
+
targetTable: bareTableName(getTableName(endpoint.collection)),
|
|
284
|
+
targetColumn: getPrimaryKeyName(endpoint.collection),
|
|
285
|
+
onDelete
|
|
286
|
+
}))
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
return plans;
|
|
290
|
+
};
|
|
291
|
+
/**
|
|
292
|
+
* The per-table RLS plan for the *declared* collections, as executable
|
|
293
|
+
* statements — what the managed runtime applies at boot so a freshly
|
|
294
|
+
* provisioned tenant database serves data instead of 401ing every read.
|
|
295
|
+
*
|
|
296
|
+
* Mirrors {@link generatePostgresPoliciesDdl} exactly (same
|
|
297
|
+
* `generatePolicyStatements`, same enable-RLS, same effective rules, same
|
|
298
|
+
* derived junction rules), so boot and `db push` produce identical policies from
|
|
299
|
+
* identical collections.
|
|
300
|
+
*
|
|
301
|
+
* Junction tables are included, and have to be: boot creates them now
|
|
302
|
+
* ({@link planJunctionTables}), and a junction with RLS left off is readable and
|
|
303
|
+
* writable by every signed-in user. A junction whose table is still absent is
|
|
304
|
+
* skipped by the applier, not planned away here.
|
|
305
|
+
*/
|
|
306
|
+
var planCollectionPolicies = (collections) => {
|
|
307
|
+
const resolveCollection = (slug) => collections.find((c) => c.slug === slug || getTableName(c) === slug);
|
|
308
|
+
const plans = [];
|
|
309
|
+
const seen = /* @__PURE__ */ new Set();
|
|
310
|
+
for (const collection of collections) {
|
|
311
|
+
const tableName = getTableName(collection);
|
|
312
|
+
if (!tableName) continue;
|
|
313
|
+
const schema = isPostgresCollectionConfig(collection) && collection.schema ? collection.schema : "public";
|
|
314
|
+
const baseTableName = tableName.includes(".") ? tableName.split(".").pop() : tableName;
|
|
315
|
+
const qualified = `${schema}.${baseTableName}`;
|
|
316
|
+
if (seen.has(qualified)) continue;
|
|
317
|
+
seen.add(qualified);
|
|
318
|
+
const policyStatements = [];
|
|
319
|
+
for (const rule of getEffectiveSecurityRules(collection)) policyStatements.push(...generatePolicyStatements(collection, rule, resolveCollection));
|
|
320
|
+
plans.push({
|
|
321
|
+
schema,
|
|
322
|
+
table: baseTableName,
|
|
323
|
+
qualified,
|
|
324
|
+
enableRls: `ALTER TABLE "${schema}"."${baseTableName}" ENABLE ROW LEVEL SECURITY;`,
|
|
325
|
+
policyStatements
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
for (const spec of resolveJunctionSpecs(collections).values()) {
|
|
329
|
+
const qualified = `${spec.schema}.${spec.table}`;
|
|
330
|
+
if (seen.has(qualified)) continue;
|
|
331
|
+
seen.add(qualified);
|
|
332
|
+
const junctionCollection = getJunctionCollectionConfig(spec);
|
|
333
|
+
const policyStatements = [];
|
|
334
|
+
for (const rule of getJunctionSecurityRules(spec)) policyStatements.push(...generatePolicyStatements(junctionCollection, rule, resolveCollection));
|
|
335
|
+
plans.push({
|
|
336
|
+
schema: spec.schema,
|
|
337
|
+
table: spec.table,
|
|
338
|
+
qualified,
|
|
339
|
+
enableRls: `ALTER TABLE "${spec.schema}"."${spec.table}" ENABLE ROW LEVEL SECURITY;`,
|
|
340
|
+
policyStatements
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
return plans;
|
|
344
|
+
};
|
|
345
|
+
//#endregion
|
|
346
|
+
//#region src/schema/ensure-collection-tables.ts
|
|
347
|
+
/**
|
|
348
|
+
* Bringing a database up to date with a bundle's collections, additively.
|
|
349
|
+
*
|
|
350
|
+
* ## Why this exists
|
|
351
|
+
*
|
|
352
|
+
* A managed runtime boots someone else's compiled project against a database it
|
|
353
|
+
* has never seen. Auth tables are ensured at boot already, but collection tables
|
|
354
|
+
* were not created by anything: the platform ran the app and every `/api/data/*`
|
|
355
|
+
* request answered 500 on a missing relation. `rebase db push` cannot help — it
|
|
356
|
+
* is an Atlas-driven CLI command, and the runtime image ships no CLI.
|
|
357
|
+
*
|
|
358
|
+
* ## Why additive-only, forever
|
|
359
|
+
*
|
|
360
|
+
* This runs unattended, against a database with customers' data in it, with no
|
|
361
|
+
* human reading a diff. So it may only ever do things that cannot lose data:
|
|
362
|
+
* create a missing table, add a missing column, create a missing enum type.
|
|
363
|
+
*
|
|
364
|
+
* It will **never** drop a table or a column, narrow a type, or alter a
|
|
365
|
+
* constraint. A removed field leaves its column behind; a renamed field looks
|
|
366
|
+
* like an addition and the old column stays. That is the correct trade for an
|
|
367
|
+
* automated path — the alternative is an unattended process that can silently
|
|
368
|
+
* destroy a column, which is precisely the failure `db push` was hardened
|
|
369
|
+
* against. Destructive changes stay a deliberate, human-reviewed migration.
|
|
370
|
+
*
|
|
371
|
+
* Because of that, this is safe to run on every boot, and re-running it is a
|
|
372
|
+
* no-op.
|
|
373
|
+
*/
|
|
374
|
+
/** Postgres identifiers this module is willing to interpolate. */
|
|
375
|
+
var SAFE_IDENTIFIER = /^[A-Za-z_][A-Za-z0-9_$]*$/;
|
|
376
|
+
function assertSafeIdentifier(value, what) {
|
|
377
|
+
if (!SAFE_IDENTIFIER.test(value)) throw new Error(`Refusing to build SQL with an unsafe ${what}: ${JSON.stringify(value)}`);
|
|
378
|
+
return value;
|
|
379
|
+
}
|
|
380
|
+
function schemaOf(collection) {
|
|
381
|
+
return isPostgresCollectionConfig(collection) && collection.schema ? collection.schema : "public";
|
|
382
|
+
}
|
|
383
|
+
function qualified(collection) {
|
|
384
|
+
return `${schemaOf(collection)}.${getTableName(collection)}`;
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* Enum types a collection's properties require, as `schema.typename`.
|
|
388
|
+
*
|
|
389
|
+
* Named exactly as the DDL generator names them (`<table>_<column>`), because
|
|
390
|
+
* a column added here has to reference the same type the generator would have
|
|
391
|
+
* created — a second, differently-named type for the same field would be a
|
|
392
|
+
* silent schema fork.
|
|
393
|
+
*/
|
|
394
|
+
function requiredEnums(collection) {
|
|
395
|
+
const table = getTableName(collection);
|
|
396
|
+
const schema = schemaOf(collection);
|
|
397
|
+
const out = [];
|
|
398
|
+
for (const [propName, prop] of Object.entries(collection.properties ?? {})) {
|
|
399
|
+
const p = prop;
|
|
400
|
+
if (!("enum" in p) || !p.enum) continue;
|
|
401
|
+
if (p.type !== "string" && p.type !== "number") continue;
|
|
402
|
+
const values = p.enum.map((entry) => entry && typeof entry === "object" && "id" in entry ? String(entry.id) : String(entry)).filter((v) => v.length > 0);
|
|
403
|
+
if (values.length === 0) continue;
|
|
404
|
+
out.push({
|
|
405
|
+
name: `${schema}.${table}_${resolveColumnName(propName, p)}`,
|
|
406
|
+
values
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
return out;
|
|
410
|
+
}
|
|
411
|
+
/** Single-quote escaping for an enum label. */
|
|
412
|
+
function quoteLiteral(value) {
|
|
413
|
+
return `'${value.replace(/'/g, "''")}'`;
|
|
414
|
+
}
|
|
415
|
+
/**
|
|
416
|
+
* Decide what to add. Pure — the caller supplies what exists and runs the result.
|
|
417
|
+
*
|
|
418
|
+
* Ordering matters and is deliberate: enum types before the tables and columns
|
|
419
|
+
* that reference them, tables before the columns added to other tables (a new
|
|
420
|
+
* table may be the target of a relation), and nothing is emitted twice.
|
|
421
|
+
*/
|
|
422
|
+
function planCollectionSchemaEnsure(collections, existing) {
|
|
423
|
+
const actions = [];
|
|
424
|
+
const plannedEnums = /* @__PURE__ */ new Set();
|
|
425
|
+
for (const collection of collections) for (const { name, values } of requiredEnums(collection)) {
|
|
426
|
+
if (existing.enums.has(name) || plannedEnums.has(name)) continue;
|
|
427
|
+
plannedEnums.add(name);
|
|
428
|
+
const [schema, typeName] = name.split(".");
|
|
429
|
+
actions.push({
|
|
430
|
+
kind: "create-enum",
|
|
431
|
+
target: name,
|
|
432
|
+
sql: `CREATE TYPE "${schema}"."${typeName}" AS ENUM (${values.map(quoteLiteral).join(", ")});`
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
const created = /* @__PURE__ */ new Set();
|
|
436
|
+
for (const collection of collections) {
|
|
437
|
+
const key = qualified(collection);
|
|
438
|
+
if (existing.tables.has(key) || created.has(key)) continue;
|
|
439
|
+
created.add(key);
|
|
440
|
+
const schema = schemaOf(collection);
|
|
441
|
+
const table = getTableName(collection);
|
|
442
|
+
const idEntry = Object.entries(collection.properties ?? {}).find(([n, p]) => isIdProperty(n, p, collection));
|
|
443
|
+
const idName = idEntry ? resolveColumnName(idEntry[0], idEntry[1]) : "id";
|
|
444
|
+
const idProp = idEntry?.[1];
|
|
445
|
+
let idDef;
|
|
446
|
+
if (idProp?.type === "number") idDef = `"${idName}" BIGSERIAL PRIMARY KEY`;
|
|
447
|
+
else if (idProp && idProp.type === "string" && idProp.isId === "uuid") idDef = `"${idName}" UUID PRIMARY KEY DEFAULT gen_random_uuid()`;
|
|
448
|
+
else idDef = `"${idName}" TEXT PRIMARY KEY`;
|
|
449
|
+
actions.push({
|
|
450
|
+
kind: "create-table",
|
|
451
|
+
target: key,
|
|
452
|
+
sql: `CREATE TABLE IF NOT EXISTS "${schema}"."${table}" (${idDef});`
|
|
453
|
+
});
|
|
454
|
+
}
|
|
455
|
+
const junctions = planJunctionTables(collections);
|
|
456
|
+
for (const junction of junctions) {
|
|
457
|
+
const key = `${junction.schema}.${junction.table}`;
|
|
458
|
+
if (existing.tables.has(key) || created.has(key)) continue;
|
|
459
|
+
created.add(key);
|
|
460
|
+
actions.push({
|
|
461
|
+
kind: "create-table",
|
|
462
|
+
target: key,
|
|
463
|
+
sql: junction.createTable
|
|
464
|
+
});
|
|
465
|
+
}
|
|
466
|
+
const addColumn = (key, schema, table, column, type) => {
|
|
467
|
+
if (existing.tables.get(key)?.has(column)) return;
|
|
468
|
+
actions.push({
|
|
469
|
+
kind: "add-column",
|
|
470
|
+
target: `${key}.${column}`,
|
|
471
|
+
sql: `ALTER TABLE "${schema}"."${table}" ADD COLUMN IF NOT EXISTS "${column}" ${type};`
|
|
472
|
+
});
|
|
473
|
+
};
|
|
474
|
+
for (const collection of collections) {
|
|
475
|
+
const key = qualified(collection);
|
|
476
|
+
const schema = schemaOf(collection);
|
|
477
|
+
const table = getTableName(collection);
|
|
478
|
+
for (const [propName, prop] of Object.entries(collection.properties ?? {})) {
|
|
479
|
+
const p = prop;
|
|
480
|
+
if (isIdProperty(propName, p, collection)) continue;
|
|
481
|
+
if (p.type === "reference" || p.type === "relation") continue;
|
|
482
|
+
addColumn(key, schema, table, resolveColumnName(propName, p), getSqlColumnType(propName, p, collection, collections));
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
for (const junction of junctions) {
|
|
486
|
+
const key = `${junction.schema}.${junction.table}`;
|
|
487
|
+
if (created.has(key)) continue;
|
|
488
|
+
for (const column of junction.columns) addColumn(key, junction.schema, junction.table, column.name, column.type);
|
|
489
|
+
}
|
|
490
|
+
for (const relational of planRelationalColumns(collections)) addColumn(`${relational.schema}.${relational.table}`, relational.schema, relational.table, relational.column, relational.type);
|
|
491
|
+
const knownConstraints = existing.constraints ?? /* @__PURE__ */ new Set();
|
|
492
|
+
const plannedConstraints = /* @__PURE__ */ new Set();
|
|
493
|
+
const foreignKeys = [...planRelationalColumns(collections).map((r) => r.foreignKey), ...junctions.flatMap((j) => j.foreignKeys)];
|
|
494
|
+
for (const fk of foreignKeys) {
|
|
495
|
+
if (!fk) continue;
|
|
496
|
+
const name = `${fk.schema}.${fk.table}.${fk.constraintName}`;
|
|
497
|
+
if (knownConstraints.has(name) || plannedConstraints.has(name)) continue;
|
|
498
|
+
plannedConstraints.add(name);
|
|
499
|
+
actions.push({
|
|
500
|
+
kind: "add-constraint",
|
|
501
|
+
target: `${fk.schema}.${fk.table}.${fk.constraintName}`,
|
|
502
|
+
sql: fk.sql
|
|
503
|
+
});
|
|
504
|
+
}
|
|
505
|
+
return {
|
|
506
|
+
actions,
|
|
507
|
+
statements: actions.map((a) => a.sql)
|
|
508
|
+
};
|
|
509
|
+
}
|
|
510
|
+
/** Read what the database has, for the schemas the collections live in. */
|
|
511
|
+
async function readExistingSchema(client, schemas) {
|
|
512
|
+
const tables = /* @__PURE__ */ new Map();
|
|
513
|
+
const enums = /* @__PURE__ */ new Set();
|
|
514
|
+
if (schemas.length === 0) return {
|
|
515
|
+
tables,
|
|
516
|
+
enums
|
|
517
|
+
};
|
|
518
|
+
const inList = schemas.map((schema) => `'${assertSafeIdentifier(schema, "schema name")}'`).join(", ");
|
|
519
|
+
const { rows: columns } = await client.query(`SELECT table_schema, table_name, column_name
|
|
520
|
+
FROM information_schema.columns
|
|
521
|
+
WHERE table_schema IN (${inList})`);
|
|
522
|
+
for (const row of columns) {
|
|
523
|
+
const key = `${row.table_schema}.${row.table_name}`;
|
|
524
|
+
if (!tables.has(key)) tables.set(key, /* @__PURE__ */ new Set());
|
|
525
|
+
tables.get(key).add(row.column_name);
|
|
526
|
+
}
|
|
527
|
+
const { rows: enumRows } = await client.query(`SELECT n.nspname AS schema, t.typname AS name
|
|
528
|
+
FROM pg_type t
|
|
529
|
+
JOIN pg_namespace n ON t.typnamespace = n.oid
|
|
530
|
+
WHERE t.typtype = 'e' AND n.nspname IN (${inList})`);
|
|
531
|
+
for (const row of enumRows) enums.add(`${row.schema}.${row.name}`);
|
|
532
|
+
const constraints = /* @__PURE__ */ new Set();
|
|
533
|
+
const { rows: constraintRows } = await client.query(`SELECT n.nspname AS schema, c.relname AS table, con.conname AS name
|
|
534
|
+
FROM pg_constraint con
|
|
535
|
+
JOIN pg_class c ON con.conrelid = c.oid
|
|
536
|
+
JOIN pg_namespace n ON c.relnamespace = n.oid
|
|
537
|
+
WHERE n.nspname IN (${inList})`);
|
|
538
|
+
for (const row of constraintRows) constraints.add(`${row.schema}.${row.table}.${row.name}`);
|
|
539
|
+
return {
|
|
540
|
+
tables,
|
|
541
|
+
enums,
|
|
542
|
+
constraints
|
|
543
|
+
};
|
|
544
|
+
}
|
|
545
|
+
/**
|
|
546
|
+
* Bring the database up to date. Returns what it did.
|
|
547
|
+
*
|
|
548
|
+
* Each statement runs on its own rather than in one transaction: they are all
|
|
549
|
+
* independently safe and idempotent, and a single failure (an enum label that
|
|
550
|
+
* cannot be added, say) should not roll back the tables that were created fine.
|
|
551
|
+
* The error is surfaced with the statement that caused it.
|
|
552
|
+
*/
|
|
553
|
+
async function ensureCollectionTables(client, collections, log) {
|
|
554
|
+
const schemas = Array.from(/* @__PURE__ */ new Set([...collections.map(schemaOf), ...planJunctionTables(collections).map((j) => j.schema)]));
|
|
555
|
+
for (const schema of schemas) {
|
|
556
|
+
assertSafeIdentifier(schema, "schema name");
|
|
557
|
+
if (schema !== "public") await client.query(`CREATE SCHEMA IF NOT EXISTS "${schema}";`);
|
|
558
|
+
}
|
|
559
|
+
const plan = planCollectionSchemaEnsure(collections, await readExistingSchema(client, schemas));
|
|
560
|
+
const failures = [];
|
|
561
|
+
if (plan.actions.length === 0) {
|
|
562
|
+
log?.("Schema is up to date; nothing to create.");
|
|
563
|
+
return {
|
|
564
|
+
...plan,
|
|
565
|
+
failures
|
|
566
|
+
};
|
|
567
|
+
}
|
|
568
|
+
for (const action of plan.actions) try {
|
|
569
|
+
await client.query(action.sql);
|
|
570
|
+
log?.(`${action.kind}: ${action.target}`);
|
|
571
|
+
} catch (err) {
|
|
572
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
573
|
+
if (action.kind === "add-constraint") {
|
|
574
|
+
failures.push({
|
|
575
|
+
target: action.target,
|
|
576
|
+
error: message
|
|
577
|
+
});
|
|
578
|
+
continue;
|
|
579
|
+
}
|
|
580
|
+
throw new Error(`Failed to ${action.kind} ${action.target}: ${message}\n ${action.sql}`);
|
|
581
|
+
}
|
|
582
|
+
return {
|
|
583
|
+
...plan,
|
|
584
|
+
failures
|
|
585
|
+
};
|
|
586
|
+
}
|
|
587
|
+
//#endregion
|
|
588
|
+
export { ensureCollectionTables, readExistingSchema, planCollectionPolicies as t };
|
|
589
|
+
|
|
590
|
+
//# sourceMappingURL=ensure-collection-tables-DsDsNl6o.js.map
|