English
atlas
@c9up/atlas: the ORM: entities and models, repositories, a query builder compiled to SQL by a Rust engine, migrations, a schema builder, seeders, factories and test helpers, on PostgreSQL, MySQL and SQLite. Guide: ORM.
Installation
bash
pnpm add @c9up/atlasream configure @c9up/atlas registers the provider and the @c9up/atlas/commands commands, writes config/database.ts and adds DB_HOST, DB_PORT, DB_DATABASE, DB_USER and DB_PASSWORD to .env. Factories that use faker need @faker-js/faker; the db() test plugin needs @c9up/helix (both optional peers).
Atlas runs on two Rust binaries: the query compiler and the database driver. Both come in one small package per platform, @c9up/atlas-<platform>, for linux x64 and arm64 (glibc), macOS x64 and arm64, and Windows x64. @c9up/atlas lists them as optional dependencies, and your package manager installs only the one that matches your machine. No Rust toolchain is needed. Do not install with optional dependencies turned off (--no-optional, --omit=optional): the binaries would be left out.
There is no JavaScript fallback. Without the binaries, the first query throws E_ATLAS_NAPI_NOT_FOUND and the first connection throws E_ATLAS_DB_DRIVER_NOT_FOUND. Both messages name the package to install, for example @c9up/atlas-linux-x64-gnu.
Entry points
| Import | Contents |
|---|---|
@c9up/atlas | Everything below except the services: models, repositories, queries, decorators, schema and migrations, seeders, factories, test helpers, commands, events, errors |
@c9up/atlas/provider | AtlasProvider (default export) |
@c9up/atlas/services/db | db (default export), the connection registry and createDbService |
@c9up/atlas/orm | BaseEntity, BaseModel, BaseRepository, the entity decorators and the hooks |
@c9up/atlas/schema | Migration (also as BaseSchema), Schema, TableBuilder, ColumnBuilder, ForeignKeyBuilder, AlterViewBuilder, SchemaDumper, RawSql |
@c9up/atlas/factories | factory, Factory, FactoryBuilder, makeFactoryCommand |
@c9up/atlas/seeders | BaseSeeder, Seeder, runSeeders, runSeederDirectory, dbSeedCommand, makeSeederCommand |
@c9up/atlas/testing | useTransaction, truncateAll, testUtils, createDbAssertions, db (helix plugin), Database, factory |
@c9up/atlas/types/relations | The relation property types HasOne, BelongsTo, HasMany, ManyToMany, HasOneThrough, HasManyThrough |
@c9up/atlas/commands | The command loader: getMetaData, getCommand, primaryConnectionName |
@c9up/atlas/configure | configure(codemods), run by ream configure |
Exports
@c9up/atlas
Models and repositories
| Export | Kind | Description |
|---|---|---|
BaseModel | class | Active-record base: static finders and writers on the model's connection, instance save() / delete(). |
BaseEntity | class | Base of an entity: dirty tracking, fill and merge, relations, serialization, domain events. |
BaseRepository | class | Data-mapper access to one entity class on a given connection. |
ModelQuery | class | The query a model or repository builds; resolves to entity instances. |
Paginator | class | A page of results: items, meta (total, perPage, currentPage, lastPage, firstPage), URL helpers. |
setAtlasStrictMode | function | setAtlasStrictMode(enabled: boolean): void: raw SQL entry points (whereRaw, joinRaw…) throw in application code. |
isAtlasStrictMode | function | isAtlasStrictMode(): boolean. |
DomainEvent | type | An event an entity records, flushed after it is saved. |
CreateOptions | interface | { quiet?: boolean; allowExtraProperties?: boolean }. |
RepositoryOptions | interface | { dialect?: AtlasDialect; scope?: QueryScope }. |
DatabaseConnection | interface | What a repository runs on: dialect?, execute, query, transaction?. |
QueryScope | type | (query: ModelQuery<BaseEntity>, table: string) => void. |
ScopedPivot | interface | Pivot constraints of a scoped many-to-many query. |
Decorators and hooks
| Export | Kind | Description |
|---|---|---|
Entity | function | Entity(tableName: string): ClassDecorator. |
Column / column | function | column(options?: ColumnOptions), with column.date(options?), column.dateTime(options?) and column.json(options?). Date columns hydrate to a chronos DateTime. |
PrimaryKey | function | PrimaryKey(options?): PropertyDecorator. |
computed | function | computed(): MethodDecorator: a getter included in serialization. |
BelongsTo / HasOne / HasMany / HasOneThrough / HasManyThrough / ManyToMany | function | Relation decorators; the lower-case forms (belongsTo…) are the same functions. |
SoftDeletes | function | SoftDeletes(): ClassDecorator: delete() sets deleted_at; queries leave deleted rows out, the filter ANDed with the query's clauses as one group. |
hasSoftDeletes | function | hasSoftDeletes(target): boolean. |
scope | function | Declare a reusable query scope on a model. |
beforeSave, afterSave, beforeCreate, afterCreate, beforeUpdate, afterUpdate, beforeDelete, afterDelete, beforeFind, afterFind, beforeFetch, afterFetch, beforePaginate, afterPaginate | function | Lifecycle hook decorators: (): MethodDecorator. |
addHook / removeHook | function | Register or drop a hook without a decorator. |
getEntityMetadata, getColumnMetadata, getRelationMetadata, getPrimaryKey, getDateColumnConfig | function | Read what the decorators recorded. |
ColumnOptions | interface | { type?; nullable?; default?; columnName?; serializeAs?; serialize?; … }. |
DateTimeColumnOptions / DateColumnConfig | interface | { autoCreate?: boolean; autoUpdate?: boolean } and the recorded config. |
JsonColumnOptions | interface | ColumnOptions & { type?: 'json' | 'jsonb' }; jsonb by default. |
ColumnAdapter | interface | { consume(raw): T; prepare(value): unknown }: converts a column between the database and the entity. |
ManyToManyOptions | interface | Pivot table, keys and pivot columns of a many-to-many relation. |
ColumnMetadata / EntityMetadata / RelationMetadata | interface | Recorded metadata. |
HookEvent / HookHandler / HookKind | type | Hook names and handler signature. |
ScopeFn | type | A scope's function. |
Queries and connections
| Export | Kind | Description |
|---|---|---|
QueryBuilder | class | A table query builder compiled by the Rust engine: select, where, whereIn, whereNull, whereExists, orderBy, groupBy, having, limit, offset, union, with, paginate, toSQL. |
RawSql | class | A raw SQL fragment with its bindings, built by db.raw(). |
transaction | function | transaction<T>(db: DatabaseConnection, callback: (trx: TransactionClient) => Promise<T> | T, options?: TransactionOptions): Promise<T>. |
createNapiConnection | function | createNapiConnection(url: string, poolMin = 1, poolMax = 10, pragmas?, retry?: ConnectRetryOptions, observability?: ObservabilityOptions): Promise<AsyncDatabaseConnection>. |
getAtlasDialect / setAtlasDialect | function | The process-wide default dialect. |
registerColumnCast / registerTableCasts | function | registerColumnCast(table, column, type): cast a bound value to a column type (PostgreSQL). |
SQLITE_PROD_PRAGMAS | const | { journal_mode: 'WAL', synchronous: 'NORMAL' }. |
CamelCaseNamingStrategy / defaultNamingStrategy / getNamingStrategy | class / const / function | How property names map to column names, and relations to keys. |
AsyncDatabaseConnection | interface | { dialect; query; execute; runInTransaction(batch); transaction?(); listen?; close(); ping() }. |
TransactionClient | interface | A connection bound to one transaction, with query builders and nested savepoints. |
TransactionOptions | interface | { isolationLevel?: IsolationLevel; … }. |
IsolationLevel | type | 'read uncommitted' | 'read committed' | 'repeatable read' | 'serializable'. |
ConnectRetryOptions / ObservabilityOptions / QueryMeta | interface | Connect retries, query events, the model a query belongs to. |
ListenFn / ListenOptions / Notification / NotificationHandler / Unlisten | type | PostgreSQL LISTEN / NOTIFY through db.listen(). |
AtlasDialect | type | 'sqlite' | 'postgres' | 'mysql'. |
WhereOperator / WhereClause / ExistsClause / OrderByClause / CteDefinition / QueryResult | type | Query builder shapes. |
NamingStrategy / RelationKind | type |
Schema, migrations and seeders
| Export | Kind | Description |
|---|---|---|
Migration / BaseSchema | class | Abstract base of a migration. |
MigrationRunner | class | Runs a directory of migrations on a DatabaseAdapter. |
Schema | class | DDL builder: tables, views, indexes, schemas. |
TableBuilder / ColumnBuilder | class | Columns, keys, indexes and checks of one table. |
SchemaDumper | class | Writes the live schema to a .sql file and a manifest. |
readSchemaDumpManifest / schemaDumpManifestPath | function | Read the manifest next to a dump. |
renderPgCreateTable / renderPgForeignKeyDdl | function | PostgreSQL DDL of an introspected table. |
introspectTable | function | introspectTable(db, dialect, table, schema?): Promise<IntrospectedColumn[] | null>. |
checkSchema | function | checkSchema(entities, db, dialect): Promise<SchemaFinding[]>: compare models' columns with the live schema. |
verifySchema | function | Same, then throw (mode: 'throw', default) or warn on drift. |
runSchemaCheck | function | Same, printed; resolves to an exit code. |
formatSchemaFindings / suggestColumn / typesCompatible | function | Helpers of the schema check. |
BaseSeeder / Seeder | class | Abstract base of a seeder: run(), this.db, static environment. |
runSeeders / runSeederDirectory | function | Run seeder instances, or every seeder file of a directory. |
DatabaseAdapter | interface | What MigrationRunner runs on; runInTransaction(batch) makes each migration atomic. |
MigrationRecord / MigrationState / MigrationStatus | type | Migration tracking. |
SchemaFinding / SchemaFindingKind | type | 'missing-table' | 'missing-in-db' | 'type-mismatch' | 'missing-in-model'. |
IntrospectedColumn / PgForeignKey / SchemaDumperOptions / SchemaDumpManifest / SchemaDumpResult | type | |
ColumnDefinition / ColumnType / DefaultValue / AlterOperation / CheckExpression / CheckOperator / CheckValue / ColumnPosition / ForeignKeyReference / IndexDefinition / ReferentialAction / TableConstraintSpec / TableOptionsSpec / TextVariant | type | Schema builder shapes. |
Commands, factories, testing, events, errors
| Export | Kind | Description |
|---|---|---|
defineConfig | function | defineConfig(config: AtlasDatabaseConfig): AtlasDatabaseConfig. |
configure | function | The ream configure hook. |
makeMigrationCommand, migrationRunCommand, migrationRollbackCommand, migrationStatusCommand, migrationResetCommand, migrationRefreshCommand, migrationFreshCommand, migrationUnlockCommand, dbWipeCommand, dbSeedCommand, makeSeederCommand, makeFactoryCommand, schemaDumpCommand, schemaGenerateCommand, schemaCheckCommand | function | Build each command class with its options (see @c9up/atlas/commands). |
generateSchemaFile / renderSchemaFile | function | What schema:generate writes. |
argument / flag | function | Command argument and flag metadata. |
MigrationCommandOptions / SeederCommandOptions / FactoryCommandOptions / SchemaDumpCommandOptions / SchemaGenerateOptions / AtlasCommandClass / CommandOptions / ArgumentMetaData / FlagMetaData | type | |
factory / Factory | function / const | Model factories (see @c9up/atlas/factories). |
FactoryContext | interface | What a factory callback receives. |
useTransaction / truncateAll | function | Test isolation (see @c9up/atlas/testing). |
TestTransaction | interface | { trx: TransactionClient; rollback: () => Promise<void> }. |
onDbQuery | function | onDbQuery(listener: DbQueryListener): () => void: every statement of a connection with debug: true. |
prettyPrintQuery | function | prettyPrintQuery(event: DbQueryEvent): string. |
onUnsafeStatement | function | onUnsafeStatement(listener: UnsafeStatementListener): () => void: every statement the Rust compiler refused. |
clearDbQueryListeners / clearUnsafeStatementListeners | function | |
DbQueryEvent / DbQueryListener / UnsafeStatementEvent / UnsafeStatementListener | type | |
errors | namespace | The package's errors, one class per code, see Errors |
@c9up/atlas/provider
| Export | Kind | Description |
|---|---|---|
default (AtlasProvider) | provider | Reads config/database.ts (the database key), opens every connection, seats the default one on services/db, verifies the schema when verifySchema is set, runs pending migrations at boot only when migrations.autoRun is on, and closes the connections on shutdown. |
AtlasAppContext | interface | The host the provider needs. |
ConnectionConfig / AtlasDatabaseConfig | interface | See Configuration. |
@c9up/atlas/services/db
| Export | Kind | Description |
|---|---|---|
default (db) | const | The DbService of the default connection. |
createDbService | function | createDbService(resolve: () => AsyncDatabaseConnection, readOnly?: boolean): DbService. |
setDb / getDb / clearDb | function | Seat, read and drop the default connection. |
registerConnection / unregisterConnection / getConnection | function | Named connections: registerConnection(name, connection, config?). |
connectionManager | function | connectionManager(): ConnectionManager. |
setDatabaseConfig / getDatabaseConfig / clearDatabaseConfig / connectionConfigFor / primaryConnectionName | function | The config the provider booted with; connectionConfigFor(name?) merges a connection over the top level. |
DbService | interface | See below. |
DbQueryOptions | interface | { client?: QueryExecutor; mode?: 'read' | 'write' }. |
ConnectionOptions | interface | { mode?: 'read' | 'write' }. |
Member of DbService | Signature | Description |
|---|---|---|
from / table / query | from(table: string): DatabaseQueryBuilder | A table query; from() also takes a subquery and an alias. |
insertQuery | insertQuery(options?: DbQueryOptions): DatabaseQueryBuilder | An insert. |
rawQuery | rawQuery<T>(sql: string, bindings?: unknown[] | Record<string, unknown>): RawQueryBuilder<T> | Raw SQL with positional or named bindings. |
raw / ref | raw(sql: string, params?): RawSql | A raw fragment, a column reference. |
modelQuery | modelQuery<Q>(model: { query(): Q }): Q | A model's query on this connection. |
connection | connection(name?: string, options?: ConnectionOptions): DbService | Another connection, or a read replica with mode: 'read'. |
execute | execute(sql: string, params?: unknown[]): Promise<{ rowsAffected: number }> | |
transaction / runInTransaction | as on the connection | Managed or manual transaction; a batch run atomically. |
listen | as on the connection | PostgreSQL LISTEN / NOTIFY. |
truncate / truncateAllTables | truncate(table: string, cascade?: boolean): Promise<void> | |
getAdvisoryLock / releaseAdvisoryLock | getAdvisoryLock(key: string | number): Promise<boolean> | |
manager / dialect | getters | |
ping / close | ping(): Promise<void> |
@c9up/atlas/testing
| Export | Kind | Description |
|---|---|---|
useTransaction | function | useTransaction(db: AsyncDatabaseConnection): Promise<TestTransaction>: run a test inside a transaction rolled back after it. |
truncateAll | function | truncateAll(db: AsyncDatabaseConnection, ignoreTables?: readonly string[]): Promise<void>. |
testUtils | function | testUtils(conn, options?: DbTestUtilsOptions): { db(): DbTestUtils }: migrate(), truncate(), seed(files?), withGlobalTransaction(). |
createDbAssertions | function | createDbAssertions(conn: DbConnectionLike): DbAssertions: assertHas, assertMissing, assertCount, assertEmpty. |
db | function | db(conn: DbConnectionLike): Plugin: helix plugin setting ctx.db to the assertions. |
Database | class | A small async database for tests: query, queryWithParams, execute, close, asAdapter(). |
factory / Factory / FactoryBuilder | function / const / interface | As in @c9up/atlas/factories. |
DbTestUtils / DbTestUtilsOptions | interface | { migrationsDir?; seedersDir?; ignoreTables? }. |
DbAssertions / DbConnectionLike | interface |
@c9up/atlas/factories
| Export | Kind | Description |
|---|---|---|
factory | function | factory<T extends BaseEntity>(entityClass, defaults): FactoryBuilder<T>. |
Factory | const | Factory.define(entityClass, defaults).build(), and Factory.stubId(generator?). |
FactoryBuilder | interface | merge, mergeRecursive, state, apply, client, connection, relation, with, tap, newUp, pivotAttributes, query, before, after, make, makeMany, makeStubbed, makeStubbedMany, create(db?), createMany(count, db?). |
makeFactoryCommand | function | The make:factory command. |
@c9up/atlas/commands
| Command | Description |
|---|---|
migration:run | Run pending migrations (--schema-path bootstraps from a dump). |
migration:rollback | Roll back the latest batch, or --batch=N; --force in production. |
migration:status | Applied and pending migrations. |
migration:reset | Roll back every migration. |
migration:refresh | Roll back every migration, then run them again. |
migration:fresh | Drop every table, then run every migration. |
migration:unlock | Clear a stuck migration lock. |
db:wipe | Drop every table, the migrations table included. |
db:seed | Run seeders (--files=A,B, --connection=name). |
make:migration | Scaffold a timestamped migration. |
make:seeder | Scaffold a seeder. |
make:factory | Scaffold a factory. |
schema:dump | Dump the schema to a .sql file and a manifest (--prune, --connection, --path). |
schema:generate | Introspect the database and generate model classes. |
atlas:check | Compare the models with the live schema. |
Commands that destroy data refuse to run in production without --force while migrations.disableRollbacksInProduction is on. With the option off, db:wipe, migration:fresh, migration:rollback, migration:reset and migration:refresh ask for confirmation in production, as migration:run does; a missing or failed prompt counts as no.
BaseModel
| Group | Members |
|---|---|
| Find | find, findOrFail, findBy, findByOrFail, findMany, findManyBy, first, firstOrFail, all, query(options?) |
| Write | create, createMany, createQuietly, createManyQuietly, firstOrCreate, firstOrNew, updateOrCreate, updateOrCreateMany, fetchOrCreateMany, fetchOrNewUpMany, truncate |
| Connection | static connection, $connection(), $repo(options?), transaction(callback, options?) or transaction(options?) |
| Instance | save(), saveQuietly(), delete(), deleteQuietly(), useTransaction(trx), useConnection(name), $trx |
BaseEntity
| Group | Members |
|---|---|
| State | $isNew, $isPersisted, $isLocal, $isDeleted, $isDirty, $dirty, $primaryKeyValue, isDirty(...) |
| Attributes | fill(values), merge(values), setExtra / getExtra, enableForceUpdate() |
| Relations | load, loadOnce, loadCount, loadAggregate, related(name), associate, dissociate, attach, detach, sync |
| Reload | refresh(), fresh() |
| Output | serialize(options?), toJSON(), toObject() |
| Domain events | addDomainEvent, getDomainEvents, hasDomainEvents, domainEventCount, clearDomainEvents, flushDomainEvents |
BaseRepository
new BaseRepository(entityClass, db: DatabaseConnection, options?: RepositoryOptions).
| Group | Members |
|---|---|
| Query | query(), find(id), findOrFail(id), findBy(column, value), findByOrFail, findMany, findManyBy, all(), allWithTrashed(), onlyTrashed(), where(column, value) |
| Write | create(data, options?), createMany(rows), createQuietly, createManyQuietly, save(entity), saveQuietly, saveMany(entities), upsert(data, uniqueBy), firstOrCreate(where, create?), firstOrNew(where, create?), updateOrCreate(where, data, options?), updateOrCreateMany(key, rows, options?), fetchOrCreateMany, fetchOrNewUpMany, updateById(id, data), updateWhere(column, value, data) |
| Delete | delete(entity), deleteQuietly, forceDelete(entity), restore(entity), truncate() |
| Counters | increment(id, column, amount?) or increment(id, { colA: 1 }); decrement likewise |
| Relations | loadRelation(entity, name, callback?), relatedProxy(entity, name), loadCount(entity, name, alias?), loadAggregate(entity, name, builder), refresh(entity), fresh(entity) |
| Low level | useTransaction(trx), raw(sql, ...params), execute, getTableName(), getPrimaryKeyColumn() |
ModelQuery
A ModelQuery is PromiseLike: await runs it. Every where* and having* method has and* and or* forms.
| Group | Members |
|---|---|
| Where | where, whereNot, whereNull, whereNotNull, whereIn, whereNotIn, whereBetween, whereNotBetween, whereLike, whereILike, whereColumn, whereNotColumn, whereExists, whereNotExists, whereRaw(sql, bindings), whereExpr(column, operator, value), whereJson, whereNotJson, whereJsonPath, whereJsonSubset / whereJsonSuperset and their Of and Not forms |
| Relations | preload(relation, callback?), preloadOnce, sideload, has, doesntHave, whereHas, whereDoesntHave, withCount(relation, callback?), withAggregate(relation, callback) |
| Pivot | wherePivot, whereNotPivot, wherePivotIn, whereInPivot, whereNotInPivot, whereNullPivot, whereNotNullPivot, pivotColumns |
| Select and aggregates | select, distinct, distinctOn, count, countDistinct, sum, sumDistinct, avg, avgDistinct, min, max, selectAggregate, as(alias) |
| Joins | join, innerJoin, leftJoin, leftOuterJoin, rightJoin, rightOuterJoin, fullOuterJoin, crossJoin, joinOn(table, left, right), joinRaw(fragment), with on, onVal, onIn, onNull, onBetween, onExists and their negations |
| Grouping | groupBy, groupByRaw, having*, groupLimit, groupOrderBy |
| Order and pages | orderBy, orderByRaw, limit, offset, forPage, paginate(page, perPage), cursorPaginate({ perPage, cursor, orderBy }) |
| Sets and CTEs | union, unionAll, intersect, intersectAll, except, exceptAll, with, withRecursive, withMaterialized, withNotMaterialized |
| Scopes and conditions | apply(callback), withScopes(callback), if, unless, ifDialect, unlessDialect, match |
| Soft deletes | withTrashed, onlyTrashed, restore, forceDelete |
| Locking | forUpdate, forShare, forNoKeyUpdate, forKeyShare, skipLocked, noWait, timeout |
| Run | exec(), first(), firstOrFail(), sole(), exists(), doesntExist(), pluck(column), all() |
| Write | update(patch, returning?), delete(returning?), increment(column, amount) or increment({ … }), decrement likewise |
| Inspect | toSQL(), toQuery(), toNative(), debug(flag), comment(text), reporterData, clone(), useTransaction(trx) |
| Clear | clearSelect, clearWhere (the relation key, scope and soft-delete filter stay), clearOrder, clearHaving, clearLimit, clearOffset |
| Group | wrapExisting(): close the clauses chained so far into one group |
The eight aggregates share one signature. Each takes a column, with an alias inline, as a second argument, or as the key of an object, and returns the builder. The value comes back as a column of the result row, in $extras on the model builder:
ts
const [row] = await db.from('sales').count('* as total')
Number(row.total)
const [order] = await Order.query().sum('amount as spent').countDistinct('sku', 'items')
Number(order.$extras.spent)None of them runs the query on its own. On db.from(), the argument is a column or *: an expression is refused with E_UNSAFE_IDENTIFIER; give it to select() instead (select('sum(price * quantity) as total')).
The DatabaseQueryBuilder that db.from() returns has the same filtering, join, grouping, set and locking methods, plus insert, multiInsert, returning, onConflict(...).ignore() / .merge(), update, delete / del, increment, decrement, withSchema and paginate.
insert() takes one row or an array of rows; an array works like multiInsert(). A row with no column inserts one row of defaults: INSERT INTO "t" DEFAULT VALUES on Postgres and SQLite, INSERT INTO `t` () VALUES () on MySQL. An empty array has no row, so insert([]) and multiInsert([]) fail with E_ATLAS_EMPTY_INSERT. Several rows that set no column fail the same way: only one row can take every default. On SQLite, an upsert (onConflict()) of a row with no column fails with E_UNSUPPORTED, because SQLite takes no ON CONFLICT after DEFAULT VALUES.
onConflict() with no column means any unique constraint. With ignore(), it compiles ON CONFLICT DO NOTHING on Postgres and SQLite, and INSERT IGNORE on MySQL. With merge(), it compiles ON CONFLICT DO UPDATE on SQLite and ON DUPLICATE KEY UPDATE on MySQL. Postgres needs the conflict columns to update a row, so onConflict().merge() fails there with E_UNSUPPORTED: name the columns, as in onConflict('email').merge().
Migration
| Member | Signature | Description |
|---|---|---|
up / down | abstract up(): Promise<void> | void | The change and its reverse. |
schema | readonly schema: Schema | The DDL builder. |
defer | defer(callback: (db) => Promise<void> | void): void | Run code against the connection after the schema statements, in the migration's transaction. |
raw | raw(expression: string): RawSql | A raw SQL expression for a column default, written as is. |
now | now(): RawSql | CURRENT_TIMESTAMP on SQLite, NOW() elsewhere, for a column default. |
disableTransactions | static disableTransactions = false | Run this migration outside a transaction. |
dryRun | dryRun = false | Set while the runner collects SQL without writing. |
getUpSQL / getDownSQL | getUpSQL(): Promise<string[]> | The compiled statements. |
MigrationRunner
new MigrationRunner(db: DatabaseAdapter, options?: { migrationsDir?; dialect?; tableName?; … }); database/migrations and table ream_migrations by default.
| Member | Signature | Description |
|---|---|---|
init | init(): Promise<void> | Create the tracking table. |
status | status(): Promise<MigrationStatus[]> | |
migrate | migrate(options?: { schemaPath?: string }): Promise<string[]> | Run pending migrations, each atomically when the adapter has runInTransaction. |
rollback | rollback(options?: { batch?: number; force?: boolean }): Promise<string[]> | The last batch, or batch N. |
reset / refresh | refresh(): Promise<{ rolled: string[]; executed: string[] }> | Roll back everything; refresh then migrates again. |
fresh | fresh(options?: { force?: boolean; schemaPath?: string }): Promise<{ rolled: string[]; executed: string[] }> | Drop every table, then migrate. |
wipe | wipe(options?: { force?: boolean }): Promise<void> | Drop every table. |
dryRun | dryRun() | The SQL pending migrations would run, without writing. |
loadDump | loadDump(sqlPath: string): Promise<void> | Run the statements of a schema dump. |
forceUnlock | forceUnlock(): Promise<boolean> | Clear a stale lock. |
Schema and TableBuilder
| Builder | Members |
|---|---|
Schema | createTable, createTableIfNotExists, createTableLike, alterTable / table, renameTable, dropTable, dropTableIfExists, hasTable, hasColumn, createIndex, dropIndex, createView, createViewOrReplace, alterView, renameView, dropView, dropViewIfExists, createMaterializedView, refreshMaterializedView, dropMaterializedView, dropMaterializedViewIfExists, createSchema, createSchemaIfNotExists, dropSchema, dropSchemaIfExists, withSchema, raw, toSQL, reset |
TableBuilder columns | increments, bigIncrements, id, integer, bigInteger, smallint, tinyint, mediumint, float, double, decimal, boolean, string, text, binary, uuid, enum, json, jsonb, date, time, dateTime, timestamp, timestamptz, timestamps, specificType |
TableBuilder keys and constraints | primary, unique, uniqueIndex, index, foreign, check, checkBetween, checkIn, checkNotIn, checkLength, checkPositive, checkNegative, checkRegex |
TableBuilder alterations | renameColumn, dropColumn, dropColumns, dropPrimary, dropUnique, dropIndex, dropForeign, dropChecks, dropTimestamps, setNullable, dropNullable, engine, charset, collate, comment |
ColumnBuilder | notNullable, nullable, defaultTo, unsigned, primary, unique, index, references, inTable, onDelete, onUpdate, comment, first, after, alter, check* |
Configuration
config/database.ts, read under the database key.
| Option | Type | Default | Description |
|---|---|---|---|
connection | string | 'primary' | The default connection when connections is set (default is accepted as an older alias). |
connections | Record<string, ConnectionConfig> | — | Named connections. |
verifySchema | { entities; mode?: 'throw' | 'warn' } | unset | Compare these models with the live schema after boot; throw by default. |
prettyPrintDebugQueries | boolean | false | Print each statement of a debug connection. |
<connection>.url / .connection | string | — | sqlite:data/app.db, postgres://…, mysql://…. url wins. |
<connection>.pool | { min?: number; max?: number } | { min: 1, max: 10 } | Pool size (poolMin / poolMax are accepted too). |
<connection>.debug | boolean | false | Emit a db:query event per statement. |
<connection>.pragmas | Record<string, string | number> | — | SQLite pragmas, such as SQLITE_PROD_PRAGMAS. |
<connection>.connectRetries | number | 0 | Extra attempts of the first connection. |
<connection>.connectBackoffMs | number | 200 | Base of the exponential backoff, capped at 30 s. |
<connection>.connectTimeoutMs | number | — | Per-attempt acquire timeout. |
<connection>.migrations.paths | string[] | ['database/migrations'] | Migration directories. |
<connection>.migrations.tableName | string | 'ream_migrations' | Tracking table; a plain identifier. |
<connection>.migrations.disableRollbacksInProduction | boolean | true | Refuse rollback, reset, refresh, fresh and wipe in production without --force. |
<connection>.migrations.autoRun | boolean | false | Run pending migrations at boot; ignored in development. |
<connection>.migrations.naturalSort | boolean | false | Sort 2_x before 10_x. |
<connection>.migrations.disableTransactions | boolean | false | Run every migration outside a transaction. |
<connection>.seeders.paths | string[] | ['database/seeders'] | Seeder directory (the first is used). |
<connection>.seeders.naturalSort | boolean | false | |
<connection>.factories.path | string | 'database/factories' | Where make:factory writes. |
<connection>.schemaGeneration | SchemaGenerateOptions | — | outputPath and options of schema:generate. |
Container bindings
| Token | Resolves to |
|---|---|
'atlas.db' / 'db' | The default AsyncDatabaseConnection |
'atlas.db.connection' / 'db.connection' | The same connection |
'atlas.db:<name>' / 'db:<name>' | A named connection |
All are typed on ContainerBindings once @c9up/atlas is imported.
Errors
Each code is a class of the errors namespace (import { errors } from '@c9up/atlas') and a @c9up/flare Exception: branch on error.code or on error instanceof errors.E_…. The status is 500, except E_ROW_NOT_FOUND (404); help, when set, says how to fix the cause. The Rust query compiler's and driver's refusals (E_UNSAFE_*, E_INJECTION_PATTERN, E_UNSUPPORTED, E_ATLAS_INSECURE_SSLMODE…) are rethrown as their class too, with the native error as cause. Every compiler refusal carries a code, a malformed spec (E_ATLAS_INVALID_SPEC) and a compiler panic (E_ATLAS_INTERNAL) included. See Error handling for the classes that carry data.
| Code | Raised when |
|---|---|
E_ROW_NOT_FOUND | A query that must return a row found none: findOrFail, findByOrFail, refresh, fresh, firstOrFail(), sole(). |
E_MASS_ASSIGNMENT | fill() / merge() (and create, createMany, updateOrCreate) set an attribute outside fillable, or listed in guarded. |
E_EXTRA_PROPERTIES | fill() / merge(), or updateOrCreate / updateOrCreateMany on a found row, named a property that is not a declared column. |
E_UNKNOWN_COLUMN | A value was assigned to a key that is not a column. Pass { allowExtraProperties: true } to keep unknown keys in $extras. |
E_INVALID_COLUMN | A query names a column the entity does not declare. |
E_INVALID_FIND_VALUE | find() was called without a usable primary key value. |
E_MODEL_NOT_PERSISTED | An operation that needs a stored row (delete(), refresh(), load*()…) ran on a local instance. |
E_MISSING_PRIMARY_KEY | The same operation ran on a persisted projection that holds no primary key. |
E_MODEL_DELETED | save() was called on a deleted instance. |
E_MISSING_OWNER_KEY | associate() was given a parent whose key is not set. |
E_RELATION_NOT_LOADED | A relation was read before it was loaded. Call .preload() on the query or .load() on the instance. |
E_OPTIMISTIC_LOCK | A save lost an optimistic lock: another transaction modified the row since it was read. |
E_STRICT_MODE | Raw SQL refused by strict mode (a repository's raw(), a db.raw() value written by a repository, or whereRaw() / havingRaw() / joinRaw() on a model query). |
E_NO_INTERACTIVE_TRANSACTION | The connection has no interactive transaction() (a model transaction, or useTransaction() in tests). |
E_ARRAY_PARAM | A list was bound to a single placeholder. Expand it into one placeholder per element. |
E_UNMANAGED_DB_CONNECTION | A connection name that is not registered: a model's static connection, db.connection(name), a factory's connection(name), manager.connect(name). |
E_ATLAS_MISSING_CONNECTION | No connection to use at all: no default one (atlas has not booted), a default the config does not declare, or a repository built without a connection. |
E_ATLAS_EMPTY_SELECT | select() was called with no columns. Provide at least one column name. |
E_ATLAS_INVALID_PAGE | paginate() was called with page < 1. |
E_ATLAS_INVALID_PER_PAGE | paginate() was called with perPage < 1. |
E_ATLAS_INVALID_PAGINATION | A page, page size or row count that is not a finite, non-negative integer. |
E_ATLAS_INVALID_CTE_NAME | A CTE name must be a valid identifier (letters, numbers, underscores). |
E_ATLAS_INVALID_OPERATOR | A where() or wherePivot() operator outside the allowed list. |
E_ATLAS_INVALID_INCREMENT | increment() / decrement() was given an amount that is not a finite number. |
E_INVALID_DATE_COLUMN_VALUE | A date column was given a value that is not a valid date. |
E_ATLAS_UNDEFINED_BINDING | A query value is undefined. Validate the input first; use whereNull() to match NULL. |
E_ATLAS_DIALECT_MISMATCH | useTransaction(trx) was given a transaction of another dialect than the query was built for. Start the transaction on the query's connection, or build the query from the transaction. |
E_ATLAS_RAW_BINDINGS | A raw fragment's placeholders and bindings do not match: a placeholder with no binding, a binding no placeholder uses, ? mixed with $N or with named bindings, or a ?? binding that is not a string. |
E_ATLAS_RAW_VALUE | A db.raw() was used where it cannot go: defaultTo(db.raw()) with bindings, or a raw value compared with an operator that takes none (IS NULL, BETWEEN). |
E_ATLAS_NON_FINITE_NUMBER | defaultTo() was given a number that is not a SQL number (NaN, Infinity). |
E_ATLAS_FACTORY_FAKER_MISSING | A factory uses faker, an optional peer of atlas that is not installed. |
E_ATLAS_NAPI_NOT_FOUND | The Rust query compiler is not loaded. The message names the platform package to install; cause holds the load failure. See Troubleshooting. |
E_ATLAS_INSECURE_SSLMODE | The connection asks for sslmode=prefer or allow (URL or PGSSLMODE), which fall back to plaintext without saying so. Use disable or verify-full. See Encrypted connections. |
E_ATLAS_LISTEN_UNSUPPORTED | LISTEN / NOTIFY was used on a connection that is not PostgreSQL. |
E_ATLAS_LISTEN_CONNECT | The listener could not open its own PostgreSQL connection. |
E_ATLAS_LISTEN_CONNECTION | The listener's connection failed; every subscription is told. |
E_ATLAS_LISTEN_CHANNEL | A LISTEN channel name is empty, longer than 63 bytes, or holds a NUL. |
E_ATLAS_LISTEN_CLOSED | The listener was closed while a request was pending. |
E_ATLAS_LISTEN_EVENT | The native listener reported an event atlas does not define. |
E_ATLAS_MIGRATION_INVALID_TABLE_NAME | migrations.tableName is not a plain identifier. |
E_MISSING_SCHEMA_FILES | A migration recorded as applied has no file in the migrations directory, so it cannot be rolled back. |
E_ATLAS_MIGRATION_INVALID | A migration file does not default-export a class. |
E_ATLAS_MIGRATION_DUPLICATE | Two migration files share a name. |
E_ATLAS_MIGRATION_COMPILE_EMPTY | A migration compiled to no SQL statement. |
E_UNABLE_ACQUIRE_LOCK | Another run holds the migration lock. Wait for it, or clear a stale lock with migration:unlock. |
E_UNABLE_RELEASE_LOCK | The run succeeded, but its migration lock had been cleared under it (migration:unlock during the run), so another run may have migrated at the same time. |
E_ROLLBACK_DISABLED_IN_PRODUCTION | A rollback, reset, refresh, fresh or wipe in production without --force. |
E_ATLAS_DRY_RUN_WRITE | A migration tried to write while running under dryRun(). |
E_DEFER_REQUIRES_TRANSACTION | A migration uses this.defer() on an adapter without an interactive transaction(). |
E_MYSQL_FK_REQUIRES_TRANSACTION | Suspending MySQL foreign-key checks needs an interactive transaction() to restore them. |
E_SCHEMA_DUMP_DIALECT_MISMATCH | The schema dump was made for another dialect. |
E_SCHEMA_DUMP_TABLE_MISMATCH | The schema dump uses another migrations tracking table. |
E_SEEDER_DIR_NOT_FOUND | The seeders directory does not exist. |
E_SEEDER_INVALID | A seeder file does not export a seeder class. |
E_SEEDER_INVALID_PATH | A seeder path resolves outside the seeders directory. |
E_NO_CONNECTION | schema.<method>() needs a live connection; it is available inside a migration, not on a standalone Schema. |
E_ALTER_EMPTY | schema.alterTable() declared no operation. |
E_ATLAS_EMPTY_TABLE | schema.createTable() declared no column. |
E_ALTER_MISUSE | .alter() does not follow a column definition, or an alter-only method was used on a new table. |
E_CHECK_EMPTY | A check constraint (or checkBetween) was given no value or interval. |
E_CHECK_RANGE | checkBetween was given an interval that is not [min, max]. |
E_CONSTRAINT_EMPTY | A constraint lists no columns. |
E_ENUM_EMPTY | An enum column declares no values. |
E_EMPTY_VIEW_SELECT | A view has no SELECT, or a materialized view was asked of a dialect that has none. |
E_SPECIFIC_TYPE_EMPTY | specificType() was given an empty type. |
E_SQLITE_DECIMAL_PRECISION | A decimal precision SQLite cannot store exactly. |
E_UNSUPPORTED | A SQL feature the connection's dialect does not have (DISTINCT ON off Postgres, UNION ALL variants on SQLite, advisory locks on SQLite, an upsert of a row with no column on SQLite, onConflict().merge() with no column on Postgres…). |
E_UNSAFE_IDENTIFIER | An identifier is not a plain name: refused by atlas's own checks or by the Rust compiler. |
E_UNSAFE_SQL | The Rust compiler refused a SQL fragment, or a keyword outside its allowed list (set operation, ORDER BY direction, lock mode, referential action…). |
E_UNSAFE_EXPRESSION | The Rust compiler refused an expression. |
E_UNSAFE_LITERAL | The Rust compiler refused a literal value. |
E_INJECTION_PATTERN | The Rust compiler refused an input that looks like an injection attempt. |
E_ATLAS_INVALID_SPEC | The statement the Rust compiler received is malformed: a clause misses a field, a field has the wrong shape, or the spec does not parse. Atlas builds every spec itself, so this points at an atlas bug or at a spec built by hand. |
E_ATLAS_EMPTY_INSERT | An INSERT or an upsert has no row (insert([]), multiInsert([])), or has several rows that set no column. |
E_UNSUPPORTED_CLIENT | The connection names a dialect atlas does not know. Atlas compiles for sqlite, postgres (or postgresql) and mysql (or mariadb). |
E_ATLAS_FACTORY_INVALID | make:factory was given a name that is not a safe file name. |
E_ATLAS_SEEDER_INVALID | make:seeder was given a name that is not a safe file name. |
E_ATLAS_NO_URL | A connection config has no url (or connection), or the database config has neither a top-level url nor connections. |
E_ATLAS_NOT_BOOTED | The db service (@c9up/atlas/services/db) was used before AtlasProvider.boot() ran. |
E_ATLAS_CONNECTION_UNAVAILABLE | A container connection token was resolved before boot opened it, or after shutdown closed it. |
E_ATLAS_BOOT_FAILED | AtlasProvider.boot() could not open one or more configured connections; cause holds the first failure. |
E_ATLAS_READ_ONLY | A write ran on a query builder scoped to a connection opened with { mode: 'read' }. |
E_ATLAS_INVALID_PRAGMA | A SQLite pragma key or value is outside [A-Za-z0-9_]. |
E_ATLAS_DB_DRIVER_NOT_FOUND | The Rust database driver could not be loaded, or the loaded binary is stale. The message names the platform package to install; cause holds the load failure. |
E_ATLAS_NO_REPOSITORY | refresh(), fresh(), load(), loadCount(), loadAggregate() or related() was called on an entity no repository hydrated. |
E_ATLAS_FILLABLE_AND_GUARDED | A model declares both static fillable and static guarded. |
E_ATLAS_INVALID_ENTITY_CLASS | A relation's target (or through model) does not resolve to a BaseEntity subclass. |
E_ATLAS_INVALID_HOOK | A hook decorator is not on a static method, or addHook() was given something other than a function. |
E_ATLAS_INVALID_DECORATOR | A date column decorator was applied to something other than a class property. |
E_UNDEFINED_RELATIONSHIP | A relation name the model does not declare (load(), withCount(), whereHas()…). |
E_ATLAS_INVALID_RELATION | Relation metadata lacks what its kind needs: a @ManyToMany without pivot options, a through relation without a through model. |
E_ATLAS_RELATION_UNSUPPORTED_OPERATION | related() was asked for an operation its relation does not have: a write on @BelongsTo other than associate() / dissociate(), a write on a read-only through relation, a bulk write on @HasOne. |
E_ATLAS_ASSOCIATE_NULL | associate() was given null or undefined; use dissociate(). |
E_ATLAS_PIVOT_KEY_COLLISION | attach() / sync() extras name a pivot key column or a pivot timestamp column. |
E_ATLAS_COLUMN_ADAPTER | A @Column prepare / consume adapter threw (cause holds its error) or returned a Promise. |
E_ATLAS_HAS_ONE_MULTIPLE_ROWS | A @HasOne preload found more than one related row for a parent. |
E_ATLAS_THROUGH_KEY_NOT_UNIQUE | A through relation's secondLocalKey maps to several parents. |
E_ATLAS_MULTIPLE_ROWS | sole() matched more than one row. |
E_ATLAS_SUBQUERY_ALIAS | select() was given a subquery not named with .as(). |
E_ATLAS_INVALID_ALIAS | An alias is not a bare identifier. |
E_ATLAS_INVALID_EXPRESSION | A whereExpr() extra expression is not a tail of arithmetic: it holds a comment or another forbidden character, unbalanced parentheses, a SQL keyword, or a function outside the allowed list. |
E_ATLAS_INVALID_JSON_PATH | A whereJsonPath() path does not start with $. |
E_ATLAS_INVALID_GROUP_LIMIT | groupLimit() was given a value that is not a non-negative integer. |
E_ATLAS_INVALID_LOCK | skipLocked() / noWait() was used without a base row lock. |
E_ATLAS_INVALID_COMMENT | comment() text contains */. |
E_ATLAS_INVALID_JOIN | A string-form join() is missing its right operand. |
E_ATLAS_INVALID_AGGREGATE | A withAggregate() callback set no aggregate. |
E_ATLAS_INVALID_PLUCK | pluck() read a column whose value is an object or a relation. |
E_ATLAS_UNKNOWN_SCOPE | apply() reached a scope the model does not declare, or a model with no static scopes. |
E_ATLAS_CURSOR_ORDER_REQUIRED | cursorPaginate() was called without an order column. |
E_ATLAS_INVALID_CURSOR | A cursorPaginate() cursor does not decode, or its tuple length does not match the order columns. |
E_ATLAS_EMPTY_UPDATE | An update was given an empty payload: a model query's update(), or an UPDATE with no SET assignment that reached the compiler. |
E_ATLAS_QUERY_TIMEOUT | A query ran longer than its timeout(ms). |
E_ATLAS_COMPILE_EMPTY | The query compiler returned no statement. |
E_ATLAS_SCHEMA_MISMATCH | verifySchema() in throw mode found drift between the models and the database; the message lists the findings. |
E_ATLAS_SCHEMA_DUMP_MANIFEST | A schema-dump manifest (.meta.json) is not version 1 with a schemaTableName. |
E_ATLAS_SCHEMA_DUMP_SEQUENCE | schema:dump read a sequence parameter from the catalog that is not an integer. |
E_ATLAS_SCHEMA_DUMP_PRUNE | schema:dump --prune ran without a migrationsDir. |
E_ATLAS_SCHEMA_GENERATE_IDENTIFIER | schema:generate read a table or column name that does not make a TypeScript identifier. |
E_ATLAS_DB_ASSERTION | A database assertion (assertHas, assertMissing, assertCount, assertEmpty) does not hold. |
E_ATLAS_FACTORY_NO_CONNECTION | A factory's create() has no connection: none passed and none bound with .client() / .connection(). |
E_ATLAS_FACTORY_STATE | A factory state is not defined. |
E_ATLAS_FACTORY_UNKNOWN_RELATION | A factory's with() names a relation the model does not declare, or one with no related factory. |
E_ATLAS_FACTORY_PIVOT_LENGTH | A factory's pivotAttributes array length differs from the related-row count. |
E_ATLAS_FACTORY_UNSUPPORTED_RELATION | A factory's with() names a relation kind it cannot build (a through relation). |
E_ATLAS_SEEDER_DIR_REQUIRED | testUtils().db().seed() ran without a seedersDir. |
E_ATLAS_INTERNAL | An invariant inside atlas broke, or the Rust compiler panicked or could not serialize what it compiled: always a bug in atlas itself. |
Migration
AtlasErroris gone: testerror.code, orerror instanceof errors.E_…withimport { errors } from '@c9up/atlas'. Itshintis nowhelp.EntityNotFoundErrorbecomeserrors.E_ROW_NOT_FOUND,MassAssignmentErrorbecomeserrors.E_MASS_ASSIGNMENT,OptimisticLockErrorbecomeserrors.E_OPTIMISTIC_LOCKandRelationNotLoadedErrorbecomeserrors.E_RELATION_NOT_LOADED; their fields are unchanged, except those ofE_ROW_NOT_FOUND(below).- A Rust compiler refusal (
E_UNSAFE_*,E_INJECTION_PATTERN,E_UNSUPPORTED…) now carries its code inerror.code; its message no longer starts with the code, so matcherror.codeinstead of the message. error.nameis now the class name (Exceptionfor most codes), notAtlasError.insert({})now inserts a row of defaults. It used to resolve[]without running anything. A model saved with nothing set now inserts its row too: it used to be marked persisted with no row in the table.insert([])andmultiInsert([])now fail withE_ATLAS_EMPTY_INSERT. They used to resolve[].E_ATLAS_INVALID_DATE_COLUMN_VALUEis nowE_INVALID_DATE_COLUMN_VALUE,E_ATLAS_UNKNOWN_RELATIONis nowE_UNDEFINED_RELATIONSHIP,E_MIGRATION_LOCKEDis nowE_UNABLE_ACQUIRE_LOCKandE_ATLAS_MIGRATION_NOT_FOUNDis nowE_MISSING_SCHEMA_FILES.- A connection name that is not registered is now
E_UNMANAGED_DB_CONNECTION("Cannot connect to unregistered connection<name>");E_ATLAS_MISSING_CONNECTIONstays for a missing default connection and a repository built without one. E_ROW_NOT_FOUNDcarries the model class onmodel(it was the class name onentityClass), and its message is always "Row not found": the lookup criteria stay oncriteriaand never reach the message, which an exception handler sends to the client.E_MODEL_DELETEDreads "Cannot mutate delete model instance", and a successful migration run whose lock was cleared under it now fails withE_UNABLE_RELEASE_LOCK.whereExpr()'s extra expression must now be a tail of arithmetic that starts with+,-,*or/. A comment (--,/*,*/),current_dateand the like, a qualified name or a fragment that opens with a parenthesis is refused withE_ATLAS_INVALID_EXPRESSION; usewhereRaw()with bindings instead.- A
select()/having()expression is now parsed against a grammar.IS DISTINCT FROM, a row constructor(a, b),WITHIN GROUP,exists(…), a cast to an unlisted type and a barecurrent_user/session_userare refused withE_UNSAFE_EXPRESSION; giveselect()orhaving()adb.raw()fragment for those, and write"current_user"quoted for a column of that name. - On
db.from(),count(),sum(),avg(),min(),max()and theirDistinctforms now refuse anything but a column or*withE_UNSAFE_IDENTIFIER; move an expression toselect('sum(price * quantity) as total'). groupBy()ondb.from()and onQueryBuildernow refuses a value that is not a column name withE_UNSAFE_IDENTIFIER. An object{ raw: '…' }used to be compiled as SQL; callgroupByRaw(sql, bindings)or passdb.raw(sql, bindings)instead.- The constraint a relation adds (the key of
related().query(), the correlation ofwhereHas()/withCount()/withAggregate(), the parent keys of a preload) and the@SoftDeletesfilter are now ANDed with your clauses taken as one group.where(a).orWhere(b)on such a query used to return, update and delete rows of other parents, and deleted rows; it now stays inside the relation and the live rows. Queries withoutorWhere()are unchanged. update(),delete(),increment()anddecrement()on a many-to-manyrelated().query()now change only the related rows linked to the parent. They used to ignore the pivot and reach every row of the related table.clearWhere()on arelated().query()now keeps the relation's key; it used to drop it and return the rows of every parent.whereHas(),withCount()andwithAggregate()on a@SoftDeletesrelated model now leave the deleted rows out; callwithTrashed()in the callback to count them.- Bulk
restore()now applies the repository scope and the key of arelated().query(); it used to restore matching rows of every tenant. updateOrCreate(),updateOrCreateMany()andrelated().updateOrCreate()now apply the payload of a found row withmerge(): a key that is not a declared property or its column name throwsE_EXTRA_PROPERTIES(it used to be written,{ IsAdmin: 1 }pastguarded,{ Id: … }onto the primary key). Pass{ allowExtraProperties: true }to keep such keys in$extras; on the new-row branch an unknown key throwsE_UNKNOWN_COLUMN, ascreate()does.upsert()on a repository with ascopeno longer updates a conflicting row outside the scope (PostgreSQL, SQLite: that row is left as it is). On MySQL anupsert()withupdateColumnson such a repository now fails withE_UNSUPPORTED; check that the rows belong to the tenant, then upsert through an unscoped repository.cursorPaginate()now firesbeforeFetchon its query andafterFetchon the page's items;exists()anddoesntExist()now firebeforeFind. A filter a hook adds now applies to them.db:seedwithout--environmentnow gatesstatic environmenton the application's environment, elseREAM_ENV/NODE_ENVthrough the alias table (production when either says so). With neither variable set, a seeder that lists environments no longer runs; setNODE_ENVor pass--environment.prod,devandtestnow matchproduction,developmentandtestingin the list.- With
migrations.disableRollbacksInProduction: false,db:wipe,migration:fresh,migration:rollback,migration:resetandmigration:refreshnow ask for confirmation in production; without an answer (a non-interactive run) they do nothing. Pass--forcein a deploy script. save()now writes declared columns only; an undeclared property set on the instance is no longer turned into a column name.- A
beforeFind,beforeFetchorbeforePaginatehook now receives the query with the caller's clauses closed into one group (wrapExisting()), so a filter it adds guards everyorWhere()branch.