@type32/tauri-sqlite-orm 0.1.0 → 0.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
@@ -5,7 +5,7 @@ A Drizzle-like TypeScript ORM tailored for Tauri v2's `@tauri-apps/plugin-sql` (
5
5
  ### Install
6
6
 
7
7
  ```bash
8
- pnpm add tauri-sqlite-orm @tauri-apps/plugin-sql
8
+ pnpm add @type32/tauri-sqlite-orm @tauri-apps/plugin-sql
9
9
  ```
10
10
 
11
11
  Make sure the SQL plugin is registered on the Rust side (see Tauri docs).
@@ -35,6 +35,7 @@ export const users = defineTable("users", {
35
35
  export const posts = defineTable("posts", {
36
36
  id: integer("id", { isPrimaryKey: true }).primaryKey(),
37
37
  content: text("content"),
38
+ randomId: text("random_id").$defaultFn(() => crypto.randomUUID()),
38
39
  authorId: integer("author_id"),
39
40
  });
40
41
 
package/dist/index.d.mts CHANGED
@@ -25,6 +25,7 @@ interface Column<T = any> {
25
25
  autoIncrement?: boolean;
26
26
  isNotNull?: boolean;
27
27
  defaultValue?: T | SQLExpression;
28
+ defaultFn?: () => any;
28
29
  references?: {
29
30
  table: string;
30
31
  column: string;
@@ -46,6 +47,7 @@ type ColumnBuilder<T> = Column<T> & {
46
47
  notNull: () => ColumnBuilder<T>;
47
48
  default: (value: T | SQLExpression) => ColumnBuilder<T>;
48
49
  $type: <U>() => ColumnBuilder<U>;
50
+ $defaultFn: (fn: () => any) => ColumnBuilder<T>;
49
51
  references: (target: () => Column<any>, actions?: {
50
52
  onDelete?: UpdateDeleteAction;
51
53
  onUpdate?: UpdateDeleteAction;
package/dist/index.d.ts CHANGED
@@ -25,6 +25,7 @@ interface Column<T = any> {
25
25
  autoIncrement?: boolean;
26
26
  isNotNull?: boolean;
27
27
  defaultValue?: T | SQLExpression;
28
+ defaultFn?: () => any;
28
29
  references?: {
29
30
  table: string;
30
31
  column: string;
@@ -46,6 +47,7 @@ type ColumnBuilder<T> = Column<T> & {
46
47
  notNull: () => ColumnBuilder<T>;
47
48
  default: (value: T | SQLExpression) => ColumnBuilder<T>;
48
49
  $type: <U>() => ColumnBuilder<U>;
50
+ $defaultFn: (fn: () => any) => ColumnBuilder<T>;
49
51
  references: (target: () => Column<any>, actions?: {
50
52
  onDelete?: UpdateDeleteAction;
51
53
  onUpdate?: UpdateDeleteAction;
package/dist/index.js CHANGED
@@ -95,6 +95,10 @@ function createColumn(params) {
95
95
  return col;
96
96
  };
97
97
  col.$type = () => col;
98
+ col.$defaultFn = (fn) => {
99
+ col.defaultFn = fn;
100
+ return col;
101
+ };
98
102
  col.references = (target, actions) => {
99
103
  const t = target();
100
104
  col.references = {
@@ -289,8 +293,15 @@ var TauriORM = class {
289
293
  async execute() {
290
294
  const db3 = getDb();
291
295
  for (const data of this._rows) {
292
- const keys = Object.keys(data);
293
- const values = Object.values(data);
296
+ const finalData = { ...data };
297
+ const schema = this._table._schema;
298
+ for (const [key, col] of Object.entries(schema)) {
299
+ if (finalData[key] === void 0 && col.defaultFn) {
300
+ finalData[key] = col.defaultFn();
301
+ }
302
+ }
303
+ const keys = Object.keys(finalData);
304
+ const values = Object.values(finalData);
294
305
  const placeholders = values.map(() => "?").join(", ");
295
306
  const query = `INSERT INTO ${this._table._tableName} (${keys.join(
296
307
  ", "
package/dist/index.mjs CHANGED
@@ -36,6 +36,10 @@ function createColumn(params) {
36
36
  return col;
37
37
  };
38
38
  col.$type = () => col;
39
+ col.$defaultFn = (fn) => {
40
+ col.defaultFn = fn;
41
+ return col;
42
+ };
39
43
  col.references = (target, actions) => {
40
44
  const t = target();
41
45
  col.references = {
@@ -230,8 +234,15 @@ var TauriORM = class {
230
234
  async execute() {
231
235
  const db3 = getDb();
232
236
  for (const data of this._rows) {
233
- const keys = Object.keys(data);
234
- const values = Object.values(data);
237
+ const finalData = { ...data };
238
+ const schema = this._table._schema;
239
+ for (const [key, col] of Object.entries(schema)) {
240
+ if (finalData[key] === void 0 && col.defaultFn) {
241
+ finalData[key] = col.defaultFn();
242
+ }
243
+ }
244
+ const keys = Object.keys(finalData);
245
+ const values = Object.values(finalData);
235
246
  const placeholders = values.map(() => "?").join(", ");
236
247
  const query = `INSERT INTO ${this._table._tableName} (${keys.join(
237
248
  ", "
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@type32/tauri-sqlite-orm",
3
- "version": "0.1.0",
3
+ "version": "0.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",