Configuration

Datrix uses a defineConfig function to declare your configuration and returns a factory function that lazily initializes the singleton instance on first call.

datrix.config.ts

import { defineConfig } from '@datrix/core'
import { PostgresAdapter } from '@datrix/adapter-postgres'
import { userSchema } from './schemas/user.schema'
import { postSchema } from './schemas/post.schema'

export default defineConfig(() => ({
  adapter: new PostgresAdapter({
    connectionString: process.env.DATABASE_URL!,
  }),
  schemas: [userSchema, postSchema],
}))

Using the instance

Import the factory and call it to get the initialized Datrix instance. Subsequent calls return the same singleton — initialization only runs once.

import getDatrix from './datrix.config'

const datrix = await getDatrix()

const users = await datrix.findMany('user')

Configuration options

adapter (required)

The database adapter instance. See Adapters for available options.

import { PostgresAdapter } from '@datrix/adapter-postgres'

adapter: new PostgresAdapter({ connectionString: process.env.DATABASE_URL! })

schemas (required)

Array of schema definitions. Order does not matter — Datrix resolves relations automatically.

schemas: [userSchema, postSchema, commentSchema]

plugins (optional)

Plugin instances to register. Plugins are initialized in the order they appear.

import { SoftDeletePlugin } from '@datrix/plugin-soft-delete'

plugins: [new SoftDeletePlugin()]

idType (optional)

Primary key strategy for every user schema: "int" (default) or "uuid".

ValuePrimary keyGenerated by
"int"Auto-incremented integerThe database
"uuid"UUID v7 string (time-ordered)Datrix core, before insert
idType: "uuid"

The choice applies to ids, foreign keys and junction tables alike, and behaves the same on every adapter — switching adapters keeps the same ids. Internal tables (_datrix, migration history) always use integer ids.

  • uuid ids are accepted in any case and stored lowercase; passing a number (or a malformed string) where a uuid is expected throws.
  • UUID v7 is time-ordered, so orderBy: id still follows creation order.
  • Run datrix generate types after choosing "uuid" — the generated file registers the id type so DatrixId (and every id / relation id in your types) becomes string.

Changing idType on a database that already has data is a migration — see Changing idType.

migration (optional)

Controls migration behavior.

OptionTypeDefaultDescription
autobooleanfalseRun migrations automatically on startup
directorystring'./migrations'Directory to store migration files
modelNamestring'_datrix_migration'Table name for migration history
migration: {
  auto: false,
  directory: './migrations',
}

dev (optional)

Development mode options for debugging.

OptionTypeDefaultDescription
loggingbooleanfalseLog all executed queries
validateQueriesbooleanfalseValidate queries before execution
prettyErrorsbooleanfalsePretty-print errors with stack traces
dev: {
  logging: process.env.NODE_ENV === 'development',
  prettyErrors: true,
}