Writing Plugins

A plugin is a class that implements the DatrixPlugin interface. Datrix provides a BasePlugin abstract class that covers the optional hooks with no-op defaults, so you only need to implement what you need.


BasePlugin vs DatrixPlugin

BasePluginDatrixPlugin
UsageExtend the classImplement the interface
Optional hooksNo-op defaults providedMust implement all yourself
Recommended forMost pluginsLow-level / framework use
import { BasePlugin } from "@datrix/core"
import type { PluginContext } from "@datrix/core"

export class MyPlugin extends BasePlugin<MyPluginOptions> {
  readonly name    = "my-plugin"
  readonly version = "1.0.0"

  async init(context: PluginContext): Promise<void> {
    this.context = context
    // setup work here
  }

  async destroy(): Promise<void> {
    // cleanup here
  }
}

Register it in defineConfig:

import { defineConfig } from "@datrix/core"

export default defineConfig(() => ({
  adapter: ...,
  schemas: [...],
  plugins: [new MyPlugin({ ... })],
}))

Initialization order

When defineConfig starts up, plugins are processed in this order:

1
init(context)

Called once with adapter, schemas, and config

2
getSchemas()

Plugin schemas registered into the registry

3
extendSchemas(ctx)

Field/index extensions applied to existing schemas

4
Finalize registry

Relations resolved, junction tables created

5
onSchemaLoad(schemas)

Called after registry is fully ready

destroy() is called on datrix.shutdown().


Schema hooks

getSchemas

Return new SchemaDefinition objects the plugin needs. They are registered alongside user schemas before the registry is finalized.

override async getSchemas(): Promise<SchemaDefinition[]> {
  return [
    defineSchema({
      name: "audit_log",
      fields: {
        action:     { type: "string", required: true },
        recordId:   { type: "string", required: true },
        schemaName: { type: "string", required: true },
        userId:     { type: "string" },
        diff:       { type: "json" },
      },
    }),
  ]
}

Plugins run under both idType modes, so don't hard-code type: "number" for columns that hold record ids:

  • Points at a known schema → use a relation field. The foreign key column follows that schema's id type automatically.
  • Points at any schema (like recordId above) → use type: "string" and store String(id); it holds integer and uuid ids alike.

extendSchemas

Add fields or indexes to existing user schemas. Receives a SchemaExtensionContext with helper methods.

override async extendSchemas(ctx: SchemaExtensionContext): Promise<SchemaExtension[]> {
  // Add createdBy to every schema that has a permission block
  return ctx.extendWhere(
    (schema) => schema.permission !== undefined,
    () => ({
      fields: {
        createdBy: { type: "number" },
      },
    }),
  )
}

SchemaExtensionContext helpers:

MethodDescription
extendAll(modifier)Apply modifier to every schema
extendWhere(predicate, modifier)Apply modifier to schemas matching predicate
extendByPattern(pattern, modifier)Filter by name, prefix, suffix, or custom fn

onSchemaLoad

Called after the registry is fully finalized. Use this for setup that needs the complete schema list, such as creating prepared statements or building caches.

override async onSchemaLoad(schemas: ISchemaRegistry): Promise<void> {
  const names = schemas.getNames()
  // e.g. pre-warm a schema cache
}

Query hooks

Query hooks are called for every non-raw operation, in this order per query:

onCreateQueryContext
onBeforeQuery
[query runs]
onAfterQuery

All hooks run serially across plugins in registration order. Hooks are skipped for datrix.raw.* calls.

onCreateQueryContext

Called first, before the query is dispatched. Use it to enrich the context — for example, inject the authenticated user so it's available in onBeforeQuery and permission functions.

This is how ApiPlugin injects the request user:

override async onCreateQueryContext(context: QueryContext): Promise<QueryContext> {
  if (this.currentUser) {
    context.user = this.currentUser
  }
  return context
}

context.metadata is a plain mutable object. Anything you write here is available in onBeforeQuery and onAfterQuery for the same operation.

onBeforeQuery

Receives the QueryObject and must return it (modified or unchanged). Use it to inject additional WHERE conditions, rewrite queries, or set metadata for onAfterQuery.

override async onBeforeQuery<T extends DatrixEntry>(
  query: QueryObject<T>,
  context: QueryContext,
): Promise<QueryObject<T>> {
  // Inject a soft-delete filter on every SELECT
  if (query.type === "select") {
    return {
      ...query,
      where: { ...query.where, deletedAt: null } as WhereClause<T>,
    }
  }
  return query
}

Upserts reach onBeforeQuery as query.type === "upsert", with context.action set to "upsert" or "upsertMany".

onAfterQuery

