meteor-wapi-collection

    0.1.1 • Public • Published

    Build Status Coverage Status Dependency Status devDependency Status

    meteor-wapi-collection

    Easy CUD (no R) collections for meteor-wapi

    Install

    npm i --save meteor-wapi-collection

    Example

    var express     = require("express");
    var MongoClient = require("mongodb").MongoClient;
    var MW          = require("meteor-wapi");
    var Collection  = require("meteor-wapi-collection");
     
    var mongoUrl = process.env.MONGO_URL || "mongodb://localhost:3001/meteor";
     
    MongoClient.connect(mongoUrl, function (err, db) {
        var mw = new MW(db);
        var Books = new Collection(mw, "books");
        /*
        *   We now have three remote methods defined:
        *     - `/books/insert`
        *     - `/books/update`
        *     - `/books/remove`
        */
        var app = express()
            .use("/call", mw.getRouter())
            .listen(process.env.PORT || 4000);
    });

    API

    new Collection(mw, name)

    Creates a new Collection instance and attaches three remote methods to the supplied MW instance. The three methods are:

    • /collectionName/insert
    • /collectionName/update
    • /collectionName/remove
    Arguments
    • mw MW instance required: the MW instance we wish to use with the collection.

    • name String required: the name of the collection.

    Returns

    A Collection instance.

    .addValidationRules(rulesMap)

    Registers validation rules for the insert, update and remove methods.

    Validation rules can either:

    • throw or return a value (which is ignored)
    • return a promise which either gets fulfilled (with a value which is ignored) or rejected

    If the rule returns a value (even undefined) or an eventually fulfilled promise, the value (fulfillment-value) is ignored and the operation continues (after subsequent rules are run).

    If a rule throws, or returns a promise which gets rejected, the operation is aborted (subsequent rules won't run). If the error thrown (or passed as rejection value of the promise) is an instance of a MW.Error, then it'll get sent back to the client. Otherwise, a generic "500 Internal server error" error will be sent.

    Validation rules have access to the same context of their associated method.

    Arguments
    • rulesMap string-function dictionary required: a dictionary of validation functions, where the keys can either be insert, update or remove

    TODO: consider giving the possibility to extend the context in which the validation rules run (as meteor-wapi does for methods).

    Returns

    The Collection instance to allow for method chaining.

    Attached remote methods

    /collectionName/insert (newDocument)

    Inserts a document.

    Arguments

    • newDocument Object required: the document to insert.

    Returns

    • 200 OK if successful, 4** or 5** otherwise.

    /collectionName/update (documentId, patches)

    Updates a document.

    Arguments

    • documentId String required: the _id of the document to update.

    • patches Array of JSON-Patches required: the array of JSON patches to apply to the document. JSON-Patches must conform to the rfc 6902 standard.

    Returns

    • 200 OK if successful, 4** or 5** otherwise.

    /collectionName/remove (documentId)

    Removes a document.

    Arguments

    • documentId String required: the _id of the document to remove.

    Returns

    • 200 OK if successful, 4** or 5** otherwise.

    Keywords

    none

    Install

    npm i meteor-wapi-collection

    DownloadsWeekly Downloads

    1

    Version

    0.1.1

    License

    MIT

    Last publish

    Collaborators

    • pscanf