-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1 parent
77aca8a
commit 5a314e7
Showing
5 changed files
with
436 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
local.db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Local | ||
|
||
This example demonstrates how to use transactions with libSQL. | ||
|
||
## Install Dependencies | ||
|
||
```bash | ||
npm i | ||
``` | ||
|
||
## Running | ||
|
||
Execute the example: | ||
|
||
```bash | ||
node index.mjs | ||
``` | ||
|
||
This example will: | ||
|
||
1. Create a new table called `users`. | ||
2. Start a transaction. | ||
3. Insert multiple users within the transaction. | ||
4. Demonstrate how to rollback a transaction. | ||
5. Start another transaction. | ||
6. Insert more users and commit the transaction. | ||
7. Query and display the final state of the `users` table. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { createClient } from "@libsql/client"; | ||
|
||
const client = createClient({ | ||
url: "file:local.db", | ||
}); | ||
|
||
await client.batch( | ||
[ | ||
"DROP TABLE IF EXISTS users", | ||
"CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)", | ||
"INSERT INTO users (name) VALUES ('Iku Turso')", | ||
], | ||
"write", | ||
); | ||
|
||
const names = ["John Doe", "Mary Smith", "Alice Jones", "Mark Taylor"]; | ||
|
||
try { | ||
const transaction = await client.transaction("write"); | ||
|
||
for (const name of names) { | ||
await transaction.execute({ | ||
sql: "INSERT INTO users (name) VALUES (?)", | ||
args: [name], | ||
}); | ||
} | ||
await transaction.rollback(); | ||
|
||
const secondTransaction = await client.transaction("write"); | ||
|
||
for (const name of names) { | ||
await secondTransaction.execute({ | ||
sql: "INSERT INTO users (name) VALUES (?)", | ||
args: [name], | ||
}); | ||
} | ||
|
||
await secondTransaction.commit(); | ||
} catch (e) { | ||
console.error(e); | ||
await transaction.rollback(); | ||
await secondTransaction.rollback(); | ||
} | ||
|
||
const result = await client.execute("SELECT * FROM users"); | ||
|
||
console.log("Users:", result.rows); |
Oops, something went wrong.