mirror of
https://github.com/aljazceru/turso.git
synced 2025-12-18 17:14:20 +01:00
serverless: Add Statement.run() method
This commit is contained in:
@@ -45,6 +45,12 @@ test.serial('prepare() method creates statement', async t => {
|
||||
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 => {
|
||||
// Ensure test data exists
|
||||
await client.execute('CREATE TABLE IF NOT EXISTS test_users (id INTEGER PRIMARY KEY, name TEXT, email TEXT)');
|
||||
|
||||
@@ -22,6 +22,24 @@ export class Statement {
|
||||
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.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user