npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

mongoose-restifier-emitter

v1.0.29

Published

A library that helps you build a RESTful API and suscribe to events

Readme

Mongoose Restifier Emitter

It helps you building restful APIs and it emit events

first, register the parser in express

import { parser } from 'mongoose-restifier-emitter'

...
/*
 * this will parse all query params into objects, it uses
 * a forin and JSON.parse and will replace req.query
 */ 
app.use(parser) 
...

then, in your routes

const mongoose = require('mongoose')
const { handler } = require('mongoose-restifier-emitter')

const todosSchema = mongoose.Schema({
    title: String,
    desc: String,
    created_at: Date,
})

const Todos = mongoose.model('Todo', todosSchema)
const omitSeveralAttributes = ['created_at', 'desc']
const omitOnlyAttribute = 'created_at'
const softDeleteAttrUpdate = {
	booleanAttrOne: true, // return true
	booleanAttrTwo: [Boolean, false], // return false
	booleanAttrCallMethod: [Boolean, false, 'toString'], // return "false"
	update_at: [Date, null, 'toISOString'], // return "2019-01-01T00:00:00.000Z"
	deleted_at: [Date, null], // return new Date()
	otherAttr: [Date, '2019'], // return Tue Jan 01 2019 00:00:00
}

router.get('/', handler.index(Todos, omitSeveralAttributes || omitOnlyAttribute))
router.get('/:id', handler.show(Todos, omitSeveralAttributes || omitOnlyAttribute))
router.post('/', handler.create(Todos))
router.put('/:id', handler.update(Todos))

router.delete('/:id', handler.destroy(Todos))
router.delete('/', handler.destroyBatch(Todos))
// or 
router.delete('/:id', handler.softDelete(Todos, softDeleteAttrUpdate))
router.delete('/', handler.softDeleteBatch(Todos, softDeleteAttrUpdate))

After every action, an event will be dispatched:

index

The dispatched event will contain the request object and the collection found.

const { events } = require('mongoose-restifier-emitter')
events.on('Todo:listed', function(req, collection) {
	// code here...
})

show

The dispatched event will contain the request object and the object found.

const { events } = require('mongoose-restifier-emitter')
events.on('Todo:detailed', function(req, doc) {
	// code here...
})

create

The dispatched event will contain the request object and the id of the created object.

const { events } = require('mongoose-restifier-emitter')
events.on('Todo:created', function(req, id) {
	// code here...
})

update

The dispatched event will contain the request object.

const { events } = require('mongoose-restifier-emitter')
events.on('Todo:updated', function(req) {
	// code here...
})

delete

The dispatched event will contain the request object.

const { events } = require('mongoose-restifier-emitter')
events.on('Todo:deleted', function(req) {
	// code here...
})

Events format

The events format is based on the model name, so, if you have a model with this structure:

const mongoose = require('mongoose')

const todosSchema = mongoose.Schema({
    desc: String,
})
// Model name here, first argument of model method
const Todos = mongoose.model('Todo', todosSchema)

The events dispatched will have the structure Todo:action

The index method

The most important endpoint in a RESTful API is the index method. You can make queries to this endpoint:

GET
http://localhost:3000/?q={"desc":"foo"}

Will return the elements that match the attribute with foo

GET
http://localhost:3000/?select="desc -_id"

Will return the objects but only with the desc field and will omit the _id field

GET
http://localhost:3000/?limit=10

Will get only the first 10 elements

You can also retrieve the total amount of certain documents:

GET
http://localhost:3000/?count={"desc":"lala"}

This will return an integer and not the actual documents.

You can also make aggregate queries:

GET
http://localhost:3000/?aggregate={"$match":{"desc":"lala"},{"$sort":{"_id":-1}}}

Of course you can compose this queries:

http://localhost:3000/?q={"desc":"lala"}&limit=10&select="desc -_id"