Receives the result and must return it (modified or unchanged). Use it to strip sensitive fields, transform output, or trigger side effects.

override async onAfterQuery<T extends DatrixEntry>(
  result: T,
  context: QueryContext,
): Promise<T> {
  // Strip internal fields from read results
  if (context.action === "findMany" || context.action === "findOne") {
    const rows = Array.isArray(result) ? result : [result]
    const stripped = rows.map(({ internalField: _, ...rest }) => rest)
    return (Array.isArray(result) ? stripped : stripped[0]) as T
  }
  return result
}

New in 0.2.2: context.createdIds is set before onAfterQuery runs on create and upsert. It holds the ids of the records the query inserted — for create/createMany every returned record, for upsert/upsertMany only the new ones, the rest were updated. It is undefined in before-hooks and for find, count, update and delete.

override async onAfterQuery<T extends DatrixEntry>(
  result: T,
  context: QueryContext,
): Promise<T> {
  if (context.action === "upsertMany" && context.createdIds) {
    const rows = result as unknown as DatrixEntry[]
    const inserted = rows.filter((row) => context.createdIds!.has(row.id))
    const matched = rows.filter((row) => !context.createdIds!.has(row.id))
    // inserted rows are new, matched rows were updated
  }
  return result
}

onAfterCommitError

Called when an afterCommit callback throws. The data is already committed, so the error cannot affect the operation — use this hook to report it. It is called on every plugin.

override async onAfterCommitError(error: unknown): Promise<void> {
  console.error("afterCommit callback failed", error)
}

Inside query hooks, use context.crud for extra queries so they join the current transaction, and context.afterCommit(callback) to defer side effects until commit. context.datrix queries run outside the transaction. See Transactions.


Full example: audit log plugin

A plugin that writes an audit log entry after every create, update, or delete operation.

import { BasePlugin } from "@datrix/core"
import { defineSchema } from "@datrix/core"
import type { PluginContext, SchemaDefinition } from "@datrix/core"
import type { QueryContext } from "@datrix/core"
import type { DatrixEntry } from "@datrix/core"
import type { QueryObject } from "@datrix/core"

interface AuditLogOptions {
  readonly schemas?: readonly string[]  // limit to specific schemas — undefined = all
}

export class AuditLogPlugin extends BasePlugin<AuditLogOptions> {
  readonly name    = "audit-log"
  readonly version = "1.0.0"

  async init(context: PluginContext): Promise<void> {
    this.context = context
  }

  async destroy(): Promise<void> {}

  override async getSchemas(): Promise<SchemaDefinition[]> {
    return [
      defineSchema({
        name: "auditLog",
        fields: {
          action:     { type: "string", required: true },
          schemaName: { type: "string", required: true },
          recordId:   { type: "string" },  // String(id) — works for int and uuid ids
          userId:     { type: "string" },
          diff:       { type: "json" },
        },
      }),
    ]
  }

  override async onBeforeQuery<T extends DatrixEntry>(
    query: QueryObject<T>,
    context: QueryContext,
  ): Promise<QueryObject<T>> {
    // Snapshot write data before the operation so we can log it after
    if (query.type === "insert" || query.type === "update") {
      context.metadata.auditData = query.type === "insert"
        ? (query as { data?: unknown }).data
        : (query as { data?: unknown }).data
    }
    return query
  }

  override async onAfterQuery<T extends DatrixEntry>(
    result: T,
    context: QueryContext,
  ): Promise<T> {
    const tracked = this.options.schemas
    const schemaName = context.schema.name

    if (tracked && !tracked.includes(schemaName)) return result
    if (schemaName === "auditLog") return result  // avoid infinite loop

    const writeActions = ["create", "createMany", "update", "updateMany", "upsert", "upsertMany", "delete", "deleteMany"]
    if (!writeActions.includes(context.action)) return result

    const ctx = this.getContext()
    const rows = Array.isArray(result) ? result : [result]
    const userId = (context.user as { id?: unknown } | undefined)?.id

    for (const row of rows) {
      const recordId = (row as DatrixEntry).id
      if (recordId == null) continue

      // datrix.raw: skips hooks (no infinite loop) and still assigns the id
      // (core-generated with idType "uuid") — never write via ctx.adapter
      await ctx.datrix.raw.create("auditLog", {
        action:     context.action,
        schemaName,
        recordId:   String(recordId),
        userId:     userId == null ? null : String(userId),
        diff:       context.metadata.auditData ?? null,
      })
    }

    return result
  }
}

Usage:

export default defineConfig(() => ({
  adapter: new PostgresAdapter({ ... }),
  schemas: [userSchema, postSchema],
  plugins: [
    new AuditLogPlugin({ schemas: ["post", "user"] }),
  ],
}))