Your First Schema

Schemas are plain TypeScript objects that describe your data model. Datrix automatically adds id, createdAt, and updatedAt fields to every schema.

Defining a schema

// schemas/user.schema.ts
import type { SchemaDefinition } from '@datrix/core'

export const userSchema = {
  name: 'user',
  fields: {
    name:  { type: 'string', required: true },
    email: { type: 'string', required: true, unique: true },
    age:   { type: 'number' },
    role:  { type: 'enum', values: ['admin', 'editor', 'user'], default: 'user' },
  },
} satisfies SchemaDefinition

The satisfies keyword gives you full type checking without losing the literal type information.

Available field types

TypeDescription
stringText values — supports minLength, maxLength, pattern, unique
numberNumeric values — supports min, max, integer, unique
booleanTrue/false values
dateDate objects — supports min, max
enumOne of a fixed set of string values
arrayList of values — supports minItems, maxItems
jsonArbitrary JSON object
relationReference to another schema

For full field options see Core: Schema.

Register schemas in config

Add your schemas to datrix.config.ts:

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

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