Skip to content

Instantly share code, notes, and snippets.

@sethvincent
Last active August 29, 2015 14:18
Show Gist options
  • Save sethvincent/927bce34b0f58d2c4a76 to your computer and use it in GitHub Desktop.
Save sethvincent/927bce34b0f58d2c4a76 to your computer and use it in GitHub Desktop.
Ideas for activity module

activity-tracker

This is a set of ideas for how an activity module could work that uses leveldb and is meant to track all the important activity happening on a site. Primary usage would be on the server. It could presumably also work on the client side, but probably wouldn't need to.

API

var activityTracker = require('activity-tracker')

or some similar package name. activity-tracker is available, though

var activity = activityTracker(db, options)

  • db: a levelup instance for storing all the values
  • options: TBD

activity.add(options, callback)

  • options:
    • type: string slug that identifies the model that is being acted on
    • action: string that specifies an action like create, update, delete
    • message: string that is used as the main description of the activity
    • data: object with arbitrary data that can be attached to the activity

internally this function adds a timestamp and a key.

  • callback provides error and response arguments, the response being the activity object.

activity.remove(key, callback)

  • callback provides an errorargument.

activity.get(key, callback)

  • callback provides error and response arguments, the response being the activity object.

activity.createReadStream(options)

Events

activity.on('add', function (item) {})

activity.on('remove', function (key) {})

Usage examples

Usage example #1:

function Post (options) {
 this.activity = require('activity-tracker')(db)
}

Post.prototype.create = function (options, cb) {
 this.activity.add({ type: 'post', action: 'create', message: options.title })
 
 // ... usual create stuff
}

Usage example #2:

module.exports = Post

function Posts (options) {
 // make Posts an event emitter
}

Posts.prototype.create = function (options, cb) {
  // do the create stuff that results in the final `post` object
  this.emit('create', post)
}

Then in another file:

var posts = require('./posts')
var activity = require('activity-tracker')(db)

posts.on('create', function (post) {
  activity.add({ type: 'post', action: 'create', message: post.title })
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment