Skip to content

Commit 80c2767

Browse files
authored
Merge pull request #255 from tursodatabase/simpler-batch-api
use simpler batch input
2 parents d84a4a4 + 0cfe870 commit 80c2767

File tree

6 files changed

+74
-19
lines changed

6 files changed

+74
-19
lines changed

packages/libsql-client/examples/example.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,22 @@ async function example() {
1414
],
1515
"write",
1616
);
17+
18+
await db.batch(
19+
[
20+
{
21+
sql: "INSERT INTO users (email) VALUES (?)",
22+
args: ["alice@example.com"],
23+
},
24+
["INSERT INTO users (email) VALUES (?)", ["bob@example.com"]],
25+
{
26+
sql: "INSERT INTO users (email) VALUES (:email)",
27+
args: { email: "charlie@example.com" },
28+
},
29+
],
30+
"write",
31+
);
32+
1733
const rs = await db.execute("SELECT * FROM users");
1834
console.log(rs);
1935
}

packages/libsql-client/src/hrana.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type {
44
ResultSet,
55
Transaction,
66
TransactionMode,
7+
InArgs,
78
} from "@libsql/core/api";
89
import { LibsqlError } from "@libsql/core/api";
910
import type { SqlCache } from "./sql_cache.js";
@@ -309,17 +310,27 @@ export async function executeHranaBatch(
309310
return resultSets;
310311
}
311312

312-
export function stmtToHrana(stmt: InStatement): hrana.Stmt {
313-
if (typeof stmt === "string") {
314-
return new hrana.Stmt(stmt);
315-
}
313+
export function stmtToHrana(stmt: InStatement | [string, InArgs?]): hrana.Stmt {
314+
let sql: string;
315+
let args: InArgs | undefined;
316316

317-
const hranaStmt = new hrana.Stmt(stmt.sql);
318-
if (Array.isArray(stmt.args)) {
319-
hranaStmt.bindIndexes(stmt.args);
317+
if (Array.isArray(stmt)) {
318+
[sql, args] = stmt;
319+
} else if (typeof stmt === "string") {
320+
sql = stmt;
320321
} else {
321-
for (const [key, value] of Object.entries(stmt.args)) {
322-
hranaStmt.bindName(key, value);
322+
sql = stmt.sql;
323+
args = stmt.args;
324+
}
325+
326+
const hranaStmt = new hrana.Stmt(sql);
327+
if (args) {
328+
if (Array.isArray(args)) {
329+
hranaStmt.bindIndexes(args);
330+
} else {
331+
for (const [key, value] of Object.entries(args)) {
332+
hranaStmt.bindName(key, value);
333+
}
323334
}
324335
}
325336

packages/libsql-client/src/http.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,22 @@ export class HttpClient implements Client {
135135
}
136136

137137
async batch(
138-
stmts: Array<InStatement>,
138+
stmts: Array<InStatement | [string, InArgs?]>,
139139
mode: TransactionMode = "deferred",
140140
): Promise<Array<ResultSet>> {
141141
return this.limit<Array<ResultSet>>(async () => {
142142
try {
143-
const hranaStmts = stmts.map(stmtToHrana);
143+
const normalizedStmts = stmts.map(stmt => {
144+
if (Array.isArray(stmt)) {
145+
return {
146+
sql: stmt[0],
147+
args: stmt[1] || []
148+
};
149+
}
150+
return stmt;
151+
});
152+
153+
const hranaStmts = normalizedStmts.map(stmtToHrana);
144154
const version = await this.#client.getVersion();
145155

146156
// Pipeline all operations, so `hrana.HttpClient` can open the stream, execute the batch and

packages/libsql-client/src/sqlite3.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export class Sqlite3Client implements Client {
139139
}
140140

141141
async batch(
142-
stmts: Array<InStatement>,
142+
stmts: Array<InStatement | [string, InArgs?]>,
143143
mode: TransactionMode = "deferred",
144144
): Promise<Array<ResultSet>> {
145145
this.#checkNotClosed();
@@ -153,7 +153,10 @@ export class Sqlite3Client implements Client {
153153
"TRANSACTION_CLOSED",
154154
);
155155
}
156-
return executeStmt(db, stmt, this.#intMode);
156+
const normalizedStmt: InStatement = Array.isArray(stmt)
157+
? { sql: stmt[0], args: stmt[1] || [] }
158+
: stmt;
159+
return executeStmt(db, normalizedStmt, this.#intMode);
157160
});
158161
executeStmt(db, "COMMIT", this.#intMode);
159162
return resultSets;
@@ -271,10 +274,15 @@ export class Sqlite3Transaction implements Transaction {
271274
return executeStmt(this.#database, stmt, this.#intMode);
272275
}
273276

274-
async batch(stmts: Array<InStatement>): Promise<Array<ResultSet>> {
277+
async batch(
278+
stmts: Array<InStatement | [string, InArgs?]>,
279+
): Promise<Array<ResultSet>> {
275280
return stmts.map((stmt) => {
276281
this.#checkNotClosed();
277-
return executeStmt(this.#database, stmt, this.#intMode);
282+
const normalizedStmt: InStatement = Array.isArray(stmt)
283+
? { sql: stmt[0], args: stmt[1] || [] }
284+
: stmt;
285+
return executeStmt(this.#database, normalizedStmt, this.#intMode);
278286
});
279287
}
280288

packages/libsql-client/src/ws.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,23 @@ export class WsClient implements Client {
192192
}
193193

194194
async batch(
195-
stmts: Array<InStatement>,
195+
stmts: Array<InStatement | [string, InArgs?]>,
196196
mode: TransactionMode = "deferred",
197197
): Promise<Array<ResultSet>> {
198198
return this.limit<Array<ResultSet>>(async () => {
199199
const streamState = await this.#openStream();
200200
try {
201-
const hranaStmts = stmts.map(stmtToHrana);
201+
const normalizedStmts = stmts.map(stmt => {
202+
if (Array.isArray(stmt)) {
203+
return {
204+
sql: stmt[0],
205+
args: stmt[1] || []
206+
};
207+
}
208+
return stmt;
209+
});
210+
211+
const hranaStmts = normalizedStmts.map(stmtToHrana);
202212
const version = await streamState.conn.client.getVersion();
203213

204214
// Schedule all operations synchronously, so they will be pipelined and executed in a single

packages/libsql-core/src/api.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ export interface Client {
123123
* ```
124124
*/
125125
batch(
126-
stmts: Array<InStatement>,
126+
stmts: Array<InStatement | [string, InArgs?]>,
127127
mode?: TransactionMode,
128128
): Promise<Array<ResultSet>>;
129129

@@ -469,7 +469,7 @@ export type Value = null | string | number | bigint | ArrayBuffer;
469469

470470
export type InValue = Value | boolean | Uint8Array | Date;
471471

472-
export type InStatement = { sql: string; args: InArgs } | string;
472+
export type InStatement = { sql: string; args?: InArgs } | string;
473473
export type InArgs = Array<InValue> | Record<string, InValue>;
474474

475475
/** Error thrown by the client. */

0 commit comments

Comments
 (0)