Back to packages list

Vals using @libsql/client/web

Deprecated in favor of std/sqlite (also powered by Turso)

std/turso was the initial version of our integration with Turso. It was so popular, we rebuilt it to be faster and easier to use: std/sqlite.

turso (2).png

Turso is a serverless SQLite platform designed for the edge. It runs libSQL, their open contribution fork of SQLite.

Every Val Town user automatically gets their own Turso SQLite database! It's great for >100kb data (ie bigger than a val) or when you need SQL: relations, ACID transactions, etc.

Storage used in Turso will count against your Val Town total storage (10mb for free users; 1gb for Pro users). Contact us if you'd need more – it should be no problem!

Getting started

This val uses our public key auth scheme.

  1. Generate your keypair
  2. On your publicKey click the lock icon🔒 to change the permissions to Unlisted.
  3. Fork this helper function replacing stevekrouse with your own username
  4. Try out some queries!

Usage

This val returns a Turso SDK's Client, which supports execute, batch, and transaction.

Create valawait @me.turso().execute(`create table blobs( key text unique, value text )`)

More example usage

Architecture

This @std.turso function is the client or SDK to @std.tursoAPI, which acts as a "proxy" to Turso. It handles authentication, creates databases, and forwards on your SQL queries. You can get lower latency (~200ms vs ~800ms), more storage, databases, CLI & API access by having your own Turso account.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import { runValAPIAuth } from "https://esm.town/v/stevekrouse/runValAPIAuth";
// grab the types off turso's client without importing it
let tursoImport = () => import("https://esm.sh/@libsql/client/web");
type createClient = Awaited<ReturnType<typeof tursoImport>>["createClient"];
export function turso(keys, handle?) {
let val = "@std.tursoAPI";
let f = (method) => (...args) =>
runValAPIAuth({
val,
args: [method, args],
keys,
handle,
});
return {
execute: f("execute") as ReturnType<createClient>["execute"],
batch: f("batch") as ReturnType<createClient>["batch"],
transaction: f("transaction") as ReturnType<createClient>["transaction"],
};
}
export const endpoint = function(req, res) {
res.end("hello, world!");
};
1
2
3
4
5
6
7
8
9
10
11
12
13
import { createClient } from "https://esm.sh/@libsql/client/web";
import process from "node:process";
let client = createClient({
url: "libsql://grateful-lanolin-stevekrouse.turso.io",
authToken: process.env.tursoGratefulLanolin,
});
export const todo_db_repl = await client.execute(`CREATE TABLE IF NOT EXISTS todos (
id INTEGER PRIMARY KEY AUTOINCREMENT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
text TEXT NOT NULL
)`);

Deprecated in favor of std/sqlite (also powered by Turso)

std/turso was the initial version of our integration with Turso. It was so popular, we rebuilt it to be faster and easier to use: std/sqlite.

turso (2).png

Turso is a serverless SQLite platform designed for the edge. It runs libSQL, their open contribution fork of SQLite.

Every Val Town user automatically gets their own Turso SQLite database! It's great for >100kb data (ie bigger than a val) or when you need SQL: relations, ACID transactions, etc.

Storage used in Turso will count against your Val Town total storage (10mb for free users; 1gb for Pro users). Contact us if you'd need more – it should be no problem!

Getting started

This val uses our public key auth scheme.

  1. Generate your keypair
  2. On your publicKey click the lock icon🔒 to change the permissions to Unlisted.
  3. Fork this helper function replacing stevekrouse with your own username
  4. Try out some queries!

Usage

This val returns a Turso SDK's Client, which supports execute, batch, and transaction.

Create valawait @me.turso().execute(`create table blobs( key text unique, value text )`)

More example usage

Architecture

This @std.turso function is the client or SDK to @std.tursoAPI, which acts as a "proxy" to Turso. It handles authentication, creates databases, and forwards on your SQL queries. You can get lower latency (~200ms vs ~800ms), more storage, databases, CLI & API access by having your own Turso account.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import { runValAPIAuth } from "https://esm.town/v/stevekrouse/runValAPIAuth";
// grab the types off turso's client without importing it
let tursoImport = () => import("https://esm.sh/@libsql/client/web");
type createClient = Awaited<ReturnType<typeof tursoImport>>["createClient"];
export function turso(keys, handle?) {
let val = "@std.tursoAPI";
let f = (method) => (...args) =>
runValAPIAuth({
val,
args: [method, args],
keys,
handle,
});
return {
execute: f("execute") as ReturnType<createClient>["execute"],
batch: f("batch") as ReturnType<createClient>["batch"],
transaction: f("transaction") as ReturnType<createClient>["transaction"],
};
}
1
2
3
4
5
6
7
8
export let tursoOld = async ({ url, authToken }) => {
// https://cdn.jsdelivr.net/npm/@libsql/client/web
const { createClient } = await import("https://esm.sh/@libsql/client/web");
return createClient({
url,
authToken,
});
};
1
Next