Kaizen
Browse modulesRulesrules/serverFunctions

Function: matchRules()

function matchRules<TContext, TDomainConfig>(context: TContext, options: {
  customConditions?: CustomConditionDef<unknown, TContext>[];
  domain: string;
  domainConfigSchema: ZodType<TDomainConfig>;
  organizationId: string;
  scopeMapping: Record<string, string>;
}): Promise<MatchResult<TDomainConfig>>;

Defined in: server/rules/match-rules.ts:54

Server-side rules engine entry point. Loads active rules + conditions for the given domain and organization from Postgres and runs the engine kernel against the supplied context.

Resolves the Prisma client from the ambient registry — call initPrismaClient (or registerPrismaClient) on @the-kaizen-labs/taproot/persistence at application startup before using this function.

Mirrors the kernel signature from rules/shared, minus the rulesTable / conditionsTable arguments — those come from the database. Consumers who already have rules in memory (e.g. tests, static authoring tools) should use the kernel directly:

import { matchRulesAgainstTables } from "@the-kaizen-labs/taproot/rules/shared";

Type Parameters

Type Parameter
TContext
TDomainConfig

Parameters

ParameterType
contextTContext
options{ customConditions?: CustomConditionDef<unknown, TContext>[]; domain: string; domainConfigSchema: ZodType<TDomainConfig>; organizationId: string; scopeMapping: Record<string, string>; }
options.customConditions?CustomConditionDef<unknown, TContext>[]
options.domainstring
options.domainConfigSchemaZodType<TDomainConfig>
options.organizationIdstring
options.scopeMappingRecord<string, string>

Returns

Promise<MatchResult<TDomainConfig>>

Example

import { Prisma, PrismaClient } from "@prisma/client";
import { initPrismaClient } from "@the-kaizen-labs/taproot/persistence";
import { matchRules } from "@the-kaizen-labs/taproot/rules/server";

// Once at startup:
initPrismaClient(
  PrismaClient,
  { connection: process.env.DATABASE_URL! },
  Prisma,
);

// Per request:
const result = await matchRules(context, {
  scopeMapping,
  domain: "pricing",
  organizationId: currentOrg.id,
  domainConfigSchema: PricingDomainConfig,
});

On this page