Skip to Content
Database

Admin

An admin API and admin tools generated from your schema, deny by default.

Usage

import { adminTools, defineAdmin, roles } from "@dapplelight/signet-db/admin"
import * as schema from "./schema"
import { db } from "./db"
import { me } from "./session"

export const admin = defineAdmin(schema, {
  authorize: roles(), // admin: all, staff: all but delete, readonly: list and get
  tables: {
    users: { search: ["name", "email"], readonly: ["email"], hidden: ["phone"] },
    outbox: false, // leave a table out
  },
})

const ctx = { actor: { type: "user", id: me.id }, role: me.role }
const page = await admin.list(db, ctx, "users", {
  filter: { plan: { eq: "pro" } },
  sort: "createdAt",
  limit: 20,
})
await admin.update(db, ctx, "users", page.rows[0].id, { name: "Asha Rao" })

The db needs an auditLog() table: writes go through audited.

API

ExportWhat it does
defineAdmin(schema, options)Build the admin API for a schema module or a table list
roles(map?)An authorize from ctx.role; unknown roles are denied
DEFAULT_ROLESadmin, staff, readonly presets for roles()
ADMIN_OPSlist, get, create, update, delete, action
adminTools(admin, options)MCP tools for one caller (admin_schema, per-table list, search, get, writes)
AdminErrorError with an HTTP status and the Signet envelope code
TypesAdminOp, AdminContext, AdminCheck, Authorize, AdminAction, AdminTableConfig, AdminOptions, FilterOp, AdminFilter, ListQuery, ListResult, AdminColumn, AdminTableMeta, AdminMeta, AdminRequest, AdminResponse, Admin, AdminTool, Approve, AdminToolsOptions

defineAdmin(schema, options)

OptionTypeDefault
authorize(ctx, check) => boolean | Promise<boolean>requiredOnly true allows; anything else or a throw is 403
tables{ [sqlName]: config | false }every tablePer-table config; false leaves it out
maxRowsnumber100Most rows a list returns
tenantbooleanfalseRequire ctx.orgId on every call

Per-table config:

KeyDefault
listvisible columnsColumns a list view shows
searchvisible text columnsColumns q searches
sortvisible NOT NULL scalarsColumns a list may sort by
filtervisible scalarsColumns a list may filter on
readonlynoneShown, never written
hiddennoneHidden on top of encrypted and secret-looking columns
revealnoneDefault-hidden columns to show anyway
opsallOps the table allows before authorize
actionsnonePer-row operations { description, input?, destructive?, run }

The returned admin has tables, meta(ctx), list, get, create, update, delete, bulkDelete, action and handle(db, ctx, request) for HTTP.

Filter operators: eq, ne, in, isNull, lt, lte, gt, gte, contains, startsWith.

adminTools(admin, options)

OptionTypeDefault
dbSignetDbrequiredDb for this request
ctxAdminContextrequiredThe caller
approveApprovenoneGate for writes; without it no write tools exist
actorActor{ type: "agent", id: ctx.actor.id }Recorded in the audit log

Examples

Refund action with a custom role

import { DEFAULT_ROLES, defineAdmin, roles } from "@dapplelight/signet-db/admin"
import { eq } from "drizzle-orm"
import { z } from "zod"
import * as schema from "./schema"

export const admin = defineAdmin(schema, {
  authorize: roles({ ...DEFAULT_ROLES, support: ["list", "get", "update", "action"] }),
  tables: {
    orders: {
      actions: {
        refund: {
          description: "Mark an order refunded",
          destructive: true,
          input: z.object({ reason: z.string() }),
          run: async ({ row, input, ops }) => {
            await ops.update(schema.orders, { status: "refunded", refundReason: input.reason }, eq(schema.orders.id, row.id as string))
            return { refunded: true }
          },
        },
      },
    },
  },
})

Admin tools for an MCP server

import { requireApproval } from "@dapplelight/signet-server/mcp"
import { adminTools } from "@dapplelight/signet-db/admin"
import { app } from "./app"
import { admin } from "./admin"
import { adminCtx, dbFor } from "./context"

app.post("/mcp", async (c) => {
  const tools = await adminTools(admin, {
    db: dbFor(c),
    ctx: await adminCtx(c),
    approve: requireApproval,
  })
})