dev/setup.ts

Dev workflow helpers for setup, reset, and database management.

Composable functions that consumer projects use in their dev setup and reset tasks. File and env access come through narrow *Deps interfaces from runtime/deps.ts, which a RuntimeDeps from create_node_runtime() satisfies. The Postgres client programs the database helpers run come through PostgresProgramDeps, each a factory the task root writes over its spawn (with gro, (env) => spawn.find('createdb', {pass_env: ['PG*'], env})), so the root decides which binary runs and which environment it sees.

view source

Declarations
#

23 declarations

create_database
#

dev/setup.ts view source

(deps: Pick<PostgresProgramDeps, "createdb">, database_url: string, options?: CreateDatabaseOptions | undefined): Promise<...> import {create_database} from '@fuzdev/fuz_app/dev/setup.js';

Create the PostgreSQL database database_url names, on the server it names, when it's missing.

Runs createdb --no-password --maintenance-db=<maintenance URL> -- <name> (to_createdb_args) through the program deps.createdb makes, with the URL's password in its environment (see PostgresProgramDeps). A URL that isn't Postgres (file://, memory://) has nothing to create and is skipped, as is a missing createdb. Does not throw on a failed createdb — the result says what happened, and the log says why; a createdb that can't start throws.

On a server whose lc_messages isn't English, an existing database is reported as failed, since only Postgres's English refusal is recognized. A capped (truncated) output is read as it came: a clean exit is still success, and a cut stderr is still searched for the refusal.

deps

the createdb factory

type Pick<PostgresProgramDeps, "createdb">

database_url

the DATABASE_URL value

type string

options?

logger

optional

returns

Promise<CreateDatabaseResult>

what happened

mutates

  • external database — creates the database on the server `database_url` names

CreateDatabaseOptions
#

CreateDatabaseResult
#

dev/setup.ts view source

CreateDatabaseResult

type { kind: "created"; database: string; } | { kind: "exists"; database: string; } | { kind: "skipped"; db_type: SetupDbType; message: string; } | { kind: "failed"; message: string; }

import type {CreateDatabaseResult} from '@fuzdev/fuz_app/dev/setup.js';

Result of create_database.

default_setup_logger
#

dev/setup.ts view source

SetupLogger import {default_setup_logger} from '@fuzdev/fuz_app/dev/setup.js';

Default logger using bracket format.

generate_cookie_key
#

PostgresProgramDeps
#

dev/setup.ts view source

PostgresProgramDeps import type {PostgresProgramDeps} from '@fuzdev/fuz_app/dev/setup.js';

The Postgres client programs the database helpers run, each as a factory that makes the program with extra environment, null when it isn't installed. A helper calls each factory once per run with the environment that carries DATABASE_URL's password ({PGPASSWORD}, or {} for a URL with none), so the password reaches the program the way libpq reads it and never its argv. At a gro task root: (env) => spawn.find('createdb', {pass_env: ['PG*'], env}) — env wins over a host PGPASSWORD that PG* forwards, and gro's spawn base passes HOME for ~/.pgpass.

Each program connects to the server DATABASE_URL names: host, port, role, and query parameters (sslmode, a socket host) all come from the URL, through --maintenance-db (to_maintenance_database_url). With no host, as in postgres:///app, libpq uses its default socket.

createdb

Makes createdb, or null when it isn't installed.

type (env: Readonly<Record<string, string>>): Program | null

env

type Readonly<Record<string, string>>
returns Program | null

dropdb

Makes dropdb, or null when it isn't installed.

type (env: Readonly<Record<string, string>>): Program | null

env

type Readonly<Record<string, string>>
returns Program | null

read_env_var
#

dev/setup.ts view source

(deps: Pick<FsReadDeps, "read_text_file">, env_path: string, name: string): Promise<string | undefined> import {read_env_var} from '@fuzdev/fuz_app/dev/setup.js';

Read a single env var from a dotenv-style file.

Delegates to load_env_file so the value is tokenized exactly like every other dotenv read — surrounding quotes, inline comments, and escapes are handled, and a leading export is tolerated. Returns undefined when the file is absent or the variable is unset; a read error other than the file not existing (permission denied, I/O failure) propagates rather than being masked as "not found".

deps

file read capability

type Pick<FsReadDeps, "read_text_file">

env_path

path to the .env file

type string

name

the variable name to read

type string

returns

Promise<string | undefined>

the value, or undefined if the file or variable doesn't exist

throws

  • Error - if reading fails for any reason other than `ENOENT`

reset_bootstrap_token
#

dev/setup.ts view source

(deps: FsReadDeps & FsWriteDeps & FsRemoveDeps & EnvDeps, app_name: string, options?: SetupBootstrapTokenOptions | undefined): Promise<...> import {reset_bootstrap_token} from '@fuzdev/fuz_app/dev/setup.js';

Remove an existing bootstrap token and create a new one, in the state directory setup_bootstrap_token uses.

deps

file, env, and remove capabilities

app_name

application name

type string

options?

state_dir override, logger

optional

returns

Promise<SetupTokenResult>

result from creating the new token

mutates

  • filesystem — removes the existing token file (if any) then writes a fresh one

reset_database
#

dev/setup.ts view source

(deps: PostgresProgramDeps & Pick<FsReadDeps, "stat"> & FsRemoveDeps, database_url: string, options?: ResetDatabaseOptions | undefined): Promise<...> import {reset_database} from '@fuzdev/fuz_app/dev/setup.js';

Reset a database to a clean slate.

For PostgreSQL: drops and recreates the database database_url names, on the server it names, through the programs deps.dropdb and deps.createdb make (see PostgresProgramDeps; argv from to_dropdb_args / to_createdb_args). For PGlite (file://): removes options.pglite_data_dir when given. For an in-memory (memory://), empty, or missing URL: skips. An unsupported URL is an error, never dropped.

deps

the Postgres program factories and file capabilities

database_url

the DATABASE_URL value

type string

options?

pglite_data_dir, logger

optional

returns

Promise<ResetDbResult>

result describing what happened

mutates

  • external database — drops and recreates the PostgreSQL database on the server `database_url` names
  • filesystem — removes `options.pglite_data_dir` recursively for `file://` URLs

ResetDatabaseOptions
#

dev/setup.ts view source

ResetDatabaseOptions import type {ResetDatabaseOptions} from '@fuzdev/fuz_app/dev/setup.js';

Options for reset_database.

pglite_data_dir?

The PGlite data directory to remove for a file:// URL. Named by the caller, never derived from the URL, so a mistyped DATABASE_URL can't aim a recursive remove; without it a file:// reset is skipped.

type string

log?

type SetupLogger

ResetDbResult
#

dev/setup.ts view source

ResetDbResult import type {ResetDbResult} from '@fuzdev/fuz_app/dev/setup.js';

Result of reset_database.

reset

Whether the database was actually reset.

type boolean

skipped

Whether there was nothing to reset (no URL, an in-memory URL, PGlite with no data dir).

type boolean

db_type

What type of database the URL names.

type SetupDbType

seed_dev_account
#

dev/setup.ts view source

(deps: SeedDevAccountDeps, input: SeedDevAccountInput, options?: { log?: SetupLogger | undefined; } | undefined): Promise<...> import {seed_dev_account} from '@fuzdev/fuz_app/dev/setup.js';

Seed a development test account, bypassing the username and password policy: the username is stored lowercased, for the account and its actor's name, its length and grammar unchecked, and the password is hashed as given.

Idempotent by username, compared case-insensitively — if an account with the given username already exists, reuses it and only reconciles the requested role grants. Never updates an existing password (rerun would silently rotate it).

Intended for a dev setup task — do not call in production.

deps

input

options?

type { log?: SetupLogger | undefined; }
optional

returns

Promise<SeedDevAccountResult>

throws

  • Error - if an existing account is found without an associated actor row

mutates

  • database — inserts an account/actor pair when missing and grants any requested role role_grants

SeedDevAccountDeps
#

dev/setup.ts view source

SeedDevAccountDeps import type {SeedDevAccountDeps} from '@fuzdev/fuz_app/dev/setup.js';

Dependencies for seed_dev_account.

inheritance

extends: QueryDeps

hash_password

Password hasher (e.g., argon2_password_deps.hash_password).

type (password: string): Promise<string>

password

type string
returns Promise<string>

SeedDevAccountInput
#

dev/setup.ts view source

SeedDevAccountInput import type {SeedDevAccountInput} from '@fuzdev/fuz_app/dev/setup.js';

username

Account username, stored lowercased, as the account_username_lowercase CHECK requires. Its length and grammar (Username) are not checked.

type string

password

Account password, hashed as given — the password policy is bypassed.

type string

roles?

Roles to grant via role_grant (idempotent).

type readonly string[]

SeedDevAccountResult
#

dev/setup.ts view source

SeedDevAccountResult import type {SeedDevAccountResult} from '@fuzdev/fuz_app/dev/setup.js';

Result of seed_dev_account.

account_id

type string

actor_id

type string

created

True if a new account was created; false if one already existed.

type boolean

setup_bootstrap_token
#

dev/setup.ts view source

(deps: FsReadDeps & FsWriteDeps & EnvDeps, app_name: string, options?: SetupBootstrapTokenOptions | undefined): Promise<...> import {setup_bootstrap_token} from '@fuzdev/fuz_app/dev/setup.js';

Create a bootstrap token file if it doesn't exist.

The token is a one-shot secret used to create the first admin account: 32 random bytes as 64 lowercase hex characters plus one LF, the file openssl rand -hex 32 writes. Stored at {state_dir}/secret_bootstrap_token, state_dir defaulting to ~/.{app_name}; skipped only when no state_dir is given and $HOME is unset.

deps

file and env capabilities

app_name

application name (used for default state directory)

type string

options?

state_dir override, logger

optional

returns

Promise<SetupTokenResult>

result indicating whether a token was created

mutates

  • filesystem — creates the state directory (`0o700`) and writes the token file atomically at `0o600`

setup_env_file
#

dev/setup.ts view source

(deps: FsReadDeps & FsWriteDeps, env_path: string, example_path: string, options?: SetupEnvOptions | undefined): Promise<SetupEnvResult> import {setup_env_file} from '@fuzdev/fuz_app/dev/setup.js';

Create an env file from its example template, auto-generating SECRET_FUZ_COOKIE_KEYS.

If the file already exists, backfills any empty values that have generators. Idempotent — safe to re-run. Values are written through update_env_content, the one env-file writer.

deps

file read and write capabilities

env_path

path for the env file (e.g. .env.development)

type string

example_path

path to the example template

type string

options?

extra replacements, logger

optional

returns

Promise<SetupEnvResult>

result indicating whether the file was created or updated

mutates

  • filesystem — writes `env_path` atomically at mode `0o600` (creating from `example_path` if missing)

SetupBootstrapTokenOptions
#

dev/setup.ts view source

SetupBootstrapTokenOptions import type {SetupBootstrapTokenOptions} from '@fuzdev/fuz_app/dev/setup.js';

state_dir?

State directory override. Defaults to ~/.{app_name}, which needs $HOME.

type string

log?

type SetupLogger

SetupDbType
#

dev/setup.ts view source

SetupDbType

type "postgres" | "pglite" | "memory" | "none"

import type {SetupDbType} from '@fuzdev/fuz_app/dev/setup.js';

The kind of database a DATABASE_URL names, by the scheme create_db routes on: postgres:// / postgresql://, file:// (PGlite), memory:// (pglet), or 'none' for an empty or unsupported URL.

SetupEnvOptions
#

dev/setup.ts view source

SetupEnvOptions import type {SetupEnvOptions} from '@fuzdev/fuz_app/dev/setup.js';

Options for setup_env_file.

replacements?

Extra env var replacements beyond the default SECRET_FUZ_COOKIE_KEYS.

Keys are env var names, values are async generators. Each fills a key the env file assigns an empty value (KEY=, KEY=""), written through update_env_content (quoted when the value needs it, never interpreted); a key the file doesn't assign is left out.

type Record<string, () => Promise<string>>

log?

type SetupLogger

SetupEnvResult
#

dev/setup.ts view source

SetupEnvResult import type {SetupEnvResult} from '@fuzdev/fuz_app/dev/setup.js';

Result of setup_env_file.

created

Whether a new file was created (vs updating existing).

type boolean

updated

Whether any values were generated/replaced.

type boolean

path

The env file path.

type string

SetupLogger
#

dev/setup.ts view source

SetupLogger import type {SetupLogger} from '@fuzdev/fuz_app/dev/setup.js';

Optional logger for setup helpers.

Functions that accept a logger use it for status messages. When omitted, a default bracket-format logger writes to console.

ok

type (msg: string): void

msg

type string
returns void

skip

type (msg: string): void

msg

type string
returns void

error

type (msg: string): void

msg

type string
returns void

SetupTokenResult
#

dev/setup.ts view source

SetupTokenResult import type {SetupTokenResult} from '@fuzdev/fuz_app/dev/setup.js';

created

Whether a new token was created (false if already existed).

type boolean

token_path

The token file path.

type string

Depends on
#