1.0.16 • Published 8 years ago

parseable v1.0.16

Weekly downloads
18
License
-
Repository
github
Last release
8 years ago

Parseable

Parseable allows NodeJS developers to build REST API applications with client-driven (ad hoc, dynamic) queries similar to noBackend platforms. It is influenced by the features provided by the LoopBack and Parse.com Backend-as-a-Service platforms. Parseable can parse RESTful API endpoint queries into properly structured MongoDB syntax.

##Parseable Middlewares

middleware

  • invokes operationMiddleware and filterMiddleware in sequence

operationMiddleware

  • parse req.body with operationParser

filterMiddleware

  • parse req.query with whereParser, sortParser, limitParser, skipParser, and keysParser in sequence

default values

If req.query does't contain property "limit", Parseble will add it to req.query with value 200. The default value can be changed, e.g.:

  var defaultValues = require('parseable').defaultValues; 
  defaultValues.limit = 100;

Example of using Parseable as a middleware:

  var parseable = require('parseable').middleware;  
  var router = require('express').Router();
    
  router.get('/', function(req, res, next) {
    //raw request:
    console.log(req.query);//{"where":{"a1":{"b1":{"c1":{$gt:10}}}},"sort":{},"skip":0,"keys":"a,b,-c"}
    next();
  });
  
  router.get('/', parseable, function(req, res) {
    //after:
    console.log(req.query);//{"where":{"a1.b1.c1":{$gt:10}},"sort":{},"limit":200,"skip":0,"keys":{a:1,b:1,c:0}}
  });

Parseable.operationParser

  var operationParser = require('parseable').operationParser; 
  
  var input = {"field":{"__op": "Increment", "amount": 1234}};

  operationParser(input,function(err,output){
    console.log(output); // {$inc:{"field":1234}}
  });
  • Increment: increment a number field

       input:  {"field":{"__op": "Increment", "amount": 1234}}
       output: {$inc:{"field":1234}}
  • Add: add objects to array field

       input: {"field":{"__op": "Add", "objects": [1,2,3]}}
       output: {$pushAll:{"field":[1,2,3]}}
  • AddUnique: add objects if not existed

       input: {"field":{"__op": "AddUnique", "objects": [1,2,3]}}
       output: {$addToSet:{"field":{$each:[1,2,3]}}}
  • Remove: removes from an existing array all instances of a value or values that match a specified query

       input: {"field":{"__op": "Remove", "object": {"a":1}}}
       output: {$pull:{"field":{"a":1}}}
  • RemoveAll: remove objects from array field

       input: {"field":{"__op": "RemoveAll", "objects": [1,2,3]}}
       output: {$pullAll:{"field":[1,2,3]}}
  • Delete: delete a field

       input: {"field":{"__op": "Delete"}}
       output: {$unset:{"field":""}}
  • Set: replaces the value of a field to the specified value.

       input: {"field":123}
       output: {$set:{field:123}}

Parseable.whereParser:

  var whereParser = require('parseable').whereParser; 
  var input = {"a1":{"b1":{"c1":1}}};

  whereParser(input,function(err,output){
    console.log(output); // {"a1.b1.c1":1}
  });
   input: {"a1":{"b1":{"c1":1}}}
   output: {"a1.b1.c1":1}

   input: "{\"a1\":{\"b1\":{\"c1\":1}}}"
   output: {"a1.b1.c1":1}

   input: {"a1":{"b1":{"c1":[1,2,3],"c2":"abc"},"b2":1},"a2":1}
   output: {"a1.b1.c1":[1,2,3],"a1.b1.c2":"abc","a1.b2":1,"a2":1}

   input: "{'a':123}"
   err:  SyntaxError

Parseable.sortParser :

  var sortParser = require('parseable').sortParser; 

  var input = {"a1":{"b1":{"c1":1}}};

  sortParser(input,function(err,output){
    console.log(output); // {"a1.b1.c1":1}
  });
   input: {"a1":{"b1":{"c1":1}}}
   output: {"a1.b1.c1":1}

   input: "{\"a1\":{\"b1\":{\"c1\":1}}}"
   output: {"a1.b1.c1":1}

   input: {"a1":{"b1":{"c1":1},"b2":-1},"a2":1}
   output: {"a1.b1.c1":1,"a1.b2":-1,"a2":1}

   input: {"a1":{"b1":{"c1":[1,2,3]}}}
   err: "bad sort specification: 1,2,3"

   input: {"a1":{"b1":{"c1":1},"b2":0}}
   err: "bad sort specification: 0"

Parseable.limitParser

Parseable.skipParser

  var limitParser = require('parseable').limitParser; 
  var skipParser = require('parseable').skipParser; 
  
  var input = 123;

  limitParser(input,function(err,output){
    console.log(output); // 123
  });
   input: 123
   output: 123

   input: "123"
   output: 123

   input: 2147483649
   err: value out of range

   input: -2147483649
   err: value out of range

   input: "123a"
   err: SyntaxError

   input: "{limit:123}"
   err: SyntaxError

Parseable.keysParser

  var keysParser = require('parseable').keysParser; 
  
  var input = "foo,-koo";

  keysParser(input,function(err,output){
    console.log(output); // {"foo":1,"koo":0}
  });
   input: "a,b,c,-d"
   output: {a:1,b:1,c:1,d:0}

   input: "a,b,c,,,"
   output: {a:1,b:1,c:1}

   input: 123
   err: "SyntaxError: keys is not string"
1.0.16

8 years ago

1.0.15

9 years ago

1.0.14

9 years ago

1.0.13

9 years ago

1.0.12

9 years ago

1.0.11

10 years ago

1.0.10

10 years ago

1.0.9

10 years ago

1.0.8

10 years ago

1.0.7

10 years ago

1.0.6

10 years ago

1.0.5

10 years ago

1.0.4

10 years ago

1.0.2

10 years ago

1.0.1

10 years ago

1.0.0

10 years ago