@rebasepro/server-postgres 0.13.1-canary.g18cfeb7 → 0.13.1-canary.g1bf9303

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.
Files changed (88) hide show
  1. package/dist/PostgresBootstrapper.d.ts +26 -0
  2. package/dist/auth/services.d.ts +21 -0
  3. package/dist/{auth-users-columns-Do9mw5Y5.js → auth-users-columns-BxT6FRyu.js} +819 -68
  4. package/dist/auth-users-columns-BxT6FRyu.js.map +1 -0
  5. package/dist/{backup-service-Bww-Lg0s.js → backup-service-BH0Dzo_h.js} +2 -3
  6. package/dist/{backup-service-Bww-Lg0s.js.map → backup-service-BH0Dzo_h.js.map} +1 -1
  7. package/dist/cli-helpers.d.ts +56 -0
  8. package/dist/cli-output.d.ts +34 -0
  9. package/dist/data-transformer.d.ts +7 -2
  10. package/dist/data_driver-ULAyJEi9.js +193 -0
  11. package/dist/data_driver-ULAyJEi9.js.map +1 -0
  12. package/dist/ensure-collection-policies-CP5_YttS.js +124 -0
  13. package/dist/ensure-collection-policies-CP5_YttS.js.map +1 -0
  14. package/dist/{ensure-collection-tables-DSIxvLLD.js → ensure-collection-tables-DWVISfcC.js} +119 -13
  15. package/dist/ensure-collection-tables-DWVISfcC.js.map +1 -0
  16. package/dist/index.es.js +998 -600
  17. package/dist/index.es.js.map +1 -1
  18. package/dist/{rls-bootstrap-sql-Bpv3nUZo.js → rls-bootstrap-sql-69hYT8nr.js} +2 -2
  19. package/dist/{rls-bootstrap-sql-Bpv3nUZo.js.map → rls-bootstrap-sql-69hYT8nr.js.map} +1 -1
  20. package/dist/rls-enforcement-Ct9ab42o.js +425 -0
  21. package/dist/rls-enforcement-Ct9ab42o.js.map +1 -0
  22. package/dist/schema/auth-schema.d.ts +102 -0
  23. package/dist/schema/doctor-policy-checks.d.ts +28 -0
  24. package/dist/schema/doctor.d.ts +41 -25
  25. package/dist/schema/ensure-collection-policies.d.ts +33 -9
  26. package/dist/schema/ensure-collection-tables.d.ts +60 -6
  27. package/dist/schema/generate-drizzle-schema-logic.d.ts +9 -1
  28. package/dist/schema/generate-postgres-ddl-logic.d.ts +48 -0
  29. package/dist/schema/search-column.d.ts +248 -0
  30. package/dist/security/rls-enforcement.d.ts +8 -3
  31. package/dist/services/FetchService.d.ts +24 -0
  32. package/dist/services/RelationService.d.ts +30 -0
  33. package/dist/services/collection-helpers.d.ts +26 -0
  34. package/dist/services/dataService.d.ts +3 -0
  35. package/dist/services/realtimeService.d.ts +139 -2
  36. package/dist/{src-C_wvdMnl.js → src-DCdn3Val.js} +35 -3
  37. package/dist/src-DCdn3Val.js.map +1 -0
  38. package/dist/utils/drizzle-conditions.d.ts +124 -2
  39. package/dist/{websocket-D0TBU3ia.js → websocket-C8ZqVBiV.js} +75 -18
  40. package/dist/websocket-C8ZqVBiV.js.map +1 -0
  41. package/package.json +6 -6
  42. package/src/PostgresBackendDriver.ts +7 -3
  43. package/src/PostgresBootstrapper.ts +95 -9
  44. package/src/auth/ensure-tables.ts +27 -5
  45. package/src/auth/services.ts +82 -5
  46. package/src/backup/backup-cli.ts +59 -57
  47. package/src/cli-errors.ts +6 -6
  48. package/src/cli-helpers.ts +110 -4
  49. package/src/cli-output.ts +43 -0
  50. package/src/cli.ts +210 -145
  51. package/src/collections/buildRegistry.ts +3 -1
  52. package/src/data-transformer.ts +107 -14
  53. package/src/history/ensure-history-table.ts +2 -2
  54. package/src/schema/auth-schema.ts +17 -1
  55. package/src/schema/doctor-cli.ts +14 -65
  56. package/src/schema/doctor-policy-checks.ts +105 -0
  57. package/src/schema/doctor.ts +149 -72
  58. package/src/schema/ensure-collection-policies.ts +99 -6
  59. package/src/schema/ensure-collection-tables.ts +286 -13
  60. package/src/schema/generate-drizzle-schema-logic.ts +126 -42
  61. package/src/schema/generate-drizzle-schema.ts +11 -10
  62. package/src/schema/generate-postgres-ddl-logic.ts +204 -10
  63. package/src/schema/generate-postgres-ddl.ts +38 -14
  64. package/src/schema/introspect-db-logic.ts +65 -33
  65. package/src/schema/introspect-db.ts +24 -24
  66. package/src/schema/search-column.ts +643 -0
  67. package/src/security/rls-enforcement.ts +9 -4
  68. package/src/services/FetchService.ts +229 -15
  69. package/src/services/PersistService.ts +24 -1
  70. package/src/services/RelationService.ts +119 -69
  71. package/src/services/cdc/trigger-cdc.ts +5 -1
  72. package/src/services/channel-history.ts +9 -3
  73. package/src/services/channel-presence.ts +10 -3
  74. package/src/services/collection-helpers.ts +54 -1
  75. package/src/services/dataService.ts +3 -0
  76. package/src/services/pg-notify-listener.ts +1 -1
  77. package/src/services/realtimeService.ts +347 -86
  78. package/src/utils/drizzle-conditions.ts +365 -23
  79. package/src/utils/pg-error-utils.ts +8 -3
  80. package/src/websocket.ts +113 -16
  81. package/dist/auth-users-columns-Do9mw5Y5.js.map +0 -1
  82. package/dist/ensure-collection-policies-DMUdRQdM.js +0 -57
  83. package/dist/ensure-collection-policies-DMUdRQdM.js.map +0 -1
  84. package/dist/ensure-collection-tables-DSIxvLLD.js.map +0 -1
  85. package/dist/policy-CPkCqVTz.js +0 -105
  86. package/dist/policy-CPkCqVTz.js.map +0 -1
  87. package/dist/src-C_wvdMnl.js.map +0 -1
  88. package/dist/websocket-D0TBU3ia.js.map +0 -1
@@ -1,6 +1,7 @@
1
1
  import { CollectionConfig, NumberProperty, Property, ResolvedRelation, RelationProperty, SecurityOperation, SecurityRule, StringProperty, isPostgresCollectionConfig, DateProperty, ArrayProperty, MapProperty, ReferenceProperty, VectorProperty, BinaryProperty, isManyToMany, type ResolvedManyToMany, type ResolvedBelongsTo, type ResolvedForeignKeyOnTarget, hasForeignKeyOnTarget } from "@rebasepro/types";
2
2
  import { getPrimaryKeys } from "../services/collection-helpers";
3
- import { getEnumVarName, getTableName, getTableVarName, resolveCollectionRelations, findRelation, securityRuleToConditions, policyToPostgres, getEffectiveSecurityRules, resolveJunctionSpecs, getJunctionSecurityRules, getJunctionCollectionConfig, resolveStringColumnLength, relationalCollections } from "@rebasepro/common";
3
+ import { buildSearchColumnSpec } from "./search-column";
4
+ import { getEnumVarName, getTableName, getTableVarName, resolveCollectionRelations, findRelation, securityRuleToConditions, policyToPostgres, getEffectiveSecurityRules, resolveJunctionSpecs, getJunctionSecurityRules, getJunctionCollectionConfig, resolveStringColumnLength, relationalCollections, sortCollectionsBySlug } from "@rebasepro/common";
4
5
  import { toSnakeCase, getPolicyNamesForRule } from "@rebasepro/utils";
5
6
  import { logger } from "@rebasepro/server";
6
7
  // --- Helper Functions ---
@@ -10,6 +11,31 @@ import { logger } from "@rebasepro/server";
10
11
  * Uses the explicit `columnName` when set (e.g. from introspection),
11
12
  * falling back to `toSnakeCase(propName)` for manually-authored collections.
12
13
  */
