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

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.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,8 @@ 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
+ /** Lazy reference (Drizzle-style) - use getter to allow self-refs and forward refs */
290
+ references(getRef: () => AnySQLiteColumn, options?: {
283
291
  onDelete?: 'cascade' | 'set null' | 'set default' | 'restrict' | 'no action';
284
292
  onUpdate?: 'cascade' | 'set null' | 'set default' | 'restrict' | 'no action';
285
293
  }): 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,8 @@ 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
+ /** Lazy reference (Drizzle-style) - use getter to allow self-refs and forward refs */
290
+ references(getRef: () => AnySQLiteColumn, options?: {
283
291
  onDelete?: 'cascade' | 'set null' | 'set default' | 'restrict' | 'no action';
284
292
  onUpdate?: 'cascade' | 'set null' | 'set default' | 'restrict' | 'no action';
285
293
  }): SQLiteColumn<TName, TType, TMode, TNotNull, THasDefault, TAutoincrement, TEnum, TCustomType>;
package/dist/index.js CHANGED
@@ -1254,17 +1254,20 @@ 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
+ /** Lazy reference (Drizzle-style) - use getter to allow self-refs and forward refs */
1258
+ references(getRef, options) {
1260
1259
  return new _SQLiteColumn(
1261
1260
  this._.name,
1262
1261
  this.type,
1263
1262
  {
1264
1263
  ...this.options,
1265
1264
  references: {
1266
- table: ref,
1267
- column: columnObj,
1265
+ getRef: () => {
1266
+ const column = getRef();
1267
+ const table = column.__table;
1268
+ if (!table) throw new Error(`Column ${column._?.name} has no __table - ensure it belongs to a table created with sqliteTable()`);
1269
+ return { table, column };
1270
+ },
1268
1271
  onDelete: options?.onDelete,
1269
1272
  onUpdate: options?.onUpdate
1270
1273
  },
@@ -1293,7 +1296,17 @@ var Table = class {
1293
1296
  }
1294
1297
  };
1295
1298
  var sqliteTable = (tableName, columns) => {
1296
- return new Table(tableName, columns);
1299
+ const table = new Table(tableName, columns);
1300
+ for (const col of Object.values(columns)) {
1301
+ col.__table = table;
1302
+ }
1303
+ for (const key of Object.keys(columns)) {
1304
+ Object.defineProperty(table, key, {
1305
+ get: () => table._.columns[key],
1306
+ enumerable: true
1307
+ });
1308
+ }
1309
+ return table;
1297
1310
  };
1298
1311
  var asc = (column) => import_kysely5.sql`${import_kysely5.sql.ref(column._.name)} ASC`;
1299
1312
  var desc = (column) => import_kysely5.sql`${import_kysely5.sql.ref(column._.name)} DESC`;
@@ -1326,13 +1339,11 @@ var TauriORM = class {
1326
1339
  sql6 += ` DEFAULT ${typeof value === "string" ? `'${value.replace(/'/g, "''")}'` : value}`;
1327
1340
  }
1328
1341
  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
- }
1342
+ const ref = "getRef" in col.options.references ? col.options.references.getRef() : col.options.references;
1343
+ sql6 += ` REFERENCES ${ref.table._.name}(${ref.column._.name})`;
1344
+ const opts = col.options.references;
1345
+ if (opts.onDelete) sql6 += ` ON DELETE ${opts.onDelete.toUpperCase()}`;
1346
+ if (opts.onUpdate) sql6 += ` ON UPDATE ${opts.onUpdate.toUpperCase()}`;
1336
1347
  }
1337
1348
  return sql6;
1338
1349
  }
package/dist/index.mjs CHANGED
@@ -1158,17 +1158,20 @@ 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
+ /** Lazy reference (Drizzle-style) - use getter to allow self-refs and forward refs */
1162
+ references(getRef, options) {
1164
1163
  return new _SQLiteColumn(
1165
1164
  this._.name,
1166
1165
  this.type,
1167
1166
  {
1168
1167
  ...this.options,
1169
1168
  references: {
1170
- table: ref,
1171
- column: columnObj,
1169
+ getRef: () => {
1170
+ const column = getRef();
1171
+ const table = column.__table;
1172
+ if (!table) throw new Error(`Column ${column._?.name} has no __table - ensure it belongs to a table created with sqliteTable()`);
1173
+ return { table, column };
1174
+ },
1172
1175
  onDelete: options?.onDelete,
1173
1176
  onUpdate: options?.onUpdate
1174
1177
  },
@@ -1197,7 +1200,17 @@ var Table = class {
1197
1200
  }
1198
1201
  };
1199
1202
  var sqliteTable = (tableName, columns) => {
1200
- return new Table(tableName, columns);
1203
+ const table = new Table(tableName, columns);
1204
+ for (const col of Object.values(columns)) {
1205
+ col.__table = table;
1206
+ }
1207
+ for (const key of Object.keys(columns)) {
1208
+ Object.defineProperty(table, key, {
1209
+ get: () => table._.columns[key],
1210
+ enumerable: true
1211
+ });
1212
+ }
1213
+ return table;
1201
1214
  };
1202
1215
  var asc = (column) => kyselySql`${kyselySql.ref(column._.name)} ASC`;
1203
1216
  var desc = (column) => kyselySql`${kyselySql.ref(column._.name)} DESC`;
@@ -1230,13 +1243,11 @@ var TauriORM = class {
1230
1243
  sql6 += ` DEFAULT ${typeof value === "string" ? `'${value.replace(/'/g, "''")}'` : value}`;
1231
1244
  }
1232
1245
  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
- }
1246
+ const ref = "getRef" in col.options.references ? col.options.references.getRef() : col.options.references;
1247
+ sql6 += ` REFERENCES ${ref.table._.name}(${ref.column._.name})`;
1248
+ const opts = col.options.references;
1249
+ if (opts.onDelete) sql6 += ` ON DELETE ${opts.onDelete.toUpperCase()}`;
1250
+ if (opts.onUpdate) sql6 += ` ON UPDATE ${opts.onUpdate.toUpperCase()}`;
1240
1251
  }
1241
1252
  return sql6;
1242
1253
  }
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-1",
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",