Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@libsql/hrana-client

libsql403.5kMIT0.8.0TypeScript support: included

Hrana client for connecting to sqld over HTTP or WebSocket

hrana, libsql, sqld, database

readme

Hrana client for TypeScript

API docs | Github | npm

This package implements a Hrana client for TypeScript. Hrana is the protocol for connecting to sqld using WebSocket or HTTP.

This package is intended mostly for internal use. Consider using the @libsql/client package, which will use Hrana automatically.

Usage

import * as hrana from "@libsql/hrana-client";

// Open a `hrana.Client`, which works like a connection pool in standard SQL
// databases. 
const url = process.env.URL ?? "ws://localhost:8080"; // Address of the sqld server
const jwt = process.env.JWT; // JWT token for authentication
// Here we are using Hrana over WebSocket:
const client = hrana.openWs(url, jwt);
// But we can also use Hrana over HTTP:
// const client = hrana.openHttp(url, jwt);

// Open a `hrana.Stream`, which is an interactive SQL stream. This corresponds
// to a "connection" from other SQL databases
const stream = client.openStream();

// Fetch all rows returned by a SQL statement
const books = await stream.query("SELECT title, year FROM book WHERE author = 'Jane Austen'");
// The rows are returned in an Array...
for (const book of books.rows) {
    // every returned row works as an array (`book[1]`) and as an object (`book.year`)
    console.log(`${book.title} from ${book.year}`);
}

// Fetch a single row
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]}`);
}

// Fetch a single value, using a bound parameter
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}`);
}

// Execute a statement that does not return any rows
const res = await stream.run(["DELETE FROM book WHERE author = ?", ["J. K. Rowling"]])
console.log(`${res.affectedRowCount} books have been cancelled`);

// When you are done, remember to close the client
client.close();

changelog

Changelog

0.8.0 -- 2024-09-16

  • Replace isomorphic-fetch dependency with cross-fetch

0.7.0 -- 2024-09-16

0.6.3 -- 2024-08-25

  • Make sure fetch response body is read or cancelled during flush, which fixes random networking errors observed by users.

0.6.2 -- 2024-06-03

  • Make row properties writable to match the behavior of the SQLite client.

0.6.1 -- 2024-06-03

  • Remove confusing 404 error message.

0.6.0 -- 2024-03-28

  • Update isomorphic-fetch dependency for built-in Node fetch(). This package now requires Node 18 or later.

0.5.2 -- 2023-09-11

  • Switch to use Hrana 2 by default to let Hrana 3 cook a bit longer.

0.5.1 -- 2023-09-11

  • Update isomorphic-{fetch, ws} dependencies for Bun support.

0.5.0 -- 2023-07-29

  • Added support for Hrana 3, which included some API changes:
    • Added variant 3 to the ProtocolVersion type
    • Added BatchCond.isAutocommit()
    • Added Stream.getAutocommit()
    • Added parameter useCursor to Stream.batch()
  • Changed meaning of Stream.close(), which now closes the stream immediately
    • Added Stream.closeGracefully()
  • Changed type of StmtResult.lastInsertRowid to bigint
  • Changed BatchCond.and() and BatchCond.or() to pass the Batch object
  • Added Stream.client()
  • Added MisuseError and InternalError
  • Added reexport of WebSocket from @libsql/isomorphic-ws
  • Added reexports of fetch, Request, Response and other types from @libsql/isomorphic-fetch
  • Dropped workarounds for broken WebSocket support in Miniflare 2

0.4.4 -- 2023-08-15

  • Pass a string instead of URL to the Request constructor

0.4.3 -- 2023-07-18

  • Added customFetch argument to openHttp() to override the fetch() function

0.4.2 -- 2023-06-22

  • Added IntMode, Client.intMode and Stream.intMode

0.4.1 -- 2023-06-12

  • Fixed environments that don't support queueMicrotask() by implementing a ponyfill libsql-client-ts#47

0.4.0 -- 2023-06-07

  • Added support for Hrana over HTTP, which included some API changes:
    • Removed open(), replaced with openHttp() and openWs()
    • Added SqlOwner interface for the storeSql() method, which is implemented by WsClient and HttpStream
  • Added HTTP status to HttpServerError
  • Changed parseLibsqlUrl() to support both WebSocket and HTTP URLs
  • Changed Value type to include bigint