Your First Project
Quick Start showed one file. A real project uses a config file and migrations. This guide builds that scaffold.
Project layout
Section titled “Project layout”Directorymy-app/
- mountsqli.config.ts
Directorysrc/
- db.ts
- main.ts
- package.json
1. Define tables
Section titled “1. Define tables”import { defineTable, int, text, timestamp } from "@mountsqli/core";
export const posts = defineTable("posts", { id: int().pk(), title: text().notNull(), body: text(), createdAt: timestamp().notNull().defaultNow(),});2. Write the config
Section titled “2. Write the config”mountsqli.config.ts is the single source of truth. The Db type is derived
from it — never hand-rolled.
import { defineConfig } from "@mountsqli/core";import { posts } from "./src/tables";
export default defineConfig({ driver: "sqlite", url: "./app.db", // file URL persists _mount_migrations tables: [posts],});3. Derive the typed Db
Section titled “3. Derive the typed Db”import { mountsqli, type DbFromConfig } from "@mountsqli/core";import config from "../mountsqli.config";
export const db = mountsqli(config);export type AppDb = DbFromConfig<typeof config>;4. Generate and apply migrations
Section titled “4. Generate and apply migrations”npx mountsqli migrate generatenpx mountsqli migrate applyThis creates posts and records the step in _mount_migrations.
5. Use it
Section titled “5. Use it”import { db, posts } from "./db";
await db.from(posts).insert({ title: "Hello", body: "First post" });
const all = await db.from(posts).all();console.log(all);Mermaid: project flow
Section titled “Mermaid: project flow”flowchart TD A[tables.ts] --> B[mountsqli.config.ts] B --> C[db.ts: typed Db] C --> D[migrate generate] D --> E[_mount_migrations] C --> F[your app queries]
Best practices
Section titled “Best practices”- Keep tables in their own module and import them into the config.
- Commit
mountsqli.config.ts; it is your schema of record. - Use a file URL (not
:memory:) so migrations persist between runs.
Common mistakes
Section titled “Common mistakes”- Editing the table in code but forgetting
migrate generate— the live DB drifts. - Deriving
Dbby hand instead ofDbFromConfig— you lose type accuracy.
Related
Section titled “Related”- Migrations → How Migrations Work — diff, generate, apply.
- Core Concepts — the config-as-source-of-truth model.
- CLI → Commands — every
mountsqlisubcommand.
