Skip to content

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 prisma

Schema

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_log

Setup

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 mongoose

Schema

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

PrismaMongoose
DatabasePostgreSQL, MySQL, SQLite, …MongoDB
SchemaDefined in schema.prismaAuto-registered
Migrationprisma migrate devNone required
ImportprismaAdaptermongooseAdapter

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
}

Need an admin panel?

Get a beautiful admin UI for all actinode packages. Contact us to learn more about our premium admin package.

No spam, ever. Unsubscribe at any time.

Released under the MIT License.