serverless: Add Statement.run() method

This commit is contained in:
Pekka Enberg
2025-07-22 15:29:02 +03:00
parent c58511b71c
commit 099dba4ba9
2 changed files with 24 additions and 0 deletions

View File

@@ -45,6 +45,12 @@ test.serial('prepare() method creates statement', async t => {
t.is(rows[0][1], 'John Doe'); t.is(rows[0][1], 'John Doe');
}); });
test.serial('Statement.run()', async t => {
const stmt = client.prepare('INSERT INTO test_users (name, email) VALUES (?, ?)');
const row = await stmt.run(['Jane Doe', 'jane@example.com']);
t.is(row.lastInsertRowid, 2);
});
test.serial('statement iterate() method works', async t => { test.serial('statement iterate() method works', async t => {
// Ensure test data exists // Ensure test data exists
await client.execute('CREATE TABLE IF NOT EXISTS test_users (id INTEGER PRIMARY KEY, name TEXT, email TEXT)'); await client.execute('CREATE TABLE IF NOT EXISTS test_users (id INTEGER PRIMARY KEY, name TEXT, email TEXT)');

View File

@@ -22,6 +22,24 @@ export class Statement {
this.sql = sql; this.sql = sql;
} }
/**
* Executes the prepared statement.
*
* @param args - Optional array of parameter values for the SQL statement
* @returns Promise resolving to the result of the statement
*
* @example
* ```typescript
* const stmt = client.prepare("INSERT INTO users (name, email) VALUES (?, ?)");
* const result = await stmt.run(['John Doe', 'john.doe@example.com']);
* console.log(`Inserted user with ID ${result.lastInsertRowid}`);
* ```
*/
async run(args: any[] = []): Promise<any> {
const result = await this.session.execute(this.sql, args);
return { changes: result.rowsAffected, lastInsertRowid: result.lastInsertRowid };
}
/** /**
* Execute the statement and return the first row. * Execute the statement and return the first row.
* *