Skip to content

Commit b05074b

Browse files
committed
feat(builder): remove try-catch on execute
1 parent 1891a4a commit b05074b

File tree

2 files changed

+28
-50
lines changed

2 files changed

+28
-50
lines changed

packages/sqlite-builder/src/builder.ts

Lines changed: 25 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ export class SqliteBuilder<DB extends Record<string, any>> {
4242
private logger?: DBLogger
4343
private serializer = defaultSerializer
4444

45+
/**
46+
* sqlite builder
47+
* @param options options
48+
*/
4549
constructor(options: SqliteBuilderOptions) {
4650
const {
4751
dialect,
@@ -123,6 +127,7 @@ export class SqliteBuilder<DB extends Record<string, any>> {
123127

124128
private logError(e: unknown, errorMsg?: string) {
125129
errorMsg && this.logger?.error(errorMsg, e instanceof Error ? e : undefined)
130+
return undefined
126131
}
127132

128133
/**
@@ -139,12 +144,10 @@ export class SqliteBuilder<DB extends Record<string, any>> {
139144
.execute(async (trx) => {
140145
this.trx = trx
141146
this.logger?.debug('run in transaction')
142-
const result = await fn(trx)
143-
return result
147+
return await fn(trx)
144148
})
145149
} catch (e) {
146-
this.logError(e, errorMsg)
147-
return undefined
150+
return this.logError(e, errorMsg)
148151
} finally {
149152
this.trx = undefined
150153
}
@@ -155,16 +158,14 @@ export class SqliteBuilder<DB extends Record<string, any>> {
155158
this.logger?.debug(`run in savepoint:${this.trxCount}`)
156159

157160
try {
158-
// @ts-expect-error _db assert Transaction
159-
const result = await fn(_db)
161+
const result = await fn(_db as Transaction<DB>)
160162
await sp.release()
161163
this.trxCount--
162164
return result
163165
} catch (e) {
164166
await sp.rollback()
165-
this.logError(e, errorMsg)
166167
this.trxCount--
167-
return undefined
168+
return this.logError(e, errorMsg)
168169
}
169170
}
170171

@@ -174,38 +175,28 @@ export class SqliteBuilder<DB extends Record<string, any>> {
174175
*/
175176
public async execute<O>(
176177
fn: (db: Kysely<DB> | Transaction<DB>) => AvailableBuilder<DB, O>,
177-
errorMsg?: string,
178178
): Promise<Simplify<O>[] | undefined> {
179-
try {
180-
return await fn(this.getDB()).execute()
181-
} catch (e) {
182-
this.logError(e, errorMsg)
183-
return undefined
184-
}
179+
return await fn(this.getDB()).execute()
185180
}
186181

187182
/**
188183
* execute and return first result,
189184
* auto detect transaction, auto catch error
185+
*
186+
* if is select, auto append `.limit(1)`
190187
*/
191188
public async executeTakeFirst<O>(
192189
fn: (db: Kysely<DB> | Transaction<DB>) => AvailableBuilder<DB, O>,
193-
errorMsg?: string,
194190
): Promise<Simplify<O> | undefined> {
195-
try {
196-
let _sql = fn(this.getDB())
197-
if (isSelectQueryBuilder(_sql)) {
198-
_sql = _sql.limit(1)
199-
}
200-
return await _sql.executeTakeFirstOrThrow()
201-
} catch (e) {
202-
this.logError(e, errorMsg)
203-
return undefined
191+
let _sql = fn(this.getDB())
192+
if (isSelectQueryBuilder(_sql)) {
193+
_sql = _sql.limit(1)
204194
}
195+
return await _sql.executeTakeFirstOrThrow()
205196
}
206197

207198
/**
208-
* precompile query, call it later
199+
* precompile query, call it with different params later
209200
*
210201
* used for better performance, details: {@link precompileQuery}
211202
*/
@@ -222,14 +213,8 @@ export class SqliteBuilder<DB extends Record<string, any>> {
222213
*/
223214
public async executeCompiled<O>(
224215
query: CompiledQuery<O>,
225-
errorMsg?: string,
226-
): Promise<QueryResult<O> | undefined> {
227-
try {
228-
return await this.getDB().executeQuery(query)
229-
} catch (e) {
230-
this.logError(e, errorMsg)
231-
return undefined
232-
}
216+
): Promise<QueryResult<O>> {
217+
return await this.getDB().executeQuery(query)
233218
}
234219

235220
/**
@@ -238,9 +223,8 @@ export class SqliteBuilder<DB extends Record<string, any>> {
238223
*/
239224
public async executeCompiledTakeList<O>(
240225
query: CompiledQuery<O>,
241-
errorMsg?: string,
242-
): Promise<O[] | undefined> {
243-
const ret = await this.executeCompiled(query, errorMsg)
226+
): Promise<O[]> {
227+
const ret = await this.executeCompiled(query)
244228
return ret?.rows
245229
}
246230

@@ -250,16 +234,10 @@ export class SqliteBuilder<DB extends Record<string, any>> {
250234
*/
251235
public async raw<O = unknown>(
252236
rawSql: RawBuilder<O> | string,
253-
errorMsg?: string,
254-
): Promise<QueryResult<O | unknown> | undefined> {
255-
try {
256-
return typeof rawSql === 'string'
257-
? await this.getDB().executeQuery(CompiledQuery.raw(rawSql))
258-
: await rawSql.execute(this.getDB())
259-
} catch (e) {
260-
this.logError(e, errorMsg)
261-
return undefined
262-
}
237+
): Promise<QueryResult<O | unknown>> {
238+
return typeof rawSql === 'string'
239+
? await this.getDB().executeQuery(CompiledQuery.raw(rawSql))
240+
: await rawSql.execute(this.getDB())
263241
}
264242

265243
/**

packages/sqlite-builder/src/sync/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ export type Columns = Record<string, ColumnProperty>
6363

6464
export type ColumnsWithErrorInfo<T extends Columns> = {
6565
[K in keyof T]: T[K] extends ColumnProperty<
66-
infer Type,
67-
infer DefaultTo,
68-
infer NotNull
66+
infer Type,
67+
infer DefaultTo,
68+
infer NotNull
6969
>
7070
? {
7171
type: Type

0 commit comments

Comments
 (0)