Platform referenceReference
Examples
Runnable code examples for each pattern, cross-linked to their concept pages.
Snippets you can read top-to-bottom. Each example is grounded in real types from @fabricorg/platform. Vertical-specific bits use placeholder names.
Example 1 — Defining a vertical action
import { z } from "zod";
import type { ActionDefinition } from "@fabricorg/platform/actions";
import type { Prisma } from "@repo/database";
const acceptOfferSchema = z.object({
offerId: z.string(),
acceptanceSource: z.enum(["consultant_ui", "borrower_portal_token"]),
acceptedByPartyId: z.string(),
});
export const acceptOfferAction: ActionDefinition<Prisma.TransactionClient> = {
actionId: "lending.accept_offer",
namespace: "lending",
version: 1,
schema: acceptOfferSchema,
handler: async (ctx, params) => {
const parsed = acceptOfferSchema.parse(params);
const offer = await ctx.db.offer.findUniqueOrThrow({ where: { id: parsed.offerId } });
// Domain write + event append in the same transaction.
await ctx.db.offer.update({
where: { id: offer.id },
data: {
status: "accepted",
acceptedAt: new Date(),
properties: { ...offer.properties, acceptanceSource: parsed.acceptanceSource },
},
});
await ctx.db.assetEvent.create({
data: {
eventType: "OfferAccepted",
subjectType: "Offer",
subjectId: offer.id,
eventSchemaVersion: 1,
payload: { acceptanceSource: parsed.acceptanceSource, acceptedBy: parsed.acceptedByPartyId },
actionInvocationId: ctx.actionInvocationId,
correlationId: ctx.correlationId,
// …other envelope fields stamped by the platform helper
},
});
return { success: true, data: { offerId: offer.id } };
},
policies: ["lending.borrower_consent.v1"],
emitsEvents: ["OfferAccepted"],
idempotent: false,
mutatesDomain: true,
stateMachine: {
entityType: "Offer",
targetState: "accepted",
getEntityId: (params) => (params as any).offerId,
},
};See: vertical extensions, stages.
Example 2 — A code policy
import type { PolicyEvaluator } from "@fabricorg/platform/policies";
import type { Prisma } from "@repo/database";
export const borrowerConsentPolicy: PolicyEvaluator<Prisma.TransactionClient> = {
policyId: "lending.borrower_consent.v1",
version: 1,
previewSafe: true,
evaluate: async (ctx) => {
const params = ctx.parameters as { offerId: string };
const offer = await ctx.db.offer.findUniqueOrThrow({
where: { id: params.offerId },
include: { borrower: true },
});
const consent = await ctx.db.consentRecord.findFirst({
where: { partyId: offer.borrower.id, type: "offer_acceptance" },
orderBy: { signedAt: "desc" },
});
if (!consent) {
return {
policyId: "lending.borrower_consent.v1",
policyVersion: 1,
result: "block",
reason: "No offer-acceptance consent on file for this borrower.",
};
}
return { policyId: "lending.borrower_consent.v1", policyVersion: 1, result: "pass" };
},
};See: policy model, enforcement.
Example 3 — A data policy
import type { RuntimePolicyDefinition } from "@fabricorg/platform/policies";
const offerAmountCapPolicy: RuntimePolicyDefinition = {
policyId: "lending.offer_amount_cap.v1",
policyVersion: 1,
kind: "data",
dataDefinition: {
conditions: [
{
conditionId: "amount_under_cap",
type: "parameter",
path: "amount",
operator: "lte",
value: 100_000,
onPass: "pass",
onFail: "block",
reason: "Offer amount exceeds the $100,000 cap.",
},
],
defaultResult: "block",
},
};See: policy model.
Example 4 — A state machine
import type { StateMachineDefinition } from "@fabricorg/platform/state-machines";
export const offerStateMachine: StateMachineDefinition = {
entityType: "Offer",
states: {
drafted: { id: "drafted", label: "Drafted", stateClass: "initial" },
presented: { id: "presented", label: "Presented", stateClass: "active" },
accepted: { id: "accepted", label: "Accepted", stateClass: "terminal" },
declined: { id: "declined", label: "Declined", stateClass: "terminal" },
expired: { id: "expired", label: "Expired", stateClass: "terminal" },
},
transitions: [
{ from: "drafted", to: "presented", causedByAction: "lending.send_offer" },
{ from: "presented", to: "accepted", causedByAction: "lending.accept_offer" },
{ from: "presented", to: "declined", causedByAction: "lending.decline_offer" },
{ from: "presented", to: "expired", causedByAction: "lending.expire_stale_offer" },
],
};See: entity overview, invariants.
Example 5 — A FabricModule manifest
import type { FabricModule } from "@fabricorg/platform/modules";
import type { Prisma } from "@repo/database";
export const lendingModule: FabricModule<Prisma.TransactionClient> = {
namespace: "lending",
version: "1.0.0",
objectTypes: ["Offer", "Party", "Vehicle", "Document"],
eventTypes: [
{ eventType: "OfferAccepted", schema: offerAcceptedSchema, version: 1 },
{ eventType: "OfferDeclined", schema: offerDeclinedSchema, version: 1 },
],
actions: [acceptOfferAction, declineOfferAction, sendOfferAction],
policies: [borrowerConsentPolicy, offerAmountCapPolicy],
stateMachines: [offerStateMachine],
};Register at startup:
import { registerFabricModules } from "@fabricorg/platform/modules";
import { messagingModule } from "@repo/messaging";
import { lendingModule } from "@repo/lending";
registerFabricModules([messagingModule, lendingModule]);See: extending entities, vertical extensions pattern.
Example 6 — A projection reducer
import { replayEvents, type ReplayableAssetEvent } from "@fabricorg/platform/projections";
interface OfferProjection {
status: "drafted" | "presented" | "accepted" | "declined" | "expired";
acceptedAt?: string;
declineReason?: string;
}
const initialState: OfferProjection = { status: "drafted" };
const result = await replayEvents<OfferProjection>({
events: eventStream,
scope: { tenantId, spaceId, subjectType: "Offer", subjectId: offerId },
initialState,
applyEvent: (state, event) => {
switch (event.eventType) {
case "OfferPresented":
return { ...state, status: "presented" };
case "OfferAccepted":
return { ...state, status: "accepted", acceptedAt: String(event.recordedAt) };
case "OfferDeclined":
return { ...state, status: "declined", declineReason: (event.payload as any).reason };
default:
return state;
}
},
});
console.log(result.state, result.warnings);See: projection mechanics.
Example 7 — Invoking from an external token holder
// packages/api/modules/borrower-portal/procedures/accept-offer.ts
import { publicProcedure } from "../../../orpc/procedures";
import { invokeAction } from "../../platform/lib/invoke-action";
import { resolveOfferFromToken } from "../lib/tokens";
export const acceptOfferProcedure = publicProcedure
.input(acceptOfferTokenSchema)
.handler(async ({ input }) => {
// 1. Verify token BEFORE invokeAction.
const tokenPayload = await resolveOfferFromToken(input.token);
if (!tokenPayload) throw new Error("Invalid token");
// 2. Invoke with actorType: external_system.
return await invokeAction({
actionId: "lending.accept_offer",
actorType: "external_system",
actorId: tokenPayload.partyId,
tenantId: tokenPayload.tenantId,
spaceId: tokenPayload.spaceId,
parameters: {
offerId: tokenPayload.offerId,
acceptanceSource: "borrower_portal_token",
acceptedByPartyId: tokenPayload.partyId,
},
});
});See: integrations → external actor calls, triggers.
See also
- Vertical-modules guide — companion how-to with deeper walkthroughs.
- Module contract — full manifest reference.
- Platform README — source-of-truth contracts.