Skip to content

Commit

Permalink
Add async API to SQLite
Browse files Browse the repository at this point in the history
  • Loading branch information
dankochetov committed Aug 12, 2023
1 parent 09d699f commit c063144
Show file tree
Hide file tree
Showing 28 changed files with 1,113 additions and 2,482 deletions.
19 changes: 13 additions & 6 deletions drizzle-orm/src/better-sqlite3/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ import { fillPlaceholders, type Query, sql } from '~/sql';
import { SQLiteTransaction } from '~/sqlite-core';
import type { SQLiteSyncDialect } from '~/sqlite-core/dialect';
import type { SelectedFieldsOrdered } from '~/sqlite-core/query-builders/select.types';
import type { PreparedQueryConfig as PreparedQueryConfigBase, SQLiteTransactionConfig } from '~/sqlite-core/session';
import { PreparedQuery as PreparedQueryBase, SQLiteSession } from '~/sqlite-core/session';
import {
PreparedQuery as PreparedQueryBase,
type PreparedQueryConfig as PreparedQueryConfigBase,
type SQLiteExecuteMethod,
SQLiteSession,
type SQLiteTransactionConfig,
} from '~/sqlite-core/session';
import { mapResultRow } from '~/utils';

export interface BetterSQLiteSessionOptions {
Expand Down Expand Up @@ -38,10 +43,11 @@ export class BetterSQLiteSession<
prepareQuery<T extends Omit<PreparedQueryConfig, 'run'>>(
query: Query,
fields: SelectedFieldsOrdered | undefined,
executeMethod: SQLiteExecuteMethod,
customResultMapper?: (rows: unknown[][]) => unknown,
): PreparedQuery<T> {
const stmt = this.client.prepare(query.sql);
return new PreparedQuery(stmt, query.sql, query.params, this.logger, fields, customResultMapper);
return new PreparedQuery(stmt, query.sql, query.params, this.logger, fields, executeMethod, customResultMapper);
}

override transaction<T>(
Expand Down Expand Up @@ -76,7 +82,7 @@ export class BetterSQLiteTransaction<
}

export class PreparedQuery<T extends PreparedQueryConfig = PreparedQueryConfig> extends PreparedQueryBase<
{ type: 'sync'; run: RunResult; all: T['all']; get: T['get']; values: T['values'] }
{ type: 'sync'; run: RunResult; all: T['all']; get: T['get']; values: T['values']; execute: T['execute'] }
> {
static readonly [entityKind]: string = 'BetterSQLitePreparedQuery';

Expand All @@ -86,9 +92,10 @@ export class PreparedQuery<T extends PreparedQueryConfig = PreparedQueryConfig>
private params: unknown[],
private logger: Logger,
private fields: SelectedFieldsOrdered | undefined,
executeMethod: SQLiteExecuteMethod,
private customResultMapper?: (rows: unknown[][]) => unknown,
) {
super();
super('sync', executeMethod);
}

run(placeholderValues?: Record<string, unknown>): RunResult {
Expand All @@ -105,7 +112,7 @@ export class PreparedQuery<T extends PreparedQueryConfig = PreparedQueryConfig>
return stmt.all(...params);
}

const rows = this.values(placeholderValues);
const rows = this.values(placeholderValues) as unknown[][];
if (customResultMapper) {
return customResultMapper(rows) as T['all'];
}
Expand Down
18 changes: 12 additions & 6 deletions drizzle-orm/src/bun-sqlite/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import { fillPlaceholders, type Query, sql } from '~/sql';
import { SQLiteTransaction } from '~/sqlite-core';
import type { SQLiteSyncDialect } from '~/sqlite-core/dialect';
import type { SelectedFieldsOrdered } from '~/sqlite-core/query-builders/select.types';
import type { PreparedQueryConfig as PreparedQueryConfigBase, SQLiteTransactionConfig } from '~/sqlite-core/session';
import type {
PreparedQueryConfig as PreparedQueryConfigBase,
SQLiteExecuteMethod,
SQLiteTransactionConfig,
} from '~/sqlite-core/session';
import { PreparedQuery as PreparedQueryBase, SQLiteSession } from '~/sqlite-core/session';
import { mapResultRow } from '~/utils';

Expand Down Expand Up @@ -44,11 +48,12 @@ export class SQLiteBunSession<

prepareQuery<T extends Omit<PreparedQueryConfig, 'run'>>(
query: Query,
fields?: SelectedFieldsOrdered,
fields: SelectedFieldsOrdered | undefined,
executeMethod: SQLiteExecuteMethod,
customResultMapper?: (rows: unknown[][]) => unknown,
): PreparedQuery<T> {
const stmt = this.client.prepare(query.sql);
return new PreparedQuery(stmt, query.sql, query.params, this.logger, fields, customResultMapper);
return new PreparedQuery(stmt, query.sql, query.params, this.logger, fields, executeMethod, customResultMapper);
}

override transaction<T>(
Expand Down Expand Up @@ -87,7 +92,7 @@ export class SQLiteBunTransaction<
}

export class PreparedQuery<T extends PreparedQueryConfig = PreparedQueryConfig> extends PreparedQueryBase<
{ type: 'sync'; run: void; all: T['all']; get: T['get']; values: T['values'] }
{ type: 'sync'; run: void; all: T['all']; get: T['get']; values: T['values']; execute: T['execute'] }
> {
static readonly [entityKind]: string = 'SQLiteBunPreparedQuery';

Expand All @@ -97,9 +102,10 @@ export class PreparedQuery<T extends PreparedQueryConfig = PreparedQueryConfig>
private params: unknown[],
private logger: Logger,
private fields: SelectedFieldsOrdered | undefined,
executeMethod: SQLiteExecuteMethod,
private customResultMapper?: (rows: unknown[][]) => unknown,
) {
super();
super('sync', executeMethod);
}

run(placeholderValues?: Record<string, unknown>): void {
Expand All @@ -116,7 +122,7 @@ export class PreparedQuery<T extends PreparedQueryConfig = PreparedQueryConfig>
return stmt.all(...params);
}

const rows = this.values(placeholderValues);
const rows = this.values(placeholderValues) as unknown[][];

if (customResultMapper) {
return customResultMapper(rows) as T['all'];
Expand Down
11 changes: 7 additions & 4 deletions drizzle-orm/src/d1/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { SQLiteAsyncDialect } from '~/sqlite-core/dialect';
import type { SelectedFieldsOrdered } from '~/sqlite-core/query-builders/select.types';
import {
type PreparedQueryConfig as PreparedQueryConfigBase,
type SQLiteExecuteMethod,
type SQLiteTransactionConfig,
} from '~/sqlite-core/session';
import { PreparedQuery as PreparedQueryBase, SQLiteSession } from '~/sqlite-core/session';
Expand Down Expand Up @@ -42,11 +43,12 @@ export class SQLiteD1Session<

prepareQuery(
query: Query,
fields?: SelectedFieldsOrdered,
fields: SelectedFieldsOrdered | undefined,
executeMethod: SQLiteExecuteMethod,
customResultMapper?: (rows: unknown[][]) => unknown,
): PreparedQuery {
const stmt = this.client.prepare(query.sql);
return new PreparedQuery(stmt, query.sql, query.params, this.logger, fields, customResultMapper);
return new PreparedQuery(stmt, query.sql, query.params, this.logger, fields, executeMethod, customResultMapper);
}

override async transaction<T>(
Expand Down Expand Up @@ -88,7 +90,7 @@ export class D1Transaction<
}

export class PreparedQuery<T extends PreparedQueryConfig = PreparedQueryConfig> extends PreparedQueryBase<
{ type: 'async'; run: D1Result; all: T['all']; get: T['get']; values: T['values'] }
{ type: 'async'; run: D1Result; all: T['all']; get: T['get']; values: T['values']; execute: T['execute'] }
> {
static readonly [entityKind]: string = 'D1PreparedQuery';

Expand All @@ -98,9 +100,10 @@ export class PreparedQuery<T extends PreparedQueryConfig = PreparedQueryConfig>
private params: unknown[],
private logger: Logger,
private fields: SelectedFieldsOrdered | undefined,
executeMethod: SQLiteExecuteMethod,
private customResultMapper?: (rows: unknown[][]) => unknown,
) {
super();
super('async', executeMethod);
}

run(placeholderValues?: Record<string, unknown>): Promise<D1Result> {
Expand Down
24 changes: 18 additions & 6 deletions drizzle-orm/src/libsql/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type { SQLiteAsyncDialect } from '~/sqlite-core/dialect';
import type { SelectedFieldsOrdered } from '~/sqlite-core/query-builders/select.types';
import {
type PreparedQueryConfig as PreparedQueryConfigBase,
type SQLiteExecuteMethod,
type SQLiteTransactionConfig,
} from '~/sqlite-core/session';
import { PreparedQuery as PreparedQueryBase, SQLiteSession } from '~/sqlite-core/session';
Expand Down Expand Up @@ -41,10 +42,20 @@ export class LibSQLSession<

prepareQuery<T extends Omit<PreparedQueryConfig, 'run'>>(
query: Query,
fields: SelectedFieldsOrdered,
fields: SelectedFieldsOrdered | undefined,
executeMethod: SQLiteExecuteMethod,
customResultMapper?: (rows: unknown[][]) => unknown,
): PreparedQuery<T> {
return new PreparedQuery(this.client, query.sql, query.params, this.logger, fields, this.tx, customResultMapper);
return new PreparedQuery(
this.client,
query.sql,
query.params,
this.logger,
fields,
this.tx,
executeMethod,
customResultMapper,
);
}

/*override */ batch(queries: SQL[]): Promise<ResultSet[]> {
Expand Down Expand Up @@ -96,7 +107,7 @@ export class LibSQLTransaction<
}

export class PreparedQuery<T extends PreparedQueryConfig = PreparedQueryConfig> extends PreparedQueryBase<
{ type: 'async'; run: ResultSet; all: T['all']; get: T['get']; values: T['values'] }
{ type: 'async'; run: ResultSet; all: T['all']; get: T['get']; values: T['values']; execute: T['execute'] }
> {
static readonly [entityKind]: string = 'LibSQLPreparedQuery';

Expand All @@ -107,9 +118,10 @@ export class PreparedQuery<T extends PreparedQueryConfig = PreparedQueryConfig>
private logger: Logger,
private fields: SelectedFieldsOrdered | undefined,
private tx: Transaction | undefined,
executeMethod: SQLiteExecuteMethod,
private customResultMapper?: (rows: unknown[][], mapColumnValue?: (value: unknown) => unknown) => unknown,
) {
super();
super('async', executeMethod);
}

run(placeholderValues?: Record<string, unknown>): Promise<ResultSet> {
Expand All @@ -128,7 +140,7 @@ export class PreparedQuery<T extends PreparedQueryConfig = PreparedQueryConfig>
return (tx ? tx.execute(stmt) : client.execute(stmt)).then(({ rows }) => rows.map((row) => normalizeRow(row)));
}

const rows = await this.values(placeholderValues);
const rows = await this.values(placeholderValues) as unknown[][];

if (customResultMapper) {
return customResultMapper(rows, normalizeFieldValue) as T['all'];
Expand All @@ -152,7 +164,7 @@ export class PreparedQuery<T extends PreparedQueryConfig = PreparedQueryConfig>
return (tx ? tx.execute(stmt) : client.execute(stmt)).then(({ rows }) => normalizeRow(rows[0]));
}

const rows = await this.values(placeholderValues);
const rows = await this.values(placeholderValues) as unknown[][];

if (!rows[0]) {
return undefined;
Expand Down
Loading

0 comments on commit c063144

Please sign in to comment.