1.1.2 • Published 6 years ago

mongoose-requests v1.1.2

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

mongoose-requests

Mongoose ODM plugin for handling requests between users.

Installation

with npm:
npm install mongoose-requests
with yarn:
yarn add mongoose-requests

Description

mongoose-requests makes it easy to handle requests between user models. Requests are stored as an array of subdocuments under the user schema. All functions have both static and instance forms and return JavaScript promises.

Usage

Start by including the plugin in the schema:

const mongoose = require('mongoose')
const requests = require('mongoose-requests')
const schema = new mongoose.Schema({
  // regular schema building
})

// optionally one may specify both
// the name of the field that will be added
// and the name of the model that it will be added to
schema.plugin(requests, { userModel: 'Racer', propName: 'challenges' })

const Racer = mongoose.model('Racer', schema)

Send a request from one document to another either:

Racer.sendRequest(racer1._id, racer2._id)

Requests can be fetched with getRequests

Racer.getRequests(racer1._id)
  .then(requests => console.log(requests))

A request document looks like this:

{
  sender: [racer1._id],
  receiver: [racer2._id],
  status: [one of ('pending', 'accepted', 'denied', 'requested')]
  sent: [Date request was sent],
  responded: [Date request was responded to or null]
}

One can either accept a request:

Racer.acceptRequest(racer2._id, racer1._id)

Or deny a request:

Racer.denyRequest(racer2._id, racer1._id)

Finally, can get all requests of a particular status:

Racer.getOfStatus(racer1._id, 'accepted')
  .then(requests => console.log(requests))

Want to cancel a request? Just delete it:

Racer.deleteRequest(racer1._id, racer2._id)

All functions have instance versions:

racer1.sendRequest(racer2._id)
racer2.getRequests().then(requests => console.log(requests))
racer2.acceptRequest(racer1._id)
racer1.getOfStatus('accepted').then(requests => console.log(requests))
racer1.deleteRequest(racer2._id)

What's next?

mongoose-requests is still very much in its infancy, and will be expanded upon in the future. Need a specific feature that you think would fit in nicely? Submit a feature request as an issue and I'll see what I can do.

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago