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.

await @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.

Readme
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"],
};
}
👆 This is a val. Vals are TypeScript snippets of code, written in the browser and run on our servers. Create scheduled functions, email yourself, and persist small pieces of data — all from the browser.
Comments
neverstew avatar

@stevekrouse - this needs to be updated so that the usage steps point to using @me.exportedKeys

See https://www.val.town/v/neverstew.tursoExample

neverstew avatar

Or maybe default keys to that

std avatar

That's what Step 4 of the getting started does, it passed your keys in to your personal helper function

neverstew avatar

Ahhh I see... I didn't read that link as an instruction at the time so I forked this turso val