Hrana client for TypeScript
API docs | Github | npm
This package implements a Hrana client for TypeScript. Hrana is a protocol based on WebSockets that can be used to connect to sqld. It is more efficient than the postgres wire protocol (especially for edge deployments) and it supports interactive stateful SQL connections (called "streams") which are not supported by the HTTP API.
This package is intended mostly for internal use. Consider using the @libsql/client
package, which will automatically use Hrana if you connect to a ws://
or wss://
URL.
Usage
import * as hrana from "@libsql/hrana-client";
const url = process.env.URL ?? "ws://localhost:8080";
const jwt = process.env.JWT;
const client = hrana.open(url, jwt);
const stream = client.openStream();
const books = await stream.query("SELECT title, year FROM book WHERE author = 'Jane Austen'");
for (const book of books.rows) {
console.log(`${book.title} from ${book.year}`);
}
const book = await stream.queryRow("SELECT title, MIN(year) FROM book");
if (book.row !== undefined) {
console.log(`The oldest book is ${book.row.title} from year ${book.row[1]}`);
}
const year = await stream.queryValue(["SELECT MAX(year) FROM book WHERE author = ?", ["Jane Austen"]]);
if (year.value !== undefined) {
console.log(`Last book from Jane Austen was published in ${year.value}`);
}
const res = await stream.run(["DELETE FROM book WHERE author = ?", ["J. K. Rowling"]])
console.log(`${res.affectedRowCount} books have been cancelled`);
client.close();