TypeScript + Kysely one-off scripts (SQLite)
Overview
- Run ad-hoc, type-safe scripts against a local
sqlite.db. - Uses Kysely with
better-sqlite3for fast, synchronous SQLite access.
Requirements
- Node.js 18+
Install
-
Install dependencies:
npm install
If you cloned only the files, you may need to initialize first:
npm install kysely better-sqlite3 npm install -D typescript tsx @types/better-sqlite3
-
Optionally adjust the DB path via env var
DB_PATH(default:./sqlite.db).
Project structure
src/db.ts: Creates a Kysely instance for SQLite.src/types.ts: DB table typings for Kysely.src/tasks.ts: Discovers tasks by filename order.src/tasks/*: Task files (use numeric prefixes for order).src/index.ts: Minimal CLI to list/run tasks.
Usage
-
List tasks:
npm run tasks
-
Run all tasks:
npm run run:all
-
Run specific tasks (comma-separated):
npm run run -- run 01_create_people_table,03_print_people_summary
Environment
-
.envis auto-loaded (viadotenv). Create a.envfile with:MOCHI_DECK_ID=yourDeckId MOCHI_TEMPLATE_ID=yourTemplateId MOCHI_BASIC_AUTH=yourUsernameOrToken DB_PATH=./sqlite.db
-
Basic Auth rule: the Mochi API uses HTTP Basic with an empty password. The code constructs the header as
Basic base64(username:). You only need to provide the username/token viaMOCHI_BASIC_AUTH; the script handles the base64 encoding with the trailing colon. -
DB_PATH: path to your SQLite file (default./sqlite.db).
Notes
- Tasks are discovered dynamically; execution order is filename order. Name files like
01_do_this.ts,02_do_that.ts. - The example
peopletable includes a unique index onnameso demo inserts are idempotent. - Add your own tasks under
src/tasks(no registry needed).