@simplehealth/filterql
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -1,3 +0,9 @@ | ||
# v0.0.1 | ||
# v0.2.0 | ||
- Better organize exports | ||
- Rename `FilterQLBuilder` class to `Q` | ||
- Export SQLCondition constructors, too | ||
# v0.1.0 | ||
- Initial release |
368
index.js
@@ -6,273 +6,103 @@ "use strict"; | ||
}); | ||
exports["default"] = void 0; | ||
Object.defineProperty(exports, "Q", { | ||
enumerable: true, | ||
get: function get() { | ||
return _Q.Q; | ||
} | ||
}); | ||
Object.defineProperty(exports, "serializeExpr", { | ||
enumerable: true, | ||
get: function get() { | ||
return _Q.serializeExpr; | ||
} | ||
}); | ||
Object.defineProperty(exports, "toSQLConditions", { | ||
enumerable: true, | ||
get: function get() { | ||
return _sqlbuilder.toSQLConditions; | ||
} | ||
}); | ||
Object.defineProperty(exports, "RAW", { | ||
enumerable: true, | ||
get: function get() { | ||
return _SQLCondition.RAW; | ||
} | ||
}); | ||
Object.defineProperty(exports, "TRUE", { | ||
enumerable: true, | ||
get: function get() { | ||
return _SQLCondition.TRUE; | ||
} | ||
}); | ||
Object.defineProperty(exports, "FALSE", { | ||
enumerable: true, | ||
get: function get() { | ||
return _SQLCondition.FALSE; | ||
} | ||
}); | ||
Object.defineProperty(exports, "IS_NULL", { | ||
enumerable: true, | ||
get: function get() { | ||
return _SQLCondition.IS_NULL; | ||
} | ||
}); | ||
Object.defineProperty(exports, "NOT_NULL", { | ||
enumerable: true, | ||
get: function get() { | ||
return _SQLCondition.NOT_NULL; | ||
} | ||
}); | ||
Object.defineProperty(exports, "EQ", { | ||
enumerable: true, | ||
get: function get() { | ||
return _SQLCondition.EQ; | ||
} | ||
}); | ||
Object.defineProperty(exports, "IN", { | ||
enumerable: true, | ||
get: function get() { | ||
return _SQLCondition.IN; | ||
} | ||
}); | ||
Object.defineProperty(exports, "GTE", { | ||
enumerable: true, | ||
get: function get() { | ||
return _SQLCondition.GTE; | ||
} | ||
}); | ||
Object.defineProperty(exports, "EXISTS", { | ||
enumerable: true, | ||
get: function get() { | ||
return _SQLCondition.EXISTS; | ||
} | ||
}); | ||
Object.defineProperty(exports, "INSTR", { | ||
enumerable: true, | ||
get: function get() { | ||
return _SQLCondition.INSTR; | ||
} | ||
}); | ||
Object.defineProperty(exports, "NOT", { | ||
enumerable: true, | ||
get: function get() { | ||
return _SQLCondition.NOT; | ||
} | ||
}); | ||
Object.defineProperty(exports, "AND", { | ||
enumerable: true, | ||
get: function get() { | ||
return _SQLCondition.AND; | ||
} | ||
}); | ||
Object.defineProperty(exports, "OR", { | ||
enumerable: true, | ||
get: function get() { | ||
return _SQLCondition.OR; | ||
} | ||
}); | ||
var _SQLCondition = require("./SQLCondition"); | ||
var _Q = require("./Q"); | ||
var _decoders = require("decoders"); | ||
var _sqlbuilder = require("./sqlbuilder"); | ||
var _toolkit = require("@simplehealth/toolkit"); | ||
var _builder = _interopRequireDefault(require("./builder")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; } | ||
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | ||
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | ||
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } | ||
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } | ||
// Throw all parse errors as FilterQLParseError's specifically, so we can | ||
// exclude them from getting reported to Sentry | ||
function FilterQLParseError(message) { | ||
var err = new Error(message); | ||
err.name = 'FilterQLParseError'; | ||
return err; | ||
} | ||
/** | ||
* This will take a definition of possible fields, and a raw text string, and | ||
* will parse the fields and flags from the raw text string into a full-blown | ||
* FilterQL instance. | ||
*/ | ||
function toSQLConditions(_x, _x2) { | ||
return _toSQLConditions.apply(this, arguments); | ||
} | ||
function _toSQLConditions() { | ||
_toSQLConditions = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(definitions, qText) { | ||
var q, conditions, _iterator, _step, _loop, searchDefintions, searchDefinition, translate, searchConditions; | ||
return regeneratorRuntime.wrap(function _callee$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
q = _builder["default"].from(qText); | ||
conditions = []; | ||
_iterator = _createForOfIteratorHelper(q.exprs()); | ||
_context2.prev = 3; | ||
_loop = /*#__PURE__*/regeneratorRuntime.mark(function _loop() { | ||
var expr_, expr, negated, field, condition, definition, rawStrValue, _definition, decoder, _translate, value; | ||
return regeneratorRuntime.wrap(function _loop$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
expr_ = _step.value; | ||
expr = void 0; | ||
negated = void 0; | ||
if (expr_.kind === 'NotExpr') { | ||
negated = true; | ||
expr = expr_.expr; | ||
} else { | ||
negated = false; | ||
expr = expr_; | ||
} | ||
field = void 0; | ||
condition = void 0; | ||
if (!(expr.kind === 'FlagExpr')) { | ||
_context.next = 18; | ||
break; | ||
} | ||
field = expr.fullField; | ||
definition = definitions.find(function (def) { | ||
return def.flag && def.flag === field; | ||
}); | ||
if (definition) { | ||
_context.next = 11; | ||
break; | ||
} | ||
throw FilterQLParseError("Invalid flag: ".concat(JSON.stringify(field), ". Valid flags are: ").concat(definitions.map(function (def) { | ||
return def.flag ? JSON.stringify(def.flag) : null; | ||
}).filter(Boolean).join(', '))); | ||
case 11: | ||
if (definition.flag) { | ||
_context.next = 13; | ||
break; | ||
} | ||
throw new Error('This should be a flag, not a field'); | ||
case 13: | ||
_context.next = 15; | ||
return definition.translate(true, q); | ||
case 15: | ||
condition = _context.sent; | ||
_context.next = 40; | ||
break; | ||
case 18: | ||
if (!(expr.kind === 'FieldExpr')) { | ||
_context.next = 39; | ||
break; | ||
} | ||
field = expr.field; | ||
rawStrValue = expr.value; | ||
_definition = definitions.find(function (def) { | ||
return def.field && def.field === field; | ||
}); | ||
if (_definition) { | ||
_context.next = 24; | ||
break; | ||
} | ||
throw FilterQLParseError("Invalid field: ".concat(JSON.stringify(field), ". Valid fields are: ").concat(definitions.map(function (def) { | ||
return def.field ? JSON.stringify(def.field) : null; | ||
}).filter(Boolean).join(', '))); | ||
case 24: | ||
if (_definition.field) { | ||
_context.next = 26; | ||
break; | ||
} | ||
throw new Error('This should be a field, not a flag'); | ||
case 26: | ||
decoder = _definition.decoder, _translate = _definition.translate; | ||
_context.prev = 27; | ||
value = (0, _decoders.guard)(decoder, { | ||
style: 'simple' | ||
})(rawStrValue); | ||
_context.next = 34; | ||
break; | ||
case 31: | ||
_context.prev = 31; | ||
_context.t0 = _context["catch"](27); | ||
throw FilterQLParseError("Invalid field value for ".concat(JSON.stringify(field), ": ").concat(_context.t0.message)); | ||
case 34: | ||
_context.next = 36; | ||
return _translate(value, q); | ||
case 36: | ||
condition = _context.sent; | ||
_context.next = 40; | ||
break; | ||
case 39: | ||
throw FilterQLParseError("Unknown expression kind: \"".concat(expr.kind, "\"")); | ||
case 40: | ||
if (negated && condition !== null) { | ||
condition = (0, _SQLCondition.NOT)(condition); | ||
} | ||
if (condition !== null) { | ||
conditions.push(condition); | ||
} | ||
case 42: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _loop, null, [[27, 31]]); | ||
}); | ||
_iterator.s(); | ||
case 6: | ||
if ((_step = _iterator.n()).done) { | ||
_context2.next = 10; | ||
break; | ||
} | ||
return _context2.delegateYield(_loop(), "t0", 8); | ||
case 8: | ||
_context2.next = 6; | ||
break; | ||
case 10: | ||
_context2.next = 15; | ||
break; | ||
case 12: | ||
_context2.prev = 12; | ||
_context2.t1 = _context2["catch"](3); | ||
_iterator.e(_context2.t1); | ||
case 15: | ||
_context2.prev = 15; | ||
_iterator.f(); | ||
return _context2.finish(15); | ||
case 18: | ||
// Text searching is a little bit different than normal field/flag handling, | ||
// so we handle this separately here | ||
searchDefintions = definitions.filter(function (def) { | ||
return def.text && Boolean(def.text); | ||
}); | ||
if (!(searchDefintions.length > 1)) { | ||
_context2.next = 21; | ||
break; | ||
} | ||
throw new Error('Cannot have more than one search definition in FilterQL config'); | ||
case 21: | ||
searchDefinition = (0, _toolkit.head)(searchDefintions); | ||
if (!searchDefinition) { | ||
_context2.next = 30; | ||
break; | ||
} | ||
if (searchDefinition.text) { | ||
_context2.next = 25; | ||
break; | ||
} | ||
throw new Error('This should be text search definition'); | ||
case 25: | ||
translate = searchDefinition.translate; // Can return null if there are no search conditions | ||
_context2.next = 28; | ||
return translate(q.textExprs(), q); | ||
case 28: | ||
searchConditions = _context2.sent; | ||
if (searchConditions) { | ||
conditions.push(searchConditions); | ||
} | ||
case 30: | ||
return _context2.abrupt("return", _SQLCondition.AND.apply(void 0, conditions)); | ||
case 31: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _callee, null, [[3, 12, 15, 18]]); | ||
})); | ||
return _toSQLConditions.apply(this, arguments); | ||
} | ||
var _default = { | ||
toSQLConditions: toSQLConditions | ||
}; | ||
exports["default"] = _default; | ||
var _SQLCondition = require("./SQLCondition"); |
{ | ||
"name": "@simplehealth/filterql", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "The FilterQL mini-language.", | ||
@@ -8,8 +8,8 @@ "license": "MIT", | ||
"type": "git", | ||
"url": "git+https://github.com/SimpleContats/filterql.git" | ||
"url": "git+https://github.com/SimpleContacts/filterql.git" | ||
}, | ||
"author": "SimpleHealth team", | ||
"homepage": "https://github.com/SimpleContats/filterql#readme", | ||
"homepage": "https://github.com/SimpleContacts/filterql#readme", | ||
"bugs": { | ||
"url": "https://github.com/SimpleContats/filterql/issues" | ||
"url": "https://github.com/SimpleContacts/filterql/issues" | ||
}, | ||
@@ -24,4 +24,4 @@ "main": "./index.js", | ||
}, | ||
"githubUrl": "https://github.com/SimpleContats/filterql", | ||
"githubUrl": "https://github.com/SimpleContacts/filterql", | ||
"sideEffects": false | ||
} |
@@ -10,4 +10,14 @@ [![npm](https://img.shields.io/npm/v/@simplehealth/filterql.svg)](https://www.npmjs.com/package/@simplehealth/filterql) | ||
# Usage | ||
```js | ||
import { Q } from '@simplehealth/filterql'; | ||
const q = new Q('status:OPEN sort:modified-asc'); | ||
q.has('status:OPEN'); // => true | ||
q.replace('status:ISSUE').toString(); // => 'status:ISSUE sort:modified-asc' | ||
``` | ||
# API | ||
TODO: Document this. |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
52095
12
847
23
1