Adapters
An adapter is what connects express-activitylog to your database. You pass one adapter instance to both the middleware and the fluent builder.
Prisma
Installation
sh
pnpm add @prisma/client
pnpm add -D prismaSchema
Add the ActivityLog model to your prisma/schema.prisma:
prisma
model ActivityLog {
id String @id @default(cuid())
description String
causerId String?
causerType String?
subjectId String?
subjectType String?
properties Json @default("{}")
createdAt DateTime @default(now())
}Run the migration:
sh
pnpm prisma migrate dev --name add_activity_logSetup
ts
import { PrismaClient } from '@prisma/client'
import { prismaAdapter } from '@actinode/express-activitylog'
const prisma = new PrismaClient()
export const adapter = prismaAdapter(prisma)Pass adapter to activityLog() or activity().
Mongoose
Installation
sh
pnpm add mongooseSchema
The adapter registers the ActivityLog model automatically using this schema:
ts
{
description: { type: String, required: true },
causerId: { type: String, default: null },
causerType: { type: String, default: null },
subjectId: { type: String, default: null },
subjectType: { type: String, default: null },
properties: { type: Object, default: {} },
createdAt: { type: Date, default: Date.now },
}You do not need to define the schema yourself — it is created on first use. If a model named ActivityLog already exists in your Mongoose registry it will be reused.
Setup
ts
import mongoose from 'mongoose'
import { mongooseAdapter } from '@actinode/express-activitylog'
await mongoose.connect(process.env.MONGODB_URI!)
export const adapter = mongooseAdapter(mongoose)Comparison
| Prisma | Mongoose | |
|---|---|---|
| Database | PostgreSQL, MySQL, SQLite, … | MongoDB |
| Schema | Defined in schema.prisma | Auto-registered |
| Migration | prisma migrate dev | None required |
| Import | prismaAdapter | mongooseAdapter |
Custom adapters
Implement the ActivityAdapter interface to use any storage backend:
ts
import type { ActivityAdapter, ActivityPayload } from '@actinode/express-activitylog'
const myAdapter: ActivityAdapter = {
async save(payload: ActivityPayload): Promise<void> {
// write payload to your store
},
}ActivityPayload shape:
ts
interface ActivityPayload {
description: string
causerId?: string
causerType?: string
subjectId?: string
subjectType?: string
properties?: Record<string, unknown>
createdAt: Date
}