Skip to content

Node Adapter

Works with Node.js, Bun and Deno.

import {
const makeAdapter: ({ sync, ...options }: NodeAdapterOptions & {
sync?: SyncOptions;
}) => Adapter

Runs everything in the same thread. Use makeWorkerAdapter for multi-threaded implementation.

makeAdapter
} from '@livestore/adapter-node'
import {
const makeWsSync: (options: WsSyncOptions) => SyncBackendConstructor<SyncMetadata>

Creates a sync backend that uses WebSocket to communicate with the sync backend.

@example

import { makeWsSync } from '@livestore/sync-cf/client'
const syncBackend = makeWsSync({ url: 'wss://sync.example.com' })

makeWsSync
} from '@livestore/sync-cf/client'
const
const adapter: Adapter
adapter
=
function makeAdapter({ sync, ...options }: NodeAdapterOptions & {
sync?: SyncOptions;
}): Adapter

Runs everything in the same thread. Use makeWorkerAdapter for multi-threaded implementation.

makeAdapter
({
NodeAdapterOptions.storage: {
readonly type: "in-memory";
readonly importSnapshot?: Uint8Array<ArrayBuffer> | undefined;
} | {
readonly type: "fs";
readonly baseDirectory?: string | undefined;
}
storage
: {
type: "fs"
type
: 'fs' },
// or in-memory:
// storage: { type: 'in-memory' },
sync?: SyncOptions
sync
: {
backend?: SyncBackendConstructor<any, JsonValue>
backend
:
function makeWsSync(options: WsSyncOptions): SyncBackendConstructor<SyncMetadata>

Creates a sync backend that uses WebSocket to communicate with the sync backend.

@example

import { makeWsSync } from '@livestore/sync-cf/client'
const syncBackend = makeWsSync({ url: 'wss://sync.example.com' })

makeWsSync
({
WsSyncOptions.url: string

URL of the sync backend

The protocol can either http/https or ws/wss

url
: 'ws://localhost:8787' }) },
// To enable devtools:
// devtools: { schemaPath: new URL('./schema.ts', import.meta.url) },
})

During development you can instruct the adapter to wipe the locally persisted state and eventlog databases on startup:

import {
const makeAdapter: ({ sync, ...options }: NodeAdapterOptions & {
sync?: SyncOptions;
}) => Adapter

Runs everything in the same thread. Use makeWorkerAdapter for multi-threaded implementation.

makeAdapter
} from '@livestore/adapter-node'
const
const adapter: Adapter
adapter
=
function makeAdapter({ sync, ...options }: NodeAdapterOptions & {
sync?: SyncOptions;
}): Adapter

Runs everything in the same thread. Use makeWorkerAdapter for multi-threaded implementation.

makeAdapter
({
NodeAdapterOptions.storage: {
readonly type: "in-memory";
readonly importSnapshot?: Uint8Array<ArrayBuffer> | undefined;
} | {
readonly type: "fs";
readonly baseDirectory?: string | undefined;
}
storage
: {
type: "fs"
type
: 'fs' },
NodeAdapterOptions.resetPersistence?: boolean

Warning: This will reset both the app and eventlog database. This should only be used during development.

@defaultfalse

resetPersistence
,
})

The worker adapter can be used for more advanced scenarios where it’s preferable to reduce the load of the main thread and run persistence/syncing in a worker thread.

import {
const makeWorkerAdapter: ({ workerUrl, workerExtraArgs, ...options }: NodeAdapterOptions & {
workerUrl: URL;
workerExtraArgs?: JsonValue;
}) => Adapter

Runs persistence and syncing in a worker thread.

makeWorkerAdapter
} from '@livestore/adapter-node'
NodeAdapterOptions.storage: {
readonly type: "in-memory";
readonly importSnapshot?: Uint8Array<ArrayBuffer> | undefined;
} | {
readonly type: "fs";
readonly baseDirectory?: string | undefined;
}
storage
: {
type: "fs"
type
: 'fs' },
workerUrl: URL

Example: new URL('./livestore.worker.ts', import.meta.url)

workerUrl
: new
var URL: new (url: string | URL, base?: string | URL) => URL

The URL interface is used to parse, construct, normalize, and encode URL.

MDN Reference

URL
('./livestore.worker.js', import.

The type of import.meta.

If you need to declare that a given property exists on import.meta, this type may be augmented via interface merging.

meta
.
ImportMeta.url: string

file:// url string for the current module.

The absolute file: URL of the module.

This is defined exactly the same as it is in browsers providing the URL of the current module file.

This enables useful patterns such as relative file loading:

import { readFileSync } from 'node:fs';
const buffer = readFileSync(new URL('./data.proto', import.meta.url));

@example

console.log(import.meta.url);
"file:///Users/me/projects/my-app/src/my-app.ts"

url
),
})
import {
const makeWorker: (options: WorkerOptions) => void
makeWorker
} from '@livestore/adapter-node/worker'
import {
const makeWsSync: (options: WsSyncOptions) => SyncBackendConstructor<SyncMetadata>

Creates a sync backend that uses WebSocket to communicate with the sync backend.

@example

import { makeWsSync } from '@livestore/sync-cf/client'
const syncBackend = makeWsSync({ url: 'wss://sync.example.com' })

makeWsSync
} from '@livestore/sync-cf/client'
import {
const schema: FromInputSchema.DeriveSchema<{
events: {
readonly todoCreated: EventDef<"v1.TodoCreated", {
readonly id: string;
readonly text: string;
}, {
readonly id: string;
readonly text: string;
}, false>;
};
state: InternalState;
}>
schema
} from './schema.ts'
function makeWorker(options: WorkerOptions): void
makeWorker
({
schema: LiveStoreSchema<DbSchema, EventDefRecord>
schema
,
sync?: SyncOptions
sync
: {
backend?: SyncBackendConstructor<any, JsonValue>
backend
:
function makeWsSync(options: WsSyncOptions): SyncBackendConstructor<SyncMetadata>

Creates a sync backend that uses WebSocket to communicate with the sync backend.

@example

import { makeWsSync } from '@livestore/sync-cf/client'
const syncBackend = makeWsSync({ url: 'wss://sync.example.com' })

makeWsSync
({
WsSyncOptions.url: string

URL of the sync backend

The protocol can either http/https or ws/wss

url
: 'ws://localhost:8787' }) },
})