@type32/tauri-sqlite-orm 0.1.18-7 → 0.1.18-8

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
@@ -16,159 +16,3 @@ bun add @type32/tauri-sqlite-orm @tauri-apps/plugin-sql
16
16
  ```
17
17
 
18
18
  Make sure the SQL plugin is registered on the Rust side (see Tauri docs).
19
-
20
- ### Quick Start
21
-
22
- Here’s a quick example to get you started:
23
-
24
- ```typescript
25
- // src/db/schema.ts
26
- import { sqliteTable, text, integer } from "@type32/tauri-sqlite-orm";
27
-
28
- export const users = sqliteTable("users", {
29
- id: integer("id").primaryKey().autoincrement(),
30
- name: text("name").notNull(),
31
- email: text("email").unique(),
32
- });
33
-
34
- export const posts = sqliteTable("posts", {
35
- id: integer("id").primaryKey(),
36
- content: text("content"),
37
- authorId: integer("author_id").references(() => users.id),
38
- });
39
-
40
- // src/db/index.ts
41
- import { TauriORM } from "@type32/tauri-sqlite-orm";
42
- import Database from "@tauri-apps/plugin-sql";
43
- import * as schema from "./schema";
44
-
45
- // Load the database
46
- const dbInstance = await Database.load("sqlite:app.db");
47
-
48
- // Create the ORM instance
49
- export const db = new TauriORM(dbInstance, schema);
50
-
51
- // Migrate the database if the schema has changed
52
- await db.migrateIfDirty();
53
-
54
- // Now you can use the ORM to interact with your database
55
- const newUser = await db
56
- .insert(schema.users)
57
- .values({ name: "John Doe", email: "john.doe@example.com" });
58
- const allUsers = await db.select(schema.users).execute();
59
- ```
60
-
61
- ### Documentation
62
-
63
- - [Getting Started](docs/getting-started.md)
64
- - [Schema and Types](docs/schema-and-types.md)
65
- - [CRUD Operations (SELECT)](docs/queries-select.md)
66
- - [CRUD Operations (INSERT)](docs/crud-insert.md)
67
- - [CRUD Operations (UPDATE)](docs/crud-update.md)
68
- - [CRUD Operations (DELETE)](docs/crud-delete.md)
69
- - [Migrations](docs/migrations.md)
70
- - [Transactions](docs/transactions.md)
71
- - [Relations](docs/relations.md)
72
-
73
- ### Schema Definition
74
-
75
- Define your tables and columns using a chainable, Drizzle-style API.
76
-
77
- ```typescript
78
- import { sqliteTable, text, integer, boolean } from "@type32/tauri-sqlite-orm";
79
-
80
- export const users = sqliteTable("users", {
81
- id: integer("id").primaryKey().autoincrement(),
82
- name: text("name").notNull(),
83
- email: text("email").unique(),
84
- isActive: boolean("is_active").default(true),
85
- createdAt: integer("created_at", { mode: "timestamp" }).$defaultFn(
86
- () => new Date()
87
- ),
88
- });
89
- ```
90
-
91
- ### CRUD Operations
92
-
93
- Perform `CREATE`, `READ`, `UPDATE`, and `DELETE` operations using a type-safe query builder.
94
-
95
- **SELECT**
96
-
97
- ```typescript
98
- import { eq, and } from "@type32/tauri-sqlite-orm";
99
-
100
- // Select all users
101
- const allUsers = await db.select(users).execute();
102
-
103
- // Select specific columns
104
- const userNames = await db.select(users, ["name"]).execute();
105
-
106
- // Use WHERE conditions
107
- const activeUsers = await db
108
- .select(users)
109
- .where(eq(users.isActive, true))
110
- .execute();
111
- ```
112
-
113
- **INSERT**
114
-
115
- ```typescript
116
- // Insert a single user
117
- const newUser = await db
118
- .insert(users)
119
- .values({ name: "Jane Doe", email: "jane.doe@example.com" });
120
-
121
- // Insert multiple users
122
- await db.insert(users).values([
123
- { name: "Alice", email: "alice@example.com" },
124
- { name: "Bob", email: "bob@example.com" },
125
- ]);
126
- ```
127
-
128
- **UPDATE**
129
-
130
- ```typescript
131
- import { eq } from "@type32/tauri-sqlite-orm";
132
-
133
- // Update a user's email
134
- await db
135
- .update(users)
136
- .set({ email: "new.email@example.com" })
137
- .where(eq(users._.columns.id, 1));
138
- ```
139
-
140
- **DELETE**
141
-
142
- ```typescript
143
- import { eq } from "@type32/tauri-sqlite-orm";
144
-
145
- // Delete a user
146
- await db.delete(users).where(eq(users._.columns.id, 1));
147
- ```
148
-
149
- ### Migrations
150
-
151
- The ORM includes a simple migration system that automatically detects schema changes and applies them to the database.
152
-
153
- ```typescript
154
- // This will check if the schema has changed and run migrations if it has
155
- await db.migrateIfDirty();
156
-
157
- // You can also run migrations manually
158
- await db.migrate();
159
- ```
160
-
161
- ### Transactions
162
-
163
- Run multiple database operations within a transaction to ensure atomicity.
164
-
165
- ```typescript
166
- await db.transaction(async (tx) => {
167
- await tx.insert(users).values({ name: "From Transaction" });
168
- await tx.delete(users).where(eq(users._.columns.id, 1));
169
- });
170
- ```
171
-
172
- ### License
173
-
174
- MIT
package/dist/index.d.mts CHANGED
@@ -194,7 +194,7 @@ declare class WithQueryBuilder {
194
194
  declare class TauriORM {
195
195
  private db;
196
196
  private tables;
197
- constructor(db: Database, schema?: Record<string, AnyTable> | undefined);
197
+ constructor(db: Database, schema?: Record<string, AnyTable | Record<string, Relation>> | undefined);
198
198
  private buildColumnDefinition;
199
199
  migrate(): Promise<void>;
200
200
  select<T extends AnyTable, C extends (keyof T["_"]["columns"])[] | undefined = undefined>(table: T, columns?: C): SelectQueryBuilder<T, C>;
@@ -222,11 +222,32 @@ declare class TauriORM {
222
222
  }>;
223
223
  migrateIfDirty(): Promise<boolean>;
224
224
  }
225
- declare const relations: <T extends AnyTable, R extends Record<string, any>>(table: T, relationsCallback: (helpers: {
226
- one: any;
227
- many: any;
228
- }) => R) => R;
225
+ declare class Relation<T extends AnyTable = AnyTable> {
226
+ foreignTable: T;
227
+ constructor(foreignTable: T);
228
+ }
229
+ declare class OneRelation<T extends AnyTable = AnyTable> extends Relation<T> {
230
+ config?: {
231
+ fields: AnySQLiteColumn[];
232
+ references: AnySQLiteColumn[];
233
+ } | undefined;
234
+ constructor(foreignTable: T, config?: {
235
+ fields: AnySQLiteColumn[];
236
+ references: AnySQLiteColumn[];
237
+ } | undefined);
238
+ }
239
+ declare class ManyRelation<T extends AnyTable = AnyTable> extends Relation<T> {
240
+ constructor(foreignTable: T);
241
+ }
242
+ type RelationsBuilder = {
243
+ one: <U extends AnyTable>(table: U, config?: {
244
+ fields: AnySQLiteColumn[];
245
+ references: AnySQLiteColumn[];
246
+ }) => OneRelation<U>;
247
+ many: <U extends AnyTable>(table: U) => ManyRelation<U>;
248
+ };
249
+ declare const relations: <T extends AnyTable, R extends Record<string, Relation>>(_table: T, relationsCallback: (helpers: RelationsBuilder) => R) => R;
229
250
  declare const getTableColumns: <T extends AnyTable>(table: T) => Record<string, AnySQLiteColumn>;
230
251
  declare const alias: <T extends AnyTable>(table: T, alias: string) => Table<T["_"]["columns"], T["_"]["name"]>;
231
252
 
232
- export { type AnySQLiteColumn, type AnyTable, type ColumnDataType, type ColumnOptions, DeleteQueryBuilder, type InferInsertModel, type InferSelectModel, InsertQueryBuilder, type Mode, type SQLCondition, SQLiteColumn, SelectQueryBuilder, Table, TauriORM, UpdateQueryBuilder, WithQueryBuilder, alias, and, asc, avg, blob, boolean, count, countDistinct, desc, eq, getTableColumns, gt, gte, inArray, integer, isNotNull, isNull, like, lt, lte, max, min, not, or, real, relations, sql, sqliteTable, sum, text };
253
+ export { type AnySQLiteColumn, type AnyTable, type ColumnDataType, type ColumnOptions, DeleteQueryBuilder, type InferInsertModel, type InferSelectModel, InsertQueryBuilder, ManyRelation, type Mode, OneRelation, Relation, type SQLCondition, SQLiteColumn, SelectQueryBuilder, Table, TauriORM, UpdateQueryBuilder, WithQueryBuilder, alias, and, asc, avg, blob, boolean, count, countDistinct, desc, eq, getTableColumns, gt, gte, inArray, integer, isNotNull, isNull, like, lt, lte, max, min, not, or, real, relations, sql, sqliteTable, sum, text };
package/dist/index.d.ts CHANGED
@@ -194,7 +194,7 @@ declare class WithQueryBuilder {
194
194
  declare class TauriORM {
195
195
  private db;
196
196
  private tables;
197
- constructor(db: Database, schema?: Record<string, AnyTable> | undefined);
197
+ constructor(db: Database, schema?: Record<string, AnyTable | Record<string, Relation>> | undefined);
198
198
  private buildColumnDefinition;
199
199
  migrate(): Promise<void>;
200
200
  select<T extends AnyTable, C extends (keyof T["_"]["columns"])[] | undefined = undefined>(table: T, columns?: C): SelectQueryBuilder<T, C>;
@@ -222,11 +222,32 @@ declare class TauriORM {
222
222
  }>;
223
223
  migrateIfDirty(): Promise<boolean>;
224
224
  }
225
- declare const relations: <T extends AnyTable, R extends Record<string, any>>(table: T, relationsCallback: (helpers: {
226
- one: any;
227
- many: any;
228
- }) => R) => R;
225
+ declare class Relation<T extends AnyTable = AnyTable> {
226
+ foreignTable: T;
227
+ constructor(foreignTable: T);
228
+ }
229
+ declare class OneRelation<T extends AnyTable = AnyTable> extends Relation<T> {
230
+ config?: {
231
+ fields: AnySQLiteColumn[];
232
+ references: AnySQLiteColumn[];
233
+ } | undefined;
234
+ constructor(foreignTable: T, config?: {
235
+ fields: AnySQLiteColumn[];
236
+ references: AnySQLiteColumn[];
237
+ } | undefined);
238
+ }
239
+ declare class ManyRelation<T extends AnyTable = AnyTable> extends Relation<T> {
240
+ constructor(foreignTable: T);
241
+ }
242
+ type RelationsBuilder = {
243
+ one: <U extends AnyTable>(table: U, config?: {
244
+ fields: AnySQLiteColumn[];
245
+ references: AnySQLiteColumn[];
246
+ }) => OneRelation<U>;
247
+ many: <U extends AnyTable>(table: U) => ManyRelation<U>;
248
+ };
249
+ declare const relations: <T extends AnyTable, R extends Record<string, Relation>>(_table: T, relationsCallback: (helpers: RelationsBuilder) => R) => R;
229
250
  declare const getTableColumns: <T extends AnyTable>(table: T) => Record<string, AnySQLiteColumn>;
230
251
  declare const alias: <T extends AnyTable>(table: T, alias: string) => Table<T["_"]["columns"], T["_"]["name"]>;
231
252
 
232
- export { type AnySQLiteColumn, type AnyTable, type ColumnDataType, type ColumnOptions, DeleteQueryBuilder, type InferInsertModel, type InferSelectModel, InsertQueryBuilder, type Mode, type SQLCondition, SQLiteColumn, SelectQueryBuilder, Table, TauriORM, UpdateQueryBuilder, WithQueryBuilder, alias, and, asc, avg, blob, boolean, count, countDistinct, desc, eq, getTableColumns, gt, gte, inArray, integer, isNotNull, isNull, like, lt, lte, max, min, not, or, real, relations, sql, sqliteTable, sum, text };
253
+ export { type AnySQLiteColumn, type AnyTable, type ColumnDataType, type ColumnOptions, DeleteQueryBuilder, type InferInsertModel, type InferSelectModel, InsertQueryBuilder, ManyRelation, type Mode, OneRelation, Relation, type SQLCondition, SQLiteColumn, SelectQueryBuilder, Table, TauriORM, UpdateQueryBuilder, WithQueryBuilder, alias, and, asc, avg, blob, boolean, count, countDistinct, desc, eq, getTableColumns, gt, gte, inArray, integer, isNotNull, isNull, like, lt, lte, max, min, not, or, real, relations, sql, sqliteTable, sum, text };
package/dist/index.js CHANGED
@@ -22,6 +22,9 @@ var index_exports = {};
22
22
  __export(index_exports, {
23
23
  DeleteQueryBuilder: () => DeleteQueryBuilder,
24
24
  InsertQueryBuilder: () => InsertQueryBuilder,
25
+ ManyRelation: () => ManyRelation,
26
+ OneRelation: () => OneRelation,
27
+ Relation: () => Relation,
25
28
  SQLiteColumn: () => SQLiteColumn,
26
29
  SelectQueryBuilder: () => SelectQueryBuilder,
27
30
  Table: () => Table,
@@ -610,7 +613,9 @@ var TauriORM = class {
610
613
  this.db = db;
611
614
  if (schema) {
612
615
  for (const table of Object.values(schema)) {
613
- this.tables.set(table._.name, table);
616
+ if (table instanceof Table) {
617
+ this.tables.set(table._.name, table);
618
+ }
614
619
  }
615
620
  }
616
621
  }
@@ -755,18 +760,30 @@ var TauriORM = class {
755
760
  return false;
756
761
  }
757
762
  };
758
- var relations = (table, relationsCallback) => {
763
+ var Relation = class {
764
+ constructor(foreignTable) {
765
+ this.foreignTable = foreignTable;
766
+ }
767
+ };
768
+ var OneRelation = class extends Relation {
769
+ constructor(foreignTable, config) {
770
+ super(foreignTable);
771
+ this.config = config;
772
+ }
773
+ };
774
+ var ManyRelation = class extends Relation {
775
+ constructor(foreignTable) {
776
+ super(foreignTable);
777
+ }
778
+ };
779
+ var relations = (_table, relationsCallback) => {
759
780
  return relationsCallback({
760
- one: (table2, config) => ({
761
- table: table2,
762
- type: "one",
763
- foreignKey: config.fields[0],
764
- localKey: config.references[0]
765
- }),
766
- many: (table2) => ({
767
- table: table2,
768
- type: "many"
769
- })
781
+ one: (table, config) => {
782
+ return new OneRelation(table, config);
783
+ },
784
+ many: (table) => {
785
+ return new ManyRelation(table);
786
+ }
770
787
  });
771
788
  };
772
789
  var getTableColumns = (table) => {
@@ -779,6 +796,9 @@ var alias = (table, alias2) => {
779
796
  0 && (module.exports = {
780
797
  DeleteQueryBuilder,
781
798
  InsertQueryBuilder,
799
+ ManyRelation,
800
+ OneRelation,
801
+ Relation,
782
802
  SQLiteColumn,
783
803
  SelectQueryBuilder,
784
804
  Table,
package/dist/index.mjs CHANGED
@@ -547,7 +547,9 @@ var TauriORM = class {
547
547
  this.db = db;
548
548
  if (schema) {
549
549
  for (const table of Object.values(schema)) {
550
- this.tables.set(table._.name, table);
550
+ if (table instanceof Table) {
551
+ this.tables.set(table._.name, table);
552
+ }
551
553
  }
552
554
  }
553
555
  }
@@ -692,18 +694,30 @@ var TauriORM = class {
692
694
  return false;
693
695
  }
694
696
  };
695
- var relations = (table, relationsCallback) => {
697
+ var Relation = class {
698
+ constructor(foreignTable) {
699
+ this.foreignTable = foreignTable;
700
+ }
701
+ };
702
+ var OneRelation = class extends Relation {
703
+ constructor(foreignTable, config) {
704
+ super(foreignTable);
705
+ this.config = config;
706
+ }
707
+ };
708
+ var ManyRelation = class extends Relation {
709
+ constructor(foreignTable) {
710
+ super(foreignTable);
711
+ }
712
+ };
713
+ var relations = (_table, relationsCallback) => {
696
714
  return relationsCallback({
697
- one: (table2, config) => ({
698
- table: table2,
699
- type: "one",
700
- foreignKey: config.fields[0],
701
- localKey: config.references[0]
702
- }),
703
- many: (table2) => ({
704
- table: table2,
705
- type: "many"
706
- })
715
+ one: (table, config) => {
716
+ return new OneRelation(table, config);
717
+ },
718
+ many: (table) => {
719
+ return new ManyRelation(table);
720
+ }
707
721
  });
708
722
  };
709
723
  var getTableColumns = (table) => {
@@ -715,6 +729,9 @@ var alias = (table, alias2) => {
715
729
  export {
716
730
  DeleteQueryBuilder,
717
731
  InsertQueryBuilder,
732
+ ManyRelation,
733
+ OneRelation,
734
+ Relation,
718
735
  SQLiteColumn,
719
736
  SelectQueryBuilder,
720
737
  Table,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@type32/tauri-sqlite-orm",
3
- "version": "0.1.18-7",
3
+ "version": "0.1.18-8",
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",