@schemeless/event-store-adapter-prisma
v3.0.4
Published
Official Prisma adapter for the Schemeless Event Store. This package lets you use a Prisma Client instance as the persistence layer behind `@schemeless/event-store`, embracing Prisma's schema-first workflow while reusing the existing event sourcing APIs.
Readme
@schemeless/event-store-adapter-prisma
Official Prisma adapter for the Schemeless Event Store. This package lets you use a Prisma Client instance as the persistence layer behind @schemeless/event-store, embracing Prisma's schema-first workflow while reusing the existing event sourcing APIs.
Installation
yarn add @schemeless/event-store-adapter-prisma @prisma/client
yarn add -D prismaSetup
- Add the
EventStoreEntitymodel to yourprisma/schema.prismafile. A ready-to-copy example lives atschema.prisma.exampleand is also exported fromsrc/schema.prisma.examplefor programmatic access. - Run Prisma migrations to apply the schema to your database:
- During development:
npx prisma migrate dev - In production:
npx prisma migrate deploy
- During development:
- Generate the Prisma Client that this adapter expects:
npx prisma generateAfter these steps the generated client will include the eventStoreEntity model used by the adapter.
Usage
import { PrismaClient } from '@prisma/client';
import { makeEventStore } from '@schemeless/event-store';
import { EventStoreRepo } from '@schemeless/event-store-adapter-prisma';
const prisma = new PrismaClient();
const repo = new EventStoreRepo(prisma);
await repo.init(); // ensures the Prisma client is connected
const eventStore = await makeEventStore({
repo,
});
// Store new events through the Event Store APIs as usualThe adapter never runs migrations or schema updates for you. Make sure your deployment pipeline runs the appropriate prisma migrate or prisma db push commands before the application starts.
API
EventStoreRepo implements the shared IEventStoreRepo interface and exposes the following methods:
constructor(prismaClient)– Accepts an existingPrismaClientinstance so you can share connection pooling with the rest of your application.init()– Callsprisma.$connect()to ensure the adapter has an active database connection before persisting events.storeEvents(events)– Persists the provided events inside a Prisma interactive transaction, maintaining write ordering.getAllEvents(pageSize, startFromId?)– Returns an async iterator that replays events ordered by creation time and identifier.resetStore()– Clears all persisted events viadeleteMany, useful for tests.
