@type32/tauri-sqlite-orm 0.1.18-16 → 0.1.18-18

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.d.mts CHANGED
@@ -138,7 +138,7 @@ type RelationsBuilder = {
138
138
  many: <U extends AnyTable>(table: U) => ManyRelation<U>;
139
139
  };
140
140
 
141
- declare class SQLiteColumn<TName extends string = string, TType extends ColumnDataType = ColumnDataType, TMode extends Mode = "default", TNotNull extends boolean = false, THasDefault extends boolean = false, TAutoincrement extends boolean = false> {
141
+ declare class SQLiteColumn<TName extends string = string, TType extends ColumnDataType = ColumnDataType, TMode extends Mode = 'default', TNotNull extends boolean = false, THasDefault extends boolean = false, TAutoincrement extends boolean = false> {
142
142
  type: TType;
143
143
  options: ColumnOptions<ColumnValueTypes<TType, TMode>>;
144
144
  _: {
@@ -156,11 +156,11 @@ declare class SQLiteColumn<TName extends string = string, TType extends ColumnDa
156
156
  primaryKey(): SQLiteColumn<TName, TType, TMode, true, THasDefault, TAutoincrement>;
157
157
  autoincrement(): SQLiteColumn<TName, TType, TMode, TNotNull, THasDefault, true>;
158
158
  unique(): SQLiteColumn<TName, TType, TMode, TNotNull, THasDefault, TAutoincrement>;
159
- references<T extends AnyTable, K extends keyof T["_"]["columns"] & string>(ref: T, column: K): SQLiteColumn<TName, TType, TMode, TNotNull, THasDefault, TAutoincrement>;
159
+ references<T extends AnyTable, K extends keyof T['_']['columns'] & string>(ref: T, column: K): SQLiteColumn<TName, TType, TMode, TNotNull, THasDefault, TAutoincrement>;
160
160
  $onUpdateFn(fn: () => ColumnValueTypes<TType, TMode>): SQLiteColumn<TName, TType, TMode, TNotNull, THasDefault, TAutoincrement>;
161
161
  as(alias: string): SQLiteColumn<TName, TType, TMode, TNotNull, THasDefault, TAutoincrement>;
162
162
  }
163
- type IsOptionalOnInsert<C extends AnySQLiteColumn> = C["_"]["notNull"] extends false ? true : C["_"]["hasDefault"] extends true ? true : C["_"]["autoincrement"] extends true ? true : false;
163
+ type IsOptionalOnInsert<C extends AnySQLiteColumn> = C['_']['notNull'] extends false ? true : C['_']['hasDefault'] extends true ? true : C['_']['autoincrement'] extends true ? true : false;
164
164
  type OptionalColumns<TColumns extends Record<string, AnySQLiteColumn>> = {
165
165
  [K in keyof TColumns]: IsOptionalOnInsert<TColumns[K]> extends true ? K : never;
166
166
  }[keyof TColumns];
@@ -168,9 +168,9 @@ type RequiredColumns<TColumns extends Record<string, AnySQLiteColumn>> = {
168
168
  [K in keyof TColumns]: IsOptionalOnInsert<TColumns[K]> extends true ? never : K;
169
169
  }[keyof TColumns];
170
170
  type InferInsertModel<T extends AnyTable> = {
171
- [K in RequiredColumns<T["_"]["columns"]>]: ExtractColumnType<T["_"]["columns"][K]>;
171
+ [K in RequiredColumns<T['_']['columns']>]: ExtractColumnType<T['_']['columns'][K]>;
172
172
  } & {
173
- [K in OptionalColumns<T["_"]["columns"]>]?: ExtractColumnType<T["_"]["columns"][K]>;
173
+ [K in OptionalColumns<T['_']['columns']>]?: ExtractColumnType<T['_']['columns'][K]>;
174
174
  };
175
175
  declare class Table<TColumns extends Record<string, AnySQLiteColumn>, TTableName extends string> {
176
176
  _: {
@@ -204,7 +204,7 @@ declare class TauriORM {
204
204
  constructor(db: Database, schema?: Record<string, AnyTable | Record<string, Relation>> | undefined);
205
205
  private buildColumnDefinition;
206
206
  migrate(): Promise<void>;
207
- select<T extends AnyTable, C extends (keyof T["_"]["columns"])[] | undefined = undefined>(table: T, columns?: C): SelectQueryBuilder<T, C>;
207
+ select<T extends AnyTable, C extends (keyof T['_']['columns'])[] | undefined = undefined>(table: T, columns?: C): SelectQueryBuilder<T, C>;
208
208
  insert<T extends AnyTable>(table: T): InsertQueryBuilder<T>;
209
209
  update<T extends AnyTable>(table: T): UpdateQueryBuilder<T>;
210
210
  delete<T extends AnyTable>(table: T): DeleteQueryBuilder<T>;
@@ -246,7 +246,7 @@ declare class OneRelation<T extends AnyTable = AnyTable> extends Relation<T> {
246
246
  declare class ManyRelation<T extends AnyTable = AnyTable> extends Relation<T> {
247
247
  constructor(foreignTable: T);
248
248
  }
249
- declare const relations: <T extends AnyTable, R extends Record<string, Relation>>(_table: T, relationsCallback: (helpers: RelationsBuilder) => R) => R;
249
+ declare const relations: <T extends AnyTable, R extends Record<string, Relation>>(table: T, relationsCallback: (helpers: RelationsBuilder) => R) => R;
250
250
  declare const getTableColumns: <T extends AnyTable>(table: T) => Record<string, AnySQLiteColumn>;
251
251
  declare const alias: <T extends AnyTable>(table: T, alias: string) => Table<T["_"]["columns"], T["_"]["name"]>;
252
252
 
package/dist/index.d.ts CHANGED
@@ -138,7 +138,7 @@ type RelationsBuilder = {
138
138
  many: <U extends AnyTable>(table: U) => ManyRelation<U>;
139
139
  };
140
140
 
141
- declare class SQLiteColumn<TName extends string = string, TType extends ColumnDataType = ColumnDataType, TMode extends Mode = "default", TNotNull extends boolean = false, THasDefault extends boolean = false, TAutoincrement extends boolean = false> {
141
+ declare class SQLiteColumn<TName extends string = string, TType extends ColumnDataType = ColumnDataType, TMode extends Mode = 'default', TNotNull extends boolean = false, THasDefault extends boolean = false, TAutoincrement extends boolean = false> {
142
142
  type: TType;
143
143
  options: ColumnOptions<ColumnValueTypes<TType, TMode>>;
144
144
  _: {
@@ -156,11 +156,11 @@ declare class SQLiteColumn<TName extends string = string, TType extends ColumnDa
156
156
  primaryKey(): SQLiteColumn<TName, TType, TMode, true, THasDefault, TAutoincrement>;
157
157
  autoincrement(): SQLiteColumn<TName, TType, TMode, TNotNull, THasDefault, true>;
158
158
  unique(): SQLiteColumn<TName, TType, TMode, TNotNull, THasDefault, TAutoincrement>;
159
- references<T extends AnyTable, K extends keyof T["_"]["columns"] & string>(ref: T, column: K): SQLiteColumn<TName, TType, TMode, TNotNull, THasDefault, TAutoincrement>;
159
+ references<T extends AnyTable, K extends keyof T['_']['columns'] & string>(ref: T, column: K): SQLiteColumn<TName, TType, TMode, TNotNull, THasDefault, TAutoincrement>;
160
160
  $onUpdateFn(fn: () => ColumnValueTypes<TType, TMode>): SQLiteColumn<TName, TType, TMode, TNotNull, THasDefault, TAutoincrement>;
161
161
  as(alias: string): SQLiteColumn<TName, TType, TMode, TNotNull, THasDefault, TAutoincrement>;
162
162
  }
163
- type IsOptionalOnInsert<C extends AnySQLiteColumn> = C["_"]["notNull"] extends false ? true : C["_"]["hasDefault"] extends true ? true : C["_"]["autoincrement"] extends true ? true : false;
163
+ type IsOptionalOnInsert<C extends AnySQLiteColumn> = C['_']['notNull'] extends false ? true : C['_']['hasDefault'] extends true ? true : C['_']['autoincrement'] extends true ? true : false;
164
164
  type OptionalColumns<TColumns extends Record<string, AnySQLiteColumn>> = {
165
165
  [K in keyof TColumns]: IsOptionalOnInsert<TColumns[K]> extends true ? K : never;
166
166
  }[keyof TColumns];
@@ -168,9 +168,9 @@ type RequiredColumns<TColumns extends Record<string, AnySQLiteColumn>> = {
168
168
  [K in keyof TColumns]: IsOptionalOnInsert<TColumns[K]> extends true ? never : K;
169
169
  }[keyof TColumns];
170
170
  type InferInsertModel<T extends AnyTable> = {
171
- [K in RequiredColumns<T["_"]["columns"]>]: ExtractColumnType<T["_"]["columns"][K]>;
171
+ [K in RequiredColumns<T['_']['columns']>]: ExtractColumnType<T['_']['columns'][K]>;
172
172
  } & {
173
- [K in OptionalColumns<T["_"]["columns"]>]?: ExtractColumnType<T["_"]["columns"][K]>;
173
+ [K in OptionalColumns<T['_']['columns']>]?: ExtractColumnType<T['_']['columns'][K]>;
174
174
  };
175
175
  declare class Table<TColumns extends Record<string, AnySQLiteColumn>, TTableName extends string> {
176
176
  _: {
@@ -204,7 +204,7 @@ declare class TauriORM {
204
204
  constructor(db: Database, schema?: Record<string, AnyTable | Record<string, Relation>> | undefined);
205
205
  private buildColumnDefinition;
206
206
  migrate(): Promise<void>;
207
- select<T extends AnyTable, C extends (keyof T["_"]["columns"])[] | undefined = undefined>(table: T, columns?: C): SelectQueryBuilder<T, C>;
207
+ select<T extends AnyTable, C extends (keyof T['_']['columns'])[] | undefined = undefined>(table: T, columns?: C): SelectQueryBuilder<T, C>;
208
208
  insert<T extends AnyTable>(table: T): InsertQueryBuilder<T>;
209
209
  update<T extends AnyTable>(table: T): UpdateQueryBuilder<T>;
210
210
  delete<T extends AnyTable>(table: T): DeleteQueryBuilder<T>;
@@ -246,7 +246,7 @@ declare class OneRelation<T extends AnyTable = AnyTable> extends Relation<T> {
246
246
  declare class ManyRelation<T extends AnyTable = AnyTable> extends Relation<T> {
247
247
  constructor(foreignTable: T);
248
248
  }
249
- declare const relations: <T extends AnyTable, R extends Record<string, Relation>>(_table: T, relationsCallback: (helpers: RelationsBuilder) => R) => R;
249
+ declare const relations: <T extends AnyTable, R extends Record<string, Relation>>(table: T, relationsCallback: (helpers: RelationsBuilder) => R) => R;
250
250
  declare const getTableColumns: <T extends AnyTable>(table: T) => Record<string, AnySQLiteColumn>;
251
251
  declare const alias: <T extends AnyTable>(table: T, alias: string) => Table<T["_"]["columns"], T["_"]["name"]>;
252
252
 
package/dist/index.js CHANGED
@@ -296,9 +296,18 @@ var SelectQueryBuilder = class extends BaseQueryBuilder {
296
296
  return rawResults;
297
297
  }
298
298
  processRelationResults(rawResults) {
299
- if (!rawResults.length) return rawResults;
300
- return rawResults.map((row) => {
301
- const result = {};
299
+ if (!rawResults.length) return [];
300
+ const mainTablePks = Object.values(this.table._.columns).filter((c) => c.options.primaryKey).map((c) => c._.name);
301
+ if (mainTablePks.length === 0) {
302
+ return rawResults;
303
+ }
304
+ const groupedResults = /* @__PURE__ */ new Map();
305
+ for (const row of rawResults) {
306
+ const mainTableKey = mainTablePks.map((pk) => row[`${this.selectedTableAlias}.${pk}`] ?? row[pk]).join("_");
307
+ if (!groupedResults.has(mainTableKey)) {
308
+ groupedResults.set(mainTableKey, {});
309
+ }
310
+ const result = groupedResults.get(mainTableKey);
302
311
  const relations2 = {};
303
312
  for (const [key, value] of Object.entries(row)) {
304
313
  if (key.includes(".")) {
@@ -321,15 +330,25 @@ var SelectQueryBuilder = class extends BaseQueryBuilder {
321
330
  }
322
331
  }
323
332
  for (const [relName, relData] of Object.entries(relations2)) {
333
+ const relationConfig = this.table.relations[relName];
334
+ if (!relationConfig) continue;
324
335
  const hasData = Object.values(relData).some(
325
336
  (v) => v !== null && v !== void 0 && v !== ""
326
337
  );
327
- if (hasData) {
338
+ if (!hasData) continue;
339
+ if (relationConfig.type === "many") {
340
+ if (!result[relName]) result[relName] = [];
341
+ const relatedPks = Object.values(relationConfig.foreignTable._.columns).filter((c) => c.options.primaryKey).map((c) => c._.name);
342
+ const relDataKey = relatedPks.map((pk) => relData[pk]).join("_");
343
+ if (relatedPks.length === 0 || !result[relName].some((r) => relatedPks.map((pk) => r[pk]).join("_") === relDataKey)) {
344
+ result[relName].push(relData);
345
+ }
346
+ } else {
328
347
  result[relName] = relData;
329
348
  }
330
349
  }
331
- return result;
332
- });
350
+ }
351
+ return Array.from(groupedResults.values());
333
352
  }
334
353
  // Update the return type signatures
335
354
  async all() {
@@ -626,28 +645,13 @@ var SQLiteColumn = class _SQLiteColumn {
626
645
  }
627
646
  _;
628
647
  notNull() {
629
- return new _SQLiteColumn(
630
- this._.name,
631
- this.type,
632
- { ...this.options, notNull: true },
633
- this._.mode
634
- );
648
+ return new _SQLiteColumn(this._.name, this.type, { ...this.options, notNull: true }, this._.mode);
635
649
  }
636
650
  default(value) {
637
- return new _SQLiteColumn(
638
- this._.name,
639
- this.type,
640
- { ...this.options, default: value },
641
- this._.mode
642
- );
651
+ return new _SQLiteColumn(this._.name, this.type, { ...this.options, default: value }, this._.mode);
643
652
  }
644
653
  $defaultFn(fn) {
645
- return new _SQLiteColumn(
646
- this._.name,
647
- this.type,
648
- { ...this.options, $defaultFn: fn },
649
- this._.mode
650
- );
654
+ return new _SQLiteColumn(this._.name, this.type, { ...this.options, $defaultFn: fn }, this._.mode);
651
655
  }
652
656
  primaryKey() {
653
657
  return new _SQLiteColumn(
@@ -658,20 +662,10 @@ var SQLiteColumn = class _SQLiteColumn {
658
662
  );
659
663
  }
660
664
  autoincrement() {
661
- return new _SQLiteColumn(
662
- this._.name,
663
- this.type,
664
- { ...this.options, autoincrement: true },
665
- this._.mode
666
- );
665
+ return new _SQLiteColumn(this._.name, this.type, { ...this.options, autoincrement: true }, this._.mode);
667
666
  }
668
667
  unique() {
669
- return new _SQLiteColumn(
670
- this._.name,
671
- this.type,
672
- { ...this.options, unique: true },
673
- this._.mode
674
- );
668
+ return new _SQLiteColumn(this._.name, this.type, { ...this.options, unique: true }, this._.mode);
675
669
  }
676
670
  references(ref, column) {
677
671
  return new _SQLiteColumn(
@@ -688,12 +682,7 @@ var SQLiteColumn = class _SQLiteColumn {
688
682
  );
689
683
  }
690
684
  $onUpdateFn(fn) {
691
- return new _SQLiteColumn(
692
- this._.name,
693
- this.type,
694
- { ...this.options, $onUpdateFn: fn },
695
- this._.mode
696
- );
685
+ return new _SQLiteColumn(this._.name, this.type, { ...this.options, $onUpdateFn: fn }, this._.mode);
697
686
  }
698
687
  as(alias2) {
699
688
  return this;
@@ -749,29 +738,6 @@ var TauriORM = class {
749
738
  this.tables.set(value._.name, value);
750
739
  }
751
740
  }
752
- for (const [key, value] of Object.entries(schema)) {
753
- if (!(value instanceof Table) && typeof value === "object") {
754
- const tableName = key.replace("Relations", "");
755
- const table = Array.from(this.tables.values()).find((t) => t._.name === tableName);
756
- if (table) {
757
- for (const [relName, rel] of Object.entries(value)) {
758
- if (rel instanceof OneRelation) {
759
- table.relations[relName] = {
760
- type: "one",
761
- foreignTable: rel.foreignTable,
762
- fields: rel.config?.fields,
763
- references: rel.config?.references
764
- };
765
- } else if (rel instanceof ManyRelation) {
766
- table.relations[relName] = {
767
- type: "many",
768
- foreignTable: rel.foreignTable
769
- };
770
- }
771
- }
772
- }
773
- }
774
- }
775
741
  }
776
742
  }
777
743
  tables = /* @__PURE__ */ new Map();
@@ -796,9 +762,7 @@ var TauriORM = class {
796
762
  }
797
763
  async migrate() {
798
764
  for (const table of this.tables.values()) {
799
- const existingTableInfo = await this.db.select(
800
- `PRAGMA table_info('${table._.name}')`
801
- );
765
+ const existingTableInfo = await this.db.select(`PRAGMA table_info('${table._.name}')`);
802
766
  if (existingTableInfo.length === 0) {
803
767
  const columnsSql = Object.values(table._.columns).map((col) => this.buildColumnDefinition(col)).join(", ");
804
768
  const createSql = `CREATE TABLE ${table._.name}
@@ -807,9 +771,7 @@ var TauriORM = class {
807
771
  )`;
808
772
  await this.db.execute(createSql);
809
773
  } else {
810
- const existingColumnNames = new Set(
811
- existingTableInfo.map((c) => c.name)
812
- );
774
+ const existingColumnNames = new Set(existingTableInfo.map((c) => c.name));
813
775
  for (const column of Object.values(table._.columns)) {
814
776
  if (!existingColumnNames.has(column._.name)) {
815
777
  const columnSql = this.buildColumnDefinition(column, true);
@@ -822,7 +784,14 @@ var TauriORM = class {
822
784
  }
823
785
  }
824
786
  select(table, columns) {
825
- return new SelectQueryBuilder(this.db, table, columns);
787
+ const internalTable = this.tables.get(table._.name);
788
+ if (!internalTable) {
789
+ console.warn(
790
+ `[Tauri-ORM] Table "${table._.name}" was not passed in the schema to the ORM constructor. Relations will not be available.`
791
+ );
792
+ return new SelectQueryBuilder(this.db, table, columns);
793
+ }
794
+ return new SelectQueryBuilder(this.db, internalTable, columns);
826
795
  }
827
796
  insert(table) {
828
797
  return new InsertQueryBuilder(this.db, table);
@@ -925,10 +894,7 @@ var TauriORM = class {
925
894
  const status = await this.isSchemaDirty();
926
895
  if (status.dirty) {
927
896
  await this.migrate();
928
- await this.setSchemaMeta(
929
- "schema_signature",
930
- this.computeModelSignature()
931
- );
897
+ await this.setSchemaMeta("schema_signature", this.computeModelSignature());
932
898
  return true;
933
899
  }
934
900
  return false;
@@ -950,15 +916,31 @@ var ManyRelation = class extends Relation {
950
916
  super(foreignTable);
951
917
  }
952
918
  };
953
- var relations = (_table, relationsCallback) => {
954
- return relationsCallback({
955
- one: (table, config) => {
956
- return new OneRelation(table, config);
919
+ var relations = (table, relationsCallback) => {
920
+ const builtRelations = relationsCallback({
921
+ one: (foreignTable, config) => {
922
+ return new OneRelation(foreignTable, config);
957
923
  },
958
- many: (table) => {
959
- return new ManyRelation(table);
924
+ many: (foreignTable) => {
925
+ return new ManyRelation(foreignTable);
960
926
  }
961
927
  });
928
+ for (const [name, relation] of Object.entries(builtRelations)) {
929
+ if (relation instanceof OneRelation) {
930
+ table.relations[name] = {
931
+ type: "one",
932
+ foreignTable: relation.foreignTable,
933
+ fields: relation.config?.fields,
934
+ references: relation.config?.references
935
+ };
936
+ } else if (relation instanceof ManyRelation) {
937
+ table.relations[name] = {
938
+ type: "many",
939
+ foreignTable: relation.foreignTable
940
+ };
941
+ }
942
+ }
943
+ return builtRelations;
962
944
  };
963
945
  var getTableColumns = (table) => {
964
946
  return table._.columns;
package/dist/index.mjs CHANGED
@@ -229,9 +229,18 @@ var SelectQueryBuilder = class extends BaseQueryBuilder {
229
229
  return rawResults;
230
230
  }
231
231
  processRelationResults(rawResults) {
232
- if (!rawResults.length) return rawResults;
233
- return rawResults.map((row) => {
234
- const result = {};
232
+ if (!rawResults.length) return [];
233
+ const mainTablePks = Object.values(this.table._.columns).filter((c) => c.options.primaryKey).map((c) => c._.name);
234
+ if (mainTablePks.length === 0) {
235
+ return rawResults;
236
+ }
237
+ const groupedResults = /* @__PURE__ */ new Map();
238
+ for (const row of rawResults) {
239
+ const mainTableKey = mainTablePks.map((pk) => row[`${this.selectedTableAlias}.${pk}`] ?? row[pk]).join("_");
240
+ if (!groupedResults.has(mainTableKey)) {
241
+ groupedResults.set(mainTableKey, {});
242
+ }
243
+ const result = groupedResults.get(mainTableKey);
235
244
  const relations2 = {};
236
245
  for (const [key, value] of Object.entries(row)) {
237
246
  if (key.includes(".")) {
@@ -254,15 +263,25 @@ var SelectQueryBuilder = class extends BaseQueryBuilder {
254
263
  }
255
264
  }
256
265
  for (const [relName, relData] of Object.entries(relations2)) {
266
+ const relationConfig = this.table.relations[relName];
267
+ if (!relationConfig) continue;
257
268
  const hasData = Object.values(relData).some(
258
269
  (v) => v !== null && v !== void 0 && v !== ""
259
270
  );
260
- if (hasData) {
271
+ if (!hasData) continue;
272
+ if (relationConfig.type === "many") {
273
+ if (!result[relName]) result[relName] = [];
274
+ const relatedPks = Object.values(relationConfig.foreignTable._.columns).filter((c) => c.options.primaryKey).map((c) => c._.name);
275
+ const relDataKey = relatedPks.map((pk) => relData[pk]).join("_");
276
+ if (relatedPks.length === 0 || !result[relName].some((r) => relatedPks.map((pk) => r[pk]).join("_") === relDataKey)) {
277
+ result[relName].push(relData);
278
+ }
279
+ } else {
261
280
  result[relName] = relData;
262
281
  }
263
282
  }
264
- return result;
265
- });
283
+ }
284
+ return Array.from(groupedResults.values());
266
285
  }
267
286
  // Update the return type signatures
268
287
  async all() {
@@ -559,28 +578,13 @@ var SQLiteColumn = class _SQLiteColumn {
559
578
  }
560
579
  _;
561
580
  notNull() {
562
- return new _SQLiteColumn(
563
- this._.name,
564
- this.type,
565
- { ...this.options, notNull: true },
566
- this._.mode
567
- );
581
+ return new _SQLiteColumn(this._.name, this.type, { ...this.options, notNull: true }, this._.mode);
568
582
  }
569
583
  default(value) {
570
- return new _SQLiteColumn(
571
- this._.name,
572
- this.type,
573
- { ...this.options, default: value },
574
- this._.mode
575
- );
584
+ return new _SQLiteColumn(this._.name, this.type, { ...this.options, default: value }, this._.mode);
576
585
  }
577
586
  $defaultFn(fn) {
578
- return new _SQLiteColumn(
579
- this._.name,
580
- this.type,
581
- { ...this.options, $defaultFn: fn },
582
- this._.mode
583
- );
587
+ return new _SQLiteColumn(this._.name, this.type, { ...this.options, $defaultFn: fn }, this._.mode);
584
588
  }
585
589
  primaryKey() {
586
590
  return new _SQLiteColumn(
@@ -591,20 +595,10 @@ var SQLiteColumn = class _SQLiteColumn {
591
595
  );
592
596
  }
593
597
  autoincrement() {
594
- return new _SQLiteColumn(
595
- this._.name,
596
- this.type,
597
- { ...this.options, autoincrement: true },
598
- this._.mode
599
- );
598
+ return new _SQLiteColumn(this._.name, this.type, { ...this.options, autoincrement: true }, this._.mode);
600
599
  }
601
600
  unique() {
602
- return new _SQLiteColumn(
603
- this._.name,
604
- this.type,
605
- { ...this.options, unique: true },
606
- this._.mode
607
- );
601
+ return new _SQLiteColumn(this._.name, this.type, { ...this.options, unique: true }, this._.mode);
608
602
  }
609
603
  references(ref, column) {
610
604
  return new _SQLiteColumn(
@@ -621,12 +615,7 @@ var SQLiteColumn = class _SQLiteColumn {
621
615
  );
622
616
  }
623
617
  $onUpdateFn(fn) {
624
- return new _SQLiteColumn(
625
- this._.name,
626
- this.type,
627
- { ...this.options, $onUpdateFn: fn },
628
- this._.mode
629
- );
618
+ return new _SQLiteColumn(this._.name, this.type, { ...this.options, $onUpdateFn: fn }, this._.mode);
630
619
  }
631
620
  as(alias2) {
632
621
  return this;
@@ -682,29 +671,6 @@ var TauriORM = class {
682
671
  this.tables.set(value._.name, value);
683
672
  }
684
673
  }
685
- for (const [key, value] of Object.entries(schema)) {
686
- if (!(value instanceof Table) && typeof value === "object") {
687
- const tableName = key.replace("Relations", "");
688
- const table = Array.from(this.tables.values()).find((t) => t._.name === tableName);
689
- if (table) {
690
- for (const [relName, rel] of Object.entries(value)) {
691
- if (rel instanceof OneRelation) {
692
- table.relations[relName] = {
693
- type: "one",
694
- foreignTable: rel.foreignTable,
695
- fields: rel.config?.fields,
696
- references: rel.config?.references
697
- };
698
- } else if (rel instanceof ManyRelation) {
699
- table.relations[relName] = {
700
- type: "many",
701
- foreignTable: rel.foreignTable
702
- };
703
- }
704
- }
705
- }
706
- }
707
- }
708
674
  }
709
675
  }
710
676
  tables = /* @__PURE__ */ new Map();
@@ -729,9 +695,7 @@ var TauriORM = class {
729
695
  }
730
696
  async migrate() {
731
697
  for (const table of this.tables.values()) {
732
- const existingTableInfo = await this.db.select(
733
- `PRAGMA table_info('${table._.name}')`
734
- );
698
+ const existingTableInfo = await this.db.select(`PRAGMA table_info('${table._.name}')`);
735
699
  if (existingTableInfo.length === 0) {
736
700
  const columnsSql = Object.values(table._.columns).map((col) => this.buildColumnDefinition(col)).join(", ");
737
701
  const createSql = `CREATE TABLE ${table._.name}
@@ -740,9 +704,7 @@ var TauriORM = class {
740
704
  )`;
741
705
  await this.db.execute(createSql);
742
706
  } else {
743
- const existingColumnNames = new Set(
744
- existingTableInfo.map((c) => c.name)
745
- );
707
+ const existingColumnNames = new Set(existingTableInfo.map((c) => c.name));
746
708
  for (const column of Object.values(table._.columns)) {
747
709
  if (!existingColumnNames.has(column._.name)) {
748
710
  const columnSql = this.buildColumnDefinition(column, true);
@@ -755,7 +717,14 @@ var TauriORM = class {
755
717
  }
756
718
  }
757
719
  select(table, columns) {
758
- return new SelectQueryBuilder(this.db, table, columns);
720
+ const internalTable = this.tables.get(table._.name);
721
+ if (!internalTable) {
722
+ console.warn(
723
+ `[Tauri-ORM] Table "${table._.name}" was not passed in the schema to the ORM constructor. Relations will not be available.`
724
+ );
725
+ return new SelectQueryBuilder(this.db, table, columns);
726
+ }
727
+ return new SelectQueryBuilder(this.db, internalTable, columns);
759
728
  }
760
729
  insert(table) {
761
730
  return new InsertQueryBuilder(this.db, table);
@@ -858,10 +827,7 @@ var TauriORM = class {
858
827
  const status = await this.isSchemaDirty();
859
828
  if (status.dirty) {
860
829
  await this.migrate();
861
- await this.setSchemaMeta(
862
- "schema_signature",
863
- this.computeModelSignature()
864
- );
830
+ await this.setSchemaMeta("schema_signature", this.computeModelSignature());
865
831
  return true;
866
832
  }
867
833
  return false;
@@ -883,15 +849,31 @@ var ManyRelation = class extends Relation {
883
849
  super(foreignTable);
884
850
  }
885
851
  };
886
- var relations = (_table, relationsCallback) => {
887
- return relationsCallback({
888
- one: (table, config) => {
889
- return new OneRelation(table, config);
852
+ var relations = (table, relationsCallback) => {
853
+ const builtRelations = relationsCallback({
854
+ one: (foreignTable, config) => {
855
+ return new OneRelation(foreignTable, config);
890
856
  },
891
- many: (table) => {
892
- return new ManyRelation(table);
857
+ many: (foreignTable) => {
858
+ return new ManyRelation(foreignTable);
893
859
  }
894
860
  });
861
+ for (const [name, relation] of Object.entries(builtRelations)) {
862
+ if (relation instanceof OneRelation) {
863
+ table.relations[name] = {
864
+ type: "one",
865
+ foreignTable: relation.foreignTable,
866
+ fields: relation.config?.fields,
867
+ references: relation.config?.references
868
+ };
869
+ } else if (relation instanceof ManyRelation) {
870
+ table.relations[name] = {
871
+ type: "many",
872
+ foreignTable: relation.foreignTable
873
+ };
874
+ }
875
+ }
876
+ return builtRelations;
895
877
  };
896
878
  var getTableColumns = (table) => {
897
879
  return table._.columns;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@type32/tauri-sqlite-orm",
3
- "version": "0.1.18-16",
3
+ "version": "0.1.18-18",
4
4
  "description": "A Drizzle-like ORM for Tauri v2's SQL JS API plugin.",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",