TinyJoin logoTinyJoin

create

Essential

The create function opens a TinyJoin database in a dedicated Worker.

create(): Promise<Client>
returnsPromise<Client>

A Promise resolving to a Client whose Worker and database are ready.

This overload creates an ephemeral in-memory database using the packaged Worker. Its data is lost when the Client closes or the page reloads.

Example

import {create} from 'tinyjoin';

const db = await create();
await db.exec(`
  CREATE TABLE IF NOT EXISTS tasks (
    id INTEGER PRIMARY KEY,
    title TEXT NOT NULL
  )
`);
const {rows} = await db.query('SELECT * FROM tasks ORDER BY id');
await db.close();

Since

v0.0.5

This create overload opens a database with ClientOptions.

create(options: ClientOptions): Promise<Client>
TypeDescription
optionsClientOptions

Storage and Worker configuration.

returnsPromise<Client>

A Promise resolving to a Client whose Worker and database are ready.

Use the options object to select storage or provide a custom Worker. Omitting its dataDir property selects an ephemeral in-memory database.

Example

import {create} from 'tinyjoin';

const db = await create({dataDir: 'memory://'});
await db.close();

Since

v0.0.5

This create overload opens a database at the supplied storage URL.

create(
  dataDir: string | undefined,
  options?: ClientOptions,
): Promise<Client>
TypeDescription
dataDirstring | undefined

The memory:// URL, a named opfs:// URL, or undefined for the default in-memory database.

options?ClientOptions

Optional Worker configuration.

returnsPromise<Client>

A Promise resolving to a Client whose Worker and database are ready.

Use a stable opfs://name to persist data in the same browser, or memory:// for an ephemeral database. When providing the second argument, supply a storage URL and leave options.dataDir unset.

Example

import {create} from 'tinyjoin';

const db = await create('opfs://my-app');
await db.close();

Since

v0.0.5