Exemple : Plugin avec persistance
Plugin utilisant Prisma pour persister des données en base PostgreSQL.
Schéma Prisma
model PluginEvent {
id String @id @default(cuid())
type String
payload Json
createdAt DateTime @default(now())
}
Implémentation
import { createPlugin } from "@plugin-factory/sdk";
import { prisma } from "@plugin-factory/database";
import { z } from "zod";
const TrackEventSchema = z.object({
type: z.string().min(1),
payload: z.record(z.unknown()),
});
export const persistencePlugin = createPlugin({
name: "event-tracker",
tools: {
trackEvent: async (ctx, input) => {
const { type, payload } = TrackEventSchema.parse(input);
const event = await prisma.pluginEvent.create({
data: { type, payload },
});
return { id: event.id, createdAt: event.createdAt };
},
getEvents: async (ctx, { type }: { type?: string }) => {
return prisma.pluginEvent.findMany({
where: type ? { type } : undefined,
orderBy: { createdAt: "desc" },
take: 50,
});
},
},
});
Migrations
bun run db:migrate