@type32/tauri-sqlite-orm 0.2.15 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -53,6 +53,8 @@ __export(index_exports, {
53
53
  contains: () => contains,
54
54
  count: () => count,
55
55
  countDistinct: () => countDistinct,
56
+ defineRelations: () => defineRelations,
57
+ defineRelationsPart: () => defineRelationsPart,
56
58
  desc: () => desc,
57
59
  endsWith: () => endsWith,
58
60
  enumType: () => enumType,
@@ -92,12 +94,13 @@ __export(index_exports, {
92
94
  startsWith: () => startsWith,
93
95
  subquery: () => subquery,
94
96
  sum: () => sum,
95
- text: () => text
97
+ text: () => text,
98
+ through: () => through
96
99
  });
97
100
  module.exports = __toCommonJS(index_exports);
98
101
 
99
102
  // src/orm.ts
100
- var import_kysely4 = require("kysely");
103
+ var import_kysely5 = require("kysely");
101
104
 
102
105
  // src/dialect.ts
103
106
  var import_kysely = require("kysely");
@@ -168,6 +171,9 @@ var TauriDialect = class {
168
171
  };
169
172
 
170
173
  // src/builders/select.ts
174
+ var import_kysely3 = require("kysely");
175
+
176
+ // src/operators.ts
171
177
  var import_kysely2 = require("kysely");
172
178
 
173
179
  // src/serialization.ts
@@ -271,6 +277,83 @@ function deserializeValue(value, column) {
271
277
  return value;
272
278
  }
273
279
 
280
+ // src/operators.ts
281
+ var eq = (column, value, tableAlias) => {
282
+ const colRef = tableAlias ? `${tableAlias}.${column._.name}` : column._.name;
283
+ const serialized = serializeValue(value, column);
284
+ return import_kysely2.sql`${import_kysely2.sql.ref(colRef)} = ${import_kysely2.sql.val(serialized)}`;
285
+ };
286
+ var ne = (column, value, tableAlias) => {
287
+ const colRef = tableAlias ? `${tableAlias}.${column._.name}` : column._.name;
288
+ const serialized = serializeValue(value, column);
289
+ return import_kysely2.sql`${import_kysely2.sql.ref(colRef)} != ${import_kysely2.sql.val(serialized)}`;
290
+ };
291
+ var and = (...conditions) => {
292
+ if (conditions.length === 0) return import_kysely2.sql`1 = 1`;
293
+ if (conditions.length === 1) return conditions[0];
294
+ return import_kysely2.sql`(${import_kysely2.sql.join(conditions.map((c) => import_kysely2.sql`(${c})`), import_kysely2.sql` AND `)})`;
295
+ };
296
+ var or = (...conditions) => {
297
+ if (conditions.length === 0) return import_kysely2.sql`1 = 1`;
298
+ if (conditions.length === 1) return conditions[0];
299
+ return import_kysely2.sql`(${import_kysely2.sql.join(conditions.map((c) => import_kysely2.sql`(${c})`), import_kysely2.sql` OR `)})`;
300
+ };
301
+ var not = (condition) => import_kysely2.sql`NOT (${condition})`;
302
+ var gt = (column, value) => {
303
+ const serialized = serializeValue(value, column);
304
+ return import_kysely2.sql`${import_kysely2.sql.ref(column._.name)} > ${import_kysely2.sql.val(serialized)}`;
305
+ };
306
+ var gte = (column, value) => {
307
+ const serialized = serializeValue(value, column);
308
+ return import_kysely2.sql`${import_kysely2.sql.ref(column._.name)} >= ${import_kysely2.sql.val(serialized)}`;
309
+ };
310
+ var lt = (column, value) => {
311
+ const serialized = serializeValue(value, column);
312
+ return import_kysely2.sql`${import_kysely2.sql.ref(column._.name)} < ${import_kysely2.sql.val(serialized)}`;
313
+ };
314
+ var lte = (column, value) => {
315
+ const serialized = serializeValue(value, column);
316
+ return import_kysely2.sql`${import_kysely2.sql.ref(column._.name)} <= ${import_kysely2.sql.val(serialized)}`;
317
+ };
318
+ var like = (column, pattern) => import_kysely2.sql`${import_kysely2.sql.ref(column._.name)} LIKE ${import_kysely2.sql.val(pattern)}`;
319
+ var ilike = (column, pattern) => import_kysely2.sql`${import_kysely2.sql.ref(column._.name)} LIKE ${import_kysely2.sql.val(pattern)} COLLATE NOCASE`;
320
+ var startsWith = (column, value) => import_kysely2.sql`${import_kysely2.sql.ref(column._.name)} LIKE ${import_kysely2.sql.val(`${value}%`)}`;
321
+ var endsWith = (column, value) => import_kysely2.sql`${import_kysely2.sql.ref(column._.name)} LIKE ${import_kysely2.sql.val(`%${value}`)}`;
322
+ var contains = (column, value) => import_kysely2.sql`${import_kysely2.sql.ref(column._.name)} LIKE ${import_kysely2.sql.val(`%${value}%`)}`;
323
+ var isNull = (column) => import_kysely2.sql`${import_kysely2.sql.ref(column._.name)} IS NULL`;
324
+ var isNotNull = (column) => import_kysely2.sql`${import_kysely2.sql.ref(column._.name)} IS NOT NULL`;
325
+ var exists = (subquery2) => import_kysely2.sql`EXISTS ${subquery2}`;
326
+ var notExists = (subquery2) => import_kysely2.sql`NOT EXISTS ${subquery2}`;
327
+ var eqSubquery = (column, subquery2) => import_kysely2.sql`${import_kysely2.sql.ref(column._.name)} = (${subquery2})`;
328
+ var neSubquery = (column, subquery2) => import_kysely2.sql`${import_kysely2.sql.ref(column._.name)} != (${subquery2})`;
329
+ var gtSubquery = (column, subquery2) => import_kysely2.sql`${import_kysely2.sql.ref(column._.name)} > (${subquery2})`;
330
+ var gteSubquery = (column, subquery2) => import_kysely2.sql`${import_kysely2.sql.ref(column._.name)} >= (${subquery2})`;
331
+ var ltSubquery = (column, subquery2) => import_kysely2.sql`${import_kysely2.sql.ref(column._.name)} < (${subquery2})`;
332
+ var lteSubquery = (column, subquery2) => import_kysely2.sql`${import_kysely2.sql.ref(column._.name)} <= (${subquery2})`;
333
+ var inArray = (column, values) => {
334
+ if ((0, import_kysely2.isExpression)(values)) {
335
+ return import_kysely2.sql`${import_kysely2.sql.ref(column._.name)} IN (${values})`;
336
+ }
337
+ const arr = values;
338
+ if (arr.length === 0) return import_kysely2.sql`1 = 0`;
339
+ const serialized = arr.map((v) => import_kysely2.sql.val(serializeValue(v, column)));
340
+ return import_kysely2.sql`${import_kysely2.sql.ref(column._.name)} IN (${import_kysely2.sql.join(serialized)})`;
341
+ };
342
+ var notIn = (column, values) => {
343
+ if ((0, import_kysely2.isExpression)(values)) {
344
+ return import_kysely2.sql`${import_kysely2.sql.ref(column._.name)} NOT IN (${values})`;
345
+ }
346
+ const arr = values;
347
+ if (arr.length === 0) return import_kysely2.sql`1 = 1`;
348
+ const serialized = arr.map((v) => import_kysely2.sql.val(serializeValue(v, column)));
349
+ return import_kysely2.sql`${import_kysely2.sql.ref(column._.name)} NOT IN (${import_kysely2.sql.join(serialized)})`;
350
+ };
351
+ var between = (column, min2, max2) => {
352
+ const serializedMin = serializeValue(min2, column);
353
+ const serializedMax = serializeValue(max2, column);
354
+ return import_kysely2.sql`${import_kysely2.sql.ref(column._.name)} BETWEEN ${import_kysely2.sql.val(serializedMin)} AND ${import_kysely2.sql.val(serializedMax)}`;
355
+ };
356
+
274
357
  // src/builders/select.ts
275
358
  function getDbNameToTsName(table) {
276
359
  const map = {};
@@ -332,7 +415,7 @@ var SelectQueryBuilder = class {
332
415
  this._builder = this._builder.orderBy(column, direction);
333
416
  } else {
334
417
  this._builder = this._builder.orderBy(
335
- import_kysely2.sql.ref(column._.name),
418
+ import_kysely3.sql.ref(column._.name),
336
419
  direction
337
420
  );
338
421
  }
@@ -349,7 +432,7 @@ var SelectQueryBuilder = class {
349
432
  groupBy(...columns) {
350
433
  for (const col of columns) {
351
434
  this._builder = this._builder.groupBy(
352
- import_kysely2.sql`${import_kysely2.sql.ref(this._table._.name)}.${import_kysely2.sql.ref(col._.name)}`
435
+ import_kysely3.sql`${import_kysely3.sql.ref(this._table._.name)}.${import_kysely3.sql.ref(col._.name)}`
353
436
  );
354
437
  }
355
438
  return this;
@@ -400,32 +483,62 @@ var SelectQueryBuilder = class {
400
483
  (col) => `${foreignAlias}.${col._.name} as "${foreignAlias}.${col._.name}"`
401
484
  );
402
485
  if (relation.type === "one" && relation.fields && relation.references) {
403
- const onCondition = import_kysely2.sql`${import_kysely2.sql.join(
486
+ const onCondition = import_kysely3.sql`${import_kysely3.sql.join(
404
487
  relation.fields.map(
405
- (field, i) => import_kysely2.sql`${import_kysely2.sql.ref(`${parentAlias}.${field._.name}`)} = ${import_kysely2.sql.ref(`${foreignAlias}.${relation.references[i]._.name}`)}`
488
+ (field, i) => import_kysely3.sql`${import_kysely3.sql.ref(`${parentAlias}.${field._.name}`)} = ${import_kysely3.sql.ref(`${foreignAlias}.${relation.references[i]._.name}`)}`
406
489
  ),
407
- import_kysely2.sql` AND `
490
+ import_kysely3.sql` AND `
408
491
  )}`;
409
492
  this._builder = this._builder.leftJoin(
410
493
  `${foreignTable._.name} as ${foreignAlias}`,
411
494
  (join) => join.on(onCondition)
412
495
  ).select(aliasedCols);
413
496
  } else if (relation.type === "many") {
414
- const refRelation = Object.entries(foreignTable.relations).find(
415
- ([, r]) => r.foreignTable === parentTable
416
- );
417
- if (refRelation && refRelation[1].fields && refRelation[1].references) {
418
- const [, relationConfig] = refRelation;
419
- const onCondition = import_kysely2.sql`${import_kysely2.sql.join(
420
- relationConfig.fields.map(
421
- (field, i) => import_kysely2.sql`${import_kysely2.sql.ref(`${foreignAlias}.${field._.name}`)} = ${import_kysely2.sql.ref(`${parentAlias}.${relationConfig.references[i]._.name}`)}`
422
- ),
423
- import_kysely2.sql` AND `
424
- )}`;
497
+ if (relation.junctionTable && relation.fromJunction && relation.toJunction) {
498
+ const junctionTable = relation.junctionTable;
499
+ const junctionAlias = `${foreignAlias}_jn`;
500
+ const fromJ = relation.fromJunction;
501
+ const toJ = relation.toJunction;
502
+ const join1 = import_kysely3.sql`${import_kysely3.sql.ref(`${parentAlias}.${fromJ.column._.name}`)} = ${import_kysely3.sql.ref(`${junctionAlias}.${fromJ.junctionColumn._.name}`)}`;
503
+ let join2 = import_kysely3.sql`${import_kysely3.sql.ref(`${junctionAlias}.${toJ.junctionColumn._.name}`)} = ${import_kysely3.sql.ref(`${foreignAlias}.${toJ.column._.name}`)}`;
504
+ if (relation.where) {
505
+ join2 = and(join2, relation.where(foreignAlias));
506
+ }
425
507
  this._builder = this._builder.leftJoin(
508
+ `${junctionTable._.name} as ${junctionAlias}`,
509
+ (join) => join.on(join1)
510
+ ).leftJoin(
426
511
  `${foreignTable._.name} as ${foreignAlias}`,
427
- (join) => join.on(onCondition)
512
+ (join) => join.on(join2)
428
513
  ).select(aliasedCols);
514
+ } else {
515
+ let fields = relation.fields;
516
+ let references = relation.references;
517
+ if (!fields || !references) {
518
+ const refRelation = Object.entries(foreignTable.relations).find(
519
+ ([, r]) => r.foreignTable === parentTable
520
+ );
521
+ if (refRelation && refRelation[1].fields && refRelation[1].references) {
522
+ const [, relationConfig] = refRelation;
523
+ fields = relationConfig.fields;
524
+ references = relationConfig.references;
525
+ }
526
+ }
527
+ if (fields && references) {
528
+ let onCondition = import_kysely3.sql`${import_kysely3.sql.join(
529
+ fields.map(
530
+ (field, i) => import_kysely3.sql`${import_kysely3.sql.ref(`${foreignAlias}.${field._.name}`)} = ${import_kysely3.sql.ref(`${parentAlias}.${references[i]._.name}`)}`
531
+ ),
532
+ import_kysely3.sql` AND `
533
+ )}`;
534
+ if (relation.where) {
535
+ onCondition = and(onCondition, relation.where(foreignAlias));
536
+ }
537
+ this._builder = this._builder.leftJoin(
538
+ `${foreignTable._.name} as ${foreignAlias}`,
539
+ (join) => join.on(onCondition)
540
+ ).select(aliasedCols);
541
+ }
429
542
  }
430
543
  }
431
544
  if (typeof include === "object" && include.with) {
@@ -521,6 +634,7 @@ var SelectQueryBuilder = class {
521
634
  const column = this._table._.columns[tsName];
522
635
  result[tsName] = column ? deserializeValue(value, column) : value;
523
636
  } else {
637
+ if (tableAlias.endsWith("_jn")) continue;
524
638
  const path = parseRelationPath(tableAlias, this._table._.name);
525
639
  if (path.length > 0) {
526
640
  const relationConfig = getNestedRelation(this._table, path);
@@ -599,12 +713,12 @@ var SelectQueryBuilder = class {
599
713
  }
600
714
  async exists() {
601
715
  this.applyIncludes();
602
- const compiledResult = await this._builder.clearSelect().select(import_kysely2.sql.raw("1").as("__exists__")).limit(1).execute();
716
+ const compiledResult = await this._builder.clearSelect().select(import_kysely3.sql.raw("1").as("__exists__")).limit(1).execute();
603
717
  return compiledResult.length > 0;
604
718
  }
605
719
  async count() {
606
720
  this.applyIncludes();
607
- const result = await this._builder.clearSelect().select(import_kysely2.sql`COUNT(*)`.as("count")).execute();
721
+ const result = await this._builder.clearSelect().select(import_kysely3.sql`COUNT(*)`.as("count")).execute();
608
722
  const row = result[0];
609
723
  const val = row ? row['"count"'] ?? row.count : void 0;
610
724
  return Number(val ?? 0);
@@ -614,7 +728,7 @@ var SelectQueryBuilder = class {
614
728
  const col = this._table._.columns[column];
615
729
  const alias2 = col._.name;
616
730
  const results = await this._builder.clearSelect().select(
617
- import_kysely2.sql.raw(`${this._table._.name}.${alias2}`).as(alias2)
731
+ import_kysely3.sql.raw(`${this._table._.name}.${alias2}`).as(alias2)
618
732
  ).execute();
619
733
  return results.map((row) => {
620
734
  const val = row['"' + alias2 + '"'] ?? row[alias2];
@@ -650,7 +764,7 @@ var SelectQueryBuilder = class {
650
764
  };
651
765
 
652
766
  // src/builders/update.ts
653
- var import_kysely3 = require("kysely");
767
+ var import_kysely4 = require("kysely");
654
768
 
655
769
  // src/errors.ts
656
770
  var TauriORMError = class extends Error {
@@ -803,7 +917,7 @@ var UpdateQueryBuilder = class {
803
917
  }
804
918
  for (const op of this._incrementDecrementOps) {
805
919
  const sign = op.op === "increment" ? "+" : "-";
806
- setMap[op.column] = import_kysely3.sql.raw(`${op.column} ${sign} ${op.value}`);
920
+ setMap[op.column] = import_kysely4.sql.raw(`${op.column} ${sign} ${op.value}`);
807
921
  }
808
922
  return setMap;
809
923
  }
@@ -1181,12 +1295,12 @@ var Table = class {
1181
1295
  var sqliteTable = (tableName, columns) => {
1182
1296
  return new Table(tableName, columns);
1183
1297
  };
1184
- var asc = (column) => import_kysely4.sql`${import_kysely4.sql.ref(column._.name)} ASC`;
1185
- var desc = (column) => import_kysely4.sql`${import_kysely4.sql.ref(column._.name)} DESC`;
1298
+ var asc = (column) => import_kysely5.sql`${import_kysely5.sql.ref(column._.name)} ASC`;
1299
+ var desc = (column) => import_kysely5.sql`${import_kysely5.sql.ref(column._.name)} DESC`;
1186
1300
  var TauriORM = class {
1187
1301
  constructor(db, schema = void 0) {
1188
1302
  this.db = db;
1189
- this.kysely = new import_kysely4.Kysely({ dialect: new TauriDialect(db) });
1303
+ this.kysely = new import_kysely5.Kysely({ dialect: new TauriDialect(db) });
1190
1304
  if (schema) {
1191
1305
  for (const [, value] of Object.entries(schema)) {
1192
1306
  if (value instanceof Table) {
@@ -1575,8 +1689,9 @@ var OneRelation = class extends Relation {
1575
1689
  }
1576
1690
  };
1577
1691
  var ManyRelation = class extends Relation {
1578
- constructor(foreignTable) {
1692
+ constructor(foreignTable, config) {
1579
1693
  super(foreignTable);
1694
+ this.config = config;
1580
1695
  }
1581
1696
  };
1582
1697
  var relations = (table, relationsCallback) => {
@@ -1594,7 +1709,9 @@ var relations = (table, relationsCallback) => {
1594
1709
  type: "one",
1595
1710
  foreignTable: relation.foreignTable,
1596
1711
  fields: relation.config?.fields,
1597
- references: relation.config?.references
1712
+ references: relation.config?.references,
1713
+ optional: relation.config?.optional,
1714
+ alias: relation.config?.alias
1598
1715
  };
1599
1716
  } else if (relation instanceof ManyRelation) {
1600
1717
  table.relations[name] = {
@@ -1612,84 +1729,6 @@ var alias = (table, alias2) => {
1612
1729
  return table;
1613
1730
  };
1614
1731
 
1615
- // src/operators.ts
1616
- var import_kysely5 = require("kysely");
1617
- var eq = (column, value, tableAlias) => {
1618
- const colRef = tableAlias ? `${tableAlias}.${column._.name}` : column._.name;
1619
- const serialized = serializeValue(value, column);
1620
- return import_kysely5.sql`${import_kysely5.sql.ref(colRef)} = ${import_kysely5.sql.val(serialized)}`;
1621
- };
1622
- var ne = (column, value, tableAlias) => {
1623
- const colRef = tableAlias ? `${tableAlias}.${column._.name}` : column._.name;
1624
- const serialized = serializeValue(value, column);
1625
- return import_kysely5.sql`${import_kysely5.sql.ref(colRef)} != ${import_kysely5.sql.val(serialized)}`;
1626
- };
1627
- var and = (...conditions) => {
1628
- if (conditions.length === 0) return import_kysely5.sql`1 = 1`;
1629
- if (conditions.length === 1) return conditions[0];
1630
- return import_kysely5.sql`(${import_kysely5.sql.join(conditions.map((c) => import_kysely5.sql`(${c})`), import_kysely5.sql` AND `)})`;
1631
- };
1632
- var or = (...conditions) => {
1633
- if (conditions.length === 0) return import_kysely5.sql`1 = 1`;
1634
- if (conditions.length === 1) return conditions[0];
1635
- return import_kysely5.sql`(${import_kysely5.sql.join(conditions.map((c) => import_kysely5.sql`(${c})`), import_kysely5.sql` OR `)})`;
1636
- };
1637
- var not = (condition) => import_kysely5.sql`NOT (${condition})`;
1638
- var gt = (column, value) => {
1639
- const serialized = serializeValue(value, column);
1640
- return import_kysely5.sql`${import_kysely5.sql.ref(column._.name)} > ${import_kysely5.sql.val(serialized)}`;
1641
- };
1642
- var gte = (column, value) => {
1643
- const serialized = serializeValue(value, column);
1644
- return import_kysely5.sql`${import_kysely5.sql.ref(column._.name)} >= ${import_kysely5.sql.val(serialized)}`;
1645
- };
1646
- var lt = (column, value) => {
1647
- const serialized = serializeValue(value, column);
1648
- return import_kysely5.sql`${import_kysely5.sql.ref(column._.name)} < ${import_kysely5.sql.val(serialized)}`;
1649
- };
1650
- var lte = (column, value) => {
1651
- const serialized = serializeValue(value, column);
1652
- return import_kysely5.sql`${import_kysely5.sql.ref(column._.name)} <= ${import_kysely5.sql.val(serialized)}`;
1653
- };
1654
- var like = (column, pattern) => import_kysely5.sql`${import_kysely5.sql.ref(column._.name)} LIKE ${import_kysely5.sql.val(pattern)}`;
1655
- var ilike = (column, pattern) => import_kysely5.sql`${import_kysely5.sql.ref(column._.name)} LIKE ${import_kysely5.sql.val(pattern)} COLLATE NOCASE`;
1656
- var startsWith = (column, value) => import_kysely5.sql`${import_kysely5.sql.ref(column._.name)} LIKE ${import_kysely5.sql.val(`${value}%`)}`;
1657
- var endsWith = (column, value) => import_kysely5.sql`${import_kysely5.sql.ref(column._.name)} LIKE ${import_kysely5.sql.val(`%${value}`)}`;
1658
- var contains = (column, value) => import_kysely5.sql`${import_kysely5.sql.ref(column._.name)} LIKE ${import_kysely5.sql.val(`%${value}%`)}`;
1659
- var isNull = (column) => import_kysely5.sql`${import_kysely5.sql.ref(column._.name)} IS NULL`;
1660
- var isNotNull = (column) => import_kysely5.sql`${import_kysely5.sql.ref(column._.name)} IS NOT NULL`;
1661
- var exists = (subquery2) => import_kysely5.sql`EXISTS ${subquery2}`;
1662
- var notExists = (subquery2) => import_kysely5.sql`NOT EXISTS ${subquery2}`;
1663
- var eqSubquery = (column, subquery2) => import_kysely5.sql`${import_kysely5.sql.ref(column._.name)} = (${subquery2})`;
1664
- var neSubquery = (column, subquery2) => import_kysely5.sql`${import_kysely5.sql.ref(column._.name)} != (${subquery2})`;
1665
- var gtSubquery = (column, subquery2) => import_kysely5.sql`${import_kysely5.sql.ref(column._.name)} > (${subquery2})`;
1666
- var gteSubquery = (column, subquery2) => import_kysely5.sql`${import_kysely5.sql.ref(column._.name)} >= (${subquery2})`;
1667
- var ltSubquery = (column, subquery2) => import_kysely5.sql`${import_kysely5.sql.ref(column._.name)} < (${subquery2})`;
1668
- var lteSubquery = (column, subquery2) => import_kysely5.sql`${import_kysely5.sql.ref(column._.name)} <= (${subquery2})`;
1669
- var inArray = (column, values) => {
1670
- if ((0, import_kysely5.isExpression)(values)) {
1671
- return import_kysely5.sql`${import_kysely5.sql.ref(column._.name)} IN (${values})`;
1672
- }
1673
- const arr = values;
1674
- if (arr.length === 0) return import_kysely5.sql`1 = 0`;
1675
- const serialized = arr.map((v) => import_kysely5.sql.val(serializeValue(v, column)));
1676
- return import_kysely5.sql`${import_kysely5.sql.ref(column._.name)} IN (${import_kysely5.sql.join(serialized)})`;
1677
- };
1678
- var notIn = (column, values) => {
1679
- if ((0, import_kysely5.isExpression)(values)) {
1680
- return import_kysely5.sql`${import_kysely5.sql.ref(column._.name)} NOT IN (${values})`;
1681
- }
1682
- const arr = values;
1683
- if (arr.length === 0) return import_kysely5.sql`1 = 1`;
1684
- const serialized = arr.map((v) => import_kysely5.sql.val(serializeValue(v, column)));
1685
- return import_kysely5.sql`${import_kysely5.sql.ref(column._.name)} NOT IN (${import_kysely5.sql.join(serialized)})`;
1686
- };
1687
- var between = (column, min2, max2) => {
1688
- const serializedMin = serializeValue(min2, column);
1689
- const serializedMax = serializeValue(max2, column);
1690
- return import_kysely5.sql`${import_kysely5.sql.ref(column._.name)} BETWEEN ${import_kysely5.sql.val(serializedMin)} AND ${import_kysely5.sql.val(serializedMax)}`;
1691
- };
1692
-
1693
1732
  // src/aggregates.ts
1694
1733
  var import_kysely6 = require("kysely");
1695
1734
  var count = (column) => import_kysely6.sql`COUNT(${column ? import_kysely6.sql.ref(column._.name) : import_kysely6.sql.raw("*")})`;
@@ -1726,6 +1765,126 @@ function numeric(name, config) {
1726
1765
  }
1727
1766
  var enumType = (name, values) => text(name, { enum: values });
1728
1767
 
1768
+ // src/relations-v2.ts
1769
+ function extractTables(schema) {
1770
+ const tables = {};
1771
+ for (const [key, value] of Object.entries(schema)) {
1772
+ const v = value;
1773
+ if (v && typeof v === "object" && v._?.name && v._?.columns && typeof v.relations === "object") {
1774
+ tables[key] = v;
1775
+ }
1776
+ }
1777
+ return tables;
1778
+ }
1779
+ function through(column, junctionColumn, junctionTable) {
1780
+ return { column, junctionColumn, junctionTable };
1781
+ }
1782
+ function toArray(col) {
1783
+ return Array.isArray(col) ? col : [col];
1784
+ }
1785
+ function isThroughRef(v) {
1786
+ return v && typeof v === "object" && "column" in v && "junctionColumn" in v && "junctionTable" in v;
1787
+ }
1788
+ function buildTableRef(table) {
1789
+ return { ...table._.columns };
1790
+ }
1791
+ function buildR(tables) {
1792
+ const tableRefs = {};
1793
+ const oneFns = {};
1794
+ const manyFns = {};
1795
+ for (const [tableKey, table] of Object.entries(tables)) {
1796
+ tableRefs[tableKey] = buildTableRef(table);
1797
+ const foreignTable = table;
1798
+ oneFns[tableKey] = (opts) => {
1799
+ return new OneRelation(foreignTable, {
1800
+ fields: toArray(opts.from),
1801
+ references: toArray(opts.to),
1802
+ optional: opts.optional,
1803
+ alias: opts.alias
1804
+ });
1805
+ };
1806
+ manyFns[tableKey] = (opts) => {
1807
+ if (!opts?.from || !opts?.to) return new ManyRelation(foreignTable);
1808
+ if (isThroughRef(opts.from) && isThroughRef(opts.to)) {
1809
+ return new ManyRelation(foreignTable, {
1810
+ through: {
1811
+ junctionTable: opts.from.junctionTable,
1812
+ fromRef: { column: opts.from.column, junctionColumn: opts.from.junctionColumn },
1813
+ toRef: { column: opts.to.column, junctionColumn: opts.to.junctionColumn }
1814
+ },
1815
+ optional: opts.optional,
1816
+ alias: opts.alias,
1817
+ where: opts.where
1818
+ });
1819
+ }
1820
+ return new ManyRelation(foreignTable, {
1821
+ from: toArray(opts.from),
1822
+ to: toArray(opts.to),
1823
+ optional: opts.optional,
1824
+ alias: opts.alias,
1825
+ where: opts.where
1826
+ });
1827
+ };
1828
+ }
1829
+ return {
1830
+ ...tableRefs,
1831
+ one: oneFns,
1832
+ many: manyFns
1833
+ };
1834
+ }
1835
+ function applyRelationsToTables(tables, relationsResult) {
1836
+ for (const [tableKey, rels] of Object.entries(relationsResult)) {
1837
+ const table = tables[tableKey];
1838
+ if (!table) continue;
1839
+ for (const [relName, relation] of Object.entries(rels)) {
1840
+ if (relation instanceof OneRelation) {
1841
+ table.relations[relName] = {
1842
+ type: "one",
1843
+ foreignTable: relation.foreignTable,
1844
+ fields: relation.config?.fields,
1845
+ references: relation.config?.references,
1846
+ optional: relation.config?.optional,
1847
+ alias: relation.config?.alias
1848
+ };
1849
+ } else if (relation instanceof ManyRelation) {
1850
+ const config = relation.config;
1851
+ if (config?.through) {
1852
+ table.relations[relName] = {
1853
+ type: "many",
1854
+ foreignTable: relation.foreignTable,
1855
+ junctionTable: config.through.junctionTable,
1856
+ fromJunction: config.through.fromRef,
1857
+ toJunction: config.through.toRef,
1858
+ optional: config.optional,
1859
+ alias: config.alias,
1860
+ where: config.where
1861
+ };
1862
+ } else {
1863
+ table.relations[relName] = {
1864
+ type: "many",
1865
+ foreignTable: relation.foreignTable,
1866
+ fields: config ? config.to : void 0,
1867
+ references: config ? config.from : void 0,
1868
+ optional: config?.optional,
1869
+ alias: config?.alias,
1870
+ where: config?.where
1871
+ };
1872
+ }
1873
+ }
1874
+ }
1875
+ }
1876
+ }
1877
+ function defineRelations(schema, callback) {
1878
+ const tables = extractTables(schema);
1879
+ const r = buildR(tables);
1880
+ const result = callback(r);
1881
+ applyRelationsToTables(tables, result);
1882
+ return result;
1883
+ }
1884
+ function defineRelationsPart(schema, callback) {
1885
+ return defineRelations(schema, callback);
1886
+ }
1887
+
1729
1888
  // src/index.ts
1730
1889
  var import_kysely7 = require("kysely");
1731
1890
  // Annotate the CommonJS export names for ESM import in node:
@@ -1763,6 +1922,8 @@ var import_kysely7 = require("kysely");
1763
1922
  contains,
1764
1923
  count,
1765
1924
  countDistinct,
1925
+ defineRelations,
1926
+ defineRelationsPart,
1766
1927
  desc,
1767
1928
  endsWith,
1768
1929
  enumType,
@@ -1802,5 +1963,6 @@ var import_kysely7 = require("kysely");
1802
1963
  startsWith,
1803
1964
  subquery,
1804
1965
  sum,
1805
- text
1966
+ text,
1967
+ through
1806
1968
  });