内容提要 
JSON 对象 Mongo 样式的标准查询 .
安装
npm install @care-portals/node-json-criteria --save
使用量
常见JS
const { test } = require("@care-portals/node-json-criteria");
console.log(test({ foo: 1 }, { foo: { $gt: 0 } }));
ES 模块
import { test } from "@care-portals/node-json-criteria";
console.log(test({ foo: 1 }, { foo: { $gt: 0 } }));
行动
标准查询遵循蒙哥发行公约。 您可以使用描述于 http://docs.mongodb.org/manual/参考文献/操作者/query
- 逻辑操作
{ $and: [ ... ] }- 全部来{ $or: [ ... ] }- 任何一个{ $nor: [ ... ] }- 没有{ $not: ... }- 才没有呢.{ $not: { $gt: 0, $lt: 1 } }
- 比较操作
{ field: ... }- 平等(隐性){ field: { $eq: ... } }- 平等(明确){ field: { $ne: ... } }- 不对{ field: { $gt: ... } }- 超过{ field: { $gte: ... } }- 大于或等于{ field: { $lt: ... } }- 低于{ field: { $lte: ... } }- 低于或等于{ field: { $in: [ ... ] } }- 至少一个元素匹配值(或如果数组值的元素){ field: { $nin: [ ... ] } }- 没有元素匹配值(或值的元素如果数组)
- 元素行动
{ field: { $exists: true/false } }- 字段存在{ field: { $type: 'number|string|...' } }- 匹配字段类型
- 评价业务
{ field: { $mod: [ div, rem ] } }- 除以div有提醒雷姆{ field: { $regexp: '...', $options: 'i' } }- 将正则表达式与可选选项匹配{ field: { $where: function (v) { return true/false } } }- 使用提供的功能进行测试,用于安全目的,因为不支持字符串
- 阵列操作
{ field: { $all: [ ... ] } }- 所有值都在字段值中{ field: { $elemMatch: ... } }- 至少一个元素匹配{ field: { $size: ... } }- 匹配字段数组值的长度{ field: { $first: ... } }- 匹配数组的第一个元素{ field: { $last: ... } }- 匹配数组的最后一个元素{ field: { $pop: ... } }- 匹配数组的最后一个元素(类似于 Array.pop ())
更多例子请查看图示.
示例使用大小写
假设你有基于JSON的 RESTFOR API,你想用摩卡测试:
// spec/spec-my-api.js
var assert = require('assert')
var request = require('request')
var jc = require('@care-portals/node-json-criteria')
var endpoint = 'http://localhost:3000/api/v1'
function get (path, criteria) {
return function (done) {
request
.get(endpoint + path)
.end(function (err, resp) {
assert.ifError(err)
assert.ok(resp.body.count > 0)
assert.ok(jc.test(resp.body, criteria))
done(err)
})
}
}
describe('api', function () {
it('should have zero users', get('users/count', { count: 0 }))
it('should have empty user list', get('users', { users: { $size: 0 } }))
it('should have 3 to 5 products', get('products', { 'meta.count': { $gt: 2, $lte: 5 } }))
it('should have 3 products with meta count check', get('products', {
$and: [
{ 'meta.count': 3 },
{ products: { $size: 3 } }
]
}))
it('should have shipped flag', get('orders/1', {
tags: { $in: [ 'shipped' ] }
}))
})
□ 许可证
The MIT License (MIT)
Copyright (c) 2014 Mirek Rusin http://github.com/mirek
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.