A sequelize plugin for logging activities
$ npm install --save sequelize-activitylog
Create Activity
model named activity_log
and then use the model methods.
const Sequelize = require('sequelize')
const activitylog = require('sequelize-activitylog')
const Activity = activitylog(sequelize, { modelName: 'activity_log' })
This is the most basic way to log activity:
await new Activity().log('Look mum, I logged something')
You can retrieve the activity using the Activity model.
const activity = await Activity.findOne({ order: [ ['created_at', 'DESC'] ] }) // returns the last logged activity
activity.description // returns 'Look mum, I logged something'
You can specify on which object the activity is performed by using performedOn
:
await new Activity()
.performedOn(someContentModel)
.log('edited')
const activity = await Activity.findOne({ order: [ ['created_at', 'DESC'] ] }) // returns the last logged activity
activity.getSubject() // returns the model that was passed to `performedOn`
The performedOn()
function has a shorter alias name: on
You can set who or what caused the activity by using causedBy
:
await new Activity()
.performedOn(someContentModel)
.causedBy(userModel)
.log('edited')
const activity = await Activity.findOne({ order: [ ['created_at', 'DESC'] ] }) // returns the last logged activity
activity.getCauser() // returns the model that was passed to `causedBy`
The causedBy()
function has a shorter alias named: by
You can add any property you want to an activity by using withProperties
:
await new Activity()
.performedOn(someContentModel)
.causedBy(userModel)
.withProperties({ key: 'value' })
.log('edited')
const activity = await Activity.findOne({ order: [ ['created_at', 'DESC'] ] }) // returns the last logged activity
activity.properties // returns `{ key: 'value' }`
activity.getExtraProperty('key') // returns 'value'
The withProperties()
function has a shorter alias named: with
log(description: string): Promise
Log an activity.
performedOn(model: Model): this
Specify on which object the activity is performed.
causedBy(model: Model): this
Set who or what caused the activity.
withProperties(properties: object): this
Add properties to the activity.
withProperty(key: string, value: any): this
Add a property to the activity by key.
useLog(logName: string): this
Specify log name of the activity.
use(logName: string): this
Alias of useLog()
.
on(model: Model): this
Alias of performedOn()
.
by(model: Model): this
Alias of causedBy()
.
with(properties: object): this
with(key: string, value: any): this
Alias of withProperties()
/ withProperty()
.
Inspired by laravel-activitylog
MIT © Chun-Kai Wang