@rebasepro/server-postgresql 0.0.1-canary.eae7889 → 0.0.1-canary.f81da60

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 (32) hide show
  1. package/dist/index.es.js +171 -180
  2. package/dist/index.es.js.map +1 -1
  3. package/dist/index.umd.js +171 -180
  4. package/dist/index.umd.js.map +1 -1
  5. package/dist/server-postgresql/src/PostgresBackendDriver.d.ts +6 -0
  6. package/dist/server-postgresql/src/schema/introspect-db-logic.d.ts +82 -0
  7. package/dist/server-postgresql/src/schema/introspect-db.d.ts +1 -0
  8. package/dist/types/src/controllers/auth.d.ts +2 -2
  9. package/dist/types/src/controllers/collection_registry.d.ts +2 -1
  10. package/dist/types/src/controllers/data_driver.d.ts +36 -1
  11. package/dist/types/src/types/backend_hooks.d.ts +187 -0
  12. package/dist/types/src/types/collections.d.ts +11 -10
  13. package/dist/types/src/types/cron.d.ts +1 -1
  14. package/dist/types/src/types/entity_views.d.ts +4 -6
  15. package/dist/types/src/types/formex.d.ts +40 -0
  16. package/dist/types/src/types/index.d.ts +2 -0
  17. package/dist/types/src/types/plugins.d.ts +6 -3
  18. package/dist/types/src/types/properties.d.ts +61 -89
  19. package/dist/types/src/types/slots.d.ts +20 -10
  20. package/dist/types/src/types/translations.d.ts +4 -0
  21. package/package.json +6 -5
  22. package/src/PostgresBackendDriver.ts +9 -0
  23. package/src/cli.ts +59 -1
  24. package/src/schema/generate-drizzle-schema-logic.ts +7 -25
  25. package/src/schema/introspect-db-logic.ts +592 -0
  26. package/src/schema/introspect-db.ts +211 -0
  27. package/src/services/EntityPersistService.ts +7 -1
  28. package/test/generate-drizzle-schema.test.ts +47 -0
  29. package/test/introspect-db-generation.test.ts +436 -0
  30. package/test/introspect-db-utils.test.ts +392 -0
  31. package/test/relations.test.ts +4 -4
  32. package/test/unmapped-tables-safety.test.ts +345 -0
@@ -36,6 +36,14 @@ export type Property = StringProperty | NumberProperty | BooleanProperty | DateP
36
36
  export type Properties = {
37
37
  [key: string]: Property;
38
38
  };
39
+ export type PostgresProperty = Exclude<Property, ReferenceProperty>;
40
+ export type PostgresProperties = {
41
+ [key: string]: PostgresProperty;
42
+ };
43
+ export type FirebaseProperty = Exclude<Property, RelationProperty>;
44
+ export type FirebaseProperties = {
45
+ [key: string]: FirebaseProperty;
46
+ };
39
47
  /**
40
48
  * A helper type to infer the underlying data type from a Property definition.
41
49
  * This is the core of the type inference system.
@@ -89,7 +97,18 @@ export type InferEntityType<P extends Properties> = {
89
97
  * Interface including all common properties of a CMS property.
90
98
  * @group Entity properties
91
99
  */
100
+ export interface BaseUIConfig<CustomProps = unknown> {
101
+ columnWidth?: number;
102
+ hideFromCollection?: boolean;
103
+ readOnly?: boolean;
104
+ disabled?: boolean | PropertyDisabledConfig;
105
+ widthPercentage?: number;
106
+ customProps?: CustomProps;
107
+ Field?: React.ComponentType<any>;
108
+ Preview?: React.ComponentType<any>;
109
+ }
92
110
  export interface BaseProperty<CustomProps = unknown> {
111
+ ui?: BaseUIConfig<CustomProps>;
93
112
  /**
94
113
  * Property name (e.g. Product)
95
114
  */
@@ -105,28 +124,6 @@ export interface BaseProperty<CustomProps = unknown> {
105
124
  * overwritten by the current property config.
106
125
  */
107
126
  propertyConfig?: string;
108
- /**
109
- * Width in pixels of this column in the collection view. If not set
110
- * the width is inferred based on the other configurations
111
- */
112
- columnWidth?: number;
113
- /**
114
- * Do not show this property in the collection view
115
- */
116
- hideFromCollection?: boolean;
117
- /**
118
- * Is this a read only property. When set to true, it gets rendered as a
119
- * preview.
120
- */
121
- readOnly?: boolean;
122
- /**
123
- * Is this field disabled.
124
- * When set to true, it gets rendered as a
125
- * disabled field. You can also specify a configuration for defining the
126
- * behaviour of disabled properties (including custom messages, clear value on
127
- * disabled or hide the field completely)
128
- */
129
- disabled?: boolean | PropertyDisabledConfig;
130
127
  /**
131
128
  * Rules for validating this property
132
129
  */
@@ -135,16 +132,6 @@ export interface BaseProperty<CustomProps = unknown> {
135
132
  * This value will be set by default for new entities.
136
133
  */
137
134
  defaultValue?: unknown;
138
- /**
139
- * A number between 0 and 100 that indicates the width of the field in the form view.
140
- * It defaults to 100, but you can set it to 50 to have two fields in the same row.
141
- */
142
- widthPercentage?: number;
143
- /**
144
- * Additional props that are passed to the components defined in `field`
145
- * or in `preview`.
146
- */
147
- customProps?: CustomProps;
148
135
  /**
149
136
  * Use this to define dynamic properties that change based on certain conditions
150
137
  * or on the entity's values. For example, you can make a field read-only if
@@ -168,21 +155,19 @@ export interface BaseProperty<CustomProps = unknown> {
168
155
  * Callbacks/Hooks for this property field to transform and sanitize data during its lifecycle.
169
156
  */
170
157
  callbacks?: PropertyCallbacks;
171
- /**
172
- * Custom field component to render this property in forms.
173
- * Used by the CMS layer.
174
- */
175
- Field?: React.ComponentType<any>;
176
- /**
177
- * Custom preview component to render this property in previews/tables.
178
- * Used by the CMS layer.
179
- */
180
- Preview?: React.ComponentType<any>;
181
158
  }
182
159
  /**
183
160
  * @group Entity properties
184
161
  */
162
+ export interface StringUIConfig extends BaseUIConfig {
163
+ multiline?: boolean;
164
+ markdown?: boolean;
165
+ previewAsTag?: boolean;
166
+ clearable?: boolean;
167
+ url?: boolean | PreviewType;
168
+ }
185
169
  export interface StringProperty extends BaseProperty {
170
+ ui?: StringUIConfig;
186
171
  type: "string";
187
172
  /**
188
173
  * Optional database column type. If not set, it defaults to `varchar` or `uuid` depending on `isId` configuration.
@@ -257,10 +242,6 @@ export interface StringProperty extends BaseProperty {
257
242
  * Should this string be rendered as a tag instead of just text.
258
243
  */
259
244
  previewAsTag?: boolean;
260
- /**
261
- * Add an icon to clear the value and set it to `null`. Defaults to `false`
262
- */
263
- clearable?: boolean;
264
245
  /**
265
246
  * You can use this property (a string) to behave as a reference to another
266
247
  * collection. The stored value is the ID of the entity in the
@@ -272,7 +253,11 @@ export interface StringProperty extends BaseProperty {
272
253
  /**
273
254
  * @group Entity properties
274
255
  */
256
+ export interface NumberUIConfig extends BaseUIConfig {
257
+ clearable?: boolean;
258
+ }
275
259
  export interface NumberProperty extends BaseProperty {
260
+ ui?: NumberUIConfig;
276
261
  type: "number";
277
262
  /**
278
263
  * Optional database column type. Allows specifying exact database numeric types.
@@ -300,15 +285,12 @@ export interface NumberProperty extends BaseProperty {
300
285
  * displayed in the dropdown.
301
286
  */
302
287
  enum?: EnumValues;
303
- /**
304
- * Add an icon to clear the value and set it to `null`. Defaults to `false`
305
- */
306
- clearable?: boolean;
307
288
  }
308
289
  /**
309
290
  * @group Entity properties
310
291
  */
311
292
  export interface BooleanProperty extends BaseProperty {
293
+ ui?: BaseUIConfig;
312
294
  type: "boolean";
313
295
  /**
314
296
  * Rules for validating this property
@@ -318,7 +300,11 @@ export interface BooleanProperty extends BaseProperty {
318
300
  /**
319
301
  * @group Entity properties
320
302
  */
303
+ export interface DateUIConfig extends BaseUIConfig {
304
+ clearable?: boolean;
305
+ }
321
306
  export interface DateProperty extends BaseProperty {
307
+ ui?: DateUIConfig;
322
308
  type: "date";
323
309
  /**
324
310
  * Optional database column type. If not set, defaults to `timestamp` with timezone.
@@ -354,6 +340,7 @@ export interface DateProperty extends BaseProperty {
354
340
  * @group Entity properties
355
341
  */
356
342
  export interface GeopointProperty extends BaseProperty {
343
+ ui?: BaseUIConfig;
357
344
  type: "geopoint";
358
345
  /**
359
346
  * Rules for validating this property
@@ -363,7 +350,11 @@ export interface GeopointProperty extends BaseProperty {
363
350
  /**
364
351
  * @group Entity properties
365
352
  */
353
+ export interface ReferenceUIConfig extends BaseUIConfig {
354
+ previewProperties?: string[];
355
+ }
366
356
  export interface ReferenceProperty extends BaseProperty {
357
+ ui?: ReferenceUIConfig;
367
358
  type: "reference";
368
359
  /**
369
360
  * Marks this field as a Primary Key / Unique Identifier.
@@ -384,15 +375,9 @@ export interface ReferenceProperty extends BaseProperty {
384
375
  path?: string;
385
376
  /**
386
377
  * Allow selection of entities that pass the given filter only.
387
- * e.g. `forceFilter: { age: [">=", 18] }`
388
- */
389
- forceFilter?: FilterValues<string>;
390
- /**
391
- * Properties that need to be rendered when displaying a preview of this
392
- * reference. If not specified the first 3 are used. Only the first 3
393
- * specified values are considered.
378
+ * e.g. `fixedFilter: { age: [">=", 18] }`
394
379
  */
395
- previewProperties?: string[];
380
+ fixedFilter?: FilterValues<string>;
396
381
  /**
397
382
  * Should the reference include the ID of the entity. Defaults to `true`
398
383
  */
@@ -405,7 +390,12 @@ export interface ReferenceProperty extends BaseProperty {
405
390
  /**
406
391
  * @group Entity properties
407
392
  */
393
+ export interface RelationUIConfig extends BaseUIConfig {
394
+ previewProperties?: string[];
395
+ widget?: "select" | "dialog";
396
+ }
408
397
  export interface RelationProperty extends BaseProperty {
398
+ ui?: RelationUIConfig;
409
399
  type: "relation";
410
400
  /**
411
401
  * Marks this field as a Primary Key / Unique Identifier.
@@ -492,15 +482,9 @@ export interface RelationProperty extends BaseProperty {
492
482
  relation?: Relation;
493
483
  /**
494
484
  * Allow selection of entities that pass the given filter only.
495
- * e.g. `forceFilter: { age: [">=", 18] }`
485
+ * e.g. `fixedFilter: { age: [">=", 18] }`
496
486
  */
497
- forceFilter?: FilterValues<string>;
498
- /**
499
- * Properties that need to be rendered when displaying a preview of this
500
- * reference. If not specified the first 3 are used. Only the first 3
501
- * specified values are considered.
502
- */
503
- previewProperties?: string[];
487
+ fixedFilter?: FilterValues<string>;
504
488
  /**
505
489
  * Should the reference include the ID of the entity. Defaults to `true`
506
490
  */
@@ -518,7 +502,12 @@ export interface RelationProperty extends BaseProperty {
518
502
  /**
519
503
  * @group Entity properties
520
504
  */
505
+ export interface ArrayUIConfig extends BaseUIConfig {
506
+ expanded?: boolean;
507
+ minimalistView?: boolean;
508
+ }
521
509
  export interface ArrayProperty extends BaseProperty {
510
+ ui?: ArrayUIConfig;
522
511
  type: "array";
523
512
  /**
524
513
  * Optional database column type. Defaults to `jsonb`.
@@ -573,15 +562,6 @@ export interface ArrayProperty extends BaseProperty {
573
562
  * Rules for validating this property
574
563
  */
575
564
  validation?: ArrayPropertyValidationSchema;
576
- /**
577
- * Should the field be initially expanded. Defaults to `true`
578
- */
579
- expanded?: boolean;
580
- /**
581
- * Display the child properties directly, without being wrapped in an
582
- * extendable panel.
583
- */
584
- minimalistView?: boolean;
585
565
  /**
586
566
  * Can the elements in this array be reordered. Defaults to `true`.
587
567
  * This prop has no effect if `disabled` is set to true.
@@ -596,7 +576,13 @@ export interface ArrayProperty extends BaseProperty {
596
576
  /**
597
577
  * @group Entity properties
598
578
  */
579
+ export interface MapUIConfig extends BaseUIConfig {
580
+ expanded?: boolean;
581
+ minimalistView?: boolean;
582
+ spreadChildren?: boolean;
583
+ }
599
584
  export interface MapProperty extends BaseProperty {
585
+ ui?: MapUIConfig;
600
586
  type: "map";
601
587
  /**
602
588
  * Optional database column type. Defaults to `jsonb`.
@@ -630,20 +616,6 @@ export interface MapProperty extends BaseProperty {
630
616
  * needed
631
617
  */
632
618
  pickOnlySomeKeys?: boolean;
633
- /**
634
- * Display the child properties as independent columns in the collection
635
- * view
636
- */
637
- spreadChildren?: boolean;
638
- /**
639
- * Display the child properties directly, without being wrapped in an
640
- * extendable panel. Note that this will also hide the title of this property.
641
- */
642
- minimalistView?: boolean;
643
- /**
644
- * Should the field be initially expanded. Defaults to `true`
645
- */
646
- expanded?: boolean;
647
619
  /**
648
620
  * Render this map as a key-value table that allows to use
649
621
  * arbitrary keys. You don't need to define the properties in this case.
@@ -100,7 +100,8 @@ export interface NavigationSlotProps {
100
100
  export interface CollectionToolbarProps {
101
101
  path: string;
102
102
  collection: EntityCollection;
103
- parentCollectionIds: string[];
103
+ parentCollectionSlugs: string[];
104
+ parentEntityIds: string[];
104
105
  tableController: EntityTableController;
105
106
  selectionController: SelectionController;
106
107
  }
@@ -111,7 +112,8 @@ export interface CollectionToolbarProps {
111
112
  export interface CollectionEmptyStateProps {
112
113
  path: string;
113
114
  collection: EntityCollection;
114
- parentCollectionIds: string[];
115
+ parentCollectionSlugs: string[];
116
+ parentEntityIds: string[];
115
117
  canCreate: boolean;
116
118
  onNewClick?: () => void;
117
119
  }
@@ -123,7 +125,8 @@ export interface CollectionHeaderActionProps {
123
125
  property: Property;
124
126
  propertyKey: string;
125
127
  path: string;
126
- parentCollectionIds: string[];
128
+ parentCollectionSlugs: string[];
129
+ parentEntityIds: string[];
127
130
  onHover: boolean;
128
131
  collection: EntityCollection;
129
132
  tableController: EntityTableController;
@@ -134,7 +137,8 @@ export interface CollectionHeaderActionProps {
134
137
  */
135
138
  export interface CollectionAddColumnProps {
136
139
  path: string;
137
- parentCollectionIds: string[];
140
+ parentCollectionSlugs: string[];
141
+ parentEntityIds: string[];
138
142
  collection: EntityCollection;
139
143
  tableController: EntityTableController;
140
144
  }
@@ -145,7 +149,8 @@ export interface CollectionAddColumnProps {
145
149
  export interface CollectionErrorProps {
146
150
  path: string;
147
151
  collection: EntityCollection;
148
- parentCollectionIds?: string[];
152
+ parentCollectionSlugs?: string[];
153
+ parentEntityIds?: string[];
149
154
  error: Error;
150
155
  }
151
156
  /**
@@ -155,7 +160,8 @@ export interface CollectionErrorProps {
155
160
  export interface KanbanSetupProps {
156
161
  collection: EntityCollection;
157
162
  fullPath: string;
158
- parentCollectionIds: string[];
163
+ parentCollectionSlugs: string[];
164
+ parentEntityIds: string[];
159
165
  }
160
166
  /**
161
167
  * Props for the `kanban.add-column` slot.
@@ -164,7 +170,8 @@ export interface KanbanSetupProps {
164
170
  export interface KanbanAddColumnProps {
165
171
  collection: EntityCollection;
166
172
  fullPath: string;
167
- parentCollectionIds: string[];
173
+ parentCollectionSlugs: string[];
174
+ parentEntityIds: string[];
168
175
  columnProperty: string;
169
176
  }
170
177
  /**
@@ -177,7 +184,8 @@ export interface EntityRowActionsProps {
177
184
  entityId: string;
178
185
  path: string;
179
186
  collection: EntityCollection;
180
- parentCollectionIds: string[];
187
+ parentCollectionSlugs: string[];
188
+ parentEntityIds: string[];
181
189
  selectionController: SelectionController;
182
190
  context: RebaseContext;
183
191
  }
@@ -202,7 +210,8 @@ export interface EntityFieldSlotProps {
202
210
  export interface CollectionFilterPanelProps {
203
211
  path: string;
204
212
  collection: EntityCollection;
205
- parentCollectionIds: string[];
213
+ parentCollectionSlugs: string[];
214
+ parentEntityIds: string[];
206
215
  tableController: EntityTableController;
207
216
  context: RebaseContext;
208
217
  }
@@ -238,7 +247,8 @@ export interface ShellToolbarProps {
238
247
  export interface CollectionInsightsSlotProps {
239
248
  path: string;
240
249
  collection: EntityCollection;
241
- parentCollectionIds: string[];
250
+ parentCollectionSlugs: string[];
251
+ parentEntityIds: string[];
242
252
  }
243
253
  /**
244
254
  * Props for `home.card.insight` slot.
@@ -461,6 +461,10 @@ export interface RebaseTranslations {
461
461
  reset_password_success?: string;
462
462
  reset_password_confirmation?: string;
463
463
  error_resetting_password?: string;
464
+ /** Permission-denied empty states */
465
+ no_permission_to_view_users?: string;
466
+ no_permission_to_view_roles?: string;
467
+ no_permission_description?: string;
464
468
  /** Editor table-bubble */
465
469
  add_row_before: string;
466
470
  add_row_after: string;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@rebasepro/server-postgresql",
3
3
  "type": "module",
4
- "version": "0.0.1-canary.eae7889",
4
+ "version": "0.0.1-canary.f81da60",
5
5
  "description": "PostgreSQL data source backend implementation for Rebase with Drizzle ORM",
6
6
  "funding": {
7
7
  "url": "https://github.com/sponsors/rebaseco"
@@ -60,13 +60,14 @@
60
60
  "dependencies": {
61
61
  "arg": "^5.0.2",
62
62
  "chalk": "^4.1.2",
63
+ "chokidar": "5.0.0",
63
64
  "drizzle-orm": "^0.44.4",
64
65
  "execa": "^4.1.0",
65
66
  "pg": "^8.11.3",
66
- "@rebasepro/common": "0.0.1-canary.eae7889",
67
- "@rebasepro/server-core": "0.0.1-canary.eae7889",
68
- "@rebasepro/utils": "0.0.1-canary.eae7889",
69
- "@rebasepro/types": "0.0.1-canary.eae7889"
67
+ "@rebasepro/common": "0.0.1-canary.f81da60",
68
+ "@rebasepro/server-core": "0.0.1-canary.f81da60",
69
+ "@rebasepro/types": "0.0.1-canary.f81da60",
70
+ "@rebasepro/utils": "0.0.1-canary.f81da60"
70
71
  },
71
72
  "devDependencies": {
72
73
  "@types/jest": "^29.5.14",
@@ -101,6 +101,15 @@ export class PostgresBackendDriver implements DataDriver {
101
101
  };
102
102
  }
103
103
 
104
+ /**
105
+ * REST-optimised fetch service (include-aware eager-loading).
106
+ * Delegates to the underlying EntityFetchService which already
107
+ * implements the matching method signatures.
108
+ */
109
+ get restFetchService() {
110
+ return this.entityService.getFetchService();
111
+ }
112
+
104
113
 
105
114
  private resolveCollectionCallbacks<M extends Record<string, unknown>>(collection: EntityCollection<M> | undefined, path: string) {
106
115
  if (!collection && !path) return { collection: undefined,
package/src/cli.ts CHANGED
@@ -408,9 +408,16 @@ async function runDrizzleKit(action: string, _rawArgs: string[]): Promise<void>
408
408
 
409
409
  const interactive = ["generate", "push"].includes(action);
410
410
 
411
+ // For push: always use --strict (prompts before destructive ops) and --verbose
412
+ // (shows all SQL). This ensures unmapped tables are never silently dropped.
413
+ const drizzleKitArgs = [action];
414
+ if (action === "push") {
415
+ drizzleKitArgs.push("--strict", "--verbose");
416
+ }
417
+
411
418
  try {
412
419
  if (interactive) {
413
- await execa(drizzleKitBin, [action], {
420
+ await execa(drizzleKitBin, drizzleKitArgs, {
414
421
  cwd: process.cwd(),
415
422
  stdio: "inherit",
416
423
  env
@@ -527,6 +534,57 @@ async function schemaCommand(subcommand: string, rawArgs: string[]): Promise<voi
527
534
  console.error(chalk.red(`✗ Failed to run schema generator: ${err instanceof Error ? err.message : String(err)}`));
528
535
  process.exit(1);
529
536
  }
537
+ } else if (subcommand === "introspect") {
538
+ const argsList = arg(
539
+ {
540
+ "--output": String,
541
+ "--force": Boolean,
542
+ "--schema": String,
543
+ "-o": "--output",
544
+ "-f": "--force"
545
+ },
546
+ {
547
+ argv: rawArgs.slice(2),
548
+ permissive: true
549
+ }
550
+ );
551
+
552
+ const introspectScript = path.join(__dirname, "schema", "introspect-db.ts");
553
+ if (!fs.existsSync(introspectScript)) {
554
+ console.error(chalk.red(`✗ Could not find introspect-db.ts at ${introspectScript}`));
555
+ process.exit(1);
556
+ }
557
+
558
+ const tsxBin = resolveLocalBin("tsx");
559
+ if (!tsxBin) {
560
+ console.error(chalk.red("✗ Could not find tsx binary."));
561
+ process.exit(1);
562
+ }
563
+
564
+ const outputPath = argsList["--output"] || path.join("..", "config", "collections");
565
+
566
+ console.log("");
567
+ console.log(chalk.bold(" 🔍 Rebase Schema Introspector"));
568
+ console.log("");
569
+
570
+ const cmdParts = [
571
+ tsxBin,
572
+ introspectScript,
573
+ `--output=${outputPath}`,
574
+ ...(argsList["--force"] ? ["--force"] : []),
575
+ ...(argsList["--schema"] ? [`--schema=${argsList["--schema"]}`] : [])
576
+ ];
577
+
578
+ try {
579
+ await execa(cmdParts[0], cmdParts.slice(1), {
580
+ cwd: process.cwd(),
581
+ stdio: "inherit",
582
+ env: { ...process.env as Record<string, string> }
583
+ });
584
+ } catch (err: unknown) {
585
+ console.error(chalk.red(`✗ Failed to run schema introspector: ${err instanceof Error ? err.message : String(err)}`));
586
+ process.exit(1);
587
+ }
530
588
  } else {
531
589
  console.error(chalk.red("Unknown schema command."));
532
590
  process.exit(1);
@@ -682,31 +682,13 @@ export const generateSchema = async (collections: EntityCollection[], stripPolic
682
682
  if (rel.cardinality === "one") {
683
683
  if (rel.direction === "owning" && rel.localKey) {
684
684
  tableRelations.push(` "${relationKey}": one(${targetTableVar}, {\n fields: [${tableVarName}.${rel.localKey}],\n references: [${targetTableVar}.${getPrimaryKeyName(target)}],\n relationName: \"${drizzleRelationName}\"\n })`);
685
- } else if (rel.direction === "inverse" && rel.foreignKeyOnTarget) {
686
- const sourceIdField = getPrimaryKeyName(collection);
687
- tableRelations.push(` "${relationKey}": one(${targetTableVar}, {\n fields: [${tableVarName}.${sourceIdField}],\n references: [${targetTableVar}.${rel.foreignKeyOnTarget}],\n relationName: \"${drizzleRelationName}\"\n })`);
688
- } else if (rel.direction === "inverse" && !rel.foreignKeyOnTarget) {
689
- // Handle inverse one-to-one relations where the FK is on the target table
690
- // but foreignKeyOnTarget is not explicitly specified
691
- // In this case, we need to find the corresponding owning relation on the target
692
- try {
693
- const targetCollection = rel.target();
694
- const targetResolvedRelations = resolveCollectionRelations(targetCollection);
695
-
696
- // Find the owning relation on the target that points back to this collection
697
- const correspondingRelation = Object.values(targetResolvedRelations).find(targetRel =>
698
- targetRel.direction === "owning" &&
699
- targetRel.cardinality === "one" &&
700
- targetRel.target().slug === collection.slug
701
- );
702
-
703
- if (correspondingRelation && correspondingRelation.localKey) {
704
- const sourceIdField = getPrimaryKeyName(collection);
705
- tableRelations.push(` "${relationKey}": one(${targetTableVar}, {\n fields: [${tableVarName}.${sourceIdField}],\n references: [${targetTableVar}.${correspondingRelation.localKey}],\n relationName: \"${drizzleRelationName}\"\n })`);
706
- }
707
- } catch (e) {
708
- console.warn(`Could not resolve inverse one-to-one relation '${relationKey}':`, e);
709
- }
685
+ } else if (rel.direction === "inverse") {
686
+ // Inverse one-to-one: the FK lives on the TARGET table, not here.
687
+ // Drizzle pairs inverse relations via `relationName` alone — specifying
688
+ // `fields`/`references` on the inverse side is invalid and causes
689
+ // `normalizeRelation` to crash with "Cannot read properties of
690
+ // undefined (reading 'referencedTable')".
691
+ tableRelations.push(` "${relationKey}": one(${targetTableVar}, {\n relationName: \"${drizzleRelationName}\"\n })`);
710
692
  }
711
693
  } else if (rel.cardinality === "many") {
712
694
  if (rel.direction === "inverse" && rel.foreignKeyOnTarget) {