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()]

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,
}