create
The create function opens a TinyJoin database in a dedicated Worker.
create(): Promise<Client>| returns | Promise<Client> | A Promise resolving to a |
|---|
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>| Type | Description | |
|---|---|---|
options | ClientOptions | Storage and Worker configuration. |
| returns | Promise<Client> | A Promise resolving to a |
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>| Type | Description | |
|---|---|---|
dataDir | string | undefined | The |
options? | ClientOptions | Optional Worker configuration. |
| returns | Promise<Client> | A Promise resolving to a |
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();