0.2.1 • Published 5 years ago

bookshelf-collection-querystring-mutation v0.2.1

Weekly downloads
24
License
MIT
Repository
github
Last release
5 years ago

Bookshelf Collection Querystring Mutation Plugin

Build Status

This Bookshelf plugin allows you to filter and sort your bookshelf instances directly from URL query string parameters. Using jsep to parse query string logical expressions into QueryBuilder query.

Inspired by Microsoft API Guideline Collections.

Instalation

$ npm install bookshelf-collection-querystring-mutation

Basic Usage

const express = require("express");
const bookshelf = require("bookshelf");
const knex = require("knex");

const bcqm = reqire("bookshelf-collection-querystring-mutation");

const repository = bookshelf(
  knex({
    /*...*/
  })
);

let app = express();

// Connect Plugin
repository.plugin(bcqm);

// Define Model
const List = repository.Model.extend({
  tableName: "shopping_list",
  allowed: ["name", "qty", "price"],
  hidden: ["barcode"]
});

app.get("/list", function(req, res, next) {
  let items = new List();

  // Mutate from query
  items.filter(req.query.filter).orderBy(req.query.orderBy);

  items.fetchAll().then(r => {
    /* Do stuff. */
  });
});

How to use

GET /list?filter=price ge 10&orderBy=name

Will filter instances that have price >= 10 and order them by name.

Filter Operation

Allows you to filter collection.

Pattern: field1 <operation> value [logical_opration] ...

OperatorDescriptionExample
Comparison Operators
eqEqualcity eq 'Redmond'
neNot equalcity ne 'London'
gtGreater thanprice gt 20
geGreater than or equalprice ge 10
ltLess thanprice lt 20
leLess than or equalprice le 100
lkLikename lk '%gg%'
iliLikename il '%GG%'
ininname in 'GG', 'Salad'
isIs (null)price is null
snIs Not (null)price sn null
Logical Operators
andLogical andprice le 200 and price gt 3.5
orLogical orprice le 3.5 or price gt 200
Grouping Operators
( )Precedence grouping(priority eq 1 or city eq 'Redmond') and price gt 100

Filter examples:

The following examples illustrate the use and semantics of each of the logical operators.

Example: all products with a name equal to 'Milk'

GET /list?filter=name eq 'Milk'

Example: all products with a name not equal to 'Milk'

GET /list?filter=name ne 'Milk'

Example: all products with the name 'Milk' that also has a price less than 2.55:

GET /list?filter=name eq 'Milk' and price lt 2.55

Example: all products that either have the name 'Milk' or have a price less than 2.55:

GET /list?filter=name eq 'Milk' or price lt 2.55

Example: all products that have the name 'Milk' or 'Eggs' and have a price less than 2.55:

GET /list?filter=(name eq 'Milk' or name eq 'Eggs') and price lt 2.55

Example: all products that have the name matching 'gg':

GET /list?filter=name lk "%gg%"

Example: all products that have ids in array of 1, 2, 3:

GET /list?filter=id in [1, 2, 3]

Example: all products that have price is set to null:

GET /list?filter=price is null

Example: all products that have price is not set to null:

GET /list?filter=price sn null

OrderBy

Allows you to sort collections.

Pattern: field1 [dirrection(asc|desc)][, field2 [dirrection(asc|desc)], [...]]

OrderBy Examples

Example: will return all products sorted by name in ascending order.

GET /list?orderBy=name

For example: will return all products sorted by name in descending order.

GET /list?orderBy=name desc

Sub-sorts can be specified by a comma-separated list of property names with OPTIONAL direction qualifier.

Example: will return all people sorted by name in descending order and a secondary sort order of price in ascending order.

GET /list?orderBy=name desc,price

Plugin Configuration:

repository.plugin(bcqm, {
  filterName: "filter", //filter function name in BSModel
  orderByName: "orderBy", //orderBy function name in BSModel
  hiddenPropsName: "hidden", //name of Property that provides array of hidden fields
  allowedPropsName: "allowed", //name of Property that provides array of allowed fields
  ignoreErrors: false //throw errors or skip silently
});

Hidden and Allowed arrays provide fields visibility for a plugin. If any filter or orderBy field is an array of private fields, it will throw an Error. If any filter or orderBy field is not an array of allowed fields, it will throw an Error.

You can also not provide this props if you don't care.

Model example:

const List = repository.Model.extend({
  tableName: "shopping_list",
  allowed: ["name", "qty", "price"], //filter and orderBy by this fields
  hidden: ["barcode"] //DON'T by this
});

TODO:

  • not Logical operator
0.2.1

5 years ago

0.2.0

5 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago