@type32/tauri-sqlite-orm 0.4.1-0 → 0.4.1-2

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/README.md CHANGED
@@ -47,7 +47,7 @@ const posts = sqliteTable('posts', {
47
47
  id: integer('id').primaryKey().autoincrement(),
48
48
  title: text('title').notNull(),
49
49
  content: text('content').notNull(),
50
- userId: integer('user_id').notNull().references(users, users._.columns.id, { onDelete: 'cascade' }),
50
+ userId: integer('user_id').notNull().references(() => users._.columns.id, { onDelete: 'cascade' }),
51
51
  })
52
52
 
53
53
  // Define relations
package/dist/index.d.mts CHANGED
@@ -37,6 +37,13 @@ interface ColumnOptions<TData, TEnum extends readonly string[] = readonly string
37
37
  column: AnySQLiteColumn;
38
38
  onDelete?: 'cascade' | 'set null' | 'set default' | 'restrict' | 'no action';
39
39
  onUpdate?: 'cascade' | 'set null' | 'set default' | 'restrict' | 'no action';
40
+ } | {
41
+ getRef: () => {
42
+ table: AnyTable;
43
+ column: AnySQLiteColumn;
44
+ };
45
+ onDelete?: 'cascade' | 'set null' | 'set default' | 'restrict' | 'no action';
46
+ onUpdate?: 'cascade' | 'set null' | 'set default' | 'restrict' | 'no action';
40
47
  };
41
48
  mode?: Mode;
42
49
  $onUpdateFn?: () => TData;
@@ -279,7 +286,13 @@ declare class SQLiteColumn<TName extends string = string, TType extends ColumnDa
279
286
  primaryKey(): SQLiteColumn<TName, TType, TMode, true, THasDefault, TAutoincrement, TEnum, TCustomType>;
280
287
  autoincrement(): SQLiteColumn<TName, TType, TMode, TNotNull, THasDefault, true, TEnum, TCustomType>;
281
288
  unique(): SQLiteColumn<TName, TType, TMode, TNotNull, THasDefault, TAutoincrement, TEnum, TCustomType>;