14
+ const JS_IDENTIFIER = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
15
+
16
+ /**
17
+ * A string literal for the generated schema file.
18
+ *
19
+ * Column names, table names and enum values are all written into this file as
20
+ * literals, and none of them is constrained to be quote-free: a Postgres
21
+ * identifier only has to be quoted, and `O'Brien` is an ordinary enum value.
22
+ * Interpolating them raw ended the literal early — for enum values, inside
23
+ * single quotes, where an apostrophe is not an edge case.
24
+ */
25
+ const quote = (value: string): string => JSON.stringify(value);
26
+
27
+ /** An object key: verbatim when it is an identifier, quoted otherwise. */
28
+ const propKey = (name: string): string => (JS_IDENTIFIER.test(name) ? name : quote(name));
29
+
30
+ /**
31
+ * A property access on a generated table variable.
32
+ *
33
+ * `users.full name` is not an expression; `users["full name"]` is, and Drizzle
34
+ * treats the two identically.
35
+ */
36
+ const member = (object: string, key: string): string =>
37
+ (JS_IDENTIFIER.test(key) ? `${object}.${key}` : `${object}[${quote(key)}]`);
38
+
13
39
  const resolveColumnName = (propName: string, prop?: Property | null): string => {
14
40
  if (prop && "columnName" in prop && typeof prop.columnName === "string") {
15
41
  return prop.columnName;
@@ -82,7 +108,14 @@ const isIdProperty = (propName: string, prop: Property, collection: CollectionCo
82
108
  return !hasExplicitId && propName === "id";
83
109
  };
84
110
 
85
- const getDrizzleColumn = (propName: string, prop: Property, collection: CollectionConfig, collections: CollectionConfig[]): string | null => {
111
+ /**
112
+ * The Drizzle column declaration a property compiles to, or `null` when the
113
+ * property puts no column on *this* table (an inverse relation, whose column
114
+ * lives on the target). Exported so it can be checked against its DDL twin
115
+ * `getSqlColumnType` directly — the two disagreeing is what left `geopoint`
116
+ * with a database column and no Drizzle key.
117
+ */
118
+ export const getDrizzleColumn = (propName: string, prop: Property, collection: CollectionConfig, collections: CollectionConfig[]): string | null => {
86
119
 
87
120
  const colName = resolveColumnName(propName, prop);
88
121
  let columnDefinition: string;
@@ -92,23 +125,23 @@ const getDrizzleColumn = (propName: string, prop: Property, collection: Collecti
92
125
  const stringProp = prop as unknown as StringProperty;
93
126
  if (stringProp.enum) {
94
127
  const enumName = getEnumVarName(getTableName(collection), propName);
95
- columnDefinition = `${enumName}("${colName}")`;
128
+ columnDefinition = `${enumName}(${quote(colName)})`;
96
129
  } else if ("isId" in stringProp && stringProp.isId === "uuid") {
97
- columnDefinition = `uuid("${colName}")`;
130
+ columnDefinition = `uuid(${quote(colName)})`;
98
131
  } else if (stringProp.columnType === "uuid") {
99
- columnDefinition = `uuid("${colName}")`;
132
+ columnDefinition = `uuid(${quote(colName)})`;
100
133
  } else if (stringProp.columnType === "char") {
101
- columnDefinition = `char("${colName}", { length: ${resolveStringColumnLength(stringProp)} })`;
134
+ columnDefinition = `char(${quote(colName)}, { length: ${resolveStringColumnLength(stringProp)} })`;
102
135
  } else if (stringProp.columnType === "varchar") {
103
136
  // The length is not optional decoration: `varchar("col")` with
104
137
  // no length is an UNBOUNDED varchar in Postgres, which is what
105
138
  // this emitted while the DDL generator emitted VARCHAR(255) for
106
139
  // the very same property.
107
- columnDefinition = `varchar("${colName}", { length: ${resolveStringColumnLength(stringProp)} })`;
140
+ columnDefinition = `varchar(${quote(colName)}, { length: ${resolveStringColumnLength(stringProp)} })`;
108
141
  } else {
109
142
  // `text` is the default, and the only length-unbounded choice.
110
143
  // Ask for `varchar` explicitly if you want the length constraint.
111
- columnDefinition = `text("${colName}")`;
144
+ columnDefinition = `text(${quote(colName)})`;
112
145
  }
113
146
  if (isIdProperty(propName, prop, collection)) {
114
147
  columnDefinition += ".primaryKey()";
@@ -134,9 +167,9 @@ const getDrizzleColumn = (propName: string, prop: Property, collection: Collecti
134
167
  const numProp = prop as unknown as NumberProperty;
135
168
  const isId = isIdProperty(propName, prop, collection);
136
169
 
137
- let baseType = (numProp.validation?.integer || isId) ? `integer("${colName}")` : `numeric("${colName}")`;
170
+ let baseType = (numProp.validation?.integer || isId) ? `integer(${quote(colName)})` : `numeric(${quote(colName)})`;
138
171
  if (numProp.columnType) {
139
- if (numProp.columnType === "double precision") baseType = `doublePrecision("${colName}")`;
172
+ if (numProp.columnType === "double precision") baseType = `doublePrecision(${quote(colName)})`;
140
173
  // `bigint` and `bigserial` are the only pg-core builders that
141
174
  // *require* a config argument: without `mode`, drizzle cannot
142
175
  // know whether to hand back a `number` or a `bigint`, and the
@@ -154,9 +187,9 @@ const getDrizzleColumn = (propName: string, prop: Property, collection: Collecti
154
187
  // switching the runtime type would be a breaking change to
155
188
  // every consumer of the generated schema.
156
189
  else if (numProp.columnType === "bigint" || numProp.columnType === "bigserial") {
157
- baseType = `${numProp.columnType}("${colName}", { mode: "number" })`;
190
+ baseType = `${numProp.columnType}(${quote(colName)}, { mode: "number" })`;
158
191
  }
159
- else baseType = `${numProp.columnType}("${colName}")`;
192
+ else baseType = `${numProp.columnType}(${quote(colName)})`;
160
193
  }
161
194
 
162
195
  if ("isId" in numProp && numProp.isId === "increment") {
@@ -180,16 +213,16 @@ const getDrizzleColumn = (propName: string, prop: Property, collection: Collecti
180
213
  break;
181
214
  }
182
215
  case "boolean":
183
- columnDefinition = `boolean("${colName}")`;
216
+ columnDefinition = `boolean(${quote(colName)})`;
184
217
  break;
185
218
  case "date": {
186
219
  const dateProp = prop as DateProperty;
187
220
  if (dateProp.columnType === "date") {
188
- columnDefinition = `date("${colName}", { mode: 'string' })`;
221
+ columnDefinition = `date(${quote(colName)}, { mode: 'string' })`;
189
222
  } else if (dateProp.columnType === "time") {
190
- columnDefinition = `time("${colName}")`;
223
+ columnDefinition = `time(${quote(colName)})`;
191
224
  } else {
192
- columnDefinition = `timestamp("${colName}", { withTimezone: true, mode: 'string' })`;
225
+ columnDefinition = `timestamp(${quote(colName)}, { withTimezone: true, mode: 'string' })`;
193
226
  }
194
227
  // autoValue: database-level default for initial value on INSERT
195
228
  if (dateProp.autoValue === "on_create" || dateProp.autoValue === "on_update") {
@@ -200,12 +233,22 @@ const getDrizzleColumn = (propName: string, prop: Property, collection: Collecti
200
233
  case "map": {
201
234
  const mapProp = prop as MapProperty;
202
235
  if (mapProp.columnType === "json") {
203
- columnDefinition = `json("${colName}")`;
236
+ columnDefinition = `json(${quote(colName)})`;
204
237
  } else {
205
- columnDefinition = `jsonb("${colName}")`;
238
+ columnDefinition = `jsonb(${quote(colName)})`;
206
239
  }
207
240
  break;
208
241
  }
242
+ case "geopoint": {
243
+ // `{ latitude, longitude }`, which is what the OpenAPI schema, the
244
+ // generated TS type and the admin's field binding all describe.
245
+ // This arm did not exist: `geopoint` fell to `default: return null`
246
+ // and the caller dropped the column, so the DDL generator created a
247
+ // column the Drizzle table had no key for — and every write to it
248
+ // was silently discarded with a 201.
249
+ columnDefinition = `jsonb(${quote(colName)})`;
250
+ break;
251
+ }
209
252
  case "array": {
210
253
  const arrayProp = prop as ArrayProperty;
211
254
  let colType = arrayProp.columnType;
@@ -221,27 +264,27 @@ const getDrizzleColumn = (propName: string, prop: Property, collection: Collecti
221
264
  }
222
265
 
223
266
  if (colType === "json") {
224
- columnDefinition = `json("${colName}")`;
267
+ columnDefinition = `json(${quote(colName)})`;
225
268
  } else if (colType === "text[]") {
226
- columnDefinition = `text("${colName}").array()`;
269
+ columnDefinition = `text(${quote(colName)}).array()`;
227
270
  } else if (colType === "integer[]") {
228
- columnDefinition = `integer("${colName}").array()`;
271
+ columnDefinition = `integer(${quote(colName)}).array()`;
229
272
  } else if (colType === "boolean[]") {
230
- columnDefinition = `boolean("${colName}").array()`;
273
+ columnDefinition = `boolean(${quote(colName)}).array()`;
231
274
  } else if (colType === "numeric[]") {
232
- columnDefinition = `numeric("${colName}").array()`;
275
+ columnDefinition = `numeric(${quote(colName)}).array()`;
233
276
  } else {
234
- columnDefinition = `jsonb("${colName}")`;
277
+ columnDefinition = `jsonb(${quote(colName)})`;
235
278
  }
236
279
  break;
237
280
  }
238
281
  case "vector": {
239
282
  const vp = prop as VectorProperty;
240
- columnDefinition = `vector("${colName}", { dimensions: ${vp.dimensions} })`;
283
+ columnDefinition = `vector(${quote(colName)}, { dimensions: ${vp.dimensions} })`;
241
284
  break;
242
285
  }
243
286
  case "binary": {
244
- columnDefinition = `customType({ dataType() { return 'bytea'; } })("${colName}")`;
287
+ columnDefinition = `customType({ dataType() { return 'bytea'; } })(${quote(colName)})`;
245
288
  break;
246
289
  }
247
290
  case "relation": {
@@ -282,47 +325,60 @@ const getDrizzleColumn = (propName: string, prop: Property, collection: Collecti
282
325
  const refOptionsParts = [onUpdate, onDelete].filter(Boolean);
283
326
  const refOptions = refOptionsParts.length > 0 ? `{ ${refOptionsParts.join(", ")} }` : "";
284
327
 
285
- let columnDef = `${baseColumn}.references(() => ${targetTableVar}.${targetIdField}${refOptions ? `, ${refOptions}` : ""})`;
328
+ let columnDef = `${baseColumn}.references(() => ${member(targetTableVar, targetIdField)}${refOptions ? `, ${refOptions}` : ""})`;
286
329
 
287
330
  if (required) {
288
331
  columnDef += ".notNull()";
289
332
  }
290
333
 
291
- return ` ${relation.localKey}: ${columnDef}`;
334
+ return ` ${propKey(relation.localKey)}: ${columnDef}`;
292
335
  }
293
336
  case "reference": {
294
337
  const refProp = prop as ReferenceProperty;
295
338
  const targetCollection = collections.find(c => c.slug === refProp.path || getTableName(c) === refProp.path);
296
339
  if (!targetCollection) {
297
- columnDefinition = `text("${colName}")`;
340
+ columnDefinition = `text(${quote(colName)})`;
298
341
  break;
299
342
  }
300
343
 
301
344
  const pkProp = getPrimaryKeyProp(targetCollection);
302
345
  const targetTableVar = getTableVarName(getTableName(targetCollection));
303
346
  const targetIdField = pkProp.name;
304
- const baseColumn = pkProp.type === "number" ? `integer("${colName}")` : (pkProp.isUuid ? `uuid("${colName}")` : `text("${colName}")`);
347
+ const baseColumn = pkProp.type === "number" ? `integer(${quote(colName)})` : (pkProp.isUuid ? `uuid(${quote(colName)})` : `text(${quote(colName)})`);
305
348
 
306
349
  const required = prop.validation?.required;
307
350
  const onDelete = required ? "cascade" : "set null";
308
351
  const refOptions = `{ onDelete: "${onDelete}" }`;
309
352
 
310
- columnDefinition = `${baseColumn}.references(() => ${targetTableVar}.${targetIdField}, ${refOptions})`;
353
+ columnDefinition = `${baseColumn}.references(() => ${member(targetTableVar, targetIdField)}, ${refOptions})`;
311
354
  if (required) {
312
355
  columnDefinition += ".notNull()";
313
356
  }
314
357
  // Skip the standard notNull() handling below because we did it here with references
315
- return ` ${propName}: ${columnDefinition}`;
358
+ return ` ${propKey(propName)}: ${columnDefinition}`;
316
359
  }
317
360
  default:
318
- return null;
361
+ // Not `return null`. A `null` here means "this property puts no
362
+ // column on this table", which is true of an inverse relation and
363
+ // false of everything else — and the caller cannot tell the two
364
+ // apart, so a type this switch simply forgot produced a table
365
+ // missing a column while the DDL generator happily created one.
366
+ // That is how `geopoint` stayed unpersistable: writes to it were
367
+ // dropped before the SQL was built, forever, with a 201. A property
368
+ // type nobody mapped is a generator bug, and it says so here rather
369
+ // than at some caller's next INSERT.
370
+ throw new Error(
371
+ `No Postgres column mapping for property '${propName}' of type ` +
372
+ `'${(prop as Property).type}' in collection '${collection.slug}'. ` +
373
+ "Add a case to `getDrizzleColumn` (and to `getSqlColumnType`, which must agree)."
374
+ );
319
375
  }
320
376
 
321
377
  if (prop.validation?.required) {
322
378
  columnDefinition += ".notNull()";
323
379
  }
324
380
 
325
- return ` ${propName}: ${columnDefinition}`;
381
+ return ` ${propKey(propName)}: ${columnDefinition}`;
326
382
  };
327
383
 
328
384
  /**
@@ -399,7 +455,7 @@ const generateSinglePolicyCode = (collection: CollectionConfig, rule: SecurityRu
399
455
  if (usingClause) parts.push(`using: ${usingClause}`);
400
456
  if (withCheckClause) parts.push(`withCheck: ${withCheckClause}`);
401
457
 
402
- return ` pgPolicy("${policyName}", { ${parts.join(", ")} }),\n`;
458
+ return ` pgPolicy(${quote(policyName)}, { ${parts.join(", ")} }),\n`;
403
459
  };
404
460
 
405
461
  /**
@@ -457,7 +513,12 @@ export const generateSchema = async (allCollections: CollectionConfig[], stripPo
457
513
  // `rebase doctor` would then report the store the collection actually reads
458
514
  // from as drift. Non-SQL collections leave the toolchain here, once, rather
459
515
  // than being filtered again in each stage below.
460
- const collections = relationalCollections(allCollections);
516
+ //
517
+ // Sorted here rather than in the writer: the output is order-dependent and
518
+ // `rebase doctor` regenerates it in memory to diff against the file on
519
+ // disk. With the sort in the writer only, a project whose file order
520
+ // differed from its slug order was reported stale forever.
521
+ const collections = sortCollectionsBySlug(relationalCollections(allCollections));
461
522
  let schemaContent = "// This file is auto-generated by the Rebase Drizzle generator. Do not edit manually.\n\n";
462
523
 
463
524
 
@@ -479,11 +540,15 @@ export const generateSchema = async (allCollections: CollectionConfig[], stripPo
479
540
  )
480
541
  );
481
542
 
543
+ // drizzle ships no `tsvector` builder, so an opted-in search column reaches
544
+ // the schema the same way `bytea` does — through `customType`.
545
+ const hasSearch = collections.some(c => buildSearchColumnSpec(c) !== undefined);
546
+
482
547
  // Always import pgPolicy and sql — RLS is enabled on every table (secure by default)
483
548
  const pgCoreImports = ["primaryKey", "pgTable", "integer", "varchar", "text", "char", "boolean", "timestamp", "date", "time", "jsonb", "json", "pgEnum", "numeric", "real", "doublePrecision", "bigint", "serial", "bigserial", "pgPolicy"];
484
549
  if (hasUuid) pgCoreImports.push("uuid");
485
550
  if (hasVector) pgCoreImports.push("vector");
486
- if (hasBinary) pgCoreImports.push("customType");
551
+ if (hasBinary || hasSearch) pgCoreImports.push("customType");
487
552
 
488
553
  const uniqueSchemas = Array.from(new Set(
489
554
  collections.map(c => isPostgresCollectionConfig(c) ? c.schema : undefined).filter(Boolean)
@@ -527,7 +592,7 @@ export const generateSchema = async (allCollections: CollectionConfig[], stripPo
527
592
  )
528
593
  : Object.keys(prop.enum);
529
594
  if (values.length > 0) {
530
- schemaContent += `export const ${enumVarName} = pgEnum(\"${enumDbName}\", [${values.map(v => `'${v}'`).join(", ")}]);\n`;
595
+ schemaContent += `export const ${enumVarName} = pgEnum(${quote(enumDbName)}, [${values.map(v => quote(v)).join(", ")}]);\n`;
531
596
  if (!exportedEnumVars.includes(enumVarName)) exportedEnumVars.push(enumVarName);
532
597
  }
533
598
  }
@@ -632,6 +697,25 @@ export const generateSchema = async (allCollections: CollectionConfig[], stripPo
632
697
 
633
698
  });
634
699
 
700
+ // The opt-in search column. Declared with its real generation
701
+ // expression rather than as a bare custom type: `schema.generated.ts`
702
+ // is what a developer running drizzle-kit themselves diffs against,
703
+ // and a column declared without its expression reads to drizzle-kit
704
+ // as one it should alter.
705
+ const searchSpec = buildSearchColumnSpec(collection);
706
+ if (searchSpec) {
707
+ columns.add(
708
+ ` ${searchSpec.column}: customType({ dataType() { return 'tsvector'; } })("${searchSpec.column}")` +
709
+ `.generatedAlwaysAs(sql\`${searchSpec.expression}\`)`
710
+ );
711
+ if (searchSpec.fuzzy) {
712
+ columns.add(
713
+ ` ${searchSpec.fuzzy.column}: text("${searchSpec.fuzzy.column}")` +
714
+ `.generatedAlwaysAs(sql\`${searchSpec.fuzzy.expression}\`)`
715
+ );
716
+ }
717
+ }
718
+
635
719
  // Backwards compatibility: if no id/primary key column is found in properties, but `id` wasn't explicitly provided
636
720
  // We should generate a basic id column if one was completely omitted.
637
721
  const hasIdColumn = Array.from(columns).some(col => col.includes(".primaryKey()"));
@@ -704,7 +788,7 @@ export const generateSchema = async (allCollections: CollectionConfig[], stripPo
704
788
  }
705
789
 
706
790
  // Source side one(): pairs with owning table's many(junctionTable, { relationName })
707
- tableRelations.push(` "${relation.through.sourceColumn}": one(${sourceTableVar}, {\n fields: [${tableVarName}.${relation.through.sourceColumn}],\n references: [${sourceTableVar}.${sourceId}],\n relationName: \"${owningRelationName}\"\n })`);
791
+ tableRelations.push(` ${quote(relation.through.sourceColumn)}: one(${sourceTableVar}, {\n fields: [${member(tableVarName, relation.through.sourceColumn)}],\n references: [${member(sourceTableVar, sourceId)}],\n relationName: ${quote(owningRelationName)}\n })`);
708
792
 
709
793
  // Target side one(): pairs with inverse table's many(junctionTable, { relationName })
710
794
  // Always emit a relationName to avoid collisions with the source-side's owningRelationName.
@@ -712,7 +796,7 @@ export const generateSchema = async (allCollections: CollectionConfig[], stripPo
712
796
  const targetRelationName = inverseRelationName
713
797
  ? inverseRelationName
714
798
  : `${tableName}_${relation.through.targetColumn}`;
715
- tableRelations.push(` "${relation.through.targetColumn}": one(${targetTableVar}, {\n fields: [${tableVarName}.${relation.through.targetColumn}],\n references: [${targetTableVar}.${targetId}],\n relationName: "${targetRelationName}"\n })`);
799
+ tableRelations.push(` ${quote(relation.through.targetColumn)}: one(${targetTableVar}, {\n fields: [${member(tableVarName, relation.through.targetColumn)}],\n references: [${member(targetTableVar, targetId)}],\n relationName: ${quote(targetRelationName)}\n })`);
716
800
  }
717
801
  } else {
718
802
  const resolvedRelations = resolveCollectionRelations(collection);
@@ -752,7 +836,7 @@ export const generateSchema = async (allCollections: CollectionConfig[], stripPo
752
836
  // schema that does not compile. The three other
753
837
  // emission sites normalise; this one did not.
754
838
  const localFieldKey = resolvePropertyKeyForColumn(collection, rel.localKey);
755
- tableRelations.push(` "${relationKey}": one(${targetTableVar}, {\n fields: [${tableVarName}.${localFieldKey}],\n references: [${targetTableVar}.${getPrimaryKeyName(target)}],\n relationName: \"${drizzleRelationName}\"\n })`);
839
+ tableRelations.push(` ${quote(relationKey)}: one(${targetTableVar}, {\n fields: [${member(tableVarName, localFieldKey)}],\n references: [${member(targetTableVar, getPrimaryKeyName(target))}],\n relationName: ${quote(drizzleRelationName)}\n })`);
756
840
  break;
757
841
  }
758
842
 
@@ -817,7 +901,7 @@ export const generateSchema = async (allCollections: CollectionConfig[], stripPo
817
901
  ? resolvePropertyKeyForColumn(otherCollection, otherRel.sourceKey)
818
902
  : getPrimaryKeyName(otherCollection);
819
903
  const synthKey = `_synth_${otherTableVar}_${drizzleFieldKey}`;
820
- tableRelations.push(` "${synthKey}": one(${otherTableVar}, {\n fields: [${tableVarName}.${drizzleFieldKey}],\n references: [${otherTableVar}.${referencedKey}],\n relationName: \"${drizzleRelationName}\"\n })`);
904
+ tableRelations.push(` ${quote(synthKey)}: one(${otherTableVar}, {\n fields: [${member(tableVarName, drizzleFieldKey)}],\n references: [${member(otherTableVar, referencedKey)}],\n relationName: ${quote(drizzleRelationName)}\n })`);
821
905
  emittedRelationNames.add(deduplicationKey);
822
906
  }
823
907
  }
@@ -5,7 +5,8 @@ import { pathToFileURL } from "url";
5
5
  import chokidar from "chokidar";
6
6
  import { generateSchema } from "./generate-drizzle-schema-logic";
7
7
  import { CollectionConfig } from "@rebasepro/types";
8
- import { logger, loadCollectionsFromDirectory } from "@rebasepro/server";
8
+ import { loadCollectionsFromDirectory } from "@rebasepro/server";
9
+ import { out, outError } from "../cli-output";
9
10
 
10
11
 
11
12
  // --- Helper Functions ---
@@ -49,7 +50,7 @@ const formatTerminalText = (text: string, options: {
49
50
  const runGeneration = async (collectionsFilePath?: string, outputPath?: string) => {
50
51
  try {
51
52
  if (!collectionsFilePath) {
52
- logger.error("Error: No collections file path provided. Skipping schema generation.");
53
+ outError("Error: No collections file path provided. Skipping schema generation.");
53
54
  return;
54
55
  }
55
56
 
@@ -75,20 +76,20 @@ const runGeneration = async (collectionsFilePath?: string, outputPath?: string)
75
76
  const outputDir = path.dirname(outputPath);
76
77
  await fsPromises.mkdir(outputDir, { recursive: true });
77
78
  await fsPromises.writeFile(outputPath, schemaContent);
78
- logger.info(`✅ Drizzle schema generated successfully at ${outputPath}`);
79
+ out(`✅ Drizzle schema generated successfully at ${outputPath}`);
79
80
  } else {
80
- logger.info("✅ Drizzle schema generated successfully.");
81
- logger.info(String(schemaContent));
81
+ out("✅ Drizzle schema generated successfully.");
82
+ out(String(schemaContent));
82
83
  }
83
84
 
84
- logger.info(`You can now run ${formatTerminalText("rebase db generate", {
85
+ out(`You can now run ${formatTerminalText("rebase db generate", {
85
86
  bold: true,
86
87
  backgroundColor: "blue",
87
88
  textColor: "black"
88
89
  })} to generate the SQL migration files.`);
89
90
 
90
91
  } catch (error) {
91
- logger.error("Error generating schema", { error: error });
92
+ outError(`Error generating schema: ${error instanceof Error ? (error.stack ?? error.message) : String(error)}`);
92
93
  }
93
94
  };
94
95
 
@@ -102,7 +103,7 @@ const main = () => {
102
103
  const watch = process.argv.includes("--watch");
103
104
 
104
105
  if (!collectionsFilePath) {
105
- logger.info("Usage: ts-node generate-drizzle-schema.ts <path-to-collections-file> [--output <path-to-output-file>] [--watch]");
106
+ out("Usage: ts-node generate-drizzle-schema.ts <path-to-collections-file> [--output <path-to-output-file>] [--watch]");
106
107
  return;
107
108
  }
108
109
 
@@ -110,14 +111,14 @@ const main = () => {
110
111
  const resolvedOutputPath = outputPath ? path.resolve(process.cwd(), outputPath) : undefined;
111
112
 
112
113
  if (watch) {
113
- logger.info(`Watching for changes in ${resolvedPath}...`);
114
+ out(`Watching for changes in ${resolvedPath}...`);
114
115
  const watcher = chokidar.watch(resolvedPath, {
115
116
  persistent: true,
116
117
  ignoreInitial: false
117
118
  });
118
119
 
119
120
  watcher.on("all", (event, filePath) => {
120
- logger.info(`[${event}] ${filePath}. Regenerating schema...`);
121
+ out(`[${event}] ${filePath}. Regenerating schema...`);
121
122
  runGeneration(resolvedPath, resolvedOutputPath);
122
123
  });
123
124
  } else {