Node.js

Use the Node entrypoint for CLIs, workers, background jobs, and long-running services. It includes env config helpers and lifecycle hooks that keep resource cleanup predictable.

Why use ffdb-client/node

  • Name
    Env config utilities
    Description

    loadEnvConfig() maps Node env values into a full FFDB config object.

  • Name
    Lifecycle safety
    Description

    createNodeLifecycle() wires process shutdown signals to client destroy().

  • Name
    Same core client API
    Description

    You still get db, sql, auth, sync, and getAccess from one client instance.

Node entrypoint surface

import { createClient, createNodeLifecycle, loadEnvConfig } from 'ffdb-client/node'

Key helpers:

type NodeHelpers = {
  loadEnvConfig: (options?: { path?: string; quiet?: boolean }) => FFDB_Config
  createNodeLifecycle: () => LifecycleHooks
}

End-to-end service pattern

import { createClient, createNodeLifecycle, loadEnvConfig } from 'ffdb-client/node'
import type { Database } from './ffdb.types'

const config = loadEnvConfig({ path: '.env', quiet: true })

const { db, sync, destroy } = await createClient<Database>({
  config,
  lifecycle: createNodeLifecycle(),
})

const users = await db.selectFrom('user').select(['id', 'email']).execute()
console.log(users)

await sync?.run()
await destroy()
API_URL="https://your-app.ffdb.forever-frameworks.com"
ORIGIN="https://your-service.example.com"
FFDB_AUTH_TOKEN="your-admin-token"
FFDB_API_KEY="optional-direct-api-key"
RETRY_ATTEMPTS="2"
HEALTH_PROBE_INTERVAL_MS="1000"
LOG_LEVEL="info"
LOG_ENABLED="false"

Lifecycle behavior

createNodeLifecycle() registers cleanup on common process events (SIGINT, SIGTERM, beforeExit) so FFDB resources are released during shutdown.

Why this matters:

  1. Long-running processes avoid leaking timers and listeners.
  2. Repeated test/service boots stay clean without accumulating handlers.
  3. Shutdown behavior is consistent whether exit is manual or orchestrated.

Practical guidance

  1. Use loadEnvConfig() early and pass the resulting config explicitly.
  2. Use createNodeLifecycle() by default for services and workers.
  3. Call destroy() in short-lived scripts for deterministic teardown.
  4. Keep admin credentials in trusted Node environments only.

Next pages

  1. Electron
  2. Reference: createClient
  3. Reference: errors

Was this page helpful?