@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.
@@ -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
- return "VARCHAR(255)";
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 "VARCHAR(255)";
181
+ return "TEXT";
177
182
  }
178
183
  const pkProp = getPrimaryKeyProp(targetCollection);
179
- return pkProp.type === "number" ? "INTEGER" : (pkProp.isUuid ? "UUID" : "VARCHAR(255)");
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 "VARCHAR(255)";
189
+ if (!targetCollection) return "TEXT";
185
190
  const pkProp = getPrimaryKeyProp(targetCollection);
186
- return pkProp.type === "number" ? "INTEGER" : (pkProp.isUuid ? "UUID" : "VARCHAR(255)");
191
+ return pkProp.type === "number" ? "INTEGER" : (pkProp.isUuid ? "UUID" : "TEXT");
187
192
  }
188
193
  default:
189
- return "VARCHAR(255)";
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
- const sourceColType = isNumericId(sourceCollection) ? "INTEGER" : (getPrimaryKeyProp(sourceCollection).isUuid ? "UUID" : "VARCHAR(255)");
291
- const targetColType = isNumericId(targetCollection) ? "INTEGER" : (getPrimaryKeyProp(targetCollection).isUuid ? "UUID" : "VARCHAR(255)");
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" VARCHAR(255) PRIMARY KEY');
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
- // Currency
55
- if (colNameLower.includes("price") || colNameLower.includes("cost") || colNameLower.includes("amount") || colNameLower.includes("fee") || pgDataType === "money") {
56
- extraLines.push(" ui: {\n currency: true\n }");
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
- // Color Codes
173
- const allColors = validValues.every(v => typeof v === "string" && COLOR_HEX_REGEX.test(v));
174
- if (allColors) {
175
- extraLines.push(" ui: {\n color: true\n }");
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(" ui: {\n url: \"image\"\n }");
215
+ extraLines.push(" url: true,\n admin: {\n urlPreview: \"image\"\n }");
216
216
  } else {
217
- extraLines.push(" ui: {\n url: true\n }");
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(" ui: {\n url: \"image\"\n }");
228
+ extraLines.push(" url: true,\n admin: {\n urlPreview: \"image\"\n }");
229
229
  } else {
230
- extraLines.push(" ui: {\n url: true\n }");
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 ui: {\n readOnly: true,\n hideFromCollection: true\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 ui: {\n readOnly: true,\n hideFromCollection: true\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 ui: {\n readOnly: true\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 ui: {\n url: true\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