@rebasepro/server-postgres 0.10.1-canary.b1e3dbf → 0.10.1-canary.ff9ccd6
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/chunk-DSJWtz9O.js +40 -0
- package/dist/ensure-collection-tables-C9gy4STB.js +304 -0
- package/dist/ensure-collection-tables-C9gy4STB.js.map +1 -0
- package/dist/index.es.js +174 -4544
- package/dist/index.es.js.map +1 -1
- package/dist/schema/ensure-collection-tables.d.ts +79 -0
- package/dist/schema/generate-postgres-ddl-logic.d.ts +4 -1
- package/dist/src-CBgtrPhJ.js +336 -0
- package/dist/src-CBgtrPhJ.js.map +1 -0
- package/dist/src-DG6ZsQQ3.js +4026 -0
- package/dist/src-DG6ZsQQ3.js.map +1 -0
- package/package.json +8 -9
- package/src/PostgresBootstrapper.ts +40 -0
- package/src/schema/doctor.ts +6 -6
- package/src/schema/ensure-collection-tables.test.ts +156 -0
- package/src/schema/ensure-collection-tables.ts +297 -0
- package/src/schema/generate-drizzle-schema-logic.ts +13 -9
- package/src/schema/generate-postgres-ddl-logic.ts +22 -15
- package/src/schema/introspect-db-inference.ts +13 -13
- package/src/schema/introspect-db-logic.ts +6 -6
|
@@ -4,7 +4,7 @@ import { toSnakeCase, getPolicyNamesForRule } from "@rebasepro/utils";
|
|
|
4
4
|
|
|
5
5
|
// --- Helper Functions ---
|
|
6
6
|
|
|
7
|
-
const resolveColumnName = (propName: string, prop?: Property | null): string => {
|
|
7
|
+
export const resolveColumnName = (propName: string, prop?: Property | null): string => {
|
|
8
8
|
if (prop && "columnName" in prop && typeof prop.columnName === "string") {
|
|
9
9
|
return prop.columnName;
|
|
10
10
|
}
|
|
@@ -36,7 +36,7 @@ const getPrimaryKeyName = (collection: CollectionConfig): string => {
|
|
|
36
36
|
return getPrimaryKeyProp(collection).name;
|
|
37
37
|
};
|
|
38
38
|
|
|
39
|
-
const isIdProperty = (propName: string, prop: Property, collection: CollectionConfig): boolean => {
|
|
39
|
+
export const isIdProperty = (propName: string, prop: Property, collection: CollectionConfig): boolean => {
|
|
40
40
|
if ("isId" in prop && Boolean(prop.isId)) return true;
|
|
41
41
|
const hasExplicitId = Object.values(collection.properties ?? {}).some(p => "isId" in (p as unknown as object) && Boolean((p as unknown as Record<string, unknown>).isId));
|
|
42
42
|
return !hasExplicitId && propName === "id";
|
|
@@ -90,7 +90,7 @@ const generateSinglePolicyDdl = (collection: CollectionConfig, rule: SecurityRul
|
|
|
90
90
|
return `${ddl};\n`;
|
|
91
91
|
};
|
|
92
92
|
|
|
93
|
-
const getSqlColumnType = (propName: string, prop: Property, collection: CollectionConfig, collections: CollectionConfig[]): string => {
|
|
93
|
+
export const getSqlColumnType = (propName: string, prop: Property, collection: CollectionConfig, collections: CollectionConfig[]): string => {
|
|
94
94
|
switch (prop.type) {
|
|
95
95
|
case "string": {
|
|
96
96
|
const stringProp = prop as StringProperty;
|
|
@@ -103,13 +103,18 @@ const getSqlColumnType = (propName: string, prop: Property, collection: Collecti
|
|
|
103
103
|
if (stringProp.isId === "uuid" || stringProp.columnType === "uuid") {
|
|
104
104
|
return "UUID";
|
|
105
105
|
}
|
|
106
|
-
if (stringProp.columnType === "text" || stringProp.ui?.markdown || stringProp.ui?.multiline) {
|
|
107
|
-
return "TEXT";
|
|
108
|
-
}
|
|
109
106
|
if (stringProp.columnType === "char") {
|
|
110
107
|
return "CHAR(255)";
|
|
111
108
|
}
|
|
112
|
-
|
|
109
|
+
if (stringProp.columnType === "varchar") {
|
|
110
|
+
return "VARCHAR(255)";
|
|
111
|
+
}
|
|
112
|
+
// `text` is the default. The two generators disagreed here before:
|
|
113
|
+
// this one emitted VARCHAR(255) while the drizzle path emitted a bare
|
|
114
|
+
// `varchar()`, which Postgres treats as unbounded — so the same
|
|
115
|
+
// property produced a capped column down one path and an uncapped one
|
|
116
|
+
// down the other.
|
|
117
|
+
return "TEXT";
|
|
113
118
|
}
|
|
114
119
|
case "number": {
|
|
115
120
|
const numProp = prop as NumberProperty;
|
|
@@ -173,20 +178,20 @@ const getSqlColumnType = (propName: string, prop: Property, collection: Collecti
|
|
|
173
178
|
try {
|
|
174
179
|
targetCollection = relation.target();
|
|
175
180
|
} catch {
|
|
176
|
-
return "
|
|
181
|
+
return "TEXT";
|
|
177
182
|
}
|
|
178
183
|
const pkProp = getPrimaryKeyProp(targetCollection);
|
|
179
|
-
return pkProp.type === "number" ? "INTEGER" : (pkProp.isUuid ? "UUID" : "
|
|
184
|
+
return pkProp.type === "number" ? "INTEGER" : (pkProp.isUuid ? "UUID" : "TEXT");
|
|
180
185
|
}
|
|
181
186
|
case "reference": {
|
|
182
187
|
const refProp = prop as ReferenceProperty;
|
|
183
188
|
const targetCollection = collections.find(c => c.slug === refProp.path || getTableName(c) === refProp.path);
|
|
184
|
-
if (!targetCollection) return "
|
|
189
|
+
if (!targetCollection) return "TEXT";
|
|
185
190
|
const pkProp = getPrimaryKeyProp(targetCollection);
|
|
186
|
-
return pkProp.type === "number" ? "INTEGER" : (pkProp.isUuid ? "UUID" : "
|
|
191
|
+
return pkProp.type === "number" ? "INTEGER" : (pkProp.isUuid ? "UUID" : "TEXT");
|
|
187
192
|
}
|
|
188
193
|
default:
|
|
189
|
-
return "
|
|
194
|
+
return "TEXT";
|
|
190
195
|
}
|
|
191
196
|
};
|
|
192
197
|
|
|
@@ -287,8 +292,10 @@ export const generatePostgresDdl = async (
|
|
|
287
292
|
const targetSchema = isPostgresCollectionConfig(targetCollection) && targetCollection.schema ? targetCollection.schema : "public";
|
|
288
293
|
const { sourceColumn, targetColumn } = relation.through;
|
|
289
294
|
|
|
290
|
-
|
|
291
|
-
|
|
295
|
+
// TEXT, matching the string default: a junction column has to have the
|
|
296
|
+
// same type as the primary key it references.
|
|
297
|
+
const sourceColType = isNumericId(sourceCollection) ? "INTEGER" : (getPrimaryKeyProp(sourceCollection).isUuid ? "UUID" : "TEXT");
|
|
298
|
+
const targetColType = isNumericId(targetCollection) ? "INTEGER" : (getPrimaryKeyProp(targetCollection).isUuid ? "UUID" : "TEXT");
|
|
292
299
|
const sourceId = getPrimaryKeyName(sourceCollection);
|
|
293
300
|
const targetId = getPrimaryKeyName(targetCollection);
|
|
294
301
|
|
|
@@ -424,7 +431,7 @@ export const generatePostgresDdl = async (
|
|
|
424
431
|
// Backwards compatibility: add default id primary key if missing
|
|
425
432
|
const hasPk = columns.some(c => c.includes("PRIMARY KEY"));
|
|
426
433
|
if (!hasPk) {
|
|
427
|
-
columns.unshift(' "id"
|
|
434
|
+
columns.unshift(' "id" TEXT PRIMARY KEY');
|
|
428
435
|
}
|
|
429
436
|
|
|
430
437
|
ddl += columns.join(",\n");
|
|
@@ -51,10 +51,11 @@ export function inferPropertyFromData(
|
|
|
51
51
|
}
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
-
//
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
54
|
+
// No currency heuristic. `admin.currency` was never a declared option and
|
|
55
|
+
// nothing reads it, so a column called `price` got a block of config that did
|
|
56
|
+
// nothing — invisible because this generator emits *source text*, which no
|
|
57
|
+
// typechecker ever saw. Reinstate it by adding `currency` to
|
|
58
|
+
// AdminNumberOptions first, and something that renders it.
|
|
58
59
|
}
|
|
59
60
|
|
|
60
61
|
// ── JSON / JSONB Analysis ────────────────────────────────────────────
|
|
@@ -169,11 +170,10 @@ export function inferPropertyFromData(
|
|
|
169
170
|
if (isPk) extraLines.push(" isId: \"cuid\"");
|
|
170
171
|
}
|
|
171
172
|
|
|
172
|
-
//
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
}
|
|
173
|
+
// No colour heuristic. `admin.color` was never a declared option and nothing
|
|
174
|
+
// reads it — the same dead branch `currency` was. This generator emits *source
|
|
175
|
+
// text*, so no typechecker ever saw either. Reinstate by adding the option and
|
|
176
|
+
// something that renders it first.
|
|
177
177
|
|
|
178
178
|
// Text Lengths, Multiline & Markdown
|
|
179
179
|
let maxLength = 0;
|
|
@@ -212,9 +212,9 @@ export function inferPropertyFromData(
|
|
|
212
212
|
if (allAbsoluteUrls) {
|
|
213
213
|
const isImage = validValues.some(v => typeof v === "string" && v.match(/\.(jpeg|jpg|gif|png|webp|svg)/i));
|
|
214
214
|
if (isImage || isMedia) {
|
|
215
|
-
extraLines.push("
|
|
215
|
+
extraLines.push(" url: true,\n admin: {\n urlPreview: \"image\"\n }");
|
|
216
216
|
} else {
|
|
217
|
-
extraLines.push("
|
|
217
|
+
extraLines.push(" url: true");
|
|
218
218
|
}
|
|
219
219
|
} else {
|
|
220
220
|
const hasFileExtension = validValues.some(v => typeof v === "string" && v.match(/\.[a-zA-Z0-9]+$/));
|
|
@@ -225,9 +225,9 @@ export function inferPropertyFromData(
|
|
|
225
225
|
extraLines.push(` storage: {\n storagePath: "${inferredStoragePath}"\n }`);
|
|
226
226
|
} else if (isUrl) {
|
|
227
227
|
if (isMedia) {
|
|
228
|
-
extraLines.push("
|
|
228
|
+
extraLines.push(" url: true,\n admin: {\n urlPreview: \"image\"\n }");
|
|
229
229
|
} else {
|
|
230
|
-
extraLines.push("
|
|
230
|
+
extraLines.push(" url: true");
|
|
231
231
|
}
|
|
232
232
|
}
|
|
233
233
|
}
|
|
@@ -569,11 +569,11 @@ export function generateCollectionFile(
|
|
|
569
569
|
// Date auto-value heuristics
|
|
570
570
|
if (finalPropType === "date") {
|
|
571
571
|
if (colNameLower === "created_at" || colNameLower === "createdat") {
|
|
572
|
-
extra += "\n autoValue: \"on_create\",\n
|
|
572
|
+
extra += "\n autoValue: \"on_create\",\n admin: {\n readOnly: true,\n hideFromCollection: true\n },";
|
|
573
573
|
} else if (colNameLower === "updated_at" || colNameLower === "updatedat") {
|
|
574
|
-
extra += "\n autoValue: \"on_update\",\n
|
|
574
|
+
extra += "\n autoValue: \"on_update\",\n admin: {\n readOnly: true,\n hideFromCollection: true\n },";
|
|
575
575
|
} else if (col.column_default && (col.column_default.includes("now()") || col.column_default.includes("CURRENT_TIMESTAMP"))) {
|
|
576
|
-
extra += "\n autoValue: \"on_create\",\n
|
|
576
|
+
extra += "\n autoValue: \"on_create\",\n admin: {\n readOnly: true\n },";
|
|
577
577
|
}
|
|
578
578
|
}
|
|
579
579
|
|
|
@@ -604,13 +604,13 @@ export function generateCollectionFile(
|
|
|
604
604
|
if (isMedia) {
|
|
605
605
|
extra += `\n storage: {\n storagePath: "${tableName}/${col.column_name}"\n },`;
|
|
606
606
|
} else if (isUrl) {
|
|
607
|
-
extra += "\n
|
|
607
|
+
extra += "\n url: true,";
|
|
608
608
|
} else if (colNameLower === "description" || colNameLower === "summary" || colNameLower === "excerpt") {
|
|
609
|
-
extra += "\n multiline: true,";
|
|
609
|
+
extra += "\n admin: {\n multiline: true\n },";
|
|
610
610
|
} else if (colNameLower === "content" || colNameLower === "body") {
|
|
611
611
|
extra += "\n multiline: true,\n markdown: true,";
|
|
612
612
|
} else if (col.data_type === "text") {
|
|
613
|
-
extra += "\n multiline: true,";
|
|
613
|
+
extra += "\n admin: {\n multiline: true\n },";
|
|
614
614
|
}
|
|
615
615
|
}
|
|
616
616
|
|