mongoose-activitylog
v1.0.2
Published
A mongoose plugin for logging activities
Downloads
24
Maintainers
Readme
mongoose-activitylog
A mongoose plugin for logging activities
Install
$ npm install --save mongoose-activitylog
Usage
Define the Activity model
Add plugin to your Activity
schema and then use the model methods.
const mongoose = require('mongoose')
const activitylog = require('mongoose-activitylog')
const ActivitySchema = new mongoose.Schema()
ActivitySchema.plugin(activitylog)
const Activity = mongoose.model('Activity', ActivitySchema)
Log an activity
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().sort('-createdAt').exec() // returns the last logged activity
activity.description // returns 'Look mum, I logged something'
Set a subject
You can specify on which object the activity is performed by using performedOn
:
await new Activity()
.performedOn(someContentDocument)
.log('edited')
const activity = await Activity.findOne().sort('-createdAt').exec() // returns the last logged activity
activity.subject // returns the document that was passed to `performedOn`
The performedOn()
function has a shorter alias name: on
Set a causer
You can set who or what caused the activity by using causedBy
:
await new Activity()
.performedOn(someContentDocument)
.causedBy(someUserDocument)
.log('edited')
const activity = await Activity.findOne().sort('-createdAt').exec() // returns the last logged activity
activity.causer // returns the document that was passed to `causedBy`
The causedBy()
function has a shorter alias named: by
Set custom properties
You can add any property you want to an activity by using withProperties
:
await new Activity()
.performedOn(someContentDocument)
.causedBy(someUserDocument)
.withProperties({ key: 'value' })
.log('edited')
const activity = await Activity.findOne().sort('-createdAt').exec() // returns the last logged activity
activity.properties // returns `{ key: 'value' }`
activity.getExtraProperty('key') // returns 'value'
The withProperties()
function has a shorter alias named: with
API
log()
log(description: string): Promise
Log an activity.
performedOn()
performedOn(doc: Document): this
Specify on which object the activity is performed.
causedBy()
causedBy(doc: Document): this
Set who or what caused the activity.
withProperties()
withProperties(properties: object): this
Add properties to the activity.
withProperty()
withProperty(key: string, value: any): this
Add a property to the activity by key.
useLog()
useLog(logName: string): this
Specify log name of the activity.
use()
use(logName: string): this
Alias of useLog()
.
on()
on(doc: Document): this
Alias of performedOn()
.
by()
by(doc: Document): this
Alias of causedBy()
.
with()
with(properties: object): this
with(key: string, value: any): this
Alias of withProperties()
/ withProperty()
.
Note
Inspired by laravel-activitylog
License
MIT © Chun-Kai Wang