Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions packages/libsql-client/src/hrana.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as hrana from "@libsql/hrana-client";
import type {
InStatement,
ResultSet,
Row,
Transaction,
TransactionMode,
InArgs,
Expand Down Expand Up @@ -32,11 +33,13 @@ export abstract class HranaTransaction implements Transaction {
abstract close(): void;
abstract get closed(): boolean;

execute(stmt: InStatement): Promise<ResultSet> {
return this.batch([stmt]).then((results) => results[0]);
execute<T extends Row = Row>(stmt: InStatement): Promise<ResultSet<T>> {
return this.batch<T>([stmt]).then((results) => results[0]);
}

async batch(stmts: Array<InStatement>): Promise<Array<ResultSet>> {
async batch<T extends Row = Row>(
stmts: Array<InStatement>,
): Promise<Array<ResultSet<T>>> {
const stream = this._getStream();
if (stream.closed) {
throw new LibsqlError(
Expand Down Expand Up @@ -167,7 +170,7 @@ export abstract class HranaTransaction implements Transaction {
throw mappedError;
}
}
return resultSets;
return resultSets as Array<ResultSet<T>>;
} catch (e) {
throw mapHranaError(e);
}
Expand Down
17 changes: 9 additions & 8 deletions packages/libsql-client/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Config, Client } from "@libsql/core/api";
import type {
InStatement,
ResultSet,
Row,
Transaction,
IntMode,
InArgs,
Expand Down Expand Up @@ -114,10 +115,10 @@ export class HttpClient implements Client {
return this.#promiseLimitFunction(fn);
}

async execute(
async execute<T extends Row = Row>(
stmtOrSql: InStatement | string,
args?: InArgs,
): Promise<ResultSet> {
): Promise<ResultSet<T>> {
let stmt: InStatement;

if (typeof stmtOrSql === "string") {
Expand All @@ -129,7 +130,7 @@ export class HttpClient implements Client {
stmt = stmtOrSql;
}

return this.limit<ResultSet>(async () => {
return this.limit<ResultSet<T>>(async () => {
try {
const hranaStmt = stmtToHrana(stmt);

Expand All @@ -145,18 +146,18 @@ export class HttpClient implements Client {

const rowsResult = await rowsPromise;

return resultSetFromHrana(rowsResult);
return resultSetFromHrana(rowsResult) as ResultSet<T>;
} catch (e) {
throw mapHranaError(e);
}
});
}

async batch(
async batch<T extends Row = Row>(
stmts: Array<InStatement | [string, InArgs?]>,
mode: TransactionMode = "deferred",
): Promise<Array<ResultSet>> {
return this.limit<Array<ResultSet>>(async () => {
): Promise<Array<ResultSet<T>>> {
return this.limit<Array<ResultSet<T>>>(async () => {
try {
const normalizedStmts = stmts.map((stmt) => {
if (Array.isArray(stmt)) {
Expand Down Expand Up @@ -198,7 +199,7 @@ export class HttpClient implements Client {

const results = await resultsPromise;

return results;
return results as Array<ResultSet<T>>;
} catch (e) {
throw mapHranaError(e);
}
Expand Down
33 changes: 19 additions & 14 deletions packages/libsql-client/src/sqlite3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,10 @@ export class Sqlite3Client implements Client {
this.protocol = "file";
}

async execute(
async execute<T extends Row = Row>(
stmtOrSql: InStatement | string,
args?: InArgs,
): Promise<ResultSet> {
): Promise<ResultSet<T>> {
let stmt: InStatement;

if (typeof stmtOrSql === "string") {
Expand All @@ -138,13 +138,13 @@ export class Sqlite3Client implements Client {
}

this.#checkNotClosed();
return executeStmt(this.#getDb(), stmt, this.#intMode);
return executeStmt(this.#getDb(), stmt, this.#intMode) as ResultSet<T>;
}

async batch(
async batch<T extends Row = Row>(
stmts: Array<InStatement | [string, InArgs?]>,
mode: TransactionMode = "deferred",
): Promise<Array<ResultSet>> {
): Promise<Array<ResultSet<T>>> {
this.#checkNotClosed();
const db = this.#getDb();
try {
Expand Down Expand Up @@ -184,7 +184,7 @@ export class Sqlite3Client implements Client {
}
}
executeStmt(db, "COMMIT", this.#intMode);
return resultSets;
return resultSets as Array<ResultSet<T>>;
} finally {
if (db.inTransaction) {
executeStmt(db, "ROLLBACK", this.#intMode);
Expand Down Expand Up @@ -308,13 +308,18 @@ export class Sqlite3Transaction implements Transaction {
this.#intMode = intMode;
}

async execute(stmt: InStatement): Promise<ResultSet>;
async execute(sql: string, args?: InArgs): Promise<ResultSet>;
async execute<T extends Row = Row>(
stmt: InStatement,
): Promise<ResultSet<T>>;
async execute<T extends Row = Row>(
sql: string,
args?: InArgs,
): Promise<ResultSet<T>>;

async execute(
async execute<T extends Row = Row>(
stmtOrSql: InStatement | string,
args?: InArgs,
): Promise<ResultSet> {
): Promise<ResultSet<T>> {
let stmt: InStatement;

if (typeof stmtOrSql === "string") {
Expand All @@ -327,12 +332,12 @@ export class Sqlite3Transaction implements Transaction {
}

this.#checkNotClosed();
return executeStmt(this.#database, stmt, this.#intMode);
return executeStmt(this.#database, stmt, this.#intMode) as ResultSet<T>;
}

async batch(
async batch<T extends Row = Row>(
stmts: Array<InStatement | [string, InArgs?]>,
): Promise<Array<ResultSet>> {
): Promise<Array<ResultSet<T>>> {
const resultSets = [];
for (let i = 0; i < stmts.length; i++) {
try {
Expand Down Expand Up @@ -361,7 +366,7 @@ export class Sqlite3Transaction implements Transaction {
throw e;
}
}
return resultSets;
return resultSets as Array<ResultSet<T>>;
}

async executeMultiple(sql: string): Promise<void> {
Expand Down
17 changes: 9 additions & 8 deletions packages/libsql-client/src/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
Client,
Transaction,
ResultSet,
Row,
InStatement,
InArgs,
Replicated,
Expand Down Expand Up @@ -154,10 +155,10 @@ export class WsClient implements Client {
return this.#promiseLimitFunction(fn);
}

async execute(
async execute<T extends Row = Row>(
stmtOrSql: InStatement | string,
args?: InArgs,
): Promise<ResultSet> {
): Promise<ResultSet<T>> {
let stmt: InStatement;

if (typeof stmtOrSql === "string") {
Expand All @@ -169,7 +170,7 @@ export class WsClient implements Client {
stmt = stmtOrSql;
}

return this.limit<ResultSet>(async () => {
return this.limit<ResultSet<T>>(async () => {
const streamState = await this.#openStream();
try {
const hranaStmt = stmtToHrana(stmt);
Expand All @@ -182,7 +183,7 @@ export class WsClient implements Client {

const hranaRowsResult = await hranaRowsPromise;

return resultSetFromHrana(hranaRowsResult);
return resultSetFromHrana(hranaRowsResult) as ResultSet<T>;
} catch (e) {
throw mapHranaError(e);
} finally {
Expand All @@ -191,11 +192,11 @@ export class WsClient implements Client {
});
}

async batch(
async batch<T extends Row = Row>(
stmts: Array<InStatement | [string, InArgs?]>,
mode: TransactionMode = "deferred",
): Promise<Array<ResultSet>> {
return this.limit<Array<ResultSet>>(async () => {
): Promise<Array<ResultSet<T>>> {
return this.limit<Array<ResultSet<T>>>(async () => {
const streamState = await this.#openStream();
try {
const normalizedStmts = stmts.map((stmt) => {
Expand Down Expand Up @@ -224,7 +225,7 @@ export class WsClient implements Client {

const results = await resultsPromise;

return results;
return results as Array<ResultSet<T>>;
} catch (e) {
throw mapHranaError(e);
} finally {
Expand Down
21 changes: 13 additions & 8 deletions packages/libsql-core/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,11 @@ export interface Client {
* });
* ```
*/
execute(stmt: InStatement): Promise<ResultSet>;
execute(sql: string, args?: InArgs): Promise<ResultSet>;
execute<T extends Row = Row>(stmt: InStatement): Promise<ResultSet<T>>;
execute<T extends Row = Row>(
sql: string,
args?: InArgs,
): Promise<ResultSet<T>>;

/** Execute a batch of SQL statements in a transaction.
*
Expand Down Expand Up @@ -131,10 +134,10 @@ export interface Client {
* ], "write");
* ```
*/
batch(
batch<T extends Row = Row>(
stmts: Array<InStatement | [string, InArgs?]>,
mode?: TransactionMode,
): Promise<Array<ResultSet>>;
): Promise<Array<ResultSet<T>>>;

/** Execute a batch of SQL statements in a transaction with PRAGMA foreign_keys=off; before and PRAGMA foreign_keys=on; after.
*
Expand Down Expand Up @@ -308,14 +311,16 @@ export interface Transaction {
* });
* ```
*/
execute(stmt: InStatement): Promise<ResultSet>;
execute<T extends Row = Row>(stmt: InStatement): Promise<ResultSet<T>>;

/** Execute a batch of SQL statements in this transaction.
*
* If any of the statements in the batch fails with an error, further statements are not executed and the
* returned promise is rejected with an error, but the transaction is not rolled back.
*/
batch(stmts: Array<InStatement>): Promise<Array<ResultSet>>;
batch<T extends Row = Row>(
stmts: Array<InStatement>,
): Promise<Array<ResultSet<T>>>;

/** Execute a sequence of SQL statements separated by semicolons.
*
Expand Down Expand Up @@ -404,7 +409,7 @@ export type TransactionMode = "write" | "read" | "deferred";
* console.log(`Deleted ${rs.rowsAffected} books`);
* ```
*/
export interface ResultSet {
export interface ResultSet<T extends Row = Row> {
/** Names of columns.
*
* Names of columns can be defined using the `AS` keyword in SQL:
Expand All @@ -424,7 +429,7 @@ export interface ResultSet {
columnTypes: Array<string>;

/** Rows produced by the statement. */
rows: Array<Row>;
rows: Array<T>;

/** Number of rows that were affected by an UPDATE, INSERT or DELETE operation.
*
Expand Down