libsql-stateless
libSQL http driver for TypeScript and JavaScript running with Web API.
- This is built for edge-functions that quickly spin up, do stuff, and die.
- No bullshit classes and nonsense computation to figure out protocol type and shit.
- Separate and small functions so you're not importing stuff you're not using.
- Is very small package (85 lines of code) and uses very little memory.
- Runs extremely fast on Cloudflare Workers, Vercel Edge Funcrions, etc.
- Works with any libsql server. (eg: Turso, self-hosted, etc.)
Installation
$ npm i libsql-stateless
$ bun add libsql-stateless
Usage
import {execute, executeBatch, checkServerCompat} from 'libsql-stateless';
{execute, executeBatch, checkServerCompat} = require('libsql-stateless');
const res1 = await execute(
{
url: "https://the-pink-mad-man.turso.io",
authToken: "skdfkvskdjvfcsjdvc.bd2y309rehwfg"
},
"SELECT * FROM chicken_yum_yum;"
);
const res2 = await execute(
{
url: "https://the-pink-mad-man.turso.io",
authToken: "skdfkvskdjvfcsjdvc.bd2y309rehwfg"
},
{
q: "SELECT * FROM chicken_yum_yum WHERE owner = ?",
params: ["the_pink_man"]
}
);
{
isOk: true,
val: {
results: {
columns: Array<string>,
rows: Array<Array<_Value>>,
}
}
}
{
isOk: false,
err: string
}
const res3 = await executeBatch(
{
url: "https://the-pink-mad-man.turso.io",
authToken: "skdfkvskdjvfcsjdvc.bd2y309rehwfg"
},
[
{
q: "SELECT * FROM chicken_yum_yum WHERE owner = ?",
params: ["the_pink_man"]
},
{
q: "SELECT * FROM chicken_yum_yum WHERE dead = ?",
params: ["yes"]
},
"SELECT * FROM chicken_yum_yum;",
]
);
{
isOk: true,
val: Array<{
results: {
columns: Array<string>,
rows: Array<Array<_Value>>,
}
}>
}
{
isOk: false,
err: string
}
Because isOk
boolean field is always gaurenteed, you can you something like:
if (res.isOk) {
} else {
}
Other Helper Functions
import {extractBatchQueryResultRows, extractQueryResultRows, checkServerCompat} from 'libsql-stateless';
{extractBatchQueryResultRows, extractQueryResultRows, checkServerCompat} = require('libsql-stateless');
const res1 = await executeBatch(
);
if (res1.isOk) {
const abc = extractBatchQueryResultRows(res);
Array<Array<Array<Values>>>
}
const res2 = await execute(
);
if (res2.isOk) {
const abc = extractQueryResultRows(res);
Array<Array<Values>>
}
Checking Server Compatiblity
import {checkServerCompat} from 'libsql-stateless';
{checkServerCompat} = require('libsql-stateless');
const res = await checkServerCompat("https://the-pink-mad-man.turso.io");
{
isOk: true
}
{
isOk: false
}
if (res.isOk) {
} else {
}
Other Goodies
import {sqlite_text, sqlite_integer, sqlite_real, sqlite_blob, sqlite_null, sqlite_value, sqlite_query} from 'libsql-stateless';
{sqlite_text, sqlite_integer, sqlite_real, sqlite_blob, sqlite_null, sqlite_value, sqlite_query} = require('libsql-stateless');
export type sqlite_value = sqlite_text|sqlite_integer|sqlite_integer|sqlite_real|sqlite_blob|sqlite_null;
export type sqlite_text = string;
export type sqlite_integer = number;
export type sqlite_real = number;
export type sqlite_blob = string;
export type sqlite_null = null;
export type sqlite_query = string | { q: string, params: Record<string, sqlite_value> | Array<sqlite_value> };