couchset 0.0.0 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +231 -0
- package/dist/automate/index.d.ts +4 -0
- package/dist/automate/index.js +17 -0
- package/dist/automate/index.js.map +1 -0
- package/dist/automate/middlewares/index.d.ts +1 -0
- package/dist/automate/middlewares/index.js +14 -0
- package/dist/automate/middlewares/index.js.map +1 -0
- package/dist/automate/middlewares/isAuth.d.ts +7 -0
- package/dist/automate/middlewares/isAuth.js +28 -0
- package/dist/automate/middlewares/isAuth.js.map +1 -0
- package/dist/automate/model/client.d.ts +12 -0
- package/dist/automate/model/client.js +33 -0
- package/dist/automate/model/client.js.map +1 -0
- package/dist/automate/model/index.d.ts +44 -0
- package/dist/automate/model/index.js +304 -0
- package/dist/automate/model/index.js.map +1 -0
- package/dist/automate/morpheus/index.d.ts +14 -0
- package/dist/automate/morpheus/index.js +171 -0
- package/dist/automate/morpheus/index.js.map +1 -0
- package/dist/automate/writer/index.d.ts +10 -0
- package/dist/automate/writer/index.js +72 -0
- package/dist/automate/writer/index.js.map +1 -0
- package/dist/config/index.d.ts +3 -0
- package/dist/config/index.js +8 -0
- package/dist/config/index.js.map +1 -0
- package/dist/connection.d.ts +36 -0
- package/dist/connection.js +129 -0
- package/dist/connection.js.map +1 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +80 -0
- package/dist/index.js.map +1 -0
- package/dist/model/index.d.ts +107 -0
- package/dist/model/index.js +329 -0
- package/dist/model/index.js.map +1 -0
- package/dist/pagination/index.d.ts +1 -0
- package/dist/pagination/index.js +14 -0
- package/dist/pagination/index.js.map +1 -0
- package/dist/pagination/pagination.d.ts +19 -0
- package/dist/pagination/pagination.js +97 -0
- package/dist/pagination/pagination.js.map +1 -0
- package/dist/query/base-query.d.ts +17 -0
- package/dist/query/base-query.js +15 -0
- package/dist/query/base-query.js.map +1 -0
- package/dist/query/exceptions.d.ts +24 -0
- package/dist/query/exceptions.js +90 -0
- package/dist/query/exceptions.js.map +1 -0
- package/dist/query/helpers/builders.d.ts +64 -0
- package/dist/query/helpers/builders.js +415 -0
- package/dist/query/helpers/builders.js.map +1 -0
- package/dist/query/helpers/dictionary.d.ts +45 -0
- package/dist/query/helpers/dictionary.js +105 -0
- package/dist/query/helpers/dictionary.js.map +1 -0
- package/dist/query/helpers/index.d.ts +3 -0
- package/dist/query/helpers/index.js +16 -0
- package/dist/query/helpers/index.js.map +1 -0
- package/dist/query/helpers/reservedWords.d.ts +1 -0
- package/dist/query/helpers/reservedWords.js +200 -0
- package/dist/query/helpers/reservedWords.js.map +1 -0
- package/dist/query/index.d.ts +5 -0
- package/dist/query/index.js +27 -0
- package/dist/query/index.js.map +1 -0
- package/dist/query/interface/query.types.d.ts +236 -0
- package/dist/query/interface/query.types.js +3 -0
- package/dist/query/interface/query.types.js.map +1 -0
- package/dist/query/query.cluster.d.ts +14 -0
- package/dist/query/query.cluster.js +80 -0
- package/dist/query/query.cluster.js.map +1 -0
- package/dist/query/query.d.ts +321 -0
- package/dist/query/query.js +457 -0
- package/dist/query/query.js.map +1 -0
- package/dist/query/utils.d.ts +8 -0
- package/dist/query/utils.js +56 -0
- package/dist/query/utils.js.map +1 -0
- package/dist/search/customQuery.d.ts +17 -0
- package/dist/search/customQuery.js +83 -0
- package/dist/search/customQuery.js.map +1 -0
- package/dist/search/index.d.ts +1 -0
- package/dist/search/index.js +14 -0
- package/dist/search/index.js.map +1 -0
- package/dist/shared/ContextType.d.ts +27 -0
- package/dist/shared/ContextType.js +55 -0
- package/dist/shared/ContextType.js.map +1 -0
- package/dist/shared/common.model.d.ts +37 -0
- package/dist/shared/common.model.js +148 -0
- package/dist/shared/common.model.js.map +1 -0
- package/dist/shared/index.d.ts +2 -0
- package/dist/shared/index.js +15 -0
- package/dist/shared/index.js.map +1 -0
- package/dist/utils/awaitTo.d.ts +17 -0
- package/dist/utils/awaitTo.js +31 -0
- package/dist/utils/awaitTo.js.map +1 -0
- package/dist/utils/date.d.ts +11 -0
- package/dist/utils/date.js +21 -0
- package/dist/utils/date.js.map +1 -0
- package/dist/utils/index.d.ts +5 -0
- package/dist/utils/index.js +18 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/log.d.ts +9 -0
- package/dist/utils/log.js +19 -0
- package/dist/utils/log.js.map +1 -0
- package/dist/utils/text.utils.d.ts +14 -0
- package/dist/utils/text.utils.js +45 -0
- package/dist/utils/text.utils.js.map +1 -0
- package/dist/utils/utils.schema.d.ts +6 -0
- package/dist/utils/utils.schema.js +33 -0
- package/dist/utils/utils.schema.js.map +1 -0
- package/dist/uuid.d.ts +1 -0
- package/dist/uuid.js +9 -0
- package/dist/uuid.js.map +1 -0
- package/docs/couch.png +0 -0
- package/docs/couchset.png +0 -0
- package/docs/couchset_logo.png +0 -0
- package/package.json +108 -30
- package/register/index.js +1 -0
- package/register/transpile-only.js +3 -0
- package/register/type-check.js +3 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"exceptions.js","sourceRoot":"","sources":["../../src/query/exceptions.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA;IAA2C,yCAAK;IAC5C;QACI,sBAAsB;eACtB,kBAAM,sDAAsD,CAAC;IACjE,CAAC;IACL,4BAAC;AAAD,CAAC,AALD,CAA2C,KAAK,GAK/C;AALY,sDAAqB;AAOlC;IAA0C,wCAAK;IAC3C;eACI,kBAAM,qDAAqD,CAAC;IAChE,CAAC;IACL,2BAAC;AAAD,CAAC,AAJD,CAA0C,KAAK,GAI9C;AAJY,oDAAoB;AAMjC;IAAgD,8CAAoB;IAChE;QAAA,YACI,iBAAO,SAEV;QADG,KAAI,CAAC,OAAO,GAAG,uGAAuG,CAAC;;IAC3H,CAAC;IACL,iCAAC;AAAD,CAAC,AALD,CAAgD,oBAAoB,GAKnE;AALY,gEAA0B;AAMvC;IAAiD,+CAAK;IAClD,qCAAY,KAAa,EAAE,KAAa;eACpC,kBAAM,8CAA4C,KAAK,cAAS,KAAO,CAAC;IAC5E,CAAC;IACL,kCAAC;AAAD,CAAC,AAJD,CAAiD,KAAK,GAIrD;AAJY,kEAA2B;AAMxC;IAAoD,kDAAoB;IACpE,wCAAY,QAAgB;QAA5B,YACI,iBAAO,SAEV;QADG,KAAI,CAAC,OAAO,GAAG,yBAAuB,QAAU,CAAC;;IACrD,CAAC;IACL,qCAAC;AAAD,CAAC,AALD,CAAoD,oBAAoB,GAKvE;AALY,wEAA8B;AAO3C;IAAiD,+CAAoB;IACjE;QAAA,YACI,iBAAO,SAEV;QADG,KAAI,CAAC,OAAO,GAAG,2EAA2E,CAAC;;IAC/F,CAAC;IACL,kCAAC;AAAD,CAAC,AALD,CAAiD,oBAAoB,GAKpE;AALY,kEAA2B;AAOxC;IAA6C,2CAAK;IAC9C,iCAAY,MAAgB;eACxB,kBACI,oEAAkE,IAAI,CAAC,SAAS,CAC5E,MAAM,CACP,CACN;IACL,CAAC;IACL,8BAAC;AAAD,CAAC,AARD,CAA6C,KAAK,GAQjD;AARY,0DAAuB;AAUpC;IAAmD,iDAAK;IACpD,uCAAY,MAAgB;eACxB,kBACI,2EAAyE,IAAI,CAAC,SAAS,CACnF,MAAM,CACP,CACN;IACL,CAAC;IACL,oCAAC;AAAD,CAAC,AARD,CAAmD,KAAK,GAQvD;AARY,sEAA6B","sourcesContent":["export class SelectClauseException extends Error {\n constructor() {\n // todo update message\n super('The SELECT clause does not have the proper structure');\n }\n}\n\nexport class WhereClauseException extends Error {\n constructor() {\n super('The WHERE clause does not have the proper structure');\n }\n}\n\nexport class InWithinOperatorExceptions extends WhereClauseException {\n constructor() {\n super();\n this.message = `The search_expr, target_expr and operator (IN | WITHIN) values are required for collection operators.`;\n }\n}\nexport class MultipleQueryTypesException extends Error {\n constructor(type1: string, type2: string) {\n super(`Cannot combine multiple query types (ex: ${type1} with ${type2}`);\n }\n}\n\nexport class QueryOperatorNotFoundException extends WhereClauseException {\n constructor(operator: string) {\n super();\n this.message = `Operator not found: ${operator}`;\n }\n}\n\nexport class QueryGroupByParamsException extends WhereClauseException {\n constructor() {\n super();\n this.message = `The GROUP BY clause must be defined to use the LETTING and HAVING clauses`;\n }\n}\n\nexport class IndexParamsOnExceptions extends Error {\n constructor(clause: string[]) {\n super(\n `The ON parameter can only be applied in the following clauses: ${JSON.stringify(\n clause\n )}`\n );\n }\n}\n\nexport class IndexParamsUsingGSIExceptions extends Error {\n constructor(clause: string[]) {\n super(\n `The USING GSI parameter can only be applied in the following clauses: ${JSON.stringify(\n clause\n )}`\n );\n }\n}\n"]}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { IGroupBy, IIndexOnParams, IIndexWithParams, ILetExpr, IndexType, ISelectType, LogicalWhereExpr, SortType } from '../interface/query.types';
|
|
2
|
+
/**
|
|
3
|
+
* Build a SELECT N1QL query from user-specified parameters.
|
|
4
|
+
* {@link https://docs.couchbase.com/server/6.5/n1ql/n1ql-language-reference/select-syntax.html}
|
|
5
|
+
* @param collection Collection name
|
|
6
|
+
* @param select SELECT Clause param
|
|
7
|
+
* @param letExpr LET Clause param
|
|
8
|
+
* @param where WHERE Clause param
|
|
9
|
+
* @param orderBy ORDER BY Clause param
|
|
10
|
+
* @param limit LIMIT Clause param
|
|
11
|
+
* @param offset OFFSET Clause param
|
|
12
|
+
* @param useExpr USE Clause param
|
|
13
|
+
*
|
|
14
|
+
* @return N1QL SELECT Query
|
|
15
|
+
* */
|
|
16
|
+
export declare const selectBuilder: (collection: string, select: ISelectType[] | string, letExpr?: ILetExpr[], where?: LogicalWhereExpr, orderBy?: Record<string, SortType>, limit?: number, offset?: number, useExpr?: string[], groupByExpr?: IGroupBy[], lettingExpr?: ILetExpr[], havingExpr?: LogicalWhereExpr, plainJoinExpr?: string) => string;
|
|
17
|
+
/**
|
|
18
|
+
* Create N1QL queries from select array params
|
|
19
|
+
* @param clause SELECT Clause param
|
|
20
|
+
*
|
|
21
|
+
* @return N1QL SELECT Query
|
|
22
|
+
* */
|
|
23
|
+
export declare const buildSelectArrayExpr: (clause: ISelectType[]) => string;
|
|
24
|
+
/**
|
|
25
|
+
* Recursive function to create N1QL queries.
|
|
26
|
+
* @param n1ql N1QL Query String
|
|
27
|
+
* @param clause SELECT Clause param
|
|
28
|
+
*
|
|
29
|
+
* @return N1QL SELECT Query
|
|
30
|
+
* */
|
|
31
|
+
export declare const buildSelectExpr: (n1ql: string, clause: ISelectType) => string;
|
|
32
|
+
/**
|
|
33
|
+
* Create WHERE N1QL Expressions.
|
|
34
|
+
* {@link https://docs.couchbase.com/server/6.5/n1ql/n1ql-language-reference/where.html}
|
|
35
|
+
* @param clause WHERE Clause param
|
|
36
|
+
* @return N1QL WHERE Expression
|
|
37
|
+
* */
|
|
38
|
+
export declare const buildWhereExpr: (expr: LogicalWhereExpr | undefined, clause?: string) => string;
|
|
39
|
+
/**
|
|
40
|
+
* @ignore
|
|
41
|
+
*
|
|
42
|
+
**/
|
|
43
|
+
export declare const verifyWhereObjectKey: (clause: LogicalWhereExpr) => boolean;
|
|
44
|
+
/**
|
|
45
|
+
* Recursive function to create WHERE N1QL Expressions.
|
|
46
|
+
* @param n1ql N1QL Query String
|
|
47
|
+
* @param clause WHERE Clause param
|
|
48
|
+
*
|
|
49
|
+
* @return N1QL WHERE Expression
|
|
50
|
+
* */
|
|
51
|
+
export declare const buildWhereClauseExpr: (n1ql: string, clause: LogicalWhereExpr) => string;
|
|
52
|
+
/**
|
|
53
|
+
* Build a INDEX N1QL query from user-specified parameters.
|
|
54
|
+
* {@link https://docs.couchbase.com/server/6.5/n1ql/n1ql-language-reference/createindex.html}
|
|
55
|
+
* @param collection Collection name
|
|
56
|
+
* @param type INDEX clause types can be 'CREATE' | 'BUILD' | 'DROP' | 'CREATE PRIMARY'
|
|
57
|
+
* @param on ON Clause param
|
|
58
|
+
* @param where WHERE Clause param
|
|
59
|
+
* @param usingGSI use a Global Secondary Index(GSI).
|
|
60
|
+
* @param withExpr WITH Clause param
|
|
61
|
+
*
|
|
62
|
+
* @return N1QL INDEX Query
|
|
63
|
+
* */
|
|
64
|
+
export declare const buildIndexExpr: (collection: string, type: IndexType, name: string, on?: IIndexOnParams[], where?: LogicalWhereExpr, usingGSI?: boolean, withExpr?: IIndexWithParams) => string;
|
|
@@ -0,0 +1,415 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.buildIndexExpr = exports.buildWhereClauseExpr = exports.verifyWhereObjectKey = exports.buildWhereExpr = exports.buildSelectExpr = exports.buildSelectArrayExpr = exports.selectBuilder = void 0;
|
|
4
|
+
var exceptions_1 = require("../exceptions");
|
|
5
|
+
var utils_1 = require("../utils");
|
|
6
|
+
var dictionary_1 = require("./dictionary");
|
|
7
|
+
// select expressions functions
|
|
8
|
+
/**
|
|
9
|
+
* Build a SELECT N1QL query from user-specified parameters.
|
|
10
|
+
* {@link https://docs.couchbase.com/server/6.5/n1ql/n1ql-language-reference/select-syntax.html}
|
|
11
|
+
* @param collection Collection name
|
|
12
|
+
* @param select SELECT Clause param
|
|
13
|
+
* @param letExpr LET Clause param
|
|
14
|
+
* @param where WHERE Clause param
|
|
15
|
+
* @param orderBy ORDER BY Clause param
|
|
16
|
+
* @param limit LIMIT Clause param
|
|
17
|
+
* @param offset OFFSET Clause param
|
|
18
|
+
* @param useExpr USE Clause param
|
|
19
|
+
*
|
|
20
|
+
* @return N1QL SELECT Query
|
|
21
|
+
* */
|
|
22
|
+
var selectBuilder = function (collection, select, letExpr, where, orderBy, limit, offset, useExpr, groupByExpr, lettingExpr, havingExpr, plainJoinExpr) {
|
|
23
|
+
try {
|
|
24
|
+
var expr = '';
|
|
25
|
+
if (typeof select === 'string') {
|
|
26
|
+
expr = select;
|
|
27
|
+
}
|
|
28
|
+
if (Array.isArray(select)) {
|
|
29
|
+
expr = (0, exports.buildSelectArrayExpr)(select);
|
|
30
|
+
}
|
|
31
|
+
var _collection = collection.indexOf(' ') !== -1
|
|
32
|
+
? ("`" + collection).replace(' ', '` ')
|
|
33
|
+
: collection.indexOf('`') !== -1
|
|
34
|
+
? collection
|
|
35
|
+
: "`" + collection + "`";
|
|
36
|
+
return "SELECT " + expr + " FROM " + _collection + (plainJoinExpr ? " " + plainJoinExpr + " " : '') + _buildUseKeysExpr(useExpr) + _buildLetExpr(letExpr) + (0, exports.buildWhereExpr)(where) + _buildGroupByExpr(groupByExpr, lettingExpr, havingExpr) + _buildOrderByExpr(orderBy) + _buildLimitExpr(limit) + _buildOffsetExpr(offset);
|
|
37
|
+
}
|
|
38
|
+
catch (exception) {
|
|
39
|
+
if (exception instanceof exceptions_1.WhereClauseException) {
|
|
40
|
+
throw exception;
|
|
41
|
+
}
|
|
42
|
+
throw new exceptions_1.SelectClauseException();
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
exports.selectBuilder = selectBuilder;
|
|
46
|
+
/**
|
|
47
|
+
* @ignore
|
|
48
|
+
* */
|
|
49
|
+
var _buildAS = function (c) {
|
|
50
|
+
return c.hasOwnProperty('as') ? " AS " + c['as'] : '';
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* @ignore
|
|
54
|
+
* */
|
|
55
|
+
var _buildField = function (clause) {
|
|
56
|
+
if (clause.hasOwnProperty('name')) {
|
|
57
|
+
return "" + (0, utils_1.escapeReservedWords)(clause['name']) + _buildAS(clause);
|
|
58
|
+
}
|
|
59
|
+
return "" + (0, utils_1.escapeReservedWords)(clause);
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* Create N1QL queries from select array params
|
|
63
|
+
* @param clause SELECT Clause param
|
|
64
|
+
*
|
|
65
|
+
* @return N1QL SELECT Query
|
|
66
|
+
* */
|
|
67
|
+
var buildSelectArrayExpr = function (clause) {
|
|
68
|
+
return "" + clause.map(function (c) { return (0, exports.buildSelectExpr)('', c); }).join(',');
|
|
69
|
+
};
|
|
70
|
+
exports.buildSelectArrayExpr = buildSelectArrayExpr;
|
|
71
|
+
/**
|
|
72
|
+
* Recursive function to create N1QL queries.
|
|
73
|
+
* @param n1ql N1QL Query String
|
|
74
|
+
* @param clause SELECT Clause param
|
|
75
|
+
*
|
|
76
|
+
* @return N1QL SELECT Query
|
|
77
|
+
* */
|
|
78
|
+
var buildSelectExpr = function (n1ql, clause) {
|
|
79
|
+
try {
|
|
80
|
+
if (clause.hasOwnProperty('$field')) {
|
|
81
|
+
return _buildField(clause['$field']);
|
|
82
|
+
}
|
|
83
|
+
var key = Object.keys(clause)[0];
|
|
84
|
+
if (dictionary_1.ReturnResultDict.hasOwnProperty(key)) {
|
|
85
|
+
return dictionary_1.ReturnResultDict[key] + " " + (0, exports.buildSelectExpr)(n1ql, clause[key]);
|
|
86
|
+
}
|
|
87
|
+
if (dictionary_1.ResultExprDict.hasOwnProperty(key)) {
|
|
88
|
+
return dictionary_1.ResultExprDict[key] + " " + (0, exports.buildSelectExpr)(n1ql, clause[key]);
|
|
89
|
+
}
|
|
90
|
+
if (dictionary_1.AggDict.hasOwnProperty(key)) {
|
|
91
|
+
// todo check if have AS expr inside of Agg function.
|
|
92
|
+
return dictionary_1.AggDict[key] + "(" + _buildAggDictExpr(clause, key) + (0, exports.buildSelectExpr)(n1ql, clause[key]) + ")" + _buildAS(clause[key]);
|
|
93
|
+
}
|
|
94
|
+
throw new exceptions_1.SelectClauseException();
|
|
95
|
+
}
|
|
96
|
+
catch (_a) {
|
|
97
|
+
throw new exceptions_1.SelectClauseException();
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
exports.buildSelectExpr = buildSelectExpr;
|
|
101
|
+
/**
|
|
102
|
+
* @ignore
|
|
103
|
+
* */
|
|
104
|
+
var _buildAggDictExpr = function (clause, key) {
|
|
105
|
+
if (dictionary_1.AggDict.hasOwnProperty(key)) {
|
|
106
|
+
if (clause[key].hasOwnProperty('ro')) {
|
|
107
|
+
return dictionary_1.ReturnResultDict[clause[key]['ro']] + " ";
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return '';
|
|
111
|
+
};
|
|
112
|
+
/**
|
|
113
|
+
* @ignore
|
|
114
|
+
* */
|
|
115
|
+
var _buildLetExpr = function (letExpr, clause) {
|
|
116
|
+
return Array.isArray(letExpr)
|
|
117
|
+
? " " + (clause ? clause : 'LET') + " " + letExpr
|
|
118
|
+
.map(function (value) { return (0, utils_1.escapeReservedWords)(value.key) + "=" + value.value; })
|
|
119
|
+
.join(',')
|
|
120
|
+
: '';
|
|
121
|
+
};
|
|
122
|
+
/**
|
|
123
|
+
* @ignore
|
|
124
|
+
* */
|
|
125
|
+
var _buildOrderByExpr = function (orderExpr) {
|
|
126
|
+
return !!orderExpr
|
|
127
|
+
? " ORDER BY " + Object.keys(orderExpr)
|
|
128
|
+
.map(function (value) { return (0, utils_1.escapeReservedWords)(value) + " " + orderExpr[value]; })
|
|
129
|
+
.join(',')
|
|
130
|
+
: '';
|
|
131
|
+
};
|
|
132
|
+
/**
|
|
133
|
+
* @ignore
|
|
134
|
+
* */
|
|
135
|
+
var _buildLimitExpr = function (limit) {
|
|
136
|
+
return Number.isInteger(limit) ? " LIMIT " + limit : '';
|
|
137
|
+
};
|
|
138
|
+
/**
|
|
139
|
+
* @ignore
|
|
140
|
+
* */
|
|
141
|
+
var _buildOffsetExpr = function (offset) {
|
|
142
|
+
return Number.isInteger(offset) ? " OFFSET " + offset : '';
|
|
143
|
+
};
|
|
144
|
+
/**
|
|
145
|
+
* @ignore
|
|
146
|
+
* */
|
|
147
|
+
var _buildUseKeysExpr = function (useKeys) {
|
|
148
|
+
return Array.isArray(useKeys) ? " USE KEYS " + stringifyValues(useKeys) : '';
|
|
149
|
+
};
|
|
150
|
+
// end select expression functions
|
|
151
|
+
//group by expression functions
|
|
152
|
+
/**
|
|
153
|
+
*@ignore
|
|
154
|
+
*/
|
|
155
|
+
var _buildGroupByExpr = function (groupByExpr, lettingExpr, havingExpr) {
|
|
156
|
+
try {
|
|
157
|
+
if ((lettingExpr || havingExpr) && !groupByExpr) {
|
|
158
|
+
throw new exceptions_1.QueryGroupByParamsException();
|
|
159
|
+
}
|
|
160
|
+
if (!groupByExpr) {
|
|
161
|
+
return '';
|
|
162
|
+
}
|
|
163
|
+
return " " + _buildGroupBy(groupByExpr) + _buildLetExpr(lettingExpr, 'LETTING') + (0, exports.buildWhereExpr)(havingExpr, 'HAVING');
|
|
164
|
+
}
|
|
165
|
+
catch (_a) {
|
|
166
|
+
throw new exceptions_1.QueryGroupByParamsException();
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
/**
|
|
170
|
+
*@ignore
|
|
171
|
+
*/
|
|
172
|
+
var _buildGroupBy = function (groupByExpr) {
|
|
173
|
+
return "GROUP BY " + groupByExpr
|
|
174
|
+
.map(function (value) {
|
|
175
|
+
return "" + (0, utils_1.escapeReservedWords)(value.expr) + (value.as ? " AS " + value.as : '');
|
|
176
|
+
})
|
|
177
|
+
.join(',');
|
|
178
|
+
};
|
|
179
|
+
//end group by expression functions
|
|
180
|
+
// where expression functions
|
|
181
|
+
/**
|
|
182
|
+
* Create WHERE N1QL Expressions.
|
|
183
|
+
* {@link https://docs.couchbase.com/server/6.5/n1ql/n1ql-language-reference/where.html}
|
|
184
|
+
* @param clause WHERE Clause param
|
|
185
|
+
* @return N1QL WHERE Expression
|
|
186
|
+
* */
|
|
187
|
+
var buildWhereExpr = function (expr, clause) {
|
|
188
|
+
return expr ? " " + (clause ? clause : 'WHERE') + " " + (0, exports.buildWhereClauseExpr)('', expr) : '';
|
|
189
|
+
};
|
|
190
|
+
exports.buildWhereExpr = buildWhereExpr;
|
|
191
|
+
/**
|
|
192
|
+
* @ignore
|
|
193
|
+
*
|
|
194
|
+
**/
|
|
195
|
+
var verifyWhereObjectKey = function (clause) {
|
|
196
|
+
var exist = false;
|
|
197
|
+
for (var key in clause) {
|
|
198
|
+
if (['$and', '$or', '$not', '$any', '$$every', '$in', '$within'].includes(key)) {
|
|
199
|
+
exist = true;
|
|
200
|
+
break;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
return exist;
|
|
204
|
+
};
|
|
205
|
+
exports.verifyWhereObjectKey = verifyWhereObjectKey;
|
|
206
|
+
/**
|
|
207
|
+
* Recursive function to create WHERE N1QL Expressions.
|
|
208
|
+
* @param n1ql N1QL Query String
|
|
209
|
+
* @param clause WHERE Clause param
|
|
210
|
+
*
|
|
211
|
+
* @return N1QL WHERE Expression
|
|
212
|
+
* */
|
|
213
|
+
var buildWhereClauseExpr = function (n1ql, clause) {
|
|
214
|
+
try {
|
|
215
|
+
if (!(0, exports.verifyWhereObjectKey)(clause)) {
|
|
216
|
+
return _buildFieldClauseExpr(clause);
|
|
217
|
+
}
|
|
218
|
+
return Object.keys(clause)
|
|
219
|
+
.map(function (key) {
|
|
220
|
+
var _a;
|
|
221
|
+
if (dictionary_1.CollectionSelectOperatorDict[key]) {
|
|
222
|
+
return "" + _buildWhereCollectionExpr(key, clause[key]);
|
|
223
|
+
}
|
|
224
|
+
if (dictionary_1.CollectionInWithinOperatorDict[key]) {
|
|
225
|
+
return "" + _buildCollectionInWithinOperator(key, clause[key]);
|
|
226
|
+
}
|
|
227
|
+
if (Array.isArray(clause[key])) {
|
|
228
|
+
var prefix = key === '$not' ? dictionary_1.LogicalOperatorDict[key] + " " : '';
|
|
229
|
+
var joinOp = key === '$not' ? " AND " : " " + dictionary_1.LogicalOperatorDict[key] + " ";
|
|
230
|
+
return prefix + "(" + clause[key]
|
|
231
|
+
.map(function (value) { return (0, exports.buildWhereClauseExpr)(n1ql, value); })
|
|
232
|
+
.join(joinOp) + ")";
|
|
233
|
+
}
|
|
234
|
+
else {
|
|
235
|
+
return "" + (0, exports.buildWhereClauseExpr)(n1ql, (_a = {}, _a[key] = clause[key], _a));
|
|
236
|
+
}
|
|
237
|
+
})
|
|
238
|
+
.join(' AND ');
|
|
239
|
+
}
|
|
240
|
+
catch (exception) {
|
|
241
|
+
if (exception instanceof exceptions_1.WhereClauseException) {
|
|
242
|
+
throw exception;
|
|
243
|
+
}
|
|
244
|
+
throw new exceptions_1.WhereClauseException();
|
|
245
|
+
}
|
|
246
|
+
};
|
|
247
|
+
exports.buildWhereClauseExpr = buildWhereClauseExpr;
|
|
248
|
+
/**
|
|
249
|
+
* @ignore
|
|
250
|
+
* */
|
|
251
|
+
var _buildFieldClauseExpr = function (field) {
|
|
252
|
+
try {
|
|
253
|
+
var expr = Object.keys(field).map(function (value) {
|
|
254
|
+
if (typeof field[value] === 'object' && !Array.isArray(field[value])) {
|
|
255
|
+
return "" + _buildComparisionClauseExpr(value, field[value]);
|
|
256
|
+
}
|
|
257
|
+
if (!value.includes('$')) {
|
|
258
|
+
if (typeof field[value] === 'string') {
|
|
259
|
+
return (0, utils_1.escapeReservedWords)(value) + "=" + stringifyValues(field[value]);
|
|
260
|
+
}
|
|
261
|
+
if (typeof field[value] === 'number' ||
|
|
262
|
+
typeof field[value] === 'boolean' ||
|
|
263
|
+
Array.isArray(field[value])) {
|
|
264
|
+
return (0, utils_1.escapeReservedWords)(value) + "=" + stringifyValues(field[value]);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
throw new exceptions_1.QueryOperatorNotFoundException(value);
|
|
268
|
+
});
|
|
269
|
+
return expr.join(' AND ');
|
|
270
|
+
}
|
|
271
|
+
catch (exception) {
|
|
272
|
+
if (exception instanceof exceptions_1.WhereClauseException) {
|
|
273
|
+
throw exception;
|
|
274
|
+
}
|
|
275
|
+
throw new exceptions_1.WhereClauseException();
|
|
276
|
+
}
|
|
277
|
+
};
|
|
278
|
+
/**
|
|
279
|
+
* @ignore
|
|
280
|
+
* */
|
|
281
|
+
var _buildComparisionClauseExpr = function (fieldName, comparison) {
|
|
282
|
+
try {
|
|
283
|
+
var expr = Object.keys(comparison)
|
|
284
|
+
.map(function (value) {
|
|
285
|
+
if (!!comparison[value]) {
|
|
286
|
+
if (dictionary_1.ComparisonEmptyOperatorDict.hasOwnProperty(value)) {
|
|
287
|
+
return (0, utils_1.escapeReservedWords)(fieldName) + " " + dictionary_1.ComparisonEmptyOperatorDict[value];
|
|
288
|
+
}
|
|
289
|
+
if (dictionary_1.ComparisonSingleOperatorDict.hasOwnProperty(value)) {
|
|
290
|
+
return "" + (0, utils_1.escapeReservedWords)(fieldName) + dictionary_1.ComparisonSingleOperatorDict[value] + stringifyValues(comparison[value]);
|
|
291
|
+
}
|
|
292
|
+
if (dictionary_1.ComparisonSingleStringOperatorDict.hasOwnProperty(value)) {
|
|
293
|
+
return (0, utils_1.escapeReservedWords)(fieldName) + " " + dictionary_1.ComparisonSingleStringOperatorDict[value] + " " + stringifyValues(comparison[value]);
|
|
294
|
+
}
|
|
295
|
+
if (dictionary_1.ComparisonMultipleOperatorDict.hasOwnProperty(value) &&
|
|
296
|
+
Array.isArray(comparison[value])) {
|
|
297
|
+
return (0, utils_1.escapeReservedWords)(fieldName) + " " + dictionary_1.ComparisonMultipleOperatorDict[value] + " " + comparison[value].map(function (v) { return stringifyValues(v); }).join(' AND ');
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
throw new exceptions_1.QueryOperatorNotFoundException(value);
|
|
301
|
+
})
|
|
302
|
+
.join(" AND ");
|
|
303
|
+
return Object.keys(comparison).length > 1 ? "(" + expr + ")" : expr;
|
|
304
|
+
}
|
|
305
|
+
catch (exception) {
|
|
306
|
+
if (exception instanceof exceptions_1.WhereClauseException) {
|
|
307
|
+
throw exception;
|
|
308
|
+
}
|
|
309
|
+
throw new exceptions_1.WhereClauseException();
|
|
310
|
+
}
|
|
311
|
+
};
|
|
312
|
+
/**
|
|
313
|
+
* @ignore
|
|
314
|
+
* */
|
|
315
|
+
var _buildCollectionInWithinOperator = function (op, expr, excludeOperator) {
|
|
316
|
+
if (!op || !expr.target_expr || !expr.search_expr) {
|
|
317
|
+
throw new exceptions_1.InWithinOperatorExceptions();
|
|
318
|
+
}
|
|
319
|
+
return "" + expr.search_expr + (!excludeOperator && expr.$not ? ' NOT ' : ' ') + dictionary_1.CollectionInWithinOperatorDict[op] + " " + (excludeOperator ? expr.target_expr : stringifyValues(expr.target_expr));
|
|
320
|
+
};
|
|
321
|
+
var stringifyValues = function (value) {
|
|
322
|
+
return JSON.stringify(value).replace(/\\/gi, '');
|
|
323
|
+
};
|
|
324
|
+
/**
|
|
325
|
+
* @ignore
|
|
326
|
+
* */
|
|
327
|
+
var _buildCollectionInWithIn = function (collection) {
|
|
328
|
+
var op = collection.$in ? '$in' : collection.$within ? '$within' : undefined;
|
|
329
|
+
if (!op) {
|
|
330
|
+
throw new Error('The Collection Operator needs to have the following clauses declared (IN | WITHIN) and SATISFIES.');
|
|
331
|
+
}
|
|
332
|
+
return "" + _buildCollectionInWithinOperator(op, collection[op], true);
|
|
333
|
+
};
|
|
334
|
+
/**
|
|
335
|
+
* @ignore
|
|
336
|
+
* */
|
|
337
|
+
var _buildWhereCollectionExpr = function (op, expr) {
|
|
338
|
+
return dictionary_1.CollectionSelectOperatorDict[op] + " " + expr.$expr
|
|
339
|
+
.map(function (value) { return _buildCollectionInWithIn(value); })
|
|
340
|
+
.join(',') + " " + dictionary_1.CollectionSatisfiesOperatorDict['$satisfies'] + " " + (0, exports.buildWhereClauseExpr)('', expr.$satisfied) + " END";
|
|
341
|
+
};
|
|
342
|
+
// end where expression functions
|
|
343
|
+
// index expression functions
|
|
344
|
+
/**
|
|
345
|
+
* Build a INDEX N1QL query from user-specified parameters.
|
|
346
|
+
* {@link https://docs.couchbase.com/server/6.5/n1ql/n1ql-language-reference/createindex.html}
|
|
347
|
+
* @param collection Collection name
|
|
348
|
+
* @param type INDEX clause types can be 'CREATE' | 'BUILD' | 'DROP' | 'CREATE PRIMARY'
|
|
349
|
+
* @param on ON Clause param
|
|
350
|
+
* @param where WHERE Clause param
|
|
351
|
+
* @param usingGSI use a Global Secondary Index(GSI).
|
|
352
|
+
* @param withExpr WITH Clause param
|
|
353
|
+
*
|
|
354
|
+
* @return N1QL INDEX Query
|
|
355
|
+
* */
|
|
356
|
+
var buildIndexExpr = function (collection, type, name, on, where, usingGSI, withExpr) {
|
|
357
|
+
if (['BUILD', 'CREATE', 'CREATE PRIMARY'].includes(type) && on) {
|
|
358
|
+
return type + " INDEX `" + name + "` ON `" + collection + "`(" + buildOnExpr(on) + ")" + (0, exports.buildWhereExpr)(where) + " " + (usingGSI ? 'USING GSI' : '') + " " + buildWithExpr(withExpr);
|
|
359
|
+
}
|
|
360
|
+
else {
|
|
361
|
+
return type + " INDEX `" + collection + "`.`" + name + "`" + (usingGSI ? ' USING GSI' : '');
|
|
362
|
+
}
|
|
363
|
+
};
|
|
364
|
+
exports.buildIndexExpr = buildIndexExpr;
|
|
365
|
+
/**
|
|
366
|
+
* @ignore
|
|
367
|
+
* */
|
|
368
|
+
var buildOnExpr = function (on) {
|
|
369
|
+
return on
|
|
370
|
+
.map(function (value) {
|
|
371
|
+
return "" + (0, utils_1.escapeReservedWords)(value.name) + buildOnSortExpr(value);
|
|
372
|
+
})
|
|
373
|
+
.join(',');
|
|
374
|
+
};
|
|
375
|
+
/**
|
|
376
|
+
* @ignore
|
|
377
|
+
* */
|
|
378
|
+
var buildOnSortExpr = function (onExpr) {
|
|
379
|
+
if (onExpr && onExpr.hasOwnProperty('sort')) {
|
|
380
|
+
return "[\"" + onExpr.sort + "\"]";
|
|
381
|
+
}
|
|
382
|
+
return '';
|
|
383
|
+
};
|
|
384
|
+
/**
|
|
385
|
+
* @ignore
|
|
386
|
+
* */
|
|
387
|
+
var buildWithExpr = function (withExpr) {
|
|
388
|
+
if (withExpr) {
|
|
389
|
+
var resultExpr = Object.keys(withExpr)
|
|
390
|
+
.map(function (value) {
|
|
391
|
+
switch (value) {
|
|
392
|
+
case 'nodes':
|
|
393
|
+
return buildWithNodesExpr(withExpr[value]);
|
|
394
|
+
case 'defer_build':
|
|
395
|
+
case 'num_replica':
|
|
396
|
+
return "\"" + value + "\": " + withExpr[value];
|
|
397
|
+
default:
|
|
398
|
+
throw new Error('The WITH clause has an incorrect syntax');
|
|
399
|
+
}
|
|
400
|
+
})
|
|
401
|
+
.join(',');
|
|
402
|
+
return !!resultExpr ? "WITH {" + resultExpr + "}" : '';
|
|
403
|
+
}
|
|
404
|
+
return '';
|
|
405
|
+
};
|
|
406
|
+
/**
|
|
407
|
+
* @ignore
|
|
408
|
+
* */
|
|
409
|
+
var buildWithNodesExpr = function (withNodesExpr) {
|
|
410
|
+
if (withNodesExpr) {
|
|
411
|
+
return "\"nodes\": " + stringifyValues(withNodesExpr);
|
|
412
|
+
}
|
|
413
|
+
};
|
|
414
|
+
// end index expression functions
|
|
415
|
+
//# sourceMappingURL=builders.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"builders.js","sourceRoot":"","sources":["../../../src/query/helpers/builders.ts"],"names":[],"mappings":";;;AAkBA,4CAMuB;AACvB,kCAA6C;AAE7C,2CAYsB;AAEtB,+BAA+B;AAC/B;;;;;;;;;;;;;KAaK;AACE,IAAM,aAAa,GAAG,UACzB,UAAkB,EAClB,MAA8B,EAC9B,OAAoB,EACpB,KAAwB,EACxB,OAAkC,EAClC,KAAc,EACd,MAAe,EACf,OAAkB,EAClB,WAAwB,EACxB,WAAwB,EACxB,UAA6B,EAC7B,aAAsB;IAEtB,IAAI;QACA,IAAI,IAAI,GAAG,EAAE,CAAC;QACd,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE;YAC5B,IAAI,GAAG,MAAM,CAAC;SACjB;QACD,IAAI,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE;YACvB,IAAI,GAAG,IAAA,4BAAoB,EAAC,MAAM,CAAC,CAAC;SACvC;QACD,IAAM,WAAW,GACb,UAAU,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;YAC1B,CAAC,CAAC,CAAA,MAAK,UAAY,CAAA,CAAC,OAAO,CAAC,GAAG,EAAE,IAAI,CAAC;YACtC,CAAC,CAAC,UAAU,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;gBAChC,CAAC,CAAC,UAAU;gBACZ,CAAC,CAAC,MA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K;AACL,IAAM,aAAa,GAAG,UAAC,QAA2B;IAC9C,IAAI,QAAQ,EAAE;QACV,IAAM,UAAU,GAAG,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC;aACnC,GAAG,CAAC,UAAC,KAAa;YACf,QAAQ,KAAK,EAAE;gBACX,KAAK,OAAO;oBACR,OAAO,kBAAkB,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC;gBAC/C,KAAK,aAAa,CAAC;gBACnB,KAAK,aAAa;oBACd,OAAO,OAAI,KAAK,YAAM,QAAQ,CAAC,KAAK,CAAG,CAAC;gBAC5C;oBACI,MAAM,IAAI,KAAK,CAAC,yCAAyC,CAAC,CAAC;aAClE;QACL,CAAC,CAAC;aACD,IAAI,CAAC,GAAG,CAAC,CAAC;QACf,OAAO,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,WAAS,UAAU,MAAG,CAAC,CAAC,CAAC,EAAE,CAAC;KACrD;IACD,OAAO,EAAE,CAAC;AACd,CAAC,CAAC;AAEF;;KAEK;AACL,IAAM,kBAAkB,GAAG,UAAC,aAAwB;IAChD,IAAI,aAAa,EAAE;QACf,OAAO,gBAAY,eAAe,CAAC,aAAa,CAAG,CAAC;KACvD;AACL,CAAC,CAAC;AAEF,iCAAiC","sourcesContent":["import {\n CollectionExpressionType,\n CollectionInWithinOperator,\n CollectionInWithinOperatorType,\n CollectionInWithinOperatorValue,\n CollectionSelectOperator,\n ComparisonWhereExpr,\n IField,\n IGroupBy,\n IIndexOnParams,\n IIndexWithParams,\n ILetExpr,\n IndexType,\n ISelectAggType,\n ISelectType,\n LogicalWhereExpr,\n SortType,\n} from '../interface/query.types';\nimport {\n InWithinOperatorExceptions,\n QueryGroupByParamsException,\n QueryOperatorNotFoundException,\n SelectClauseException,\n WhereClauseException,\n} from '../exceptions';\nimport {escapeReservedWords} from '../utils';\n\nimport {\n AggDict,\n CollectionInWithinOperatorDict,\n CollectionSatisfiesOperatorDict,\n CollectionSelectOperatorDict,\n ComparisonEmptyOperatorDict,\n ComparisonMultipleOperatorDict,\n ComparisonSingleOperatorDict,\n ComparisonSingleStringOperatorDict,\n LogicalOperatorDict,\n ResultExprDict,\n ReturnResultDict,\n} from './dictionary';\n\n// select expressions functions\n/**\n * Build a SELECT N1QL query from user-specified parameters.\n * {@link https://docs.couchbase.com/server/6.5/n1ql/n1ql-language-reference/select-syntax.html}\n * @param collection Collection name\n * @param select SELECT Clause param\n * @param letExpr LET Clause param\n * @param where WHERE Clause param\n * @param orderBy ORDER BY Clause param\n * @param limit LIMIT Clause param\n * @param offset OFFSET Clause param\n * @param useExpr USE Clause param\n *\n * @return N1QL SELECT Query\n * */\nexport const selectBuilder = (\n collection: string,\n select: ISelectType[] | string,\n letExpr?: ILetExpr[],\n where?: LogicalWhereExpr,\n orderBy?: Record<string, SortType>,\n limit?: number,\n offset?: number,\n useExpr?: string[],\n groupByExpr?: IGroupBy[],\n lettingExpr?: ILetExpr[],\n havingExpr?: LogicalWhereExpr,\n plainJoinExpr?: string\n): string => {\n try {\n let expr = '';\n if (typeof select === 'string') {\n expr = select;\n }\n if (Array.isArray(select)) {\n expr = buildSelectArrayExpr(select);\n }\n const _collection =\n collection.indexOf(' ') !== -1\n ? `\\`${collection}`.replace(' ', '` ')\n : collection.indexOf('`') !== -1\n ? collection\n : `\\`${collection}\\``;\n return `SELECT ${expr} FROM ${_collection}${\n plainJoinExpr ? ` ${plainJoinExpr} ` : ''\n }${_buildUseKeysExpr(useExpr)}${_buildLetExpr(letExpr)}${buildWhereExpr(\n where\n )}${_buildGroupByExpr(groupByExpr, lettingExpr, havingExpr)}${_buildOrderByExpr(\n orderBy\n )}${_buildLimitExpr(limit)}${_buildOffsetExpr(offset)}`;\n } catch (exception) {\n if (exception instanceof WhereClauseException) {\n throw exception;\n }\n throw new SelectClauseException();\n }\n};\n\n/**\n * @ignore\n * */\nconst _buildAS = (c: ISelectAggType | IField) => {\n return c.hasOwnProperty('as') ? ` AS ${c['as']}` : '';\n};\n\n/**\n * @ignore\n * */\nconst _buildField = (clause: IField | string) => {\n if (clause.hasOwnProperty('name')) {\n return `${escapeReservedWords(clause['name'])}${_buildAS(clause as IField)}`;\n }\n return `${escapeReservedWords(clause as string)}`;\n};\n\n/**\n * Create N1QL queries from select array params\n * @param clause SELECT Clause param\n *\n * @return N1QL SELECT Query\n * */\nexport const buildSelectArrayExpr = (clause: ISelectType[]): string => {\n return `${clause.map((c) => buildSelectExpr('', c)).join(',')}`;\n};\n\n/**\n * Recursive function to create N1QL queries.\n * @param n1ql N1QL Query String\n * @param clause SELECT Clause param\n *\n * @return N1QL SELECT Query\n * */\nexport const buildSelectExpr = (n1ql: string, clause: ISelectType): string => {\n try {\n if (clause.hasOwnProperty('$field')) {\n return _buildField(clause['$field']);\n }\n const key = Object.keys(clause)[0];\n if (ReturnResultDict.hasOwnProperty(key)) {\n return `${ReturnResultDict[key]} ${buildSelectExpr(n1ql, clause[key])}`;\n }\n if (ResultExprDict.hasOwnProperty(key)) {\n return `${ResultExprDict[key]} ${buildSelectExpr(n1ql, clause[key])}`;\n }\n if (AggDict.hasOwnProperty(key)) {\n // todo check if have AS expr inside of Agg function.\n return `${AggDict[key]}(${_buildAggDictExpr(\n clause as ISelectAggType,\n key\n )}${buildSelectExpr(n1ql, clause[key])})${_buildAS(clause[key])}`;\n }\n throw new SelectClauseException();\n } catch {\n throw new SelectClauseException();\n }\n};\n\n/**\n * @ignore\n * */\nconst _buildAggDictExpr = (clause: ISelectAggType, key: string) => {\n if (AggDict.hasOwnProperty(key)) {\n if (clause[key].hasOwnProperty('ro')) {\n return `${ReturnResultDict[clause[key]['ro']]} `;\n }\n }\n return '';\n};\n\n/**\n * @ignore\n * */\nconst _buildLetExpr = (letExpr: ILetExpr[] | undefined, clause?: string) => {\n return Array.isArray(letExpr)\n ? ` ${clause ? clause : 'LET'} ${letExpr\n .map((value: ILetExpr) => `${escapeReservedWords(value.key)}=${value.value}`)\n .join(',')}`\n : '';\n};\n\n/**\n * @ignore\n * */\nconst _buildOrderByExpr = (orderExpr: Record<string, SortType> | undefined) => {\n return !!orderExpr\n ? ` ORDER BY ${Object.keys(orderExpr)\n .map((value: string) => `${escapeReservedWords(value)} ${orderExpr[value]}`)\n .join(',')}`\n : '';\n};\n\n/**\n * @ignore\n * */\nconst _buildLimitExpr = (limit: number | undefined) => {\n return Number.isInteger(limit) ? ` LIMIT ${limit}` : '';\n};\n\n/**\n * @ignore\n * */\nconst _buildOffsetExpr = (offset: number | undefined) => {\n return Number.isInteger(offset) ? ` OFFSET ${offset}` : '';\n};\n\n/**\n * @ignore\n * */\nconst _buildUseKeysExpr = (useKeys: string[] | undefined) => {\n return Array.isArray(useKeys) ? ` USE KEYS ${stringifyValues(useKeys)}` : '';\n};\n\n// end select expression functions\n\n//group by expression functions\n/**\n *@ignore\n */\nconst _buildGroupByExpr = (\n groupByExpr?: IGroupBy[],\n lettingExpr?: ILetExpr[],\n havingExpr?: LogicalWhereExpr\n) => {\n try {\n if ((lettingExpr || havingExpr) && !groupByExpr) {\n throw new QueryGroupByParamsException();\n }\n if (!groupByExpr) {\n return '';\n }\n return ` ${_buildGroupBy(groupByExpr)}${_buildLetExpr(\n lettingExpr,\n 'LETTING'\n )}${buildWhereExpr(havingExpr as LogicalWhereExpr, 'HAVING')}`;\n } catch {\n throw new QueryGroupByParamsException();\n }\n};\n\n/**\n *@ignore\n */\nconst _buildGroupBy = (groupByExpr: IGroupBy[]) => {\n return `GROUP BY ${groupByExpr\n .map((value: IGroupBy) => {\n return `${escapeReservedWords(value.expr)}${value.as ? ` AS ${value.as}` : ''}`;\n })\n .join(',')}`;\n};\n\n//end group by expression functions\n\n// where expression functions\n\n/**\n * Create WHERE N1QL Expressions.\n * {@link https://docs.couchbase.com/server/6.5/n1ql/n1ql-language-reference/where.html}\n * @param clause WHERE Clause param\n * @return N1QL WHERE Expression\n * */\nexport const buildWhereExpr = (expr: LogicalWhereExpr | undefined, clause?: string): string => {\n return expr ? ` ${clause ? clause : 'WHERE'} ${buildWhereClauseExpr('', expr)}` : '';\n};\n\n/**\n * @ignore\n *\n **/\nexport const verifyWhereObjectKey = (clause: LogicalWhereExpr) => {\n let exist = false;\n for (const key in clause) {\n if (['$and', '$or', '$not', '$any', '$$every', '$in', '$within'].includes(key)) {\n exist = true;\n break;\n }\n }\n return exist;\n};\n\n/**\n * Recursive function to create WHERE N1QL Expressions.\n * @param n1ql N1QL Query String\n * @param clause WHERE Clause param\n *\n * @return N1QL WHERE Expression\n * */\nexport const buildWhereClauseExpr = (n1ql: string, clause: LogicalWhereExpr): string => {\n try {\n if (!verifyWhereObjectKey(clause)) {\n return _buildFieldClauseExpr(\n clause as Record<string, string | number | boolean | ComparisonWhereExpr>\n );\n }\n\n return Object.keys(clause)\n .map((key) => {\n if (CollectionSelectOperatorDict[key]) {\n return `${_buildWhereCollectionExpr(\n key as CollectionSelectOperator,\n clause[key]\n )}`;\n }\n if (CollectionInWithinOperatorDict[key]) {\n return `${_buildCollectionInWithinOperator(\n key as CollectionInWithinOperator,\n clause[key]\n )}`;\n }\n if (Array.isArray(clause[key])) {\n const prefix = key === '$not' ? `${LogicalOperatorDict[key]} ` : '';\n const joinOp = key === '$not' ? ` AND ` : ` ${LogicalOperatorDict[key]} `;\n return `${prefix}(${clause[key]\n .map((value) => buildWhereClauseExpr(n1ql, value as LogicalWhereExpr))\n .join(joinOp)})`;\n } else {\n return `${buildWhereClauseExpr(n1ql, {[key]: clause[key]})}`;\n }\n })\n .join(' AND ');\n } catch (exception) {\n if (exception instanceof WhereClauseException) {\n throw exception;\n }\n throw new WhereClauseException();\n }\n};\n\n/**\n * @ignore\n * */\nconst _buildFieldClauseExpr = (\n field: Record<string, string | number | boolean | ComparisonWhereExpr>\n) => {\n try {\n const expr = Object.keys(field).map((value: string) => {\n if (typeof field[value] === 'object' && !Array.isArray(field[value])) {\n return `${_buildComparisionClauseExpr(value, field[value] as ComparisonWhereExpr)}`;\n }\n if (!value.includes('$')) {\n if (typeof field[value] === 'string') {\n return `${escapeReservedWords(value)}=${stringifyValues(field[value])}`;\n }\n if (\n typeof field[value] === 'number' ||\n typeof field[value] === 'boolean' ||\n Array.isArray(field[value])\n ) {\n return `${escapeReservedWords(value)}=${stringifyValues(field[value])}`;\n }\n }\n throw new QueryOperatorNotFoundException(value);\n });\n return expr.join(' AND ');\n } catch (exception) {\n if (exception instanceof WhereClauseException) {\n throw exception;\n }\n throw new WhereClauseException();\n }\n};\n\n/**\n * @ignore\n * */\nconst _buildComparisionClauseExpr = (fieldName: string, comparison: ComparisonWhereExpr) => {\n try {\n const expr = Object.keys(comparison)\n .map((value: string) => {\n if (!!comparison[value]) {\n if (ComparisonEmptyOperatorDict.hasOwnProperty(value)) {\n return `${escapeReservedWords(fieldName)} ${\n ComparisonEmptyOperatorDict[value]\n }`;\n }\n if (ComparisonSingleOperatorDict.hasOwnProperty(value)) {\n return `${escapeReservedWords(fieldName)}${\n ComparisonSingleOperatorDict[value]\n }${stringifyValues(comparison[value])}`;\n }\n if (ComparisonSingleStringOperatorDict.hasOwnProperty(value)) {\n return `${escapeReservedWords(fieldName)} ${\n ComparisonSingleStringOperatorDict[value]\n } ${stringifyValues(comparison[value])}`;\n }\n if (\n ComparisonMultipleOperatorDict.hasOwnProperty(value) &&\n Array.isArray(comparison[value])\n ) {\n return `${escapeReservedWords(fieldName)} ${\n ComparisonMultipleOperatorDict[value]\n } ${comparison[value].map((v) => stringifyValues(v)).join(' AND ')}`;\n }\n }\n throw new QueryOperatorNotFoundException(value);\n })\n .join(` AND `);\n return Object.keys(comparison).length > 1 ? `(${expr})` : expr;\n } catch (exception) {\n if (exception instanceof WhereClauseException) {\n throw exception;\n }\n throw new WhereClauseException();\n }\n};\n\n/**\n * @ignore\n * */\nconst _buildCollectionInWithinOperator = (\n op: CollectionInWithinOperator,\n expr: CollectionInWithinOperatorValue,\n excludeOperator?: boolean\n): string => {\n if (!op || !expr.target_expr || !expr.search_expr) {\n throw new InWithinOperatorExceptions();\n }\n return `${expr.search_expr}${!excludeOperator && expr.$not ? ' NOT ' : ' '}${\n CollectionInWithinOperatorDict[op]\n } ${excludeOperator ? expr.target_expr : stringifyValues(expr.target_expr)}`;\n};\n\nconst stringifyValues = (value: unknown) => {\n return JSON.stringify(value).replace(/\\\\/gi, '');\n};\n\n/**\n * @ignore\n * */\nconst _buildCollectionInWithIn = (collection: CollectionInWithinOperatorType) => {\n const op = collection.$in ? '$in' : collection.$within ? '$within' : undefined;\n if (!op) {\n throw new Error(\n 'The Collection Operator needs to have the following clauses declared (IN | WITHIN) and SATISFIES.'\n );\n }\n return `${_buildCollectionInWithinOperator(\n op,\n collection[op] as CollectionInWithinOperatorValue,\n true\n )}`;\n};\n\n/**\n * @ignore\n * */\nconst _buildWhereCollectionExpr = (\n op: CollectionSelectOperator,\n expr: CollectionExpressionType\n) => {\n return `${CollectionSelectOperatorDict[op]} ${expr.$expr\n .map((value) => _buildCollectionInWithIn(value))\n .join(',')} ${CollectionSatisfiesOperatorDict['$satisfies']} ${buildWhereClauseExpr(\n '',\n expr.$satisfied\n )} END`;\n};\n\n// end where expression functions\n\n// index expression functions\n\n/**\n * Build a INDEX N1QL query from user-specified parameters.\n * {@link https://docs.couchbase.com/server/6.5/n1ql/n1ql-language-reference/createindex.html}\n * @param collection Collection name\n * @param type INDEX clause types can be 'CREATE' | 'BUILD' | 'DROP' | 'CREATE PRIMARY'\n * @param on ON Clause param\n * @param where WHERE Clause param\n * @param usingGSI use a Global Secondary Index(GSI).\n * @param withExpr WITH Clause param\n *\n * @return N1QL INDEX Query\n * */\nexport const buildIndexExpr = (\n collection: string,\n type: IndexType,\n name: string,\n on?: IIndexOnParams[],\n where?: LogicalWhereExpr,\n usingGSI?: boolean,\n withExpr?: IIndexWithParams\n): string => {\n if (['BUILD', 'CREATE', 'CREATE PRIMARY'].includes(type) && on) {\n return `${type} INDEX \\`${name}\\` ON \\`${collection}\\`(${buildOnExpr(on)})${buildWhereExpr(\n where\n )} ${usingGSI ? 'USING GSI' : ''} ${buildWithExpr(withExpr)}`;\n } else {\n return `${type} INDEX \\`${collection}\\`.\\`${name}\\`${usingGSI ? ' USING GSI' : ''}`;\n }\n};\n\n/**\n * @ignore\n * */\nconst buildOnExpr = (on: IIndexOnParams[]) => {\n return on\n .map((value: IIndexOnParams) => {\n return `${escapeReservedWords(value.name)}${buildOnSortExpr(value)}`;\n })\n .join(',');\n};\n\n/**\n * @ignore\n * */\nconst buildOnSortExpr = (onExpr?: IIndexOnParams) => {\n if (onExpr && onExpr.hasOwnProperty('sort')) {\n return `[\"${onExpr.sort}\"]`;\n }\n return '';\n};\n\n/**\n * @ignore\n * */\nconst buildWithExpr = (withExpr?: IIndexWithParams) => {\n if (withExpr) {\n const resultExpr = Object.keys(withExpr)\n .map((value: string) => {\n switch (value) {\n case 'nodes':\n return buildWithNodesExpr(withExpr[value]);\n case 'defer_build':\n case 'num_replica':\n return `\"${value}\": ${withExpr[value]}`;\n default:\n throw new Error('The WITH clause has an incorrect syntax');\n }\n })\n .join(',');\n return !!resultExpr ? `WITH {${resultExpr}}` : '';\n }\n return '';\n};\n\n/**\n * @ignore\n * */\nconst buildWithNodesExpr = (withNodesExpr?: string[]) => {\n if (withNodesExpr) {\n return `\"nodes\": ${stringifyValues(withNodesExpr)}`;\n }\n};\n\n// end index expression functions\n"]}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { AggType, CollectionSelectOperator, CollectionInWithinOperator, ComparisonEmptyOperatorType, ComparisonMultipleOperatorType, ComparisonSingleOperatorType, ComparisonSingleStringOperatorType, LogicalOperatorType, ResultExprType, ReturnResultType, CollectionSatisfiesOperator } from '../interface/query.types';
|
|
2
|
+
/**
|
|
3
|
+
* Dictionary for handling aggregation functions.
|
|
4
|
+
* */
|
|
5
|
+
export declare const AggDict: Record<AggType, string>;
|
|
6
|
+
/**
|
|
7
|
+
* Dictionary for handling result expressions RAW | ELEMENT | VALUE
|
|
8
|
+
* */
|
|
9
|
+
export declare const ResultExprDict: Record<ResultExprType, string>;
|
|
10
|
+
/**
|
|
11
|
+
* Dictionary for handling result expressions ALL | DISTINCT
|
|
12
|
+
* */
|
|
13
|
+
export declare const ReturnResultDict: Record<ReturnResultType, string>;
|
|
14
|
+
/**
|
|
15
|
+
* Dictionary for handling Boolean comparison operators
|
|
16
|
+
* */
|
|
17
|
+
export declare const ComparisonEmptyOperatorDict: Record<ComparisonEmptyOperatorType, string>;
|
|
18
|
+
/**
|
|
19
|
+
* Dictionary for handling Numeric comparison operators
|
|
20
|
+
* */
|
|
21
|
+
export declare const ComparisonSingleOperatorDict: Record<ComparisonSingleOperatorType, string>;
|
|
22
|
+
/**
|
|
23
|
+
* Dictionary for handling String comparison operators
|
|
24
|
+
* */
|
|
25
|
+
export declare const ComparisonSingleStringOperatorDict: Record<ComparisonSingleStringOperatorType, string>;
|
|
26
|
+
/**
|
|
27
|
+
* Dictionary for handling Range comparison operators
|
|
28
|
+
* */
|
|
29
|
+
export declare const ComparisonMultipleOperatorDict: Record<ComparisonMultipleOperatorType, string>;
|
|
30
|
+
/**
|
|
31
|
+
* Dictionary for handling logical operators
|
|
32
|
+
* */
|
|
33
|
+
export declare const LogicalOperatorDict: Record<LogicalOperatorType, string>;
|
|
34
|
+
/**
|
|
35
|
+
* Dictionary for handling collection select operators
|
|
36
|
+
* */
|
|
37
|
+
export declare const CollectionSelectOperatorDict: Record<CollectionSelectOperator, string>;
|
|
38
|
+
/**
|
|
39
|
+
* Dictionary for handling collection (in | within) operators
|
|
40
|
+
* */
|
|
41
|
+
export declare const CollectionInWithinOperatorDict: Record<CollectionInWithinOperator, string>;
|
|
42
|
+
/**
|
|
43
|
+
* Dictionary for handling collection satisfies operators
|
|
44
|
+
* */
|
|
45
|
+
export declare const CollectionSatisfiesOperatorDict: Record<CollectionSatisfiesOperator, string>;
|