@type32/tauri-sqlite-orm 0.1.18-8 → 0.1.18-9
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 +53 -21
- package/dist/index.d.ts +53 -21
- package/dist/index.js +178 -53
- package/dist/index.mjs +176 -53
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -28,6 +28,7 @@ declare class SQLiteColumn<TName extends string = string, TType extends ColumnDa
|
|
|
28
28
|
notNull: TNotNull;
|
|
29
29
|
hasDefault: THasDefault;
|
|
30
30
|
autoincrement: TAutoincrement;
|
|
31
|
+
table: AnyTable;
|
|
31
32
|
};
|
|
32
33
|
constructor(name: TName, type: TType, options?: ColumnOptions<ColumnValueTypes<TType, TMode>>, mode?: TMode);
|
|
33
34
|
notNull(): SQLiteColumn<TName, TType, TMode, true, THasDefault, TAutoincrement>;
|
|
@@ -72,6 +73,9 @@ declare class Table<TColumns extends Record<string, AnySQLiteColumn>, TTableName
|
|
|
72
73
|
constructor(name: TTableName, columns: TColumns);
|
|
73
74
|
}
|
|
74
75
|
declare const sqliteTable: <TTableName extends string, TColumns extends Record<string, AnySQLiteColumn>>(tableName: TTableName, columns: TColumns) => Table<TColumns, TTableName>;
|
|
76
|
+
declare const getTableColumns: <T extends AnyTable>(table: T) => Record<string, AnySQLiteColumn>;
|
|
77
|
+
declare const alias: <T extends AnyTable>(table: T, alias: string) => Table<T["_"]["columns"], T["_"]["name"]>;
|
|
78
|
+
|
|
75
79
|
type SQLCondition = {
|
|
76
80
|
sql: string;
|
|
77
81
|
params: any[];
|
|
@@ -112,31 +116,57 @@ declare class BaseQueryBuilder {
|
|
|
112
116
|
protected query: string;
|
|
113
117
|
protected params: any[];
|
|
114
118
|
constructor(db: Database);
|
|
119
|
+
build(): {
|
|
120
|
+
sql: string;
|
|
121
|
+
params: any[];
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
type SelectedFields = Record<string, AnySQLiteColumn | {
|
|
125
|
+
sql: string;
|
|
126
|
+
params: any[];
|
|
127
|
+
}>;
|
|
128
|
+
declare class SelectQueryBuilder<TTable extends AnyTable, TSelection extends SelectedFields, TResult = InferSelectModel<TTable>> extends BaseQueryBuilder {
|
|
129
|
+
private selection;
|
|
130
|
+
private isDistinct;
|
|
131
|
+
private groupByColumns;
|
|
132
|
+
private havingCondition;
|
|
133
|
+
private joinClauses;
|
|
134
|
+
private fromTable;
|
|
135
|
+
private orderByClauses;
|
|
136
|
+
private limitCount;
|
|
137
|
+
private offsetCount;
|
|
138
|
+
private whereCondition;
|
|
139
|
+
constructor(db: Database, table: TTable, selection: TSelection | undefined);
|
|
115
140
|
where(condition: SQLCondition): this;
|
|
141
|
+
leftJoin(table: AnyTable, on: SQLCondition): this;
|
|
142
|
+
innerJoin(table: AnyTable, on: SQLCondition): this;
|
|
143
|
+
rightJoin(table: AnyTable, on: SQLCondition): this;
|
|
144
|
+
fullJoin(table: AnyTable, on: SQLCondition): this;
|
|
145
|
+
distinct(): this;
|
|
146
|
+
groupBy(...columns: AnySQLiteColumn[]): this;
|
|
147
|
+
having(condition: SQLCondition): this;
|
|
116
148
|
orderBy(column: AnySQLiteColumn | {
|
|
117
149
|
sql: string;
|
|
118
150
|
params: any[];
|
|
119
151
|
}, direction?: "ASC" | "DESC"): this;
|
|
120
152
|
limit(count: number): this;
|
|
121
153
|
offset(count: number): this;
|
|
154
|
+
private buildSelectQuery;
|
|
122
155
|
build(): {
|
|
123
156
|
sql: string;
|
|
124
157
|
params: any[];
|
|
125
158
|
};
|
|
159
|
+
execute(): Promise<TResult[]>;
|
|
160
|
+
all(): Promise<TResult[]>;
|
|
161
|
+
get(): Promise<TResult | undefined>;
|
|
126
162
|
}
|
|
127
|
-
declare class
|
|
128
|
-
private
|
|
129
|
-
private
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
distinct(): this;
|
|
135
|
-
groupBy(...columns: AnySQLiteColumn[]): this;
|
|
136
|
-
having(condition: SQLCondition): this;
|
|
137
|
-
execute(): Promise<TSelectedColumns extends (keyof TTable["_"]["columns"])[] ? Pick<InferSelectModel<TTable>, TSelectedColumns[number]>[] : InferSelectModel<TTable>[]>;
|
|
138
|
-
all(): Promise<TSelectedColumns extends (keyof TTable["_"]["columns"])[] ? Pick<InferSelectModel<TTable>, TSelectedColumns[number]>[] : InferSelectModel<TTable>[]>;
|
|
139
|
-
get(): Promise<TSelectedColumns extends (keyof TTable["_"]["columns"])[] ? Pick<InferSelectModel<TTable>, TSelectedColumns[number]> | undefined : InferSelectModel<TTable> | undefined>;
|
|
163
|
+
declare class SelectBuilder<TSelection extends SelectedFields | undefined = undefined> {
|
|
164
|
+
private db;
|
|
165
|
+
private selection;
|
|
166
|
+
constructor(db: Database, selection: TSelection);
|
|
167
|
+
from<TTable extends AnyTable>(table: TTable): SelectQueryBuilder<TTable, TSelection extends SelectedFields ? TSelection : {}, TSelection extends SelectedFields ? {
|
|
168
|
+
[K in keyof TSelection]: any;
|
|
169
|
+
} : InferSelectModel<TTable>>;
|
|
140
170
|
}
|
|
141
171
|
declare class InsertQueryBuilder<T extends AnyTable> extends BaseQueryBuilder {
|
|
142
172
|
private table;
|
|
@@ -147,7 +177,7 @@ declare class InsertQueryBuilder<T extends AnyTable> extends BaseQueryBuilder {
|
|
|
147
177
|
private updateSet;
|
|
148
178
|
constructor(db: Database, table: T);
|
|
149
179
|
values(data: InferInsertModel<T> | InferInsertModel<T>[]): this;
|
|
150
|
-
returning(...columns: (keyof T["_"]["columns"])[]): this;
|
|
180
|
+
returning(...columns: (AnySQLiteColumn | keyof T["_"]["columns"])[]): this;
|
|
151
181
|
onConflictDoNothing(target?: AnySQLiteColumn | AnySQLiteColumn[]): this;
|
|
152
182
|
onConflictDoUpdate(config: {
|
|
153
183
|
target: AnySQLiteColumn | AnySQLiteColumn[];
|
|
@@ -164,7 +194,7 @@ declare class UpdateQueryBuilder<T extends AnyTable> extends BaseQueryBuilder {
|
|
|
164
194
|
private returningColumns;
|
|
165
195
|
constructor(db: Database, table: T);
|
|
166
196
|
set(data: Partial<InferInsertModel<T>>): this;
|
|
167
|
-
returning(...columns: (keyof T["_"]["columns"])[]): this;
|
|
197
|
+
returning(...columns: (AnySQLiteColumn | keyof T["_"]["columns"])[]): this;
|
|
168
198
|
private buildUpdateClause;
|
|
169
199
|
execute(): Promise<T extends AnyTable ? (InferSelectModel<T> & Record<string, any>)[] : never>;
|
|
170
200
|
returningAll(): Promise<InferSelectModel<T>[]>;
|
|
@@ -173,7 +203,7 @@ declare class DeleteQueryBuilder<T extends AnyTable> extends BaseQueryBuilder {
|
|
|
173
203
|
private table;
|
|
174
204
|
private returningColumns;
|
|
175
205
|
constructor(db: Database, table: T);
|
|
176
|
-
returning(...columns: (keyof T["_"]["columns"])[]): this;
|
|
206
|
+
returning(...columns: (AnySQLiteColumn | keyof T["_"]["columns"])[]): this;
|
|
177
207
|
execute(): Promise<T extends AnyTable ? (InferSelectModel<T> & Record<string, any>)[] : never>;
|
|
178
208
|
returningAll(): Promise<InferSelectModel<T>[]>;
|
|
179
209
|
}
|
|
@@ -185,19 +215,23 @@ declare class WithQueryBuilder {
|
|
|
185
215
|
sql: string;
|
|
186
216
|
params: any[];
|
|
187
217
|
}): this;
|
|
188
|
-
select<
|
|
218
|
+
select<TTable extends AnyTable, TSelection extends SelectedFields | undefined = undefined>(table: TTable, selection?: TSelection): SelectQueryBuilder<TTable, TSelection extends SelectedFields ? TSelection : {}, TSelection extends SelectedFields ? {
|
|
219
|
+
[K in keyof TSelection]: any;
|
|
220
|
+
} : InferSelectModel<TTable>>;
|
|
189
221
|
insert<T extends AnyTable>(table: T): InsertQueryBuilder<T>;
|
|
190
222
|
update<T extends AnyTable>(table: T): UpdateQueryBuilder<T>;
|
|
191
223
|
delete<T extends AnyTable>(table: T): DeleteQueryBuilder<T>;
|
|
192
224
|
private applyWithClause;
|
|
193
225
|
}
|
|
226
|
+
|
|
194
227
|
declare class TauriORM {
|
|
195
228
|
private db;
|
|
196
229
|
private tables;
|
|
197
230
|
constructor(db: Database, schema?: Record<string, AnyTable | Record<string, Relation>> | undefined);
|
|
198
231
|
private buildColumnDefinition;
|
|
199
232
|
migrate(): Promise<void>;
|
|
200
|
-
select
|
|
233
|
+
select(): SelectBuilder<undefined>;
|
|
234
|
+
select<TSelection extends SelectedFields>(selection: TSelection): SelectBuilder<TSelection>;
|
|
201
235
|
insert<T extends AnyTable>(table: T): InsertQueryBuilder<T>;
|
|
202
236
|
update<T extends AnyTable>(table: T): UpdateQueryBuilder<T>;
|
|
203
237
|
delete<T extends AnyTable>(table: T): DeleteQueryBuilder<T>;
|
|
@@ -247,7 +281,5 @@ type RelationsBuilder = {
|
|
|
247
281
|
many: <U extends AnyTable>(table: U) => ManyRelation<U>;
|
|
248
282
|
};
|
|
249
283
|
declare const relations: <T extends AnyTable, R extends Record<string, Relation>>(_table: T, relationsCallback: (helpers: RelationsBuilder) => R) => R;
|
|
250
|
-
declare const getTableColumns: <T extends AnyTable>(table: T) => Record<string, AnySQLiteColumn>;
|
|
251
|
-
declare const alias: <T extends AnyTable>(table: T, alias: string) => Table<T["_"]["columns"], T["_"]["name"]>;
|
|
252
284
|
|
|
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 };
|
|
285
|
+
export { type AnySQLiteColumn, type AnyTable, BaseQueryBuilder, type ColumnDataType, type ColumnOptions, DeleteQueryBuilder, type InferInsertModel, type InferSelectModel, InsertQueryBuilder, ManyRelation, type Mode, OneRelation, Relation, type SQLCondition, SQLiteColumn, SelectBuilder, SelectQueryBuilder, type SelectedFields, 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
|
@@ -28,6 +28,7 @@ declare class SQLiteColumn<TName extends string = string, TType extends ColumnDa
|
|
|
28
28
|
notNull: TNotNull;
|
|
29
29
|
hasDefault: THasDefault;
|
|
30
30
|
autoincrement: TAutoincrement;
|
|
31
|
+
table: AnyTable;
|
|
31
32
|
};
|
|
32
33
|
constructor(name: TName, type: TType, options?: ColumnOptions<ColumnValueTypes<TType, TMode>>, mode?: TMode);
|
|
33
34
|
notNull(): SQLiteColumn<TName, TType, TMode, true, THasDefault, TAutoincrement>;
|
|
@@ -72,6 +73,9 @@ declare class Table<TColumns extends Record<string, AnySQLiteColumn>, TTableName
|
|
|
72
73
|
constructor(name: TTableName, columns: TColumns);
|
|
73
74
|
}
|
|
74
75
|
declare const sqliteTable: <TTableName extends string, TColumns extends Record<string, AnySQLiteColumn>>(tableName: TTableName, columns: TColumns) => Table<TColumns, TTableName>;
|
|
76
|
+
declare const getTableColumns: <T extends AnyTable>(table: T) => Record<string, AnySQLiteColumn>;
|
|
77
|
+
declare const alias: <T extends AnyTable>(table: T, alias: string) => Table<T["_"]["columns"], T["_"]["name"]>;
|
|
78
|
+
|
|
75
79
|
type SQLCondition = {
|
|
76
80
|
sql: string;
|
|
77
81
|
params: any[];
|
|
@@ -112,31 +116,57 @@ declare class BaseQueryBuilder {
|
|
|
112
116
|
protected query: string;
|
|
113
117
|
protected params: any[];
|
|
114
118
|
constructor(db: Database);
|
|
119
|
+
build(): {
|
|
120
|
+
sql: string;
|
|
121
|
+
params: any[];
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
type SelectedFields = Record<string, AnySQLiteColumn | {
|
|
125
|
+
sql: string;
|
|
126
|
+
params: any[];
|
|
127
|
+
}>;
|
|
128
|
+
declare class SelectQueryBuilder<TTable extends AnyTable, TSelection extends SelectedFields, TResult = InferSelectModel<TTable>> extends BaseQueryBuilder {
|
|
129
|
+
private selection;
|
|
130
|
+
private isDistinct;
|
|
131
|
+
private groupByColumns;
|
|
132
|
+
private havingCondition;
|
|
133
|
+
private joinClauses;
|
|
134
|
+
private fromTable;
|
|
135
|
+
private orderByClauses;
|
|
136
|
+
private limitCount;
|
|
137
|
+
private offsetCount;
|
|
138
|
+
private whereCondition;
|
|
139
|
+
constructor(db: Database, table: TTable, selection: TSelection | undefined);
|
|
115
140
|
where(condition: SQLCondition): this;
|
|
141
|
+
leftJoin(table: AnyTable, on: SQLCondition): this;
|
|
142
|
+
innerJoin(table: AnyTable, on: SQLCondition): this;
|
|
143
|
+
rightJoin(table: AnyTable, on: SQLCondition): this;
|
|
144
|
+
fullJoin(table: AnyTable, on: SQLCondition): this;
|
|
145
|
+
distinct(): this;
|
|
146
|
+
groupBy(...columns: AnySQLiteColumn[]): this;
|
|
147
|
+
having(condition: SQLCondition): this;
|
|
116
148
|
orderBy(column: AnySQLiteColumn | {
|
|
117
149
|
sql: string;
|
|
118
150
|
params: any[];
|
|
119
151
|
}, direction?: "ASC" | "DESC"): this;
|
|
120
152
|
limit(count: number): this;
|
|
121
153
|
offset(count: number): this;
|
|
154
|
+
private buildSelectQuery;
|
|
122
155
|
build(): {
|
|
123
156
|
sql: string;
|
|
124
157
|
params: any[];
|
|
125
158
|
};
|
|
159
|
+
execute(): Promise<TResult[]>;
|
|
160
|
+
all(): Promise<TResult[]>;
|
|
161
|
+
get(): Promise<TResult | undefined>;
|
|
126
162
|
}
|
|
127
|
-
declare class
|
|
128
|
-
private
|
|
129
|
-
private
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
distinct(): this;
|
|
135
|
-
groupBy(...columns: AnySQLiteColumn[]): this;
|
|
136
|
-
having(condition: SQLCondition): this;
|
|
137
|
-
execute(): Promise<TSelectedColumns extends (keyof TTable["_"]["columns"])[] ? Pick<InferSelectModel<TTable>, TSelectedColumns[number]>[] : InferSelectModel<TTable>[]>;
|
|
138
|
-
all(): Promise<TSelectedColumns extends (keyof TTable["_"]["columns"])[] ? Pick<InferSelectModel<TTable>, TSelectedColumns[number]>[] : InferSelectModel<TTable>[]>;
|
|
139
|
-
get(): Promise<TSelectedColumns extends (keyof TTable["_"]["columns"])[] ? Pick<InferSelectModel<TTable>, TSelectedColumns[number]> | undefined : InferSelectModel<TTable> | undefined>;
|
|
163
|
+
declare class SelectBuilder<TSelection extends SelectedFields | undefined = undefined> {
|
|
164
|
+
private db;
|
|
165
|
+
private selection;
|
|
166
|
+
constructor(db: Database, selection: TSelection);
|
|
167
|
+
from<TTable extends AnyTable>(table: TTable): SelectQueryBuilder<TTable, TSelection extends SelectedFields ? TSelection : {}, TSelection extends SelectedFields ? {
|
|
168
|
+
[K in keyof TSelection]: any;
|
|
169
|
+
} : InferSelectModel<TTable>>;
|
|
140
170
|
}
|
|
141
171
|
declare class InsertQueryBuilder<T extends AnyTable> extends BaseQueryBuilder {
|
|
142
172
|
private table;
|
|
@@ -147,7 +177,7 @@ declare class InsertQueryBuilder<T extends AnyTable> extends BaseQueryBuilder {
|
|
|
147
177
|
private updateSet;
|
|
148
178
|
constructor(db: Database, table: T);
|
|
149
179
|
values(data: InferInsertModel<T> | InferInsertModel<T>[]): this;
|
|
150
|
-
returning(...columns: (keyof T["_"]["columns"])[]): this;
|
|
180
|
+
returning(...columns: (AnySQLiteColumn | keyof T["_"]["columns"])[]): this;
|
|
151
181
|
onConflictDoNothing(target?: AnySQLiteColumn | AnySQLiteColumn[]): this;
|
|
152
182
|
onConflictDoUpdate(config: {
|
|
153
183
|
target: AnySQLiteColumn | AnySQLiteColumn[];
|
|
@@ -164,7 +194,7 @@ declare class UpdateQueryBuilder<T extends AnyTable> extends BaseQueryBuilder {
|
|
|
164
194
|
private returningColumns;
|
|
165
195
|
constructor(db: Database, table: T);
|
|
166
196
|
set(data: Partial<InferInsertModel<T>>): this;
|
|
167
|
-
returning(...columns: (keyof T["_"]["columns"])[]): this;
|
|
197
|
+
returning(...columns: (AnySQLiteColumn | keyof T["_"]["columns"])[]): this;
|
|
168
198
|
private buildUpdateClause;
|
|
169
199
|
execute(): Promise<T extends AnyTable ? (InferSelectModel<T> & Record<string, any>)[] : never>;
|
|
170
200
|
returningAll(): Promise<InferSelectModel<T>[]>;
|
|
@@ -173,7 +203,7 @@ declare class DeleteQueryBuilder<T extends AnyTable> extends BaseQueryBuilder {
|
|
|
173
203
|
private table;
|
|
174
204
|
private returningColumns;
|
|
175
205
|
constructor(db: Database, table: T);
|
|
176
|
-
returning(...columns: (keyof T["_"]["columns"])[]): this;
|
|
206
|
+
returning(...columns: (AnySQLiteColumn | keyof T["_"]["columns"])[]): this;
|
|
177
207
|
execute(): Promise<T extends AnyTable ? (InferSelectModel<T> & Record<string, any>)[] : never>;
|
|
178
208
|
returningAll(): Promise<InferSelectModel<T>[]>;
|
|
179
209
|
}
|
|
@@ -185,19 +215,23 @@ declare class WithQueryBuilder {
|
|
|
185
215
|
sql: string;
|
|
186
216
|
params: any[];
|
|
187
217
|
}): this;
|
|
188
|
-
select<
|
|
218
|
+
select<TTable extends AnyTable, TSelection extends SelectedFields | undefined = undefined>(table: TTable, selection?: TSelection): SelectQueryBuilder<TTable, TSelection extends SelectedFields ? TSelection : {}, TSelection extends SelectedFields ? {
|
|
219
|
+
[K in keyof TSelection]: any;
|
|
220
|
+
} : InferSelectModel<TTable>>;
|
|
189
221
|
insert<T extends AnyTable>(table: T): InsertQueryBuilder<T>;
|
|
190
222
|
update<T extends AnyTable>(table: T): UpdateQueryBuilder<T>;
|
|
191
223
|
delete<T extends AnyTable>(table: T): DeleteQueryBuilder<T>;
|
|
192
224
|
private applyWithClause;
|
|
193
225
|
}
|
|
226
|
+
|
|
194
227
|
declare class TauriORM {
|
|
195
228
|
private db;
|
|
196
229
|
private tables;
|
|
197
230
|
constructor(db: Database, schema?: Record<string, AnyTable | Record<string, Relation>> | undefined);
|
|
198
231
|
private buildColumnDefinition;
|
|
199
232
|
migrate(): Promise<void>;
|
|
200
|
-
select
|
|
233
|
+
select(): SelectBuilder<undefined>;
|
|
234
|
+
select<TSelection extends SelectedFields>(selection: TSelection): SelectBuilder<TSelection>;
|
|
201
235
|
insert<T extends AnyTable>(table: T): InsertQueryBuilder<T>;
|
|
202
236
|
update<T extends AnyTable>(table: T): UpdateQueryBuilder<T>;
|
|
203
237
|
delete<T extends AnyTable>(table: T): DeleteQueryBuilder<T>;
|
|
@@ -247,7 +281,5 @@ type RelationsBuilder = {
|
|
|
247
281
|
many: <U extends AnyTable>(table: U) => ManyRelation<U>;
|
|
248
282
|
};
|
|
249
283
|
declare const relations: <T extends AnyTable, R extends Record<string, Relation>>(_table: T, relationsCallback: (helpers: RelationsBuilder) => R) => R;
|
|
250
|
-
declare const getTableColumns: <T extends AnyTable>(table: T) => Record<string, AnySQLiteColumn>;
|
|
251
|
-
declare const alias: <T extends AnyTable>(table: T, alias: string) => Table<T["_"]["columns"], T["_"]["name"]>;
|
|
252
284
|
|
|
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 };
|
|
285
|
+
export { type AnySQLiteColumn, type AnyTable, BaseQueryBuilder, type ColumnDataType, type ColumnOptions, DeleteQueryBuilder, type InferInsertModel, type InferSelectModel, InsertQueryBuilder, ManyRelation, type Mode, OneRelation, Relation, type SQLCondition, SQLiteColumn, SelectBuilder, SelectQueryBuilder, type SelectedFields, 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
|
@@ -20,12 +20,14 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
|
+
BaseQueryBuilder: () => BaseQueryBuilder,
|
|
23
24
|
DeleteQueryBuilder: () => DeleteQueryBuilder,
|
|
24
25
|
InsertQueryBuilder: () => InsertQueryBuilder,
|
|
25
26
|
ManyRelation: () => ManyRelation,
|
|
26
27
|
OneRelation: () => OneRelation,
|
|
27
28
|
Relation: () => Relation,
|
|
28
29
|
SQLiteColumn: () => SQLiteColumn,
|
|
30
|
+
SelectBuilder: () => SelectBuilder,
|
|
29
31
|
SelectQueryBuilder: () => SelectQueryBuilder,
|
|
30
32
|
Table: () => Table,
|
|
31
33
|
TauriORM: () => TauriORM,
|
|
@@ -64,7 +66,7 @@ __export(index_exports, {
|
|
|
64
66
|
});
|
|
65
67
|
module.exports = __toCommonJS(index_exports);
|
|
66
68
|
|
|
67
|
-
// src/
|
|
69
|
+
// src/schema.ts
|
|
68
70
|
var SQLiteColumn = class _SQLiteColumn {
|
|
69
71
|
constructor(name, type, options = {}, mode) {
|
|
70
72
|
this.type = type;
|
|
@@ -75,7 +77,8 @@ var SQLiteColumn = class _SQLiteColumn {
|
|
|
75
77
|
mode: mode || "default",
|
|
76
78
|
notNull: options.notNull ?? false,
|
|
77
79
|
hasDefault: options.default !== void 0 || options.$defaultFn !== void 0,
|
|
78
|
-
autoincrement: options.autoincrement ?? false
|
|
80
|
+
autoincrement: options.autoincrement ?? false,
|
|
81
|
+
table: void 0
|
|
79
82
|
};
|
|
80
83
|
}
|
|
81
84
|
_;
|
|
@@ -168,8 +171,20 @@ var Table = class {
|
|
|
168
171
|
}
|
|
169
172
|
};
|
|
170
173
|
var sqliteTable = (tableName, columns) => {
|
|
171
|
-
|
|
174
|
+
const table = new Table(tableName, columns);
|
|
175
|
+
for (const col of Object.values(columns)) {
|
|
176
|
+
col._.table = table;
|
|
177
|
+
}
|
|
178
|
+
return table;
|
|
179
|
+
};
|
|
180
|
+
var getTableColumns = (table) => {
|
|
181
|
+
return table._.columns;
|
|
172
182
|
};
|
|
183
|
+
var alias = (table, alias2) => {
|
|
184
|
+
return table;
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
// src/query-builder.ts
|
|
173
188
|
var eq = (column, value) => ({
|
|
174
189
|
sql: `${column._.name} = ?`,
|
|
175
190
|
params: [value]
|
|
@@ -276,28 +291,6 @@ var BaseQueryBuilder = class {
|
|
|
276
291
|
}
|
|
277
292
|
query = "";
|
|
278
293
|
params = [];
|
|
279
|
-
where(condition) {
|
|
280
|
-
this.query += ` WHERE ${condition.sql}`;
|
|
281
|
-
this.params.push(...condition.params);
|
|
282
|
-
return this;
|
|
283
|
-
}
|
|
284
|
-
orderBy(column, direction = "ASC") {
|
|
285
|
-
if ("sql" in column) {
|
|
286
|
-
this.query += ` ORDER BY ${column.sql} ${direction}`;
|
|
287
|
-
this.params.push(...column.params);
|
|
288
|
-
} else {
|
|
289
|
-
this.query += ` ORDER BY ${column._.name} ${direction}`;
|
|
290
|
-
}
|
|
291
|
-
return this;
|
|
292
|
-
}
|
|
293
|
-
limit(count2) {
|
|
294
|
-
this.query += ` LIMIT ${count2}`;
|
|
295
|
-
return this;
|
|
296
|
-
}
|
|
297
|
-
offset(count2) {
|
|
298
|
-
this.query += ` OFFSET ${count2}`;
|
|
299
|
-
return this;
|
|
300
|
-
}
|
|
301
294
|
build() {
|
|
302
295
|
return {
|
|
303
296
|
sql: this.query,
|
|
@@ -306,35 +299,134 @@ var BaseQueryBuilder = class {
|
|
|
306
299
|
}
|
|
307
300
|
};
|
|
308
301
|
var SelectQueryBuilder = class extends BaseQueryBuilder {
|
|
309
|
-
constructor(db, table,
|
|
302
|
+
constructor(db, table, selection) {
|
|
310
303
|
super(db);
|
|
311
|
-
this.
|
|
312
|
-
this.
|
|
313
|
-
|
|
314
|
-
this.
|
|
304
|
+
this.selection = selection;
|
|
305
|
+
this.fromTable = table;
|
|
306
|
+
this.query = "";
|
|
307
|
+
this.params = [];
|
|
315
308
|
}
|
|
316
309
|
isDistinct = false;
|
|
317
310
|
groupByColumns = [];
|
|
318
|
-
havingCondition
|
|
311
|
+
havingCondition;
|
|
312
|
+
joinClauses = [];
|
|
313
|
+
fromTable;
|
|
314
|
+
orderByClauses = [];
|
|
315
|
+
limitCount;
|
|
316
|
+
offsetCount;
|
|
317
|
+
whereCondition;
|
|
318
|
+
where(condition) {
|
|
319
|
+
this.whereCondition = condition;
|
|
320
|
+
return this;
|
|
321
|
+
}
|
|
322
|
+
leftJoin(table, on) {
|
|
323
|
+
this.joinClauses.push({ type: "LEFT JOIN", table, on });
|
|
324
|
+
return this;
|
|
325
|
+
}
|
|
326
|
+
innerJoin(table, on) {
|
|
327
|
+
this.joinClauses.push({ type: "INNER JOIN", table, on });
|
|
328
|
+
return this;
|
|
329
|
+
}
|
|
330
|
+
rightJoin(table, on) {
|
|
331
|
+
this.joinClauses.push({ type: "RIGHT JOIN", table, on });
|
|
332
|
+
return this;
|
|
333
|
+
}
|
|
334
|
+
fullJoin(table, on) {
|
|
335
|
+
this.joinClauses.push({ type: "FULL JOIN", table, on });
|
|
336
|
+
return this;
|
|
337
|
+
}
|
|
319
338
|
distinct() {
|
|
320
339
|
this.isDistinct = true;
|
|
321
|
-
this.query = this.query.replace("SELECT", "SELECT DISTINCT");
|
|
322
340
|
return this;
|
|
323
341
|
}
|
|
324
342
|
groupBy(...columns) {
|
|
325
343
|
this.groupByColumns.push(...columns);
|
|
326
|
-
const columnNames = columns.map((col) => col._.name).join(", ");
|
|
327
|
-
this.query += ` GROUP BY ${columnNames}`;
|
|
328
344
|
return this;
|
|
329
345
|
}
|
|
330
346
|
having(condition) {
|
|
331
347
|
this.havingCondition = condition;
|
|
332
|
-
this.query += ` HAVING ${condition.sql}`;
|
|
333
|
-
this.params.push(...condition.params);
|
|
334
348
|
return this;
|
|
335
349
|
}
|
|
350
|
+
orderBy(column, direction = "ASC") {
|
|
351
|
+
if ("sql" in column) {
|
|
352
|
+
this.orderByClauses.push({
|
|
353
|
+
sql: `${column.sql} ${direction}`,
|
|
354
|
+
params: column.params
|
|
355
|
+
});
|
|
356
|
+
} else {
|
|
357
|
+
this.orderByClauses.push({
|
|
358
|
+
sql: `${column._.name} ${direction}`,
|
|
359
|
+
params: []
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
return this;
|
|
363
|
+
}
|
|
364
|
+
limit(count2) {
|
|
365
|
+
this.limitCount = count2;
|
|
366
|
+
return this;
|
|
367
|
+
}
|
|
368
|
+
offset(count2) {
|
|
369
|
+
this.offsetCount = count2;
|
|
370
|
+
return this;
|
|
371
|
+
}
|
|
372
|
+
buildSelectQuery() {
|
|
373
|
+
const selectionEntries = this.selection ? Object.entries(this.selection) : [];
|
|
374
|
+
let columnsClause;
|
|
375
|
+
const selectParams = [];
|
|
376
|
+
if (selectionEntries.length === 0) {
|
|
377
|
+
columnsClause = "*";
|
|
378
|
+
} else {
|
|
379
|
+
columnsClause = selectionEntries.map(([alias2, col]) => {
|
|
380
|
+
if (col instanceof SQLiteColumn) {
|
|
381
|
+
return `${col._.table._.name}.${col._.name} AS "${alias2}"`;
|
|
382
|
+
} else {
|
|
383
|
+
selectParams.push(...col.params);
|
|
384
|
+
return `(${col.sql}) AS "${alias2}"`;
|
|
385
|
+
}
|
|
386
|
+
}).join(", ");
|
|
387
|
+
}
|
|
388
|
+
let query = `SELECT ${this.isDistinct ? "DISTINCT " : ""}${columnsClause} FROM ${this.fromTable._.name}`;
|
|
389
|
+
const queryParams = [...selectParams];
|
|
390
|
+
if (this.joinClauses.length > 0) {
|
|
391
|
+
const joins = this.joinClauses.map((j) => {
|
|
392
|
+
queryParams.push(...j.on.params);
|
|
393
|
+
return `${j.type} ${j.table._.name} ON ${j.on.sql}`;
|
|
394
|
+
});
|
|
395
|
+
query += ` ${joins.join(" ")}`;
|
|
396
|
+
}
|
|
397
|
+
if (this.whereCondition) {
|
|
398
|
+
query += ` WHERE ${this.whereCondition.sql}`;
|
|
399
|
+
queryParams.push(...this.whereCondition.params);
|
|
400
|
+
}
|
|
401
|
+
if (this.groupByColumns.length > 0) {
|
|
402
|
+
const columnNames = this.groupByColumns.map((c) => c._.name).join(", ");
|
|
403
|
+
query += ` GROUP BY ${columnNames}`;
|
|
404
|
+
}
|
|
405
|
+
if (this.havingCondition) {
|
|
406
|
+
query += ` HAVING ${this.havingCondition.sql}`;
|
|
407
|
+
queryParams.push(...this.havingCondition.params);
|
|
408
|
+
}
|
|
409
|
+
if (this.orderByClauses.length > 0) {
|
|
410
|
+
const orderBySql = this.orderByClauses.map((c) => c.sql).join(", ");
|
|
411
|
+
query += ` ORDER BY ${orderBySql}`;
|
|
412
|
+
queryParams.push(...this.orderByClauses.flatMap((c) => c.params));
|
|
413
|
+
}
|
|
414
|
+
if (this.limitCount !== void 0) {
|
|
415
|
+
query += ` LIMIT ${this.limitCount}`;
|
|
416
|
+
}
|
|
417
|
+
if (this.offsetCount !== void 0) {
|
|
418
|
+
query += ` OFFSET ${this.offsetCount}`;
|
|
419
|
+
}
|
|
420
|
+
return {
|
|
421
|
+
sql: query,
|
|
422
|
+
params: queryParams
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
build() {
|
|
426
|
+
return this.buildSelectQuery();
|
|
427
|
+
}
|
|
336
428
|
async execute() {
|
|
337
|
-
const { sql: sql2, params } = this.
|
|
429
|
+
const { sql: sql2, params } = this.buildSelectQuery();
|
|
338
430
|
return this.db.select(sql2, params);
|
|
339
431
|
}
|
|
340
432
|
async all() {
|
|
@@ -346,6 +438,19 @@ var SelectQueryBuilder = class extends BaseQueryBuilder {
|
|
|
346
438
|
return result[0];
|
|
347
439
|
}
|
|
348
440
|
};
|
|
441
|
+
var SelectBuilder = class {
|
|
442
|
+
constructor(db, selection) {
|
|
443
|
+
this.db = db;
|
|
444
|
+
this.selection = selection;
|
|
445
|
+
}
|
|
446
|
+
from(table) {
|
|
447
|
+
return new SelectQueryBuilder(
|
|
448
|
+
this.db,
|
|
449
|
+
table,
|
|
450
|
+
this.selection
|
|
451
|
+
);
|
|
452
|
+
}
|
|
453
|
+
};
|
|
349
454
|
var InsertQueryBuilder = class extends BaseQueryBuilder {
|
|
350
455
|
constructor(db, table) {
|
|
351
456
|
super(db);
|
|
@@ -363,7 +468,13 @@ var InsertQueryBuilder = class extends BaseQueryBuilder {
|
|
|
363
468
|
return this;
|
|
364
469
|
}
|
|
365
470
|
returning(...columns) {
|
|
366
|
-
|
|
471
|
+
for (const col of columns) {
|
|
472
|
+
if (typeof col === "string") {
|
|
473
|
+
this.returningColumns.push(this.table._.columns[col]);
|
|
474
|
+
} else {
|
|
475
|
+
this.returningColumns.push(col);
|
|
476
|
+
}
|
|
477
|
+
}
|
|
367
478
|
return this;
|
|
368
479
|
}
|
|
369
480
|
onConflictDoNothing(target) {
|
|
@@ -448,7 +559,7 @@ var InsertQueryBuilder = class extends BaseQueryBuilder {
|
|
|
448
559
|
params.push(...setValues);
|
|
449
560
|
}
|
|
450
561
|
if (this.returningColumns.length > 0) {
|
|
451
|
-
const returningNames = this.returningColumns.map((col) =>
|
|
562
|
+
const returningNames = this.returningColumns.map((col) => col._.name).join(", ");
|
|
452
563
|
const queryWithReturning = `${finalQuery} RETURNING ${returningNames}`;
|
|
453
564
|
const rows = await this.db.select(queryWithReturning, params);
|
|
454
565
|
results = results.concat(rows);
|
|
@@ -483,7 +594,13 @@ var UpdateQueryBuilder = class extends BaseQueryBuilder {
|
|
|
483
594
|
return this;
|
|
484
595
|
}
|
|
485
596
|
returning(...columns) {
|
|
486
|
-
|
|
597
|
+
for (const col of columns) {
|
|
598
|
+
if (typeof col === "string") {
|
|
599
|
+
this.returningColumns.push(this.table._.columns[col]);
|
|
600
|
+
} else {
|
|
601
|
+
this.returningColumns.push(col);
|
|
602
|
+
}
|
|
603
|
+
}
|
|
487
604
|
return this;
|
|
488
605
|
}
|
|
489
606
|
buildUpdateClause() {
|
|
@@ -524,7 +641,7 @@ var UpdateQueryBuilder = class extends BaseQueryBuilder {
|
|
|
524
641
|
async execute() {
|
|
525
642
|
const { sql: updateSql, params } = this.buildUpdateClause();
|
|
526
643
|
if (this.returningColumns.length > 0) {
|
|
527
|
-
const returningNames = this.returningColumns.map((col) =>
|
|
644
|
+
const returningNames = this.returningColumns.map((col) => col._.name).join(", ");
|
|
528
645
|
const sqlWithReturning = `${updateSql} RETURNING ${returningNames}`;
|
|
529
646
|
return this.db.select(sqlWithReturning, params);
|
|
530
647
|
} else {
|
|
@@ -547,13 +664,19 @@ var DeleteQueryBuilder = class extends BaseQueryBuilder {
|
|
|
547
664
|
}
|
|
548
665
|
returningColumns = [];
|
|
549
666
|
returning(...columns) {
|
|
550
|
-
|
|
667
|
+
for (const col of columns) {
|
|
668
|
+
if (typeof col === "string") {
|
|
669
|
+
this.returningColumns.push(this.table._.columns[col]);
|
|
670
|
+
} else {
|
|
671
|
+
this.returningColumns.push(col);
|
|
672
|
+
}
|
|
673
|
+
}
|
|
551
674
|
return this;
|
|
552
675
|
}
|
|
553
676
|
async execute() {
|
|
554
677
|
const { sql: sql2, params } = this.build();
|
|
555
678
|
if (this.returningColumns.length > 0) {
|
|
556
|
-
const returningNames = this.returningColumns.map((col) =>
|
|
679
|
+
const returningNames = this.returningColumns.map((col) => col._.name).join(", ");
|
|
557
680
|
const sqlWithReturning = `${sql2} RETURNING ${returningNames}`;
|
|
558
681
|
return this.db.select(sqlWithReturning, params);
|
|
559
682
|
} else {
|
|
@@ -577,8 +700,12 @@ var WithQueryBuilder = class {
|
|
|
577
700
|
this.ctes.push({ alias: alias2, query: query.sql, params: query.params });
|
|
578
701
|
return this;
|
|
579
702
|
}
|
|
580
|
-
select(table,
|
|
581
|
-
const builder = new SelectQueryBuilder(
|
|
703
|
+
select(table, selection) {
|
|
704
|
+
const builder = new SelectQueryBuilder(
|
|
705
|
+
this.db,
|
|
706
|
+
table,
|
|
707
|
+
selection
|
|
708
|
+
);
|
|
582
709
|
this.applyWithClause(builder);
|
|
583
710
|
return builder;
|
|
584
711
|
}
|
|
@@ -608,6 +735,8 @@ var WithQueryBuilder = class {
|
|
|
608
735
|
}
|
|
609
736
|
}
|
|
610
737
|
};
|
|
738
|
+
|
|
739
|
+
// src/orm.ts
|
|
611
740
|
var TauriORM = class {
|
|
612
741
|
constructor(db, schema = void 0) {
|
|
613
742
|
this.db = db;
|
|
@@ -662,8 +791,8 @@ var TauriORM = class {
|
|
|
662
791
|
}
|
|
663
792
|
}
|
|
664
793
|
}
|
|
665
|
-
select(
|
|
666
|
-
return new
|
|
794
|
+
select(selection) {
|
|
795
|
+
return new SelectBuilder(this.db, selection);
|
|
667
796
|
}
|
|
668
797
|
insert(table) {
|
|
669
798
|
return new InsertQueryBuilder(this.db, table);
|
|
@@ -786,20 +915,16 @@ var relations = (_table, relationsCallback) => {
|
|
|
786
915
|
}
|
|
787
916
|
});
|
|
788
917
|
};
|
|
789
|
-
var getTableColumns = (table) => {
|
|
790
|
-
return table._.columns;
|
|
791
|
-
};
|
|
792
|
-
var alias = (table, alias2) => {
|
|
793
|
-
return table;
|
|
794
|
-
};
|
|
795
918
|
// Annotate the CommonJS export names for ESM import in node:
|
|
796
919
|
0 && (module.exports = {
|
|
920
|
+
BaseQueryBuilder,
|
|
797
921
|
DeleteQueryBuilder,
|
|
798
922
|
InsertQueryBuilder,
|
|
799
923
|
ManyRelation,
|
|
800
924
|
OneRelation,
|
|
801
925
|
Relation,
|
|
802
926
|
SQLiteColumn,
|
|
927
|
+
SelectBuilder,
|
|
803
928
|
SelectQueryBuilder,
|
|
804
929
|
Table,
|
|
805
930
|
TauriORM,
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// src/
|
|
1
|
+
// src/schema.ts
|
|
2
2
|
var SQLiteColumn = class _SQLiteColumn {
|
|
3
3
|
constructor(name, type, options = {}, mode) {
|
|
4
4
|
this.type = type;
|
|
@@ -9,7 +9,8 @@ var SQLiteColumn = class _SQLiteColumn {
|
|
|
9
9
|
mode: mode || "default",
|
|
10
10
|
notNull: options.notNull ?? false,
|
|
11
11
|
hasDefault: options.default !== void 0 || options.$defaultFn !== void 0,
|
|
12
|
-
autoincrement: options.autoincrement ?? false
|
|
12
|
+
autoincrement: options.autoincrement ?? false,
|
|
13
|
+
table: void 0
|
|
13
14
|
};
|
|
14
15
|
}
|
|
15
16
|
_;
|
|
@@ -102,8 +103,20 @@ var Table = class {
|
|
|
102
103
|
}
|
|
103
104
|
};
|
|
104
105
|
var sqliteTable = (tableName, columns) => {
|
|
105
|
-
|
|
106
|
+
const table = new Table(tableName, columns);
|
|
107
|
+
for (const col of Object.values(columns)) {
|
|
108
|
+
col._.table = table;
|
|
109
|
+
}
|
|
110
|
+
return table;
|
|
111
|
+
};
|
|
112
|
+
var getTableColumns = (table) => {
|
|
113
|
+
return table._.columns;
|
|
106
114
|
};
|
|
115
|
+
var alias = (table, alias2) => {
|
|
116
|
+
return table;
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
// src/query-builder.ts
|
|
107
120
|
var eq = (column, value) => ({
|
|
108
121
|
sql: `${column._.name} = ?`,
|
|
109
122
|
params: [value]
|
|
@@ -210,28 +223,6 @@ var BaseQueryBuilder = class {
|
|
|
210
223
|
}
|
|
211
224
|
query = "";
|
|
212
225
|
params = [];
|
|
213
|
-
where(condition) {
|
|
214
|
-
this.query += ` WHERE ${condition.sql}`;
|
|
215
|
-
this.params.push(...condition.params);
|
|
216
|
-
return this;
|
|
217
|
-
}
|
|
218
|
-
orderBy(column, direction = "ASC") {
|
|
219
|
-
if ("sql" in column) {
|
|
220
|
-
this.query += ` ORDER BY ${column.sql} ${direction}`;
|
|
221
|
-
this.params.push(...column.params);
|
|
222
|
-
} else {
|
|
223
|
-
this.query += ` ORDER BY ${column._.name} ${direction}`;
|
|
224
|
-
}
|
|
225
|
-
return this;
|
|
226
|
-
}
|
|
227
|
-
limit(count2) {
|
|
228
|
-
this.query += ` LIMIT ${count2}`;
|
|
229
|
-
return this;
|
|
230
|
-
}
|
|
231
|
-
offset(count2) {
|
|
232
|
-
this.query += ` OFFSET ${count2}`;
|
|
233
|
-
return this;
|
|
234
|
-
}
|
|
235
226
|
build() {
|
|
236
227
|
return {
|
|
237
228
|
sql: this.query,
|
|
@@ -240,35 +231,134 @@ var BaseQueryBuilder = class {
|
|
|
240
231
|
}
|
|
241
232
|
};
|
|
242
233
|
var SelectQueryBuilder = class extends BaseQueryBuilder {
|
|
243
|
-
constructor(db, table,
|
|
234
|
+
constructor(db, table, selection) {
|
|
244
235
|
super(db);
|
|
245
|
-
this.
|
|
246
|
-
this.
|
|
247
|
-
|
|
248
|
-
this.
|
|
236
|
+
this.selection = selection;
|
|
237
|
+
this.fromTable = table;
|
|
238
|
+
this.query = "";
|
|
239
|
+
this.params = [];
|
|
249
240
|
}
|
|
250
241
|
isDistinct = false;
|
|
251
242
|
groupByColumns = [];
|
|
252
|
-
havingCondition
|
|
243
|
+
havingCondition;
|
|
244
|
+
joinClauses = [];
|
|
245
|
+
fromTable;
|
|
246
|
+
orderByClauses = [];
|
|
247
|
+
limitCount;
|
|
248
|
+
offsetCount;
|
|
249
|
+
whereCondition;
|
|
250
|
+
where(condition) {
|
|
251
|
+
this.whereCondition = condition;
|
|
252
|
+
return this;
|
|
253
|
+
}
|
|
254
|
+
leftJoin(table, on) {
|
|
255
|
+
this.joinClauses.push({ type: "LEFT JOIN", table, on });
|
|
256
|
+
return this;
|
|
257
|
+
}
|
|
258
|
+
innerJoin(table, on) {
|
|
259
|
+
this.joinClauses.push({ type: "INNER JOIN", table, on });
|
|
260
|
+
return this;
|
|
261
|
+
}
|
|
262
|
+
rightJoin(table, on) {
|
|
263
|
+
this.joinClauses.push({ type: "RIGHT JOIN", table, on });
|
|
264
|
+
return this;
|
|
265
|
+
}
|
|
266
|
+
fullJoin(table, on) {
|
|
267
|
+
this.joinClauses.push({ type: "FULL JOIN", table, on });
|
|
268
|
+
return this;
|
|
269
|
+
}
|
|
253
270
|
distinct() {
|
|
254
271
|
this.isDistinct = true;
|
|
255
|
-
this.query = this.query.replace("SELECT", "SELECT DISTINCT");
|
|
256
272
|
return this;
|
|
257
273
|
}
|
|
258
274
|
groupBy(...columns) {
|
|
259
275
|
this.groupByColumns.push(...columns);
|
|
260
|
-
const columnNames = columns.map((col) => col._.name).join(", ");
|
|
261
|
-
this.query += ` GROUP BY ${columnNames}`;
|
|
262
276
|
return this;
|
|
263
277
|
}
|
|
264
278
|
having(condition) {
|
|
265
279
|
this.havingCondition = condition;
|
|
266
|
-
this.query += ` HAVING ${condition.sql}`;
|
|
267
|
-
this.params.push(...condition.params);
|
|
268
280
|
return this;
|
|
269
281
|
}
|
|
282
|
+
orderBy(column, direction = "ASC") {
|
|
283
|
+
if ("sql" in column) {
|
|
284
|
+
this.orderByClauses.push({
|
|
285
|
+
sql: `${column.sql} ${direction}`,
|
|
286
|
+
params: column.params
|
|
287
|
+
});
|
|
288
|
+
} else {
|
|
289
|
+
this.orderByClauses.push({
|
|
290
|
+
sql: `${column._.name} ${direction}`,
|
|
291
|
+
params: []
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
return this;
|
|
295
|
+
}
|
|
296
|
+
limit(count2) {
|
|
297
|
+
this.limitCount = count2;
|
|
298
|
+
return this;
|
|
299
|
+
}
|
|
300
|
+
offset(count2) {
|
|
301
|
+
this.offsetCount = count2;
|
|
302
|
+
return this;
|
|
303
|
+
}
|
|
304
|
+
buildSelectQuery() {
|
|
305
|
+
const selectionEntries = this.selection ? Object.entries(this.selection) : [];
|
|
306
|
+
let columnsClause;
|
|
307
|
+
const selectParams = [];
|
|
308
|
+
if (selectionEntries.length === 0) {
|
|
309
|
+
columnsClause = "*";
|
|
310
|
+
} else {
|
|
311
|
+
columnsClause = selectionEntries.map(([alias2, col]) => {
|
|
312
|
+
if (col instanceof SQLiteColumn) {
|
|
313
|
+
return `${col._.table._.name}.${col._.name} AS "${alias2}"`;
|
|
314
|
+
} else {
|
|
315
|
+
selectParams.push(...col.params);
|
|
316
|
+
return `(${col.sql}) AS "${alias2}"`;
|
|
317
|
+
}
|
|
318
|
+
}).join(", ");
|
|
319
|
+
}
|
|
320
|
+
let query = `SELECT ${this.isDistinct ? "DISTINCT " : ""}${columnsClause} FROM ${this.fromTable._.name}`;
|
|
321
|
+
const queryParams = [...selectParams];
|
|
322
|
+
if (this.joinClauses.length > 0) {
|
|
323
|
+
const joins = this.joinClauses.map((j) => {
|
|
324
|
+
queryParams.push(...j.on.params);
|
|
325
|
+
return `${j.type} ${j.table._.name} ON ${j.on.sql}`;
|
|
326
|
+
});
|
|
327
|
+
query += ` ${joins.join(" ")}`;
|
|
328
|
+
}
|
|
329
|
+
if (this.whereCondition) {
|
|
330
|
+
query += ` WHERE ${this.whereCondition.sql}`;
|
|
331
|
+
queryParams.push(...this.whereCondition.params);
|
|
332
|
+
}
|
|
333
|
+
if (this.groupByColumns.length > 0) {
|
|
334
|
+
const columnNames = this.groupByColumns.map((c) => c._.name).join(", ");
|
|
335
|
+
query += ` GROUP BY ${columnNames}`;
|
|
336
|
+
}
|
|
337
|
+
if (this.havingCondition) {
|
|
338
|
+
query += ` HAVING ${this.havingCondition.sql}`;
|
|
339
|
+
queryParams.push(...this.havingCondition.params);
|
|
340
|
+
}
|
|
341
|
+
if (this.orderByClauses.length > 0) {
|
|
342
|
+
const orderBySql = this.orderByClauses.map((c) => c.sql).join(", ");
|
|
343
|
+
query += ` ORDER BY ${orderBySql}`;
|
|
344
|
+
queryParams.push(...this.orderByClauses.flatMap((c) => c.params));
|
|
345
|
+
}
|
|
346
|
+
if (this.limitCount !== void 0) {
|
|
347
|
+
query += ` LIMIT ${this.limitCount}`;
|
|
348
|
+
}
|
|
349
|
+
if (this.offsetCount !== void 0) {
|
|
350
|
+
query += ` OFFSET ${this.offsetCount}`;
|
|
351
|
+
}
|
|
352
|
+
return {
|
|
353
|
+
sql: query,
|
|
354
|
+
params: queryParams
|
|
355
|
+
};
|
|
356
|
+
}
|
|
357
|
+
build() {
|
|
358
|
+
return this.buildSelectQuery();
|
|
359
|
+
}
|
|
270
360
|
async execute() {
|
|
271
|
-
const { sql: sql2, params } = this.
|
|
361
|
+
const { sql: sql2, params } = this.buildSelectQuery();
|
|
272
362
|
return this.db.select(sql2, params);
|
|
273
363
|
}
|
|
274
364
|
async all() {
|
|
@@ -280,6 +370,19 @@ var SelectQueryBuilder = class extends BaseQueryBuilder {
|
|
|
280
370
|
return result[0];
|
|
281
371
|
}
|
|
282
372
|
};
|
|
373
|
+
var SelectBuilder = class {
|
|
374
|
+
constructor(db, selection) {
|
|
375
|
+
this.db = db;
|
|
376
|
+
this.selection = selection;
|
|
377
|
+
}
|
|
378
|
+
from(table) {
|
|
379
|
+
return new SelectQueryBuilder(
|
|
380
|
+
this.db,
|
|
381
|
+
table,
|
|
382
|
+
this.selection
|
|
383
|
+
);
|
|
384
|
+
}
|
|
385
|
+
};
|
|
283
386
|
var InsertQueryBuilder = class extends BaseQueryBuilder {
|
|
284
387
|
constructor(db, table) {
|
|
285
388
|
super(db);
|
|
@@ -297,7 +400,13 @@ var InsertQueryBuilder = class extends BaseQueryBuilder {
|
|
|
297
400
|
return this;
|
|
298
401
|
}
|
|
299
402
|
returning(...columns) {
|
|
300
|
-
|
|
403
|
+
for (const col of columns) {
|
|
404
|
+
if (typeof col === "string") {
|
|
405
|
+
this.returningColumns.push(this.table._.columns[col]);
|
|
406
|
+
} else {
|
|
407
|
+
this.returningColumns.push(col);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
301
410
|
return this;
|
|
302
411
|
}
|
|
303
412
|
onConflictDoNothing(target) {
|
|
@@ -382,7 +491,7 @@ var InsertQueryBuilder = class extends BaseQueryBuilder {
|
|
|
382
491
|
params.push(...setValues);
|
|
383
492
|
}
|
|
384
493
|
if (this.returningColumns.length > 0) {
|
|
385
|
-
const returningNames = this.returningColumns.map((col) =>
|
|
494
|
+
const returningNames = this.returningColumns.map((col) => col._.name).join(", ");
|
|
386
495
|
const queryWithReturning = `${finalQuery} RETURNING ${returningNames}`;
|
|
387
496
|
const rows = await this.db.select(queryWithReturning, params);
|
|
388
497
|
results = results.concat(rows);
|
|
@@ -417,7 +526,13 @@ var UpdateQueryBuilder = class extends BaseQueryBuilder {
|
|
|
417
526
|
return this;
|
|
418
527
|
}
|
|
419
528
|
returning(...columns) {
|
|
420
|
-
|
|
529
|
+
for (const col of columns) {
|
|
530
|
+
if (typeof col === "string") {
|
|
531
|
+
this.returningColumns.push(this.table._.columns[col]);
|
|
532
|
+
} else {
|
|
533
|
+
this.returningColumns.push(col);
|
|
534
|
+
}
|
|
535
|
+
}
|
|
421
536
|
return this;
|
|
422
537
|
}
|
|
423
538
|
buildUpdateClause() {
|
|
@@ -458,7 +573,7 @@ var UpdateQueryBuilder = class extends BaseQueryBuilder {
|
|
|
458
573
|
async execute() {
|
|
459
574
|
const { sql: updateSql, params } = this.buildUpdateClause();
|
|
460
575
|
if (this.returningColumns.length > 0) {
|
|
461
|
-
const returningNames = this.returningColumns.map((col) =>
|
|
576
|
+
const returningNames = this.returningColumns.map((col) => col._.name).join(", ");
|
|
462
577
|
const sqlWithReturning = `${updateSql} RETURNING ${returningNames}`;
|
|
463
578
|
return this.db.select(sqlWithReturning, params);
|
|
464
579
|
} else {
|
|
@@ -481,13 +596,19 @@ var DeleteQueryBuilder = class extends BaseQueryBuilder {
|
|
|
481
596
|
}
|
|
482
597
|
returningColumns = [];
|
|
483
598
|
returning(...columns) {
|
|
484
|
-
|
|
599
|
+
for (const col of columns) {
|
|
600
|
+
if (typeof col === "string") {
|
|
601
|
+
this.returningColumns.push(this.table._.columns[col]);
|
|
602
|
+
} else {
|
|
603
|
+
this.returningColumns.push(col);
|
|
604
|
+
}
|
|
605
|
+
}
|
|
485
606
|
return this;
|
|
486
607
|
}
|
|
487
608
|
async execute() {
|
|
488
609
|
const { sql: sql2, params } = this.build();
|
|
489
610
|
if (this.returningColumns.length > 0) {
|
|
490
|
-
const returningNames = this.returningColumns.map((col) =>
|
|
611
|
+
const returningNames = this.returningColumns.map((col) => col._.name).join(", ");
|
|
491
612
|
const sqlWithReturning = `${sql2} RETURNING ${returningNames}`;
|
|
492
613
|
return this.db.select(sqlWithReturning, params);
|
|
493
614
|
} else {
|
|
@@ -511,8 +632,12 @@ var WithQueryBuilder = class {
|
|
|
511
632
|
this.ctes.push({ alias: alias2, query: query.sql, params: query.params });
|
|
512
633
|
return this;
|
|
513
634
|
}
|
|
514
|
-
select(table,
|
|
515
|
-
const builder = new SelectQueryBuilder(
|
|
635
|
+
select(table, selection) {
|
|
636
|
+
const builder = new SelectQueryBuilder(
|
|
637
|
+
this.db,
|
|
638
|
+
table,
|
|
639
|
+
selection
|
|
640
|
+
);
|
|
516
641
|
this.applyWithClause(builder);
|
|
517
642
|
return builder;
|
|
518
643
|
}
|
|
@@ -542,6 +667,8 @@ var WithQueryBuilder = class {
|
|
|
542
667
|
}
|
|
543
668
|
}
|
|
544
669
|
};
|
|
670
|
+
|
|
671
|
+
// src/orm.ts
|
|
545
672
|
var TauriORM = class {
|
|
546
673
|
constructor(db, schema = void 0) {
|
|
547
674
|
this.db = db;
|
|
@@ -596,8 +723,8 @@ var TauriORM = class {
|
|
|
596
723
|
}
|
|
597
724
|
}
|
|
598
725
|
}
|
|
599
|
-
select(
|
|
600
|
-
return new
|
|
726
|
+
select(selection) {
|
|
727
|
+
return new SelectBuilder(this.db, selection);
|
|
601
728
|
}
|
|
602
729
|
insert(table) {
|
|
603
730
|
return new InsertQueryBuilder(this.db, table);
|
|
@@ -720,19 +847,15 @@ var relations = (_table, relationsCallback) => {
|
|
|
720
847
|
}
|
|
721
848
|
});
|
|
722
849
|
};
|
|
723
|
-
var getTableColumns = (table) => {
|
|
724
|
-
return table._.columns;
|
|
725
|
-
};
|
|
726
|
-
var alias = (table, alias2) => {
|
|
727
|
-
return table;
|
|
728
|
-
};
|
|
729
850
|
export {
|
|
851
|
+
BaseQueryBuilder,
|
|
730
852
|
DeleteQueryBuilder,
|
|
731
853
|
InsertQueryBuilder,
|
|
732
854
|
ManyRelation,
|
|
733
855
|
OneRelation,
|
|
734
856
|
Relation,
|
|
735
857
|
SQLiteColumn,
|
|
858
|
+
SelectBuilder,
|
|
736
859
|
SelectQueryBuilder,
|
|
737
860
|
Table,
|
|
738
861
|
TauriORM,
|