@type32/tauri-sqlite-orm 0.1.2 → 0.1.4

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
@@ -1,18 +1,5 @@
1
1
  import Database from '@tauri-apps/plugin-sql';
2
2
 
3
- /**
4
- * Initializes the database connection. This must be called once at the start of your application.
5
- * @param dbPath The path to the database file, e.g., 'sqlite:mydatabase.db'
6
- * @returns The database instance.
7
- */
8
- declare function initDb(dbPath: string): Promise<Database>;
9
- /**
10
- * Gets the singleton database instance.
11
- * @throws If `initDb` has not been called.
12
- * @returns The database instance.
13
- */
14
- declare function getDb(): Database;
15
-
16
3
  type UpdateDeleteAction = "cascade" | "restrict" | "no action" | "set null" | "set default";
17
4
  interface SQLExpression {
18
5
  raw: string;
@@ -26,6 +13,7 @@ interface Column<T = any> {
26
13
  isNotNull?: boolean;
27
14
  defaultValue?: T | SQLExpression;
28
15
  defaultFn?: () => any;
16
+ onUpdateFn?: () => any;
29
17
  references?: {
30
18
  table: string;
31
19
  column: string;
@@ -48,29 +36,49 @@ type ColumnBuilder<T> = Column<T> & {
48
36
  default: (value: T | SQLExpression) => ColumnBuilder<T>;
49
37
  $type: <U>() => ColumnBuilder<U>;
50
38
  $defaultFn: (fn: () => any) => ColumnBuilder<T>;
39
+ $default: (fn: () => any) => ColumnBuilder<T>;
40
+ $onUpdate: (fn: () => any) => ColumnBuilder<T>;
41
+ $onUpdateFn: (fn: () => any) => ColumnBuilder<T>;
51
42
  references: (target: () => Column<any>, actions?: {
52
43
  onDelete?: UpdateDeleteAction;
53
44
  onUpdate?: UpdateDeleteAction;
54
45
  }) => ColumnBuilder<T>;
55
46
  };
56
- declare function text<TEnum extends string>(name: string, config?: {
57
- isPrimaryKey?: boolean;
47
+ type TextConfig<TEnum extends string> = {
58
48
  enum?: readonly TEnum[];
59
- }): ColumnBuilder<TEnum extends string ? TEnum : string>;
49
+ mode?: "json";
50
+ };
51
+ declare function text<TEnum extends string>(name: string, config?: TextConfig<TEnum>): ColumnBuilder<TEnum extends string ? TEnum : string>;
52
+ declare function text<TEnum extends string>(config?: TextConfig<TEnum>): ColumnBuilder<TEnum extends string ? TEnum : string>;
53
+ type IntegerMode = "number" | "boolean" | "timestamp" | "timestamp_ms";
60
54
  declare function integer(name: string, config?: {
61
- isPrimaryKey?: boolean;
62
- mode?: "number" | "boolean" | "timestamp";
63
- autoIncrement?: boolean;
55
+ mode?: IntegerMode;
56
+ }): ColumnBuilder<number | boolean | Date>;
57
+ declare function integer(config?: {
58
+ mode?: IntegerMode;
64
59
  }): ColumnBuilder<number | boolean | Date>;
65
60
  declare function real(name: string): ColumnBuilder<number>;
61
+ declare function real(): ColumnBuilder<number>;
62
+ type BlobMode = "json" | "bigint" | "buffer";
66
63
  declare function blob(name: string, config?: {
67
- mode?: "json" | "bigint";
64
+ mode?: BlobMode;
65
+ }): ColumnBuilder<unknown | bigint | Uint8Array>;
66
+ declare function blob(config?: {
67
+ mode?: BlobMode;
68
68
  }): ColumnBuilder<unknown | bigint | Uint8Array>;
69
+ type NumericMode = "string" | "number" | "bigint";
69
70
  declare function numeric(name: string, config?: {
70
- mode?: "string" | "number" | "bigint";
71
+ mode?: NumericMode;
71
72
  }): ColumnBuilder<string | number | bigint>;
72
- declare const boolean: (name: string) => ColumnBuilder<boolean>;
73
- declare const timestamp: (name: string) => ColumnBuilder<Date>;
73
+ declare function numeric(config?: {
74
+ mode?: NumericMode;
75
+ }): ColumnBuilder<string | number | bigint>;
76
+ declare function boolean(name: string): ColumnBuilder<boolean>;
77
+ declare function boolean(): ColumnBuilder<boolean>;
78
+ declare function timestamp(name: string): ColumnBuilder<Date>;
79
+ declare function timestamp(): ColumnBuilder<Date>;
80
+ declare function increments(name: string): ColumnBuilder<number>;
81
+ declare function increments(): ColumnBuilder<number>;
74
82
  type SchemaDefinition = Record<string, Column<any>>;
75
83
  type InferModel<T extends SchemaDefinition> = {
76
84
  [K in keyof T]: T[K]["_dataType"];
@@ -103,7 +111,8 @@ declare class SelectQueryBuilder<T> {
103
111
  private _limit;
104
112
  private _offset;
105
113
  private _eager;
106
- constructor(fields?: Record<string, Column<any>>);
114
+ private _dbProvider;
115
+ constructor(dbProvider: () => Promise<Database>, fields?: Record<string, Column<any>>);
107
116
  from(table: Table<any>): this;
108
117
  where(...conditions: SQL[]): this;
109
118
  leftJoin(otherTable: Table<any>, on: SQL): this;
@@ -116,6 +125,9 @@ declare class TauriORM {
116
125
  query: Record<string, any>;
117
126
  private _tables;
118
127
  private _relations;
128
+ private _dbPromise;
129
+ constructor(dbUri: string);
130
+ private getDb;
119
131
  configureQuery(tables: Record<string, Table<any>>, relations: Record<string, Record<string, RelationConfig>>): void;
120
132
  select<T>(fields?: Record<string, Column<any>>): SelectQueryBuilder<T>;
121
133
  insert(table: Table<any>): {
@@ -154,8 +166,61 @@ declare class TauriORM {
154
166
  name?: string;
155
167
  track?: boolean;
156
168
  }): Promise<void>;
169
+ diffSchema(): Promise<{
170
+ extraTables: string[];
171
+ missingTables: string[];
172
+ tables: Record<string, {
173
+ missingColumns: string[];
174
+ extraColumns: string[];
175
+ changedColumns: Array<{
176
+ name: string;
177
+ diffs: {
178
+ type?: boolean;
179
+ pk?: boolean;
180
+ notNull?: boolean;
181
+ default?: boolean;
182
+ };
183
+ }>;
184
+ }>;
185
+ }>;
186
+ generate(): Promise<{
187
+ statements: string[];
188
+ }>;
189
+ migrateCli(opts?: {
190
+ name?: string;
191
+ track?: boolean;
192
+ }): Promise<void>;
193
+ push(opts?: {
194
+ dropExtraColumns?: boolean;
195
+ preserveData?: boolean;
196
+ }): Promise<void>;
197
+ pull(): Promise<Record<string, any>>;
198
+ studio(): Promise<{
199
+ driver: string;
200
+ path: any;
201
+ }>;
202
+ private ensureSchemaMeta;
203
+ private getSchemaMeta;
204
+ private setSchemaMeta;
205
+ private normalizeColumn;
206
+ private computeModelSignature;
207
+ isSchemaDirty(): Promise<{
208
+ dirty: boolean;
209
+ current: string;
210
+ stored: string | null;
211
+ }>;
212
+ migrateIfDirty(options?: {
213
+ name?: string;
214
+ track?: boolean;
215
+ }): Promise<boolean>;
216
+ pullSchema(): Promise<Record<string, any>>;
217
+ private buildCreateTableSQL;
218
+ private tableExists;
219
+ forcePush(options?: {
220
+ dropExtraColumns?: boolean;
221
+ preserveData?: boolean;
222
+ }): Promise<void>;
157
223
  }
158
- declare const db: TauriORM;
159
224
  type OneConfig = {
160
225
  fields?: Column[];
161
226
  references?: Column[];
@@ -184,12 +249,17 @@ type WithSpec = Record<string, boolean | {
184
249
  with?: WithSpec;
185
250
  columns?: string[];
186
251
  }>;
187
- declare function makeQueryAPI(tables: Record<string, Table<any>>, relDefs: Record<string, Record<string, RelationConfig>>): { [K in keyof typeof tables]: {
252
+ declare function makeQueryAPI(tables: Record<string, Table<any>>, relDefs: Record<string, Record<string, RelationConfig>>, dbProvider: () => Promise<Database>): { [K in keyof typeof tables]: {
188
253
  findMany: (opts?: {
189
254
  with?: WithSpec;
190
255
  join?: boolean;
191
256
  columns?: string[];
192
257
  }) => Promise<any[]>;
258
+ findFirst: (opts?: {
259
+ with?: WithSpec;
260
+ join?: boolean;
261
+ columns?: string[];
262
+ }) => Promise<any | null>;
193
263
  }; };
194
264
 
195
- export { type Column, type ManyRelation, type OneRelation, type SQL, type SQLExpression, type Table, TauriORM, type UpdateDeleteAction, asc, blob, boolean, db, defineTable, desc, eq, getDb, gt, gte, initDb, integer, like, lt, lte, makeQueryAPI, ne, numeric, real, relations, sql, text, timestamp };
265
+ export { type BlobMode, type Column, type ColumnBuilder, type IntegerMode, type ManyRelation, type NumericMode, type OneRelation, type SQL, type SQLExpression, type Table, TauriORM, type UpdateDeleteAction, asc, blob, boolean, defineTable, desc, eq, gt, gte, increments, integer, like, lt, lte, makeQueryAPI, ne, numeric, real, relations, sql, text, timestamp };
package/dist/index.d.ts CHANGED
@@ -1,18 +1,5 @@
1
1
  import Database from '@tauri-apps/plugin-sql';
2
2
 
3
- /**
4
- * Initializes the database connection. This must be called once at the start of your application.
5
- * @param dbPath The path to the database file, e.g., 'sqlite:mydatabase.db'
6
- * @returns The database instance.
7
- */
8
- declare function initDb(dbPath: string): Promise<Database>;
9
- /**
10
- * Gets the singleton database instance.
11
- * @throws If `initDb` has not been called.
12
- * @returns The database instance.
13
- */
14
- declare function getDb(): Database;
15
-
16
3
  type UpdateDeleteAction = "cascade" | "restrict" | "no action" | "set null" | "set default";
17
4
  interface SQLExpression {
18
5
  raw: string;
@@ -26,6 +13,7 @@ interface Column<T = any> {
26
13
  isNotNull?: boolean;
27
14
  defaultValue?: T | SQLExpression;
28
15
  defaultFn?: () => any;
16
+ onUpdateFn?: () => any;
29
17
  references?: {
30
18
  table: string;
31
19
  column: string;
@@ -48,29 +36,49 @@ type ColumnBuilder<T> = Column<T> & {
48
36
  default: (value: T | SQLExpression) => ColumnBuilder<T>;
49
37
  $type: <U>() => ColumnBuilder<U>;
50
38
  $defaultFn: (fn: () => any) => ColumnBuilder<T>;
39
+ $default: (fn: () => any) => ColumnBuilder<T>;
40
+ $onUpdate: (fn: () => any) => ColumnBuilder<T>;
41
+ $onUpdateFn: (fn: () => any) => ColumnBuilder<T>;
51
42
  references: (target: () => Column<any>, actions?: {
52
43
  onDelete?: UpdateDeleteAction;
53
44
  onUpdate?: UpdateDeleteAction;
54
45
  }) => ColumnBuilder<T>;
55
46
  };
56
- declare function text<TEnum extends string>(name: string, config?: {
57
- isPrimaryKey?: boolean;
47
+ type TextConfig<TEnum extends string> = {
58
48
  enum?: readonly TEnum[];
59
- }): ColumnBuilder<TEnum extends string ? TEnum : string>;
49
+ mode?: "json";
50
+ };
51
+ declare function text<TEnum extends string>(name: string, config?: TextConfig<TEnum>): ColumnBuilder<TEnum extends string ? TEnum : string>;
52
+ declare function text<TEnum extends string>(config?: TextConfig<TEnum>): ColumnBuilder<TEnum extends string ? TEnum : string>;
53
+ type IntegerMode = "number" | "boolean" | "timestamp" | "timestamp_ms";
60
54
  declare function integer(name: string, config?: {
61
- isPrimaryKey?: boolean;
62
- mode?: "number" | "boolean" | "timestamp";
63
- autoIncrement?: boolean;
55
+ mode?: IntegerMode;
56
+ }): ColumnBuilder<number | boolean | Date>;
57
+ declare function integer(config?: {
58
+ mode?: IntegerMode;
64
59
  }): ColumnBuilder<number | boolean | Date>;
65
60
  declare function real(name: string): ColumnBuilder<number>;
61
+ declare function real(): ColumnBuilder<number>;
62
+ type BlobMode = "json" | "bigint" | "buffer";
66
63
  declare function blob(name: string, config?: {
67
- mode?: "json" | "bigint";
64
+ mode?: BlobMode;
65
+ }): ColumnBuilder<unknown | bigint | Uint8Array>;
66
+ declare function blob(config?: {
67
+ mode?: BlobMode;
68
68
  }): ColumnBuilder<unknown | bigint | Uint8Array>;
69
+ type NumericMode = "string" | "number" | "bigint";
69
70
  declare function numeric(name: string, config?: {
70
- mode?: "string" | "number" | "bigint";
71
+ mode?: NumericMode;
71
72
  }): ColumnBuilder<string | number | bigint>;
72
- declare const boolean: (name: string) => ColumnBuilder<boolean>;
73
- declare const timestamp: (name: string) => ColumnBuilder<Date>;
73
+ declare function numeric(config?: {
74
+ mode?: NumericMode;
75
+ }): ColumnBuilder<string | number | bigint>;
76
+ declare function boolean(name: string): ColumnBuilder<boolean>;
77
+ declare function boolean(): ColumnBuilder<boolean>;
78
+ declare function timestamp(name: string): ColumnBuilder<Date>;
79
+ declare function timestamp(): ColumnBuilder<Date>;
80
+ declare function increments(name: string): ColumnBuilder<number>;
81
+ declare function increments(): ColumnBuilder<number>;
74
82
  type SchemaDefinition = Record<string, Column<any>>;
75
83
  type InferModel<T extends SchemaDefinition> = {
76
84
  [K in keyof T]: T[K]["_dataType"];
@@ -103,7 +111,8 @@ declare class SelectQueryBuilder<T> {
103
111
  private _limit;
104
112
  private _offset;
105
113
  private _eager;
106
- constructor(fields?: Record<string, Column<any>>);
114
+ private _dbProvider;
115
+ constructor(dbProvider: () => Promise<Database>, fields?: Record<string, Column<any>>);
107
116
  from(table: Table<any>): this;
108
117
  where(...conditions: SQL[]): this;
109
118
  leftJoin(otherTable: Table<any>, on: SQL): this;
@@ -116,6 +125,9 @@ declare class TauriORM {
116
125
  query: Record<string, any>;
117
126
  private _tables;
118
127
  private _relations;
128
+ private _dbPromise;
129
+ constructor(dbUri: string);
130
+ private getDb;
119
131
  configureQuery(tables: Record<string, Table<any>>, relations: Record<string, Record<string, RelationConfig>>): void;
120
132
  select<T>(fields?: Record<string, Column<any>>): SelectQueryBuilder<T>;
121
133
  insert(table: Table<any>): {
@@ -154,8 +166,61 @@ declare class TauriORM {
154
166
  name?: string;
155
167
  track?: boolean;
156
168
  }): Promise<void>;
169
+ diffSchema(): Promise<{
170
+ extraTables: string[];
171
+ missingTables: string[];
172
+ tables: Record<string, {
173
+ missingColumns: string[];
174
+ extraColumns: string[];
175
+ changedColumns: Array<{
176
+ name: string;
177
+ diffs: {
178
+ type?: boolean;
179
+ pk?: boolean;
180
+ notNull?: boolean;
181
+ default?: boolean;
182
+ };
183
+ }>;
184
+ }>;
185
+ }>;
186
+ generate(): Promise<{
187
+ statements: string[];
188
+ }>;
189
+ migrateCli(opts?: {
190
+ name?: string;
191
+ track?: boolean;
192
+ }): Promise<void>;
193
+ push(opts?: {
194
+ dropExtraColumns?: boolean;
195
+ preserveData?: boolean;
196
+ }): Promise<void>;
197
+ pull(): Promise<Record<string, any>>;
198
+ studio(): Promise<{
199
+ driver: string;
200
+ path: any;
201
+ }>;
202
+ private ensureSchemaMeta;
203
+ private getSchemaMeta;
204
+ private setSchemaMeta;
205
+ private normalizeColumn;
206
+ private computeModelSignature;
207
+ isSchemaDirty(): Promise<{
208
+ dirty: boolean;
209
+ current: string;
210
+ stored: string | null;
211
+ }>;
212
+ migrateIfDirty(options?: {
213
+ name?: string;
214
+ track?: boolean;
215
+ }): Promise<boolean>;
216
+ pullSchema(): Promise<Record<string, any>>;
217
+ private buildCreateTableSQL;
218
+ private tableExists;
219
+ forcePush(options?: {
220
+ dropExtraColumns?: boolean;
221
+ preserveData?: boolean;
222
+ }): Promise<void>;
157
223
  }
158
- declare const db: TauriORM;
159
224
  type OneConfig = {
160
225
  fields?: Column[];
161
226
  references?: Column[];
@@ -184,12 +249,17 @@ type WithSpec = Record<string, boolean | {
184
249
  with?: WithSpec;
185
250
  columns?: string[];
186
251
  }>;
187
- declare function makeQueryAPI(tables: Record<string, Table<any>>, relDefs: Record<string, Record<string, RelationConfig>>): { [K in keyof typeof tables]: {
252
+ declare function makeQueryAPI(tables: Record<string, Table<any>>, relDefs: Record<string, Record<string, RelationConfig>>, dbProvider: () => Promise<Database>): { [K in keyof typeof tables]: {
188
253
  findMany: (opts?: {
189
254
  with?: WithSpec;
190
255
  join?: boolean;
191
256
  columns?: string[];
192
257
  }) => Promise<any[]>;
258
+ findFirst: (opts?: {
259
+ with?: WithSpec;
260
+ join?: boolean;
261
+ columns?: string[];
262
+ }) => Promise<any | null>;
193
263
  }; };
194
264
 
195
- export { type Column, type ManyRelation, type OneRelation, type SQL, type SQLExpression, type Table, TauriORM, type UpdateDeleteAction, asc, blob, boolean, db, defineTable, desc, eq, getDb, gt, gte, initDb, integer, like, lt, lte, makeQueryAPI, ne, numeric, real, relations, sql, text, timestamp };
265
+ export { type BlobMode, type Column, type ColumnBuilder, type IntegerMode, type ManyRelation, type NumericMode, type OneRelation, type SQL, type SQLExpression, type Table, TauriORM, type UpdateDeleteAction, asc, blob, boolean, defineTable, desc, eq, gt, gte, increments, integer, like, lt, lte, makeQueryAPI, ne, numeric, real, relations, sql, text, timestamp };