282
- references<T extends AnyTable, K extends keyof T['_']['columns'] & string>(ref: T, column: K | T['_']['columns'][K], options?: {
289
+ /**
290
+ * Lazy reference (Drizzle-style) - use getter to allow self-refs and forward refs.
291
+ * Use table._.columns.columnName (e.g. references(() => users._.columns.id)).
292
+ * For self-references, add explicit return type to fix TS7022/TS7024:
293
+ * references((): AnySQLiteColumn => messages._.columns.id)
294
+ */
295
+ references(getRef: () => AnySQLiteColumn, options?: {
283
296
  onDelete?: 'cascade' | 'set null' | 'set default' | 'restrict' | 'no action';
284
297
  onUpdate?: 'cascade' | 'set null' | 'set default' | 'restrict' | 'no action';
285
298
  }): SQLiteColumn<TName, TType, TMode, TNotNull, THasDefault, TAutoincrement, TEnum, TCustomType>;
package/dist/index.d.ts CHANGED
@@ -37,6 +37,13 @@ interface ColumnOptions<TData, TEnum extends readonly string[] = readonly string
37
37
  column: AnySQLiteColumn;
38
38
  onDelete?: 'cascade' | 'set null' | 'set default' | 'restrict' | 'no action';
39
39
  onUpdate?: 'cascade' | 'set null' | 'set default' | 'restrict' | 'no action';
40
+ } | {
41
+ getRef: () => {
42
+ table: AnyTable;
43
+ column: AnySQLiteColumn;
44
+ };
45
+ onDelete?: 'cascade' | 'set null' | 'set default' | 'restrict' | 'no action';
46
+ onUpdate?: 'cascade' | 'set null' | 'set default' | 'restrict' | 'no action';
40
47
  };
41
48
  mode?: Mode;
42
49
  $onUpdateFn?: () => TData;
@@ -279,7 +286,13 @@ declare class SQLiteColumn<TName extends string = string, TType extends ColumnDa
279
286
  primaryKey(): SQLiteColumn<TName, TType, TMode, true, THasDefault, TAutoincrement, TEnum, TCustomType>;
280
287
  autoincrement(): SQLiteColumn<TName, TType, TMode, TNotNull, THasDefault, true, TEnum, TCustomType>;
281
288
  unique(): SQLiteColumn<TName, TType, TMode, TNotNull, THasDefault, TAutoincrement, TEnum, TCustomType>;
282
- references<T extends AnyTable, K extends keyof T['_']['columns'] & string>(ref: T, column: K | T['_']['columns'][K], options?: {
289
+ /**
290
+ * Lazy reference (Drizzle-style) - use getter to allow self-refs and forward refs.
291
+ * Use table._.columns.columnName (e.g. references(() => users._.columns.id)).
292
+ * For self-references, add explicit return type to fix TS7022/TS7024:
293
+ * references((): AnySQLiteColumn => messages._.columns.id)
294
+ */
295
+ references(getRef: () => AnySQLiteColumn, options?: {
283
296
  onDelete?: 'cascade' | 'set null' | 'set default' | 'restrict' | 'no action';
284
297
  onUpdate?: 'cascade' | 'set null' | 'set default' | 'restrict' | 'no action';
285
298
  }): SQLiteColumn<TName, TType, TMode, TNotNull, THasDefault, TAutoincrement, TEnum, TCustomType>;
package/dist/index.js CHANGED
@@ -1254,17 +1254,25 @@ var SQLiteColumn = class _SQLiteColumn {
1254
1254
  unique() {
1255
1255
  return new _SQLiteColumn(this._.name, this.type, { ...this.options, unique: true, mode: this._.mode });
1256
1256
  }
1257
- references(ref, column, options) {
1258
- const columnKey = typeof column === "string" ? column : column._.name;
1259
- const columnObj = typeof column === "string" ? ref._.columns[column] : column;
1257
+ /**
1258
+ * Lazy reference (Drizzle-style) - use getter to allow self-refs and forward refs.
1259
+ * Use table._.columns.columnName (e.g. references(() => users._.columns.id)).
1260
+ * For self-references, add explicit return type to fix TS7022/TS7024:
1261
+ * references((): AnySQLiteColumn => messages._.columns.id)
1262
+ */
1263
+ references(getRef, options) {
1260
1264
  return new _SQLiteColumn(
1261
1265
  this._.name,
1262
1266
  this.type,
1263
1267
  {
1264
1268
  ...this.options,
1265
1269
  references: {
1266
- table: ref,
1267
- column: columnObj,
1270
+ getRef: () => {
1271
+ const column = getRef();
1272
+ const table = column.__table;
1273
+ if (!table) throw new Error(`Column ${column._?.name} has no __table - ensure it belongs to a table created with sqliteTable()`);
1274
+ return { table, column };
1275
+ },
1268
1276
  onDelete: options?.onDelete,
1269
1277
  onUpdate: options?.onUpdate
1270
1278
  },
@@ -1293,7 +1301,11 @@ var Table = class {
1293
1301
  }
1294
1302
  };
1295
1303
  var sqliteTable = (tableName, columns) => {
1296
- return new Table(tableName, columns);
1304
+ const table = new Table(tableName, columns);
1305
+ for (const col of Object.values(columns)) {
1306
+ col.__table = table;
1307
+ }
1308
+ return table;
1297
1309
  };
1298
1310
  var asc = (column) => import_kysely5.sql`${import_kysely5.sql.ref(column._.name)} ASC`;
1299
1311
  var desc = (column) => import_kysely5.sql`${import_kysely5.sql.ref(column._.name)} DESC`;
@@ -1326,13 +1338,11 @@ var TauriORM = class {
1326
1338
  sql6 += ` DEFAULT ${typeof value === "string" ? `'${value.replace(/'/g, "''")}'` : value}`;
1327
1339
  }
1328
1340
  if (col.options.references) {
1329
- sql6 += ` REFERENCES ${col.options.references.table._.name}(${col.options.references.column._.name})`;
1330
- if (col.options.references.onDelete) {
1331
- sql6 += ` ON DELETE ${col.options.references.onDelete.toUpperCase()}`;
1332
- }
1333
- if (col.options.references.onUpdate) {
1334
- sql6 += ` ON UPDATE ${col.options.references.onUpdate.toUpperCase()}`;
1335
- }
1341
+ const ref = "getRef" in col.options.references ? col.options.references.getRef() : col.options.references;
1342
+ sql6 += ` REFERENCES ${ref.table._.name}(${ref.column._.name})`;
1343
+ const opts = col.options.references;
1344
+ if (opts.onDelete) sql6 += ` ON DELETE ${opts.onDelete.toUpperCase()}`;
1345
+ if (opts.onUpdate) sql6 += ` ON UPDATE ${opts.onUpdate.toUpperCase()}`;
1336
1346
  }
1337
1347
  return sql6;
1338
1348
  }
package/dist/index.mjs CHANGED
@@ -1158,17 +1158,25 @@ var SQLiteColumn = class _SQLiteColumn {
1158
1158
  unique() {
1159
1159
  return new _SQLiteColumn(this._.name, this.type, { ...this.options, unique: true, mode: this._.mode });
1160
1160
  }
1161
- references(ref, column, options) {
1162
- const columnKey = typeof column === "string" ? column : column._.name;
1163
- const columnObj = typeof column === "string" ? ref._.columns[column] : column;
1161
+ /**
1162
+ * Lazy reference (Drizzle-style) - use getter to allow self-refs and forward refs.
1163
+ * Use table._.columns.columnName (e.g. references(() => users._.columns.id)).
1164
+ * For self-references, add explicit return type to fix TS7022/TS7024:
1165
+ * references((): AnySQLiteColumn => messages._.columns.id)
1166
+ */
1167
+ references(getRef, options) {
1164
1168
  return new _SQLiteColumn(
1165
1169
  this._.name,
1166
1170
  this.type,
1167
1171
  {
1168
1172
  ...this.options,
1169
1173
  references: {
1170
- table: ref,
1171
- column: columnObj,
1174
+ getRef: () => {
1175
+ const column = getRef();
1176
+ const table = column.__table;
1177
+ if (!table) throw new Error(`Column ${column._?.name} has no __table - ensure it belongs to a table created with sqliteTable()`);
1178
+ return { table, column };
1179
+ },
1172
1180
  onDelete: options?.onDelete,
1173
1181
  onUpdate: options?.onUpdate
1174
1182
  },
@@ -1197,7 +1205,11 @@ var Table = class {
1197
1205
  }
1198
1206
  };
1199
1207
  var sqliteTable = (tableName, columns) => {
1200
- return new Table(tableName, columns);
1208
+ const table = new Table(tableName, columns);
1209
+ for (const col of Object.values(columns)) {
1210
+ col.__table = table;
1211
+ }
1212
+ return table;
1201
1213
  };
1202
1214
  var asc = (column) => kyselySql`${kyselySql.ref(column._.name)} ASC`;
1203
1215
  var desc = (column) => kyselySql`${kyselySql.ref(column._.name)} DESC`;
@@ -1230,13 +1242,11 @@ var TauriORM = class {
1230
1242
  sql6 += ` DEFAULT ${typeof value === "string" ? `'${value.replace(/'/g, "''")}'` : value}`;
1231
1243
  }
1232
1244
  if (col.options.references) {
1233
- sql6 += ` REFERENCES ${col.options.references.table._.name}(${col.options.references.column._.name})`;
1234
- if (col.options.references.onDelete) {
1235
- sql6 += ` ON DELETE ${col.options.references.onDelete.toUpperCase()}`;
1236
- }
1237
- if (col.options.references.onUpdate) {
1238
- sql6 += ` ON UPDATE ${col.options.references.onUpdate.toUpperCase()}`;
1239
- }
1245
+ const ref = "getRef" in col.options.references ? col.options.references.getRef() : col.options.references;
1246
+ sql6 += ` REFERENCES ${ref.table._.name}(${ref.column._.name})`;
1247
+ const opts = col.options.references;
1248
+ if (opts.onDelete) sql6 += ` ON DELETE ${opts.onDelete.toUpperCase()}`;
1249
+ if (opts.onUpdate) sql6 += ` ON UPDATE ${opts.onUpdate.toUpperCase()}`;
1240
1250
  }
1241
1251
  return sql6;
1242
1252
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@type32/tauri-sqlite-orm",
3
- "version": "0.4.1-0",
3
+ "version": "0.4.1-2",
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",