Socket
Socket
Sign inDemoInstall

elasticsearch-dynamic-query

Package Overview
Dependencies
0
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.1.1 to 0.2.0

lib/exceptions/DataTypeError.d.ts

4

lib/index.js

@@ -9,3 +9,2 @@ "use strict";

constructor(command) {
this.command = {};
this.command = command;

@@ -19,3 +18,4 @@ }

const parser = new parser_1.Parser(this.command);
return parser.parse();
parser.parse();
return parser.getStatements();
}

@@ -22,0 +22,0 @@ /**

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ElasticTokenEnum = exports.DataTypeEnum = void 0;
/* A TypeScript enum. */
var DataTypeEnum;

@@ -12,2 +13,3 @@ (function (DataTypeEnum) {

})(DataTypeEnum = exports.DataTypeEnum || (exports.DataTypeEnum = {}));
/* A TypeScript enum. */
var ElasticTokenEnum;

@@ -14,0 +16,0 @@ (function (ElasticTokenEnum) {

@@ -5,9 +5,18 @@ import { Command } from './interface';

private command;
private statements;
constructor(command: Command);
/**
* It takes the command object and parses it into a list of statements
* @param {object} [root] - The root object that the command is being parsed under.
* @returns An array of Statement objects.
* Get the statements in this block
* @returns The array of statements.
*/
parse(root?: object): Statement[];
getStatements(): Statement[];
/**
* It parses the command object and creates a Statement object for each command.
*/
parse(): void;
/**
* It checks if the data object has the type and conditions properties.
* @param {IParserItem} data - The data to be validated.
* @returns A boolean value.
*/
private validate;

@@ -14,0 +23,0 @@ /**

@@ -8,22 +8,31 @@ "use strict";

this.command = {};
this.statements = [];
this.command = command;
}
/**
* It takes the command object and parses it into a list of statements
* @param {object} [root] - The root object that the command is being parsed under.
* @returns An array of Statement objects.
* Get the statements in this block
* @returns The array of statements.
*/
parse(root) {
const statements = [];
getStatements() {
return this.statements;
}
/**
* It parses the command object and creates a Statement object for each command.
*/
parse() {
Object.keys(this.command).forEach((key) => {
if (root && this.isReservedKey(key))
throw new Error('Reserved key is used under conditions!');
if (this.isReservedKey(key))
throw new Error('Reserved key is used as command!');
if (!this.validate(this.command[key]))
throw new Error('Command Item is not valid!');
statements.push(new statement_1.Statement(key, this.command[key]));
this.statements.push(new statement_1.Statement(key, this.command[key]));
});
return statements;
}
/**
* It checks if the data object has the type and conditions properties.
* @param {IParserItem} data - The data to be validated.
* @returns A boolean value.
*/
validate(data) {
return true;
return data.hasOwnProperty('type') && data.hasOwnProperty('conditions');
}

@@ -30,0 +39,0 @@ /**

@@ -1,2 +0,2 @@

import { ParserItem } from './type';
import { DataTypeEnum } from './enum';
export interface IRangeCondition {

@@ -8,3 +8,3 @@ $lt?: string | number;

}
export interface IParserCondition extends IRangeCondition {
export interface IConditionalOperator {
$eq?: any;

@@ -20,4 +20,11 @@ $neq?: any;

}
export interface IParserCondition extends IRangeCondition, IConditionalOperator {
$or?: IConditionalOperator & IRangeCondition;
}
export interface IParserItem {
type: DataTypeEnum;
conditions: IParserCondition;
}
export interface Command {
[T: string]: ParserItem;
[T: string]: IParserItem;
}
import { Condition } from './condition';
import { DataTypeEnum } from './enum';
import { ParserItem } from './type';
import { IParserItem } from './interface';
export declare class Statement {

@@ -8,3 +8,3 @@ private key;

private condtions;
constructor(key: string, data: ParserItem);
constructor(key: string, data: IParserItem);
/**

@@ -30,2 +30,8 @@ * Get the key of the current node

/**
* It returns an Eq object with the key and data.
* @param {any} [data] - The data to be used in the condition.
* @returns An instance of the Eq class.
*/
private getEqCondition;
/**
* If the data type is ID or Number, then add an Eq condition to the conditions array. If the data

@@ -35,2 +41,3 @@ * type is Text, then add a Like condition to the conditions array

private setEqCondition;
private getNotEqCondition;
/**

@@ -41,2 +48,3 @@ * If the data type is ID or Number, then add a new Neq condition to the conditions array. If the

private setNotEqCondition;
private getLikeCondition;
/**

@@ -47,2 +55,3 @@ * It adds a new condition to the list of conditions.

private setLikeCondition;
private getNotLikeCondition;
/**

@@ -53,2 +62,3 @@ * It adds a not like condition to the list of conditions.

private setNotLikeCondition;
private getInCondition;
/**

@@ -58,2 +68,3 @@ * It adds a new condition to the conditions array.

private setInCondition;
private getNotInCondition;
/**

@@ -63,2 +74,3 @@ * It adds a NotIn condition to the conditions array.

private setNotInCondition;
private getLtCondition;
/**

@@ -68,2 +80,3 @@ * It sets the condition.

private setLtCondition;
private getLteCondition;
/**

@@ -73,2 +86,3 @@ * It adds a new condition to the list of conditions.

private setLteCondition;
private getGtCondition;
/**

@@ -78,2 +92,3 @@ * It adds a greater than condition to the list of conditions.

private setGtCondition;
private getGteCondition;
/**

@@ -83,2 +98,3 @@ * It adds a greater than or equal to condition to the list of conditions.

private setGteCondition;
private getBetweenCondition;
/**

@@ -88,2 +104,3 @@ * It adds a Between condition to the conditions array.

private setBetweenCondition;
private getExistsCondition;
/**

@@ -93,2 +110,3 @@ * It sets the exists condition.

private setExistsCondition;
private setCondition;
/**

@@ -99,2 +117,4 @@ * If the field is not a text field, throw an error. Otherwise, add a new Regex condition to the

private setRegexCondition;
private getOrCondition;
private setOrCondition;
}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Statement = void 0;
const Between_1 = require("./condtions/Between");
const Eq_1 = require("./condtions/Eq");
const Exists_1 = require("./condtions/Exists");
const Gt_1 = require("./condtions/Gt");
const Gte_1 = require("./condtions/Gte");
const In_1 = require("./condtions/In");
const Like_1 = require("./condtions/Like");
const Lt_1 = require("./condtions/Lt");
const Lte_1 = require("./condtions/Lte");
const Neq_1 = require("./condtions/Neq");
const NotIn_1 = require("./condtions/NotIn");
const NotLike_1 = require("./condtions/NotLike");
const Regex_1 = require("./condtions/Regex");
const DataTypeError_1 = require("../exceptions/DataTypeError");
const EmptyConditionError_1 = require("../exceptions/EmptyConditionError");
const UnknownConditionError_1 = require("../exceptions/UnknownConditionError");
const Or_1 = require("./condtions/Or");
const _index_1 = require("./condtions/_index");
const enum_1 = require("./enum");

@@ -50,4 +42,8 @@ class Statement {

build() {
Object.keys(this.data.conditions).forEach((key) => {
switch (key) {
const conditionKVs = Object.keys(this.data.conditions);
if (!conditionKVs.length) {
throw new EmptyConditionError_1.EmptyConditionError(`${this.getKey()} has no valid conditions!`);
}
conditionKVs.forEach((conditionKey) => {
switch (conditionKey) {
case '$eq':

@@ -92,4 +88,7 @@ this.setEqCondition();

break;
case '$or':
this.setOrCondition();
break;
default:
break;
throw new UnknownConditionError_1.UnknownConditionError(`${conditionKey} is not valid conditional operator under ${this.getKey()}!`);
}

@@ -99,2 +98,10 @@ });

/**
* It returns an Eq object with the key and data.
* @param {any} [data] - The data to be used in the condition.
* @returns An instance of the Eq class.
*/
getEqCondition(data) {
return new _index_1.Eq(this.getKey(), data);
}
/**
* If the data type is ID or Number, then add an Eq condition to the conditions array. If the data

@@ -105,3 +112,3 @@ * type is Text, then add a Like condition to the conditions array

if (this.getType() === enum_1.DataTypeEnum.ID || this.getType() === enum_1.DataTypeEnum.NUMBER) {
this.condtions.push(new Eq_1.Eq(this.getKey(), this.data.conditions.$eq));
this.setCondition(this.getEqCondition(this.data.conditions.$eq));
}

@@ -112,2 +119,5 @@ if (this.getType() === enum_1.DataTypeEnum.TEXT) {

}
getNotEqCondition(data) {
return new _index_1.Neq(this.getKey(), data);
}
/**

@@ -119,3 +129,3 @@ * If the data type is ID or Number, then add a new Neq condition to the conditions array. If the

if (this.getType() === enum_1.DataTypeEnum.ID || this.getType() === enum_1.DataTypeEnum.NUMBER) {
this.condtions.push(new Neq_1.Neq(this.getKey(), this.data.conditions.$neq));
this.condtions.push(new _index_1.Neq(this.getKey(), this.data.conditions.$neq));
}

@@ -126,2 +136,5 @@ if (this.getType() === enum_1.DataTypeEnum.TEXT) {

}
getLikeCondition(data) {
return new _index_1.Like(this.getKey(), data);
}
/**

@@ -132,4 +145,8 @@ * It adds a new condition to the list of conditions.

setLikeCondition(data) {
this.condtions.push(new Like_1.Like(this.getKey(), data !== null && data !== void 0 ? data : this.data.conditions.$like));
const like = this.getLikeCondition(data !== null && data !== void 0 ? data : this.data.conditions.$like);
this.setCondition(like);
}
getNotLikeCondition(data) {
return new _index_1.NotLike(this.getKey(), data);
}
/**

@@ -140,4 +157,8 @@ * It adds a not like condition to the list of conditions.

setNotLikeCondition(data) {
this.condtions.push(new NotLike_1.NotLike(this.getKey(), data !== null && data !== void 0 ? data : this.data.conditions.$nlike));
const notLike = this.getNotLikeCondition(data !== null && data !== void 0 ? data : this.data.conditions.$nlike);
this.setCondition(notLike);
}
getInCondition(data) {
return new _index_1.In(this.getKey(), data);
}
/**

@@ -147,4 +168,11 @@ * It adds a new condition to the conditions array.

setInCondition() {
this.condtions.push(new In_1.In(this.getKey(), this.data.conditions.$in));
if (this.getType() !== enum_1.DataTypeEnum.ARRAY || !Array.isArray(this.data.conditions.$in)) {
throw new DataTypeError_1.DataTypeError(`field:${this.getKey()} data type should by array!`);
}
const inC = this.getInCondition(this.data.conditions.$in);
this.setCondition(inC);
}
getNotInCondition(data) {
return new _index_1.NotIn(this.getKey(), data);
}
/**

@@ -154,4 +182,8 @@ * It adds a NotIn condition to the conditions array.

setNotInCondition() {
this.condtions.push(new NotIn_1.NotIn(this.getKey(), this.data.conditions.$nin));
const notIn = this.getNotInCondition(this.data.conditions.$nin);
this.setCondition(notIn);
}
getLtCondition(data) {
return new _index_1.Lt(this.getKey(), data);
}
/**

@@ -161,4 +193,8 @@ * It sets the condition.

setLtCondition() {
this.condtions.push(new Lt_1.Lt(this.getKey(), this.data.conditions.$lt));
const lt = this.getLtCondition(this.data.conditions.$lt);
this.setCondition(lt);
}
getLteCondition(data) {
return new _index_1.Lte(this.getKey(), data);
}
/**

@@ -168,4 +204,8 @@ * It adds a new condition to the list of conditions.

setLteCondition() {
this.condtions.push(new Lte_1.Lte(this.getKey(), this.data.conditions.$lte));
const lte = this.getLteCondition(this.data.conditions.$lte);
this.setCondition(lte);
}
getGtCondition(data) {
return new _index_1.Gt(this.getKey(), data);
}
/**

@@ -175,4 +215,8 @@ * It adds a greater than condition to the list of conditions.

setGtCondition() {
this.condtions.push(new Gt_1.Gt(this.getKey(), this.data.conditions.$gt));
const gt = this.getGtCondition(this.data.conditions.$gt);
this.setCondition(gt);
}
getGteCondition(data) {
return new _index_1.Gte(this.getKey(), data);
}
/**

@@ -182,4 +226,8 @@ * It adds a greater than or equal to condition to the list of conditions.

setGteCondition() {
this.condtions.push(new Gte_1.Gte(this.getKey(), this.data.conditions.$gte));
const gte = this.getGteCondition(this.data.conditions.$gte);
this.setCondition(gte);
}
getBetweenCondition(data) {
return new _index_1.Between(this.getKey(), data);
}
/**

@@ -189,4 +237,8 @@ * It adds a Between condition to the conditions array.

setBetweenCondition() {
this.condtions.push(new Between_1.Between(this.getKey(), this.data.conditions.$between));
const between = this.getBetweenCondition(this.data.conditions.$between);
this.setCondition(between);
}
getExistsCondition(data) {
return new _index_1.Exists(this.getKey(), data);
}
/**

@@ -196,4 +248,8 @@ * It sets the exists condition.

setExistsCondition() {
this.condtions.push(new Exists_1.Exists(this.getKey(), this.data.conditions.$exists));
const exists = this.getExistsCondition(this.data.conditions.$exists);
this.setCondition(exists);
}
setCondition(data) {
this.condtions.push(data);
}
/**

@@ -207,5 +263,69 @@ * If the field is not a text field, throw an error. Otherwise, add a new Regex condition to the

}
this.condtions.push(new Regex_1.Regex(this.getKey(), this.data.conditions.$regex));
this.condtions.push(new _index_1.Regex(this.getKey(), this.data.conditions.$regex));
}
getOrCondition(data) {
return new Or_1.Or(this.getKey(), data);
}
setOrCondition() {
var _a;
const conditionKVs = Object.keys((_a = this.data.conditions.$or) !== null && _a !== void 0 ? _a : []);
if (!conditionKVs.length) {
throw new EmptyConditionError_1.EmptyConditionError(`${this.getKey()} has no valid conditions!`);
}
conditionKVs.forEach((conditionKey) => {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
let condition = null;
if (conditionKey === '$eq') {
if (this.getType() === enum_1.DataTypeEnum.ID || this.getType() === enum_1.DataTypeEnum.NUMBER) {
condition = this.getEqCondition((_a = this.data.conditions.$or) === null || _a === void 0 ? void 0 : _a.$eq);
}
if (this.getType() === enum_1.DataTypeEnum.TEXT) {
condition = this.getLikeCondition((_b = this.data.conditions.$or) === null || _b === void 0 ? void 0 : _b.$eq);
}
}
if (conditionKey === '$neq') {
if (this.getType() === enum_1.DataTypeEnum.ID || this.getType() === enum_1.DataTypeEnum.NUMBER) {
condition = this.getNotEqCondition((_c = this.data.conditions.$or) === null || _c === void 0 ? void 0 : _c.$eq);
}
if (this.getType() === enum_1.DataTypeEnum.TEXT) {
condition = this.getNotLikeCondition((_d = this.data.conditions.$or) === null || _d === void 0 ? void 0 : _d.$eq);
}
}
if (conditionKey === '$like') {
condition = this.getLikeCondition((_e = this.data.conditions.$or) === null || _e === void 0 ? void 0 : _e.$like);
}
if (conditionKey === '$nlike') {
condition = this.getNotLikeCondition((_f = this.data.conditions.$or) === null || _f === void 0 ? void 0 : _f.$nlike);
}
if (conditionKey === '$in') {
if (this.getType() !== enum_1.DataTypeEnum.ARRAY || !Array.isArray((_g = this.data.conditions.$or) === null || _g === void 0 ? void 0 : _g.$in)) {
throw new DataTypeError_1.DataTypeError(`field:${this.getKey()} data type should by array!`);
}
condition = this.getInCondition((_h = this.data.conditions.$or) === null || _h === void 0 ? void 0 : _h.$in);
}
if (conditionKey === '$nin') {
if (this.getType() !== enum_1.DataTypeEnum.ARRAY || !Array.isArray((_j = this.data.conditions.$or) === null || _j === void 0 ? void 0 : _j.$in)) {
throw new DataTypeError_1.DataTypeError(`field:${this.getKey()} data type should by array!`);
}
condition = this.getNotInCondition((_k = this.data.conditions.$or) === null || _k === void 0 ? void 0 : _k.$nin);
}
if (conditionKey === '$lt') {
condition = this.getLtCondition((_l = this.data.conditions.$or) === null || _l === void 0 ? void 0 : _l.$lt);
}
if (conditionKey === '$lte') {
condition = this.getLteCondition((_m = this.data.conditions.$or) === null || _m === void 0 ? void 0 : _m.$lte);
}
if (conditionKey === '$gt') {
condition = this.getGtCondition((_o = this.data.conditions.$or) === null || _o === void 0 ? void 0 : _o.$gt);
}
if (conditionKey === '$gte') {
condition = this.getGteCondition((_p = this.data.conditions.$or) === null || _p === void 0 ? void 0 : _p.$gte);
}
if (conditionKey === '$between') {
condition = this.getBetweenCondition((_q = this.data.conditions.$or) === null || _q === void 0 ? void 0 : _q.$between);
}
this.setCondition(this.getOrCondition(condition));
});
}
}
exports.Statement = Statement;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ElasticOccurEnum = exports.CompoundQueryType = void 0;
/* A way to define a type. */
var CompoundQueryType;

@@ -8,2 +9,3 @@ (function (CompoundQueryType) {

})(CompoundQueryType = exports.CompoundQueryType || (exports.CompoundQueryType = {}));
/* A way to define a type. */
var ElasticOccurEnum;

@@ -10,0 +12,0 @@ (function (ElasticOccurEnum) {

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CompoundQuery = void 0;
const Between_1 = require("../../parser/condtions/Between");
const Eq_1 = require("../../parser/condtions/Eq");
const Exists_1 = require("../../parser/condtions/Exists");
const Gt_1 = require("../../parser/condtions/Gt");
const Gte_1 = require("../../parser/condtions/Gte");
const In_1 = require("../../parser/condtions/In");
const Like_1 = require("../../parser/condtions/Like");
const Lt_1 = require("../../parser/condtions/Lt");
const Lte_1 = require("../../parser/condtions/Lte");
const Neq_1 = require("../../parser/condtions/Neq");
const NotIn_1 = require("../../parser/condtions/NotIn");
const NotLike_1 = require("../../parser/condtions/NotLike");
const Regex_1 = require("../../parser/condtions/Regex");
const Or_1 = require("../../parser/condtions/Or");
const _index_1 = require("../../parser/condtions/_index");
const bool_query_1 = require("./bool.query");

@@ -34,25 +23,25 @@ const enum_1 = require("./enum");

conditions.forEach((condition) => {
if (condition instanceof Eq_1.Eq)
if (condition instanceof _index_1.Eq)
boolQuery.setMustQuery(condition.getCondition());
if (condition instanceof Like_1.Like)
if (condition instanceof _index_1.Like)
boolQuery.setMustQuery(condition.getCondition());
if (condition instanceof Neq_1.Neq)
if (condition instanceof _index_1.Neq)
boolQuery.setMustNotQuery(condition.getCondition());
if (condition instanceof NotLike_1.NotLike)
if (condition instanceof _index_1.NotLike)
boolQuery.setMustNotQuery(condition.getCondition());
if (condition instanceof In_1.In)
if (condition instanceof _index_1.In)
boolQuery.setMustQuery(condition.getCondition());
if (condition instanceof NotIn_1.NotIn)
if (condition instanceof _index_1.NotIn)
boolQuery.setMustNotQuery(condition.getCondition());
if (condition instanceof Lt_1.Lt)
if (condition instanceof _index_1.Lt)
boolQuery.setFilterQuery(condition.getCondition());
if (condition instanceof Lte_1.Lte)
if (condition instanceof _index_1.Lte)
boolQuery.setFilterQuery(condition.getCondition());
if (condition instanceof Gt_1.Gt)
if (condition instanceof _index_1.Gt)
boolQuery.setFilterQuery(condition.getCondition());
if (condition instanceof Gte_1.Gte)
if (condition instanceof _index_1.Gte)
boolQuery.setFilterQuery(condition.getCondition());
if (condition instanceof Between_1.Between)
if (condition instanceof _index_1.Between)
boolQuery.setFilterQuery(condition.getCondition());
if (condition instanceof Exists_1.Exists) {
if (condition instanceof _index_1.Exists) {
condition.getValue()

@@ -63,4 +52,6 @@ ? boolQuery.setMustQuery(condition.getCondition())

// TODO: this is not working
if (condition instanceof Regex_1.Regex)
if (condition instanceof _index_1.Regex)
boolQuery.setFilterQuery(condition.getCondition());
if (condition instanceof Or_1.Or)
boolQuery.setShouldQuery(condition.getCondition());
});

@@ -67,0 +58,0 @@ });

{
"name": "elasticsearch-dynamic-query",
"version": "0.1.1",
"version": "0.2.0",
"description": "A simple query builder, it will helps to develop DSL query for elasticsearch",

@@ -20,6 +20,6 @@ "main": "lib/index.js",

},
"author": "Hashemi Rafsan",
"license": "ISC",
"devDependencies": {
"@faker-js/faker": "^6.0.0-beta.0",
"@types/jest": "^27.4.0",

@@ -39,3 +39,9 @@ "jest": "^27.4.7",

"url": "git+https://github.com/hashemirafsan/elasticsearch-dynamic-query.git"
}
},
"keywords": [
"elasticsearch",
"dynamic-query",
"query-builder",
"node-js"
]
}

@@ -24,3 +24,3 @@ # Elasticsearch Dynamic Query Builder

fieldName: {
type: T // ID, TEXT, NUMBER, ARRAY, DATETIME,
type: DataTypeEnum, // ID, TEXT, NUMBER, ARRAY, DATETIME,
conditions: {

@@ -39,3 +39,3 @@ $eq?: any;

$regex?: string;
$between: {
$between?: {
$lt?: string | number;

@@ -46,2 +46,5 @@ $lte?: string | number;

}
$or?: {
// Without $or, all conditional operator available under $or
}
}

@@ -82,2 +85,3 @@ }

| $between | Is between |
| $or | Or expression |

@@ -84,0 +88,0 @@ Initialize Elasticsearch Dynamic Query Builder:

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc