@fougere/adapter-sql

Entity → SQL tables through Kysely (SQLite, PostgreSQL, MySQL, SQL Server).

Use it

npm install @fougere/adapter-sql
// fougere.config.ts
import '@fougere/adapter-sql';

export default defineFougere({
  sources: { main: { type: 'sqlite', path: '…' } },
  //   or main: { type: 'pg', url: '…' }
  //   or main: { type: 'mysql', url: '…' }
  //   or main: { type: 'mssql', url: '…' }
  //   or main: { type: 'd1', binding: '…' }
});

The import is what makes the word exist: without it, the editor and the boot both refuse it.

export default class Post extends entity({ … }, { adapters: { sql: { … } } }) {}

Every key each one admits: sources.

What it extends

The interfaces of Fougere this package adds its words to, with declare module — see Interfaces to extend.

interfaceadds
FougereSourcessqlite, pg, mysql, mssql, d1
FougereEntityAdapterssql

Entries: @fougere/adapter-sql

What you import

name
autoMigrateCreate every missing table.
compilerA Kysely bound to a dialect's compiler but to no connection — renders SQL only.
createTableSQLRender CREATE TABLE for one described table.
generateSQLCREATE TABLE for every entity a source holds.
onQueryBe told about every statement this process runs, and get the unsubscription back.
toSnakeCasecamelCase → snake_case
toTableDescribe one entity as a table — the single reader of the axes.
toTableNameorderItem → order_items: the plural, then snake_case.
toTablesEvery entity an app hosts, as tables aware of their foreign keys.
orderTablesTables ordered so a ref()'s target exists before the table pointing at it.
columnTypeForThe type a column is emitted with: what the entity stated for this engine, else the shape's answer.
dialects
SqlStorage
createStorageFactoryCreate a StorageFactory backed by Kysely — same call shape on every engine.
codecsOfField name → codec, for every column that needs one. Identity columns are omitted.
createKyselySourceWrap any Kysely dialect — Postgres, MySQL, SQL Server.
createSqliteSource
createD1Source
driftWhat a live table holds that differs from its declaration, which a migration leaves alone.
actualStateRead the live schema. Only names are needed — an additive pass never inspects types.
changeSQLRender one change.
deltaCompare the two states. Pure — the only place that decides what is missing.
desiredTablesProject a source's entities into the tables they ask for.
migrateBring the database up to what the entities describe — additively.
pendingOfThe tables and columns the database lacks, read and never written.
undeclaredColumnsThe columns a live table keeps that no field declares, for the tables that also gain one.
applyStepRuns a step, or refuses it whole when the plan holds a refusal.
collapseChainCollapse a chain of steps into one, following each field through its renames.
planStepTurn a frozen step into what the tables must do, and what nobody may decide for you.

Types

TypeScript types only — they do not run. Import them with import type to type your own code: a variable, a parameter, an option object.

name
QueryEventOne statement this process ran, and what it cost.
QuerySink
EntityEntryA live class in-process, or one rebuilt from the card of a frond that never crossed.
Dialect
SqlField
SqlFieldsWhat sql holds, addressed by field — the shape every augmentation of the registry takes.
SqliteSource
SqliteSourceOptions
SqlSourceA Source realized by SQL — and the three members that are SQL's, not the routing's.
DriftOne column whose declaration moved and whose table did not follow.
Change
SchemaStateWhat the database actually holds: column names per table.
Plan
StepChangeWhat a step asks of the tables — beyond what an additive pass already covers.
Built with Fougere and Nuxt, hosted on GitHub Pages.