@molecule/api-project-archive-external-state-motherduck

Provider bond · project-archive-external-state · API (Node) · v1.1.0 · Apache-2.0

MotherDuck external-state capture for project archives — exports a project's configured MotherDuck databases (DuckDB EXPORT DATABASE, parquet) into the archive and imports them back on restore

npm install @molecule/api-project-archive-external-state-motherduck

npm · Source on GitHub · Implements @molecule/api-project-archive

How it works

@molecule/api-project-archive-external-state-motherduck is a provider bond on the API (Node) side: it implements the project-archive-external-state core interface (@molecule/api-project-archive) with a concrete vendor or library behind it.

Your code calls the core; you wire this provider once at startup. Swapping vendors later is one line in that wiring, not a rewrite.

import { setExternalStateProvider } from '@molecule/api-project-archive'
import { createMotherduckExternalStateProvider } from '@molecule/api-project-archive-external-state-motherduck'

setExternalStateProvider(
  createMotherduckExternalStateProvider({
    databases: (projectId) => [`app_${projectId.replace(/-/g, '_')}`],
  }),
)

Works with: @molecule/api-project-archive

Secrets: MOTHERDUCK_TOKEN (optional)

Reference

Capture and restore a project's MotherDuck databases for @molecule/api-project-archive.

It runs DuckDB's EXPORT DATABASE (parquet format) through @duckdb/node-api for each configured database and packs the exported files into the archive; restore recreates the database with MotherDuck's CREATE DATABASE IF NOT EXISTS + USE and DuckDB's IMPORT DATABASE.

Auto-generated, AI-first package reference for the molecule.dev ecosystem. It is written to be read by coding agents as much as by people, and is generated from this package's source — edit src/index.ts JSDoc, not this file.

Quick Start

import { setExternalStateProvider } from '@molecule/api-project-archive'
import { createMotherduckExternalStateProvider } from '@molecule/api-project-archive-external-state-motherduck'

setExternalStateProvider(
  createMotherduckExternalStateProvider({
    databases: (projectId) => [`app_${projectId.replace(/-/g, '_')}`],
  }),
)

Type

provider

Installation

npm install @molecule/api-project-archive-external-state-motherduck @duckdb/node-api @molecule/api-project-archive

API

Interfaces

MotherduckExternalStateConfig

How this deployment finds a project's MotherDuck databases, and how to reach them.

interface MotherduckExternalStateConfig {
  /**
   * The names of every MotherDuck database belonging to `projectId`.
   *
   * **This is a DECLARATION, not a search.** An empty array means the project
   * genuinely owns none — and it is the ONLY way to say that. A name that is
   * listed but absent from the account is an ERROR at capture time (the export
   * fails), never an absence: a wrong name would otherwise capture nothing,
   * report success, and let the caller destroy the only copy. Names may be
   * written with or without the `md:` prefix.
   *
   * @param projectId - The project being archived or restored.
   * @returns Its MotherDuck database names; `[]` when it owns none.
   */
  databases: (projectId: string) => readonly string[] | Promise<readonly string[]>

  /**
   * The MotherDuck token. Read lazily at capture/restore time. Defaults to the
   * `MOTHERDUCK_TOKEN` env var.
   */
  token?: string

  /** Which env var holds the token (default `MOTHERDUCK_TOKEN`). */
  tokenEnv?: string

  /**
   * Extra DuckDB settings for the `md:` connections this provider opens
   * (`connection_timeout`, `session_name`, …). `motherduck_token` is set by
   * this package.
   */
  config?: Record<string, string>
}

Functions

createMotherduckExternalStateProvider(config)

Create the provider.

function createMotherduckExternalStateProvider(
  config: MotherduckExternalStateConfig,
): ProjectExternalStateProvider
  • config — How to find a project's databases and reach MotherDuck.

Returns: A provider ready to bond with setExternalStateProvider.

Constants

KIND

Recorded on every record this provider produces; routes restores back here.

const KIND: 'motherduck'

Core Interface

Implements @molecule/api-project-archive interface.

Injection Notes

Requirements

Peer dependencies:

  • @molecule/api-project-archive ^1.0.1

Environment Variables

Runtime Dependencies

  • @duckdb/node-api
  • @molecule/api-project-archive

State in MotherDuck is not in the project's source tree, so a project that owns a md: database needs this provider (or an explicit exemption) — without it the archive verifies clean while the data is destroyed.

A configured database that does not exist FAILS the capture — the export cannot run against it — rather than being read as "this project owns none". Only an empty databases result declares that a project owns no database.

The token is required (MOTHERDUCK_TOKEN env var, or token in the config) and is read at capture/restore time, never at import time. Create one in the MotherDuck UI under Settings → "+ Create token".

Exports materialize locally. EXPORT DATABASE writes every table's parquet to the capture's scratch directory before it is packed, so the host needs disk for the database's full size. The exports are schema + data, not a zero-copy clone; MotherDuck's CREATE DATABASE … FROM snapshot cloning is the lighter in-account option when the archive does not need the bytes.