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.

It constructs a Client and resolves once that Client is ready, so a database that cannot be opened rejects here. The Client constructor returns before then, for code that needs the waitReady lifecycle.

The overload of this function with no arguments 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();

The overload of this function with one argument, an options object, opens the database that its ClientOptions describe.

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();

The overload of this function with a storage URL as its first argument, and optionally options as its second, opens a database at that 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();