@onehilltech/blueprint-mongodb
Blueprint.js module for MongoDB
Last updated a month ago by onehilltech .
Apache-2.0 · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @onehilltech/blueprint-mongodb 
SYNC missed versions from official npm registry.

blueprint-mongodb

A Blueprint.js module for MongoDB

npm version Build Status Dependencies Coverage Status

Installation

npm install @onehilltech/blueprint-mongodb --save

Usage

Configuration

Define the mongodb.js configuration file in your Blueprint.js application.

// mongodb.config.js

module.exports = {
  connections: {
    $default: {
      uri: 'mongodb://localhost/tutorial',
      seed: [true|false],    // seed the database connection [default=false]
      options : {            // mongoose connection options
        
      }
    },
    
    connection2: {
      // ...
    }
  }
};

Models

Models represent the different collections stored in the MongoDB database. The models are defined using Mongoose schemas.

// app/models/Person.js
const mongodb = require ('@onehilltech/blueprint-mongodb');
const { Schema } = mongodb;

// use mongodb.Types to access mongoose.Types

const schema = mongodb.Schema ({
  first_name: {type: String, required: true, trim: true},
  last_name : {type: String, required: true, trim: true}
});

module.exports = mongodb.models ('person', schema, 'blueprint_people');

All models are defined on the default connection unless stated otherwise. To define a models on a different connection, use the modelOn() function where the first parameter is the name of the connection as defined in mongodb.js followed by the same parameters for the models() function.

Resources

Resource Model

A resource models is a models that includes an extra _stats property that details when the resource is created and updated. Both fields are managed automatically. You define a resource models similar to how you define a regular models. Instead of using the models() function, you use the resource() function.

// app/models/Person.js
const mongodb = require ('@onehilltech/blueprint-mongodb');
const { Schema } = mongodb;

// use mongodb.Types to access mongoose.Types

const schema = mongodb.Schema ({
  first_name: {type: String, required: true, trim: true},
  last_name : {type: String, required: true, trim: true}
});

module.exports = mongodb.resource ('person', schema, 'blueprint_people');

Soft Deletes

A soft delete is when the models is marked as deleted, but remains in the database. This is important if you want to still access the models in references. Soft delete is only available on resource models.

You add soft delete to a resource by setting the softDelete option to true when you are creating the schema.

const schema = mongodb.Schema ({
  first_name: {type: String, required: true, trim: true},
  last_name : {type: String, required: true, trim: true}
}, {softDelete: true});

module.exports = mongodb.resource ('person', schema, 'blueprint_people');

Now, when the person resource models is created, the _stat property will contain a deleted_at property. This property will be managed internally by the resource controller.

Resource Controller

The ResourceController is a default implementation of a resource controller that integrates with the Blueprint.js architecture. The ResourceController can be used as-is, or extended to add domain-specific customizations.

const { ResourceController } = require ('@onehilltech/blueprint-mongodb');
const { models } = require ('@onehilltech/blueprint');

/**
 * @class PersonController 
 * 
 * Resource controller for the person resource models.
 */
module.exports = ResourceController.extend ({
  models: models ('person')
});

The resource controller exposes the following actions:

Action HTTP method Path Body Response
create POST /resource {<resource>: { values }} {<resource>: { values }}
getOne GET /resource/:rcId N/A {<resource>: { values }}
getAll GET /resource N/A {<plural-resource>: { values }}
count GET /resource/count N/A {count: n}
update PUT /resource/:rcId {<resource>: { values }} {<resource>: { values }}
delete DELETE /resource/:rcId N/A true or false

For example, the PersonController exposes the following actions:

Action HTTP method Path Body Response
create POST /person {person: { first_name: 'James', last_name: 'Hill }} {person: {_id: 'id', first_name: 'James', last_name: 'Hill' }}
getOne GET /person/id N/A {person: {_id: 'id', first_name: 'James', last_name: 'Hill' }}
getAll GET /person N/A {persons: [{_id: 'id', first_name: 'James', last_name: 'Hill' }]}
count GET /person/count N/A {count: n}
update PUT /person/id {person: { first_name: 'John' }} {person: {_id: 'id', first_name: 'John', last_name: 'Hill }}
delete DELETE /person/id N/A true or false

Messaging Framework. All actions on the default implementation of the ResourceController will generate the following events on Blueprint.js messaging framework.

Action Event Example
create [prefix.][name].created [prefix.]person.created
update [prefix.][name].updated [prefix.]person.updated
delete [prefix.][name].deleted [prefix.]person.deleted

The prefix in the event name is optional. It is defined by the eventPrefix property passed to the ResourceController constructor.

Adding Domain-specific Behavior

Each method in the ResourceController returns an action that can be extended to add domain-specific behavior. For example, you may want to do pre and post processing before and after the models is created, respectively. It is as simple as extending the action in your subclass of the ResourceController, and overloading the appropriate methods.

const { ResourceController } = require ('@onehilltech/blueprint-mongodb');
const { models } = require ('@onehilltech/blueprint');

/**
 * @class PersonController 
 * 
 * Resource controller for the person resource models.
 */
module.exports = ResourceController.extend ({
  models: models ('person'),
  
  create () {
    return this._super.call (this, ...arguments).extend ({
      prepareDocument (req, doc) {
        doc.user = req.user._id;
        return doc;
      }
    });
  }
});

The following is a list of the actions, and the method that can be overloaded in their order of execution.

  • create

    • prepareDocument (req, doc)
    • preCreateModel (req)
    • createModel (req, doc)
    • postCreateModel (req, models)
    • prepareResponse (req, res, result)
  • getOne

    • getId (req, id)
    • getProjection(req)
    • getOptions (req, options)
    • preGetModel (req)
    • getModel (req, id, projection, options)
    • postGetModel (req, models)
    • prepareResponse (req, res, result)
  • getAll

    • getFilter (req, query)
    • getProjection (req)
    • getOptions (req, options)
    • preGetModels (req)
    • getModels (req, filter, projection, options)
    • postGetModels (req, models)
    • .prepareResponse (req, res, result)
  • update

    • getId (req, id)
    • getUpdate (req, update)
    • getOptions (req, options)
    • preUpdateModel (req)
    • updateModel (req, id, update, options)
    • postUpdateModel (req, models)
    • prepareResponse (req, res, result)
  • delete

    • getId (req, id)
    • preDeleteModel (req)
    • deleteModel (req, id)
    • postDeleteModel (req, models)
    • prepareResponse (req, res, models, result)
  • count

    • getFilter (req, filter)
    • preCountModels (req)
    • getCount (req, filter)
    • postCountModels (req, count)
    • prepareResponse (req, res, result)

You can return a Promise the overloaded method if you need to perform asynchronous execution.

GridFSController

The GridFSController is a Blueprint.js resource controller designed for GridFS in MongoDB. The GridFSController supports the following operations out-of-the-box:

  • create
  • getOne
  • delete

Currently, it does not support getting multiple resources and updating an existing resource.

Because GridFS is designed to store large files, the GridFSController assumes the client is uploading a file, which can be a multi-part file. The controller handles this concern automatically. It's just your responsibility to inform the controller of the parameter used for the upload, and what connection on the database to use for GridFS. Below is an example GridFSController for storing images that use the default connection.

const { GridFSController } = require ('@onehilltech/blueprint-mongodb');

/**
 * @class ImageController
 */
module.exports = GridFSController.extend ({
  // name of resource
  name: 'image',       
});

This controller will extract the uploaded file from the image parameter. In addition, it will create collections named image.files and image.chunks in the database associated with the connection.

See GridFSController for the optional properties to configure the controller's behavior.

Current Tags

  • 4.21.3                                ...           latest (a month ago)

216 Versions

  • 4.21.3                                ...           a month ago
  • 4.21.0                                ...           a month ago
  • 4.19.6                                ...           a month ago
  • 4.19.5                                ...           a month ago
  • 4.19.4                                ...           a month ago
  • 4.19.2                                ...           a month ago
  • 4.18.3                                ...           3 months ago
  • 4.15.1                                ...           5 months ago
  • 4.18.1                                ...           4 months ago
  • 4.14.1                                ...           5 months ago
  • 4.9.0                                ...           5 months ago
  • 4.8.0                                ...           5 months ago
  • 4.15.0                                ...           5 months ago
  • 4.14.0                                ...           5 months ago
  • 4.12.0                                ...           5 months ago
  • 4.10.0                                ...           5 months ago
  • 4.7.0                                ...           5 months ago
  • 1.11.3                                ...           6 months ago
  • 1.11.2                                ...           6 months ago
  • 1.11.1                                ...           7 months ago
  • 1.11.0                                ...           9 months ago
  • 1.10.1                                ...           a year ago
  • 1.9.2                                ...           a year ago
  • 1.10.0                                ...           a year ago
  • 1.9.3                                ...           a year ago
  • 1.9.1                                ...           2 years ago
  • 1.9.0                                ...           2 years ago
  • 1.8.2                                ...           2 years ago
  • 1.8.1                                ...           2 years ago
  • 1.8.0                                ...           2 years ago
  • 1.7.3                                ...           2 years ago
  • 1.7.2                                ...           2 years ago
  • 1.7.1                                ...           2 years ago
  • 1.7.0                                ...           2 years ago
  • 1.6.9                                ...           2 years ago
  • 1.6.8                                ...           2 years ago
  • 1.6.7                                ...           2 years ago
  • 1.6.6                                ...           2 years ago
  • 1.6.5                                ...           2 years ago
  • 1.6.4                                ...           2 years ago
  • 1.6.3                                ...           2 years ago
  • 1.6.2                                ...           2 years ago
  • 1.6.1                                ...           2 years ago
  • 1.6.0                                ...           2 years ago
  • 1.5.2                                ...           2 years ago
  • 1.5.1                                ...           2 years ago
  • 1.5.0                                ...           2 years ago
  • 1.4.1                                ...           2 years ago
  • 1.4.0                                ...           2 years ago
  • 1.3.2                                ...           2 years ago
  • 1.3.1                                ...           2 years ago
  • 1.3.0                                ...           2 years ago
  • 1.2.4                                ...           2 years ago
  • 1.2.3                                ...           2 years ago
  • 1.2.2                                ...           2 years ago
  • 1.2.1                                ...           2 years ago
  • 1.2.0                                ...           2 years ago
  • 1.1.0                                ...           2 years ago
  • 1.0.4                                ...           2 years ago
  • 1.0.3                                ...           2 years ago
  • 1.0.2                                ...           2 years ago
  • 1.0.1                                ...           2 years ago
  • 1.0.0                                ...           2 years ago
  • 1.0.0-39                                ...           2 years ago
  • 1.0.0-38                                ...           2 years ago
  • 1.0.0-37                                ...           2 years ago
  • 1.0.0-36                                ...           2 years ago
  • 1.0.0-35                                ...           2 years ago
  • 1.0.0-34                                ...           2 years ago
  • 1.0.0-33                                ...           2 years ago
  • 1.0.0-32                                ...           2 years ago
  • 1.0.0-31                                ...           2 years ago
  • 1.0.0-30                                ...           2 years ago
  • 1.0.0-29                                ...           2 years ago
  • 1.0.0-28                                ...           2 years ago
  • 1.0.0-27                                ...           2 years ago
  • 1.0.0-26                                ...           2 years ago
  • 1.0.0-25                                ...           2 years ago
  • 1.0.0-24                                ...           2 years ago
  • 1.0.0-23                                ...           2 years ago
  • 1.0.0-22                                ...           2 years ago
  • 1.0.0-21                                ...           2 years ago
  • 1.0.0-20                                ...           2 years ago
  • 1.0.0-19                                ...           2 years ago
  • 1.0.0-18                                ...           2 years ago
  • 1.0.0-17                                ...           2 years ago
  • 1.0.0-16                                ...           2 years ago
  • 1.0.0-15                                ...           2 years ago
  • 1.0.0-14                                ...           2 years ago
  • 1.0.0-13                                ...           2 years ago
  • 1.0.0-12                                ...           2 years ago
  • 1.0.0-11                                ...           2 years ago
  • 1.0.0-10                                ...           2 years ago
  • 1.0.0-9                                ...           2 years ago
  • 1.0.0-8                                ...           2 years ago
  • 1.0.0-7                                ...           2 years ago
  • 1.0.0-6                                ...           2 years ago
  • 1.0.0-5                                ...           2 years ago
  • 1.0.0-4                                ...           2 years ago
  • 1.0.0-3                                ...           2 years ago
  • 1.0.0-2                                ...           2 years ago
  • 1.0.0-1                                ...           2 years ago
  • 1.0.0-0                                ...           2 years ago
  • 0.36.0                                ...           3 years ago
  • 0.35.7                                ...           3 years ago
  • 0.35.6                                ...           3 years ago
  • 0.35.5                                ...           3 years ago
  • 0.35.4                                ...           3 years ago
  • 0.35.3                                ...           3 years ago
  • 0.35.2                                ...           3 years ago
  • 0.35.1                                ...           3 years ago
  • 0.35.0                                ...           3 years ago
  • 0.34.16                                ...           3 years ago
  • 0.34.15                                ...           3 years ago
  • 0.34.14                                ...           3 years ago
  • 0.34.13                                ...           3 years ago
  • 0.34.12                                ...           3 years ago
  • 0.34.11                                ...           3 years ago
  • 0.34.10                                ...           3 years ago
  • 0.34.9                                ...           3 years ago
  • 0.34.8                                ...           3 years ago
  • 0.34.7                                ...           3 years ago
  • 0.34.6                                ...           3 years ago
  • 0.34.5                                ...           3 years ago
  • 0.34.4                                ...           3 years ago
  • 0.34.3                                ...           3 years ago
  • 0.34.2                                ...           3 years ago
  • 0.34.1                                ...           3 years ago
  • 0.34.0                                ...           3 years ago
  • 0.33.0                                ...           3 years ago
  • 0.32.13                                ...           3 years ago
  • 0.32.12                                ...           3 years ago
  • 0.32.11                                ...           3 years ago
  • 0.32.10                                ...           3 years ago
  • 0.32.9                                ...           3 years ago
  • 0.32.7                                ...           3 years ago
  • 0.32.6                                ...           3 years ago
  • 0.32.5                                ...           3 years ago
  • 0.32.4                                ...           3 years ago
  • 0.32.3                                ...           3 years ago
  • 0.32.2                                ...           3 years ago
  • 0.32.1                                ...           3 years ago
  • 0.32.0                                ...           3 years ago
  • 0.31.2                                ...           3 years ago
  • 0.31.1                                ...           3 years ago
  • 0.31.0                                ...           3 years ago
  • 0.29.11                                ...           3 years ago
  • 0.29.10                                ...           3 years ago
  • 0.29.9                                ...           3 years ago
  • 0.29.8                                ...           3 years ago
  • 0.29.7                                ...           3 years ago
  • 0.29.6                                ...           3 years ago
  • 0.29.5                                ...           3 years ago
  • 0.29.4                                ...           3 years ago
  • 0.29.3                                ...           3 years ago
  • 0.29.2                                ...           3 years ago
  • 0.29.1                                ...           3 years ago
  • 0.29.0                                ...           3 years ago
  • 0.28.0                                ...           3 years ago
  • 0.27.2                                ...           3 years ago
  • 0.27.1                                ...           3 years ago
  • 0.27.0                                ...           3 years ago
  • 0.26.0                                ...           3 years ago
  • 0.25.0                                ...           3 years ago
  • 0.24.0                                ...           3 years ago
  • 0.23.4                                ...           3 years ago
  • 0.23.3                                ...           3 years ago
  • 0.23.2                                ...           3 years ago
  • 0.23.1                                ...           3 years ago
  • 0.23.0                                ...           3 years ago
  • 0.22.0                                ...           3 years ago
  • 0.21.0                                ...           3 years ago
  • 0.20.6                                ...           3 years ago
  • 0.20.5                                ...           3 years ago
  • 0.20.4                                ...           3 years ago
  • 0.20.3                                ...           3 years ago
  • 0.20.2                                ...           3 years ago
  • 0.20.1                                ...           3 years ago
  • 0.20.0                                ...           3 years ago
  • 0.19.1                                ...           3 years ago
  • 0.19.0                                ...           3 years ago
  • 0.18.0                                ...           3 years ago
  • 0.17.6                                ...           3 years ago
  • 0.17.5                                ...           3 years ago
  • 0.17.4                                ...           3 years ago
  • 0.17.2                                ...           3 years ago
  • 0.17.1                                ...           3 years ago
  • 0.17.0                                ...           3 years ago
  • 0.16.1                                ...           3 years ago
  • 0.16.0                                ...           3 years ago
  • 0.15.1                                ...           4 years ago
  • 0.15.0                                ...           4 years ago
  • 0.14.0                                ...           4 years ago
  • 0.13.0                                ...           4 years ago
  • 0.12.1                                ...           4 years ago
  • 0.12.0                                ...           4 years ago
  • 0.11.0                                ...           4 years ago
  • 0.10.0                                ...           4 years ago
  • 0.9.1                                ...           4 years ago
  • 0.9.0                                ...           4 years ago
  • 0.8.1                                ...           4 years ago
  • 0.8.0                                ...           4 years ago
  • 0.7.1                                ...           4 years ago
  • 0.7.0                                ...           4 years ago
  • 0.6.4                                ...           4 years ago
  • 0.6.3                                ...           4 years ago
  • 0.6.2                                ...           4 years ago
  • 0.6.1                                ...           4 years ago
  • 0.6.0                                ...           4 years ago
  • 0.5.2                                ...           4 years ago
  • 0.5.1                                ...           4 years ago
  • 0.5.0                                ...           4 years ago
  • 0.4.0                                ...           4 years ago
  • 0.3.1                                ...           4 years ago
  • 0.3.0                                ...           4 years ago
  • 0.2.0                                ...           4 years ago
Maintainers (1)
Downloads
Today 0
This Week 1
This Month 7
Last Day 0
Last Week 0
Last Month 93
Dependencies (3)
Dev Dependencies (3)

Copyright 2014 - 2016 © taobao.org |