1.0.3 • Published 7 years ago

kuali-mongoose-express v1.0.3

Weekly downloads
1
License
AGPL-3.0
Repository
github
Last release
7 years ago

kuali-mongoose-express

Express middleware to initialize your mongoose models into the request object.

Installation

npm install kuali-mongoose-express

Usage

const express = require('express')
const mongoose = require('mongoose')
const bodyParser = require('body-parser')
const mongooseExpress = require('kuali-mongoose-express')

mongoose.Promise = Promise

const userSchema = {
  name: 'User',
  schema: new Schema({
    username: {
      type: String,
      required: true,
      unique: true
    },
    password: {
      type: String,
      required: true
    }
  })
}

const connection = mongoose.connect('mongodb://localhost/my_app', {
  useMongoClient: true
})

const app = express()

app.use(mongooseExpress(connection, [
  userSchema
]))
app.use(bodyParser.json())
app.post('/api/users', (req, res, next) => {
  const User = req.models.User
  return User
    .create({
      username: req.body.username,
      password: req.body.password
    })
    .then((user) => res.json({ _id: user._id, username: user.username }))
    .catch(next)
})

API

mongooseExpress(connection, schemas)

Returns express midleware that adds your mongoose models to the request

  • connection {mongoose.Connection} - Your mongoose connection object
  • schemas {Object[]} - An array of objects, with each object having a name property (the name you want to give to mongoose.Model(name)) and a schema property (the mongoose Schema for your model).
1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago