drizzle-orm 1.0.0-beta.1-9fb1d0d → 1.0.0-beta.1-34ee105

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/relations.d.cts CHANGED
@@ -20,7 +20,7 @@ export declare function buildRelationsParts<TTables extends Schema, TConfig exte
20
20
  export type RelationsRecord = Record<string, AnyRelation>;
21
21
  export type EmptyRelations = {};
22
22
  export type AnyRelations = TablesRelationalConfig;
23
- export declare abstract class Relation<TSourceTableName extends string = string, TTargetTableName extends string = string> {
23
+ export declare abstract class Relation<TTargetTableName extends string = string> {
24
24
  readonly targetTableName: TTargetTableName;
25
25
  static readonly [entityKind]: string;
26
26
  readonly $brand: 'RelationV2';
@@ -38,27 +38,25 @@ export declare abstract class Relation<TSourceTableName extends string = string,
38
38
  };
39
39
  throughTable?: SchemaEntry;
40
40
  isReversed?: boolean;
41
- /** Type-level only field */
42
- readonly sourceTableName: TSourceTableName;
43
41
  constructor(targetTable: SchemaEntry, targetTableName: TTargetTableName);
44
42
  }
45
- export type AnyRelation = Relation<string, string>;
46
- export declare class One<TSourceTableName extends string, TTargetTableName extends string, TOptional extends boolean = boolean> extends Relation<TSourceTableName, TTargetTableName> {
43
+ export type AnyRelation = Relation<string>;
44
+ export declare class One<TTargetTableName extends string, TOptional extends boolean = boolean> extends Relation<TTargetTableName> {
47
45
  static readonly [entityKind]: string;
48
46
  protected $relationBrand: 'OneV2';
49
47
  readonly relationType: "one";
50
48
  readonly optional: TOptional;
51
49
  constructor(tables: Schema, targetTable: SchemaEntry, targetTableName: TTargetTableName, config: AnyOneConfig | undefined);
52
50
  }
53
- export type AnyOne = One<string, string, boolean>;
54
- export declare class Many<TSourceTableName extends string, TTargetTableName extends string> extends Relation<TSourceTableName, TTargetTableName> {
51
+ export type AnyOne = One<string, boolean>;
52
+ export declare class Many<TTargetTableName extends string> extends Relation<TTargetTableName> {
55
53
  readonly config: AnyManyConfig | undefined;
56
54
  static readonly [entityKind]: string;
57
55
  protected $relationBrand: 'ManyV2';
58
56
  readonly relationType: "many";
59
57
  constructor(tables: Schema, targetTable: SchemaEntry, targetTableName: TTargetTableName, config: AnyManyConfig | undefined);
60
58
  }
61
- export type AnyMany = Many<string, string>;
59
+ export type AnyMany = Many<string>;
62
60
  export declare abstract class AggregatedField<T = unknown> implements SQLWrapper<T> {
63
61
  static readonly [entityKind]: string;
64
62
  readonly $brand: 'AggregatedField';
@@ -297,46 +295,36 @@ export type TableFilterColumns<TColumns extends Record<string, unknown>> = {
297
295
  export type TableFilter<TTable extends SchemaEntry = SchemaEntry, TColumns extends Record<string, unknown> = GetTableViewColumns<TTable>> = TableFilterColumns<TColumns> & TableFilterCommons<TTable, TColumns>;
298
296
  export type AnyRelationsFilter = RelationsFilter<TableRelationalConfig, TablesRelationalConfig, FieldSelection>;
299
297
  export type AnyTableFilter = TableFilter<SchemaEntry, FieldSelection>;
300
- export interface OneConfig<TSchema extends Schema, TSourceColumns extends Readonly<[RelationsBuilderColumnBase, ...RelationsBuilderColumnBase[]]> | Readonly<RelationsBuilderColumnBase>, TTargetTableName extends string, TOptional extends boolean> {
301
- from?: TSourceColumns;
302
- to?: TSourceColumns extends RelationsBuilderJunctionColumn | [RelationsBuilderJunctionColumn, ...RelationsBuilderJunctionColumn[]] ? RelationsBuilderJunctionColumn<TTargetTableName> | [
303
- RelationsBuilderJunctionColumn<TTargetTableName>,
304
- ...RelationsBuilderJunctionColumn<TTargetTableName>[]
305
- ] : TSourceColumns extends [RelationsBuilderColumn] ? RelationsBuilderColumn<TTargetTableName> | [RelationsBuilderColumn<TTargetTableName>] : TSourceColumns extends [RelationsBuilderColumn, ...RelationsBuilderColumn[]] ? {
306
- [K in keyof TSourceColumns]: RelationsBuilderColumn<TTargetTableName>;
307
- } : RelationsBuilderColumn<TTargetTableName>;
308
- where?: TableFilter<TSchema[TTargetTableName]>;
298
+ export interface OneConfig<TTargetTable extends SchemaEntry, TOptional extends boolean> {
299
+ from?: RelationsBuilderColumnBase | [RelationsBuilderColumnBase, ...RelationsBuilderColumnBase[]];
300
+ to?: RelationsBuilderColumnBase | [RelationsBuilderColumnBase, ...RelationsBuilderColumnBase[]];
301
+ where?: TableFilter<TTargetTable>;
309
302
  optional?: TOptional;
310
303
  alias?: string;
311
304
  }
312
- export type AnyOneConfig = OneConfig<Schema, Readonly<[RelationsBuilderColumnBase, ...RelationsBuilderColumnBase[]] | RelationsBuilderColumnBase>, string, boolean>;
313
- export interface ManyConfig<TSchema extends Schema, TSourceColumns extends Readonly<[RelationsBuilderColumnBase, ...RelationsBuilderColumnBase[]]> | Readonly<RelationsBuilderColumnBase>, TTargetTableName extends string> {
314
- from?: TSourceColumns;
315
- to?: TSourceColumns extends RelationsBuilderJunctionColumn | [RelationsBuilderJunctionColumn, ...RelationsBuilderJunctionColumn[]] ? RelationsBuilderJunctionColumn<TTargetTableName> | [
316
- RelationsBuilderJunctionColumn<TTargetTableName>,
317
- ...RelationsBuilderJunctionColumn<TTargetTableName>[]
318
- ] : TSourceColumns extends [RelationsBuilderColumn] ? RelationsBuilderColumn<TTargetTableName> | [RelationsBuilderColumn<TTargetTableName>] : TSourceColumns extends [RelationsBuilderColumn, ...RelationsBuilderColumn[]] ? {
319
- [K in keyof TSourceColumns]: RelationsBuilderColumn<TTargetTableName>;
320
- } : RelationsBuilderColumn<TTargetTableName>;
321
- where?: TableFilter<TSchema[TTargetTableName]>;
305
+ export type AnyOneConfig = OneConfig<SchemaEntry, boolean>;
306
+ export interface ManyConfig<TTargetTable extends SchemaEntry> {
307
+ from?: RelationsBuilderColumnBase | [RelationsBuilderColumnBase, ...RelationsBuilderColumnBase[]];
308
+ to?: RelationsBuilderColumnBase | [RelationsBuilderColumnBase, ...RelationsBuilderColumnBase[]];
309
+ where?: TableFilter<TTargetTable>;
322
310
  alias?: string;
323
311
  }
324
- export type AnyManyConfig = ManyConfig<Schema, Readonly<[RelationsBuilderColumnBase, ...RelationsBuilderColumnBase[]] | RelationsBuilderColumnBase>, string>;
325
- export interface OneFn<TTables extends Schema, TTargetTableName extends string> {
326
- <TSourceColumns extends Readonly<[RelationsBuilderColumnBase, ...RelationsBuilderColumnBase[]]> | RelationsBuilderColumnBase = any, TOptional extends boolean = true>(config?: OneConfig<TTables, TSourceColumns, TTargetTableName, TOptional>): One<TSourceColumns extends [RelationsBuilderColumnBase, ...RelationsBuilderColumnBase[]] ? TSourceColumns[number]['_']['tableName'] : Assume<TSourceColumns, RelationsBuilderColumnBase>['_']['tableName'], TTargetTableName, TOptional>;
312
+ export type AnyManyConfig = ManyConfig<SchemaEntry>;
313
+ export interface OneFn<TTargetTable extends SchemaEntry, TTargetTableName extends string> {
314
+ <TOptional extends boolean = true>(config?: OneConfig<TTargetTable, TOptional>): One<TTargetTableName, TOptional>;
327
315
  }
328
- export interface ManyFn<TTables extends Schema, TTargetTableName extends string> {
329
- <TSourceColumns extends Readonly<[RelationsBuilderColumnBase, ...RelationsBuilderColumnBase[]]> | RelationsBuilderColumnBase = any>(config?: ManyConfig<TTables, TSourceColumns, TTargetTableName>): Many<TSourceColumns extends [RelationsBuilderColumnBase, ...RelationsBuilderColumnBase[]] ? TSourceColumns[number]['_']['tableName'] : Assume<TSourceColumns, RelationsBuilderColumnBase>['_']['tableName'], TTargetTableName>;
316
+ export interface ManyFn<TTargetTable extends SchemaEntry, TTargetTableName extends string> {
317
+ (config?: ManyConfig<TTargetTable>): Many<TTargetTableName>;
330
318
  }
331
319
  export declare class RelationsHelperStatic<TTables extends Schema> {
332
320
  static readonly [entityKind]: string;
333
321
  private readonly _;
334
322
  constructor(tables: TTables);
335
323
  one: {
336
- [K in keyof TTables]: TTables[K] extends FilteredSchemaEntry ? OneFn<TTables, K & string> : DrizzleTypeError<'Views with nested selections are not supported by the relational query builder'>;
324
+ [K in keyof TTables]: TTables[K] extends FilteredSchemaEntry ? OneFn<TTables[K], K & string> : DrizzleTypeError<'Views with nested selections are not supported by the relational query builder'>;
337
325
  };
338
326
  many: {
339
- [K in keyof TTables]: TTables[K] extends FilteredSchemaEntry ? ManyFn<TTables, K & string> : DrizzleTypeError<'Views with nested selections are not supported by the relational query builder'>;
327
+ [K in keyof TTables]: TTables[K] extends FilteredSchemaEntry ? ManyFn<TTables[K], K & string> : DrizzleTypeError<'Views with nested selections are not supported by the relational query builder'>;
340
328
  };
341
329
  }
342
330
  export type RelationsBuilderColumns<TTable extends SchemaEntry, TTableName extends string> = {
package/relations.d.ts CHANGED
@@ -20,7 +20,7 @@ export declare function buildRelationsParts<TTables extends Schema, TConfig exte
20
20
  export type RelationsRecord = Record<string, AnyRelation>;
21
21
  export type EmptyRelations = {};
22
22
  export type AnyRelations = TablesRelationalConfig;
23
- export declare abstract class Relation<TSourceTableName extends string = string, TTargetTableName extends string = string> {
23
+ export declare abstract class Relation<TTargetTableName extends string = string> {
24
24
  readonly targetTableName: TTargetTableName;
25
25
  static readonly [entityKind]: string;
26
26
  readonly $brand: 'RelationV2';
@@ -38,27 +38,25 @@ export declare abstract class Relation<TSourceTableName extends string = string,
38
38
  };
39
39
  throughTable?: SchemaEntry;
40
40
  isReversed?: boolean;
41
- /** Type-level only field */
42
- readonly sourceTableName: TSourceTableName;
43
41
  constructor(targetTable: SchemaEntry, targetTableName: TTargetTableName);
44
42
  }
45
- export type AnyRelation = Relation<string, string>;
46
- export declare class One<TSourceTableName extends string, TTargetTableName extends string, TOptional extends boolean = boolean> extends Relation<TSourceTableName, TTargetTableName> {
43
+ export type AnyRelation = Relation<string>;
44
+ export declare class One<TTargetTableName extends string, TOptional extends boolean = boolean> extends Relation<TTargetTableName> {
47
45
  static readonly [entityKind]: string;
48
46
  protected $relationBrand: 'OneV2';
49
47
  readonly relationType: "one";
50
48
  readonly optional: TOptional;
51
49
  constructor(tables: Schema, targetTable: SchemaEntry, targetTableName: TTargetTableName, config: AnyOneConfig | undefined);
52
50
  }
53
- export type AnyOne = One<string, string, boolean>;
54
- export declare class Many<TSourceTableName extends string, TTargetTableName extends string> extends Relation<TSourceTableName, TTargetTableName> {
51
+ export type AnyOne = One<string, boolean>;
52
+ export declare class Many<TTargetTableName extends string> extends Relation<TTargetTableName> {
55
53
  readonly config: AnyManyConfig | undefined;
56
54
  static readonly [entityKind]: string;
57
55
  protected $relationBrand: 'ManyV2';
58
56
  readonly relationType: "many";
59
57
  constructor(tables: Schema, targetTable: SchemaEntry, targetTableName: TTargetTableName, config: AnyManyConfig | undefined);
60
58
  }
61
- export type AnyMany = Many<string, string>;
59
+ export type AnyMany = Many<string>;
62
60
  export declare abstract class AggregatedField<T = unknown> implements SQLWrapper<T> {
63
61
  static readonly [entityKind]: string;
64
62
  readonly $brand: 'AggregatedField';
@@ -297,46 +295,36 @@ export type TableFilterColumns<TColumns extends Record<string, unknown>> = {
297
295
  export type TableFilter<TTable extends SchemaEntry = SchemaEntry, TColumns extends Record<string, unknown> = GetTableViewColumns<TTable>> = TableFilterColumns<TColumns> & TableFilterCommons<TTable, TColumns>;
298
296
  export type AnyRelationsFilter = RelationsFilter<TableRelationalConfig, TablesRelationalConfig, FieldSelection>;
299
297
  export type AnyTableFilter = TableFilter<SchemaEntry, FieldSelection>;
300
- export interface OneConfig<TSchema extends Schema, TSourceColumns extends Readonly<[RelationsBuilderColumnBase, ...RelationsBuilderColumnBase[]]> | Readonly<RelationsBuilderColumnBase>, TTargetTableName extends string, TOptional extends boolean> {
301
- from?: TSourceColumns;
302
- to?: TSourceColumns extends RelationsBuilderJunctionColumn | [RelationsBuilderJunctionColumn, ...RelationsBuilderJunctionColumn[]] ? RelationsBuilderJunctionColumn<TTargetTableName> | [
303
- RelationsBuilderJunctionColumn<TTargetTableName>,
304
- ...RelationsBuilderJunctionColumn<TTargetTableName>[]
305
- ] : TSourceColumns extends [RelationsBuilderColumn] ? RelationsBuilderColumn<TTargetTableName> | [RelationsBuilderColumn<TTargetTableName>] : TSourceColumns extends [RelationsBuilderColumn, ...RelationsBuilderColumn[]] ? {
306
- [K in keyof TSourceColumns]: RelationsBuilderColumn<TTargetTableName>;
307
- } : RelationsBuilderColumn<TTargetTableName>;
308
- where?: TableFilter<TSchema[TTargetTableName]>;
298
+ export interface OneConfig<TTargetTable extends SchemaEntry, TOptional extends boolean> {
299
+ from?: RelationsBuilderColumnBase | [RelationsBuilderColumnBase, ...RelationsBuilderColumnBase[]];
300
+ to?: RelationsBuilderColumnBase | [RelationsBuilderColumnBase, ...RelationsBuilderColumnBase[]];
301
+ where?: TableFilter<TTargetTable>;
309
302
  optional?: TOptional;
310
303
  alias?: string;
311
304
  }
312
- export type AnyOneConfig = OneConfig<Schema, Readonly<[RelationsBuilderColumnBase, ...RelationsBuilderColumnBase[]] | RelationsBuilderColumnBase>, string, boolean>;
313
- export interface ManyConfig<TSchema extends Schema, TSourceColumns extends Readonly<[RelationsBuilderColumnBase, ...RelationsBuilderColumnBase[]]> | Readonly<RelationsBuilderColumnBase>, TTargetTableName extends string> {
314
- from?: TSourceColumns;
315
- to?: TSourceColumns extends RelationsBuilderJunctionColumn | [RelationsBuilderJunctionColumn, ...RelationsBuilderJunctionColumn[]] ? RelationsBuilderJunctionColumn<TTargetTableName> | [
316
- RelationsBuilderJunctionColumn<TTargetTableName>,
317
- ...RelationsBuilderJunctionColumn<TTargetTableName>[]
318
- ] : TSourceColumns extends [RelationsBuilderColumn] ? RelationsBuilderColumn<TTargetTableName> | [RelationsBuilderColumn<TTargetTableName>] : TSourceColumns extends [RelationsBuilderColumn, ...RelationsBuilderColumn[]] ? {
319
- [K in keyof TSourceColumns]: RelationsBuilderColumn<TTargetTableName>;
320
- } : RelationsBuilderColumn<TTargetTableName>;
321
- where?: TableFilter<TSchema[TTargetTableName]>;
305
+ export type AnyOneConfig = OneConfig<SchemaEntry, boolean>;
306
+ export interface ManyConfig<TTargetTable extends SchemaEntry> {
307
+ from?: RelationsBuilderColumnBase | [RelationsBuilderColumnBase, ...RelationsBuilderColumnBase[]];
308
+ to?: RelationsBuilderColumnBase | [RelationsBuilderColumnBase, ...RelationsBuilderColumnBase[]];
309
+ where?: TableFilter<TTargetTable>;
322
310
  alias?: string;
323
311
  }
324
- export type AnyManyConfig = ManyConfig<Schema, Readonly<[RelationsBuilderColumnBase, ...RelationsBuilderColumnBase[]] | RelationsBuilderColumnBase>, string>;
325
- export interface OneFn<TTables extends Schema, TTargetTableName extends string> {
326
- <TSourceColumns extends Readonly<[RelationsBuilderColumnBase, ...RelationsBuilderColumnBase[]]> | RelationsBuilderColumnBase = any, TOptional extends boolean = true>(config?: OneConfig<TTables, TSourceColumns, TTargetTableName, TOptional>): One<TSourceColumns extends [RelationsBuilderColumnBase, ...RelationsBuilderColumnBase[]] ? TSourceColumns[number]['_']['tableName'] : Assume<TSourceColumns, RelationsBuilderColumnBase>['_']['tableName'], TTargetTableName, TOptional>;
312
+ export type AnyManyConfig = ManyConfig<SchemaEntry>;
313
+ export interface OneFn<TTargetTable extends SchemaEntry, TTargetTableName extends string> {
314
+ <TOptional extends boolean = true>(config?: OneConfig<TTargetTable, TOptional>): One<TTargetTableName, TOptional>;
327
315
  }
328
- export interface ManyFn<TTables extends Schema, TTargetTableName extends string> {
329
- <TSourceColumns extends Readonly<[RelationsBuilderColumnBase, ...RelationsBuilderColumnBase[]]> | RelationsBuilderColumnBase = any>(config?: ManyConfig<TTables, TSourceColumns, TTargetTableName>): Many<TSourceColumns extends [RelationsBuilderColumnBase, ...RelationsBuilderColumnBase[]] ? TSourceColumns[number]['_']['tableName'] : Assume<TSourceColumns, RelationsBuilderColumnBase>['_']['tableName'], TTargetTableName>;
316
+ export interface ManyFn<TTargetTable extends SchemaEntry, TTargetTableName extends string> {
317
+ (config?: ManyConfig<TTargetTable>): Many<TTargetTableName>;
330
318
  }
331
319
  export declare class RelationsHelperStatic<TTables extends Schema> {
332
320
  static readonly [entityKind]: string;
333
321
  private readonly _;
334
322
  constructor(tables: TTables);
335
323
  one: {
336
- [K in keyof TTables]: TTables[K] extends FilteredSchemaEntry ? OneFn<TTables, K & string> : DrizzleTypeError<'Views with nested selections are not supported by the relational query builder'>;
324
+ [K in keyof TTables]: TTables[K] extends FilteredSchemaEntry ? OneFn<TTables[K], K & string> : DrizzleTypeError<'Views with nested selections are not supported by the relational query builder'>;
337
325
  };
338
326
  many: {
339
- [K in keyof TTables]: TTables[K] extends FilteredSchemaEntry ? ManyFn<TTables, K & string> : DrizzleTypeError<'Views with nested selections are not supported by the relational query builder'>;
327
+ [K in keyof TTables]: TTables[K] extends FilteredSchemaEntry ? ManyFn<TTables[K], K & string> : DrizzleTypeError<'Views with nested selections are not supported by the relational query builder'>;
340
328
  };
341
329
  }
342
330
  export type RelationsBuilderColumns<TTable extends SchemaEntry, TTableName extends string> = {
package/relations.js CHANGED
@@ -47,37 +47,65 @@ function processRelations(tablesConfig, tables) {
47
47
  if (!is(relation, Relation)) {
48
48
  continue;
49
49
  }
50
- const relationPrintName = `relations -> ${tableConfig.name}: { ${relationFieldName}: r.${is(relation, One) ? "one" : "many"}.${relation.targetTableName}(...) }`;
51
- if (typeof relation.alias === "string" && !relation.alias) {
50
+ let reverseRelation;
51
+ const {
52
+ targetTableName,
53
+ alias,
54
+ sourceColumns,
55
+ targetColumns,
56
+ throughTable,
57
+ sourceTable,
58
+ through,
59
+ targetTable,
60
+ where,
61
+ sourceColumnTableNames,
62
+ targetColumnTableNames
63
+ } = relation;
64
+ const relationPrintName = `relations -> ${tableConfig.name}: { ${relationFieldName}: r.${is(relation, One) ? "one" : "many"}.${targetTableName}(...) }`;
65
+ if (typeof alias === "string" && !alias) {
52
66
  throw new Error(`${relationPrintName}: "alias" cannot be an empty string - omit it if you don't need it`);
53
67
  }
54
- if (relation.sourceColumns?.length === 0) {
55
- throw new Error(`${relationPrintName}: "from" cannot be an empty array`);
68
+ if (sourceColumns?.length === 0) {
69
+ throw new Error(`${relationPrintName}: "from" cannot be empty`);
56
70
  }
57
- if (relation.targetColumns?.length === 0) {
58
- throw new Error(`${relationPrintName}: "to" cannot be an empty array`);
71
+ if (targetColumns?.length === 0) {
72
+ throw new Error(`${relationPrintName}: "to" cannot be empty`);
59
73
  }
60
- if (relation.sourceColumns && relation.targetColumns) {
61
- if (relation.sourceColumns.length !== relation.targetColumns.length && !relation.throughTable) {
74
+ if (sourceColumns && targetColumns) {
75
+ if (sourceColumns.length !== targetColumns.length && !throughTable) {
62
76
  throw new Error(
63
77
  `${relationPrintName}: "from" and "to" fields without "through" must have the same length`
64
78
  );
65
79
  }
66
- if (relation.through) {
67
- if (relation.through.source.length !== relation.sourceColumns.length || relation.through.target.length !== relation.targetColumns.length) {
80
+ for (const sName of sourceColumnTableNames) {
81
+ if (sName !== sourceTableName) {
82
+ throw new Error(
83
+ `${relationPrintName}: all "from" columns must belong to table "${sourceTableName}", found column of table "${sName}"`
84
+ );
85
+ }
86
+ }
87
+ for (const tName of targetColumnTableNames) {
88
+ if (tName !== targetTableName) {
89
+ throw new Error(
90
+ `${relationPrintName}: all "to" columns must belong to table "${targetTable}", found column of table "${tName}"`
91
+ );
92
+ }
93
+ }
94
+ if (through) {
95
+ if (through.source.length !== sourceColumns.length || through.target.length !== targetColumns.length) {
68
96
  throw new Error(
69
97
  `${relationPrintName}: ".through(column)" must be used either on all columns in "from" and "to" or not defined on any of them`
70
98
  );
71
99
  }
72
- for (const column of relation.through.source) {
73
- if (tables[column._.tableName] !== relation.throughTable) {
100
+ for (const column of through.source) {
101
+ if (tables[column._.tableName] !== throughTable) {
74
102
  throw new Error(
75
103
  `${relationPrintName}: ".through(column)" must be used on the same table by all columns of the relation`
76
104
  );
77
105
  }
78
106
  }
79
- for (const column of relation.through.target) {
80
- if (tables[column._.tableName] !== relation.throughTable) {
107
+ for (const column of through.target) {
108
+ if (tables[column._.tableName] !== throughTable) {
81
109
  throw new Error(
82
110
  `${relationPrintName}: ".through(column)" must be used on the same table by all columns of the relation`
83
111
  );
@@ -86,54 +114,52 @@ function processRelations(tablesConfig, tables) {
86
114
  }
87
115
  continue;
88
116
  }
89
- if (relation.sourceColumns || relation.targetColumns) {
117
+ if (sourceColumns || targetColumns) {
90
118
  throw new Error(
91
119
  `${relationPrintName}: relation must have either both "from" and "to" defined, or none of them`
92
120
  );
93
121
  }
94
- let reverseRelation;
95
- const targetTableTsName = relation.targetTableName;
96
- const reverseTableConfig = tablesConfig[targetTableTsName];
122
+ const reverseTableConfig = tablesConfig[targetTableName];
97
123
  if (!reverseTableConfig) {
98
124
  throw new Error(
99
- `${relationPrintName}: not enough data provided to build the relation - "from"/"to" are not defined, and no reverse relations of table "${targetTableTsName}" were found"`
125
+ `${relationPrintName}: not enough data provided to build the relation - "from"/"to" are not defined, and no reverse relations of table "${targetTableName}" were found"`
100
126
  );
101
127
  }
102
- if (relation.alias) {
128
+ if (alias) {
103
129
  const reverseRelations = Object.values(reverseTableConfig.relations).filter(
104
- (it) => is(it, Relation) && it.alias === relation.alias && it !== relation
130
+ (it) => is(it, Relation) && it.alias === alias && it !== relation
105
131
  );
106
132
  if (reverseRelations.length > 1) {
107
133
  throw new Error(
108
- `${relationPrintName}: not enough data provided to build the relation - "from"/"to" are not defined, and multiple relations with alias "${relation.alias}" found in table "${targetTableTsName}": ${reverseRelations.map((it) => `"${it.fieldName}"`).join(", ")}`
134
+ `${relationPrintName}: not enough data provided to build the relation - "from"/"to" are not defined, and multiple relations with alias "${alias}" found in table "${targetTableName}": ${reverseRelations.map((it) => `"${it.fieldName}"`).join(", ")}`
109
135
  );
110
136
  }
111
137
  reverseRelation = reverseRelations[0];
112
138
  if (!reverseRelation) {
113
139
  throw new Error(
114
- `${relationPrintName}: not enough data provided to build the relation - "from"/"to" are not defined, and there is no reverse relation of table "${targetTableTsName}" with alias "${relation.alias}"`
140
+ `${relationPrintName}: not enough data provided to build the relation - "from"/"to" are not defined, and there is no reverse relation of table "${targetTableName}" with alias "${alias}"`
115
141
  );
116
142
  }
117
143
  } else {
118
144
  const reverseRelations = Object.values(reverseTableConfig.relations).filter(
119
- (it) => is(it, Relation) && it.targetTable === relation.sourceTable && !it.alias && it !== relation
145
+ (it) => is(it, Relation) && it.targetTable === sourceTable && !it.alias && it !== relation
120
146
  );
121
147
  if (reverseRelations.length > 1) {
122
148
  throw new Error(
123
- `${relationPrintName}: not enough data provided to build the relation - "from"/"to" are not defined, and multiple relations between "${targetTableTsName}" and "${sourceTableName}" were found.
149
+ `${relationPrintName}: not enough data provided to build the relation - "from"/"to" are not defined, and multiple relations between "${targetTableName}" and "${sourceTableName}" were found.
124
150
  Hint: you can specify "alias" on both sides of the relation with the same value`
125
151
  );
126
152
  }
127
153
  reverseRelation = reverseRelations[0];
128
154
  if (!reverseRelation) {
129
155
  throw new Error(
130
- `${relationPrintName}: not enough data provided to build the relation - "from"/"to" are not defined, and no reverse relation of table "${targetTableTsName}" with target table "${sourceTableName}" was found`
156
+ `${relationPrintName}: not enough data provided to build the relation - "from"/"to" are not defined, and no reverse relation of table "${targetTableName}" with target table "${sourceTableName}" was found`
131
157
  );
132
158
  }
133
159
  }
134
160
  if (!reverseRelation.sourceColumns || !reverseRelation.targetColumns) {
135
161
  throw new Error(
136
- `${relationPrintName}: not enough data provided to build the relation - "from"/"to" are not defined, and reverse relation "${targetTableTsName}.${reverseRelation.fieldName}" does not have "from"/"to" defined`
162
+ `${relationPrintName}: not enough data provided to build the relation - "from"/"to" are not defined, and reverse relation "${targetTableName}.${reverseRelation.fieldName}" does not have "from"/"to" defined`
137
163
  );
138
164
  }
139
165
  relation.sourceColumns = reverseRelation.targetColumns;
@@ -143,8 +169,8 @@ Hint: you can specify "alias" on both sides of the relation with the same value`
143
169
  target: reverseRelation.through.source
144
170
  } : void 0;
145
171
  relation.throughTable = reverseRelation.throughTable;
146
- relation.isReversed = !relation.where;
147
- relation.where = relation.where ?? reverseRelation.where;
172
+ relation.isReversed = !where;
173
+ relation.where = where ?? reverseRelation.where;
148
174
  }
149
175
  }
150
176
  return tablesConfig;
@@ -188,6 +214,10 @@ class Relation {
188
214
  through;
189
215
  throughTable;
190
216
  isReversed;
217
+ /** @internal */
218
+ sourceColumnTableNames = [];
219
+ /** @internal */
220
+ targetColumnTableNames = [];
191
221
  }
192
222
  class One extends Relation {
193
223
  static [entityKind] = "OneV2";
@@ -200,19 +230,21 @@ class One extends Relation {
200
230
  if (config?.from) {
201
231
  this.sourceColumns = (Array.isArray(config.from) ? config.from : [config.from]).map((it) => {
202
232
  this.throughTable ??= it._.through ? tables[it._.through._.tableName] : void 0;
233
+ this.sourceColumnTableNames.push(it._.tableName);
203
234
  return it._.column;
204
235
  });
205
236
  }
206
237
  if (config?.to) {
207
238
  this.targetColumns = (Array.isArray(config.to) ? config.to : [config.to]).map((it) => {
208
239
  this.throughTable ??= it._.through ? tables[it._.through._.tableName] : void 0;
240
+ this.targetColumnTableNames.push(it._.tableName);
209
241
  return it._.column;
210
242
  });
211
243
  }
212
244
  if (this.throughTable) {
213
245
  this.through = {
214
- source: (Array.isArray(config?.from) ? config.from : [config.from]).map((c) => c._.through),
215
- target: (Array.isArray(config?.to) ? config.to : [config.to]).map((c) => c._.through)
246
+ source: (Array.isArray(config?.from) ? config.from : config?.from ? [config.from] : []).map((c) => c._.through),
247
+ target: (Array.isArray(config?.to) ? config.to : config?.to ? [config.to] : []).map((c) => c._.through)
216
248
  };
217
249
  }
218
250
  this.optional = config?.optional ?? true;
@@ -227,19 +259,21 @@ class Many extends Relation {
227
259
  if (config?.from) {
228
260
  this.sourceColumns = (Array.isArray(config.from) ? config.from : [config.from]).map((it) => {
229
261
  this.throughTable ??= it._.through ? tables[it._.through._.tableName] : void 0;
262
+ this.sourceColumnTableNames.push(it._.tableName);
230
263
  return it._.column;
231
264
  });
232
265
  }
233
266
  if (config?.to) {
234
267
  this.targetColumns = (Array.isArray(config.to) ? config.to : [config.to]).map((it) => {
235
268
  this.throughTable ??= it._.through ? tables[it._.through._.tableName] : void 0;
269
+ this.targetColumnTableNames.push(it._.tableName);
236
270
  return it._.column;
237
271
  });
238
272
  }
239
273
  if (this.throughTable) {
240
274
  this.through = {
241
- source: (Array.isArray(config?.from) ? config.from : [config.from]).map((c) => c._.through),
242
- target: (Array.isArray(config?.to) ? config.to : [config.to]).map((c) => c._.through)
275
+ source: (Array.isArray(config?.from) ? config.from : config?.from ? [config.from] : []).map((c) => c._.through),
276
+ target: (Array.isArray(config?.to) ? config.to : config?.to ? [config.to] : []).map((c) => c._.through)
243
277
  };
244
278
  }
245
279
  }