Files
turso/packages/turso-serverless/examples/remote/index.mjs
Pekka Enberg 7765bafb13 Add @tursodatabase/serverless package
This package is for serverless access to the Turso Cloud using SQL over
HTTP protocol. The purpose of this package is to provide the same
interface as `@tursodatabase/turso`, but for serverless environments
that cannot host the database engine.

The package also provides a `@libsql/client` compatibility layer in the
`@tursodatabase/serverless/compat` module for drop-in replacement for
existing clients.
2025-07-21 22:03:43 +03:00

37 lines
1.0 KiB
JavaScript

import { connect } from "@tursodatabase/serverless";
const client = connect({
url: process.env.TURSO_DATABASE_URL,
authToken: process.env.TURSO_AUTH_TOKEN,
});
await client.batch(
[
"CREATE TABLE IF NOT EXISTS users (email TEXT)",
"INSERT INTO users VALUES ('first@example.com')",
"INSERT INTO users VALUES ('second@example.com')",
"INSERT INTO users VALUES ('third@example.com')",
],
"write",
);
// Using execute method
const result = await client.execute("SELECT * FROM users");
console.log("Users (execute):", result.rows);
// Using prepare and get method
const stmt = client.prepare("SELECT * FROM users LIMIT 1");
const firstUser = await stmt.get();
console.log("First user:", firstUser);
// Using prepare and all method
const allUsers = await stmt.all();
console.log("All users (all):", allUsers);
// Using prepare and iterate method
console.log("Users (iterate):");
const iterateStmt = client.prepare("SELECT * FROM users");
for await (const user of iterateStmt.iterate()) {
console.log(" -", user[0]);
}