@narrative.io/data-collaboration-sdk-ts 0.0.57 → 0.0.59
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/build/authentication/types.d.ts +1 -1
- package/build/nql/NQLParser.d.ts +17 -17
- package/build/nql/NQLParser.js +53 -26
- package/package.json +1 -1
package/build/nql/NQLParser.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type Nql, type NqlBudget, type NqlField, type NqlPreSelectExpression, type NqlTable, type NqlWhere } from './types';
|
|
1
|
+
import { type Nql, type NqlBudget, type NqlField, type NqlFilterExpression, type NqlPreSelectExpression, type NqlTable, type NqlWhere } from './types';
|
|
2
2
|
/**
|
|
3
3
|
* Retrieves the WHERE clause from the given NQL (Nested Query Language) string and returns a parsed NqlWhereClause object.
|
|
4
4
|
*
|
|
@@ -15,6 +15,13 @@ export declare function getWhere(nql: string): NqlWhere;
|
|
|
15
15
|
* @throws {Error} - Throws an error if no FROM clause is found in the NQL string.
|
|
16
16
|
*/
|
|
17
17
|
export declare function getFrom(nql: string): NqlTable[];
|
|
18
|
+
/**
|
|
19
|
+
* Parses a field string into an NqlField object or a plain string if parsing fails.
|
|
20
|
+
*
|
|
21
|
+
* @param {string} fieldStr - The field string to parse.
|
|
22
|
+
* @returns {NqlField | string} - The parsed NqlField object or the original field string if parsing fails.
|
|
23
|
+
*/
|
|
24
|
+
export declare function getFilterExpressionFromString(str: string): NqlFilterExpression;
|
|
18
25
|
/**
|
|
19
26
|
* Parses the pre-select expression from the given NQL (Natural Query Language) string.
|
|
20
27
|
*
|
|
@@ -66,22 +73,15 @@ export declare function getLimit(nql: string): NqlBudget;
|
|
|
66
73
|
* @throws {Error} - If the provided budget object is invalid.
|
|
67
74
|
*/
|
|
68
75
|
export declare function buildLimit(budget: NqlBudget): string;
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
* @returns {string} - The constructed NQL string.
|
|
74
|
-
* @throws {Error} - Throws an error if the provided Nql object is invalid.
|
|
75
|
-
*/
|
|
76
|
-
export declare function buildNql(nql: Nql): string;
|
|
76
|
+
type Strict = true;
|
|
77
|
+
type NotStrict = false;
|
|
78
|
+
export declare function buildNql(nql: Nql, strict?: Strict): string;
|
|
79
|
+
export declare function buildNql(nql: Nql | SemanticNql, strict?: NotStrict): string;
|
|
77
80
|
export declare function buildFrom(from: NqlTable): string;
|
|
78
81
|
export declare function buildWhere(where: NqlWhere): string;
|
|
79
82
|
export declare function buildField(field: NqlField | string | number | boolean): string;
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
* @throws {Error} - If the provided NQL string is invalid.
|
|
86
|
-
*/
|
|
87
|
-
export declare function getNqlObject(nql: string): Nql;
|
|
83
|
+
type ParseType = 'full' | 'semantic';
|
|
84
|
+
type SemanticNql = Omit<Nql, 'explain' | 'limit'>;
|
|
85
|
+
export declare function getNqlObject<T extends ParseType = 'full'>(nql: string, type?: T): T extends 'full' ? Nql : SemanticNql;
|
|
86
|
+
export declare function createSemanticWhere(expressionContainer: NqlWhere): NqlWhere;
|
|
87
|
+
export {};
|
package/build/nql/NQLParser.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { NqlBooleanExpressionObj, NqlBudgetObj, NqlFieldObj, NqlFilterExpressionObj, NqlObj, NqlPreSelectExpressionObj, NqlTableObject, NqlWhereObj } from './types';
|
|
1
|
+
import { NqlBooleanExpressionObj, NqlBudgetObj, NqlFieldObj, NqlFilterExpressionObj, NqlObj, NqlPreSelectExpressionObj, NqlTableObject, NqlWhereObj, NqlFilterUnaryExpressionObj } from './types';
|
|
2
2
|
/**
|
|
3
3
|
* Retrieves the WHERE clause from the given NQL (Nested Query Language) string and returns a parsed NqlWhereClause object.
|
|
4
4
|
*
|
|
@@ -127,7 +127,7 @@ function parseString(value) {
|
|
|
127
127
|
* @param {string} fieldStr - The field string to parse.
|
|
128
128
|
* @returns {NqlField | string} - The parsed NqlField object or the original field string if parsing fails.
|
|
129
129
|
*/
|
|
130
|
-
function getFilterExpressionFromString(str) {
|
|
130
|
+
export function getFilterExpressionFromString(str) {
|
|
131
131
|
const comparison = ['<=', '>=', '!=', '=', '>', '<'];
|
|
132
132
|
const containsComparison = comparison.find((character) => str.includes(character));
|
|
133
133
|
if (containsComparison !== undefined) {
|
|
@@ -141,10 +141,10 @@ function getFilterExpressionFromString(str) {
|
|
|
141
141
|
right
|
|
142
142
|
});
|
|
143
143
|
}
|
|
144
|
-
const strComparison = [' IN ', '
|
|
144
|
+
const strComparison = [' NOT IN ', ' IN ', ' NOT LIKE ', ' LIKE '];
|
|
145
145
|
const containsStrComparison = strComparison.find((character) => str.toLowerCase().includes(character.toLowerCase()));
|
|
146
146
|
if (containsStrComparison !== undefined) {
|
|
147
|
-
const result = str.split(containsStrComparison);
|
|
147
|
+
const result = str.toLowerCase().split(containsStrComparison.toLowerCase());
|
|
148
148
|
const left = parseFieldFromString(result[0]);
|
|
149
149
|
const operator = containsStrComparison.trim().toUpperCase();
|
|
150
150
|
const right = parseFieldFromString(result[1]);
|
|
@@ -157,10 +157,10 @@ function getFilterExpressionFromString(str) {
|
|
|
157
157
|
const unaryOperators = [' IS NULL', ' IS NOT NULL'];
|
|
158
158
|
const containsUnaryOperator = unaryOperators.find((character) => str.toLowerCase().includes(character.toLowerCase()));
|
|
159
159
|
if (containsUnaryOperator !== undefined) {
|
|
160
|
-
const result = str.split(containsUnaryOperator);
|
|
160
|
+
const result = str.toLowerCase().split(containsUnaryOperator.toLowerCase());
|
|
161
161
|
const field = parseFieldFromString(result[0]);
|
|
162
162
|
const operator = containsUnaryOperator.trim().toUpperCase();
|
|
163
|
-
return
|
|
163
|
+
return NqlFilterUnaryExpressionObj.parse({
|
|
164
164
|
field,
|
|
165
165
|
operator
|
|
166
166
|
});
|
|
@@ -269,19 +269,20 @@ export function buildLimit(budget) {
|
|
|
269
269
|
NqlBudgetObj.parse(budget);
|
|
270
270
|
return `LIMIT ${budget.value} ${budget.currency} PER ${budget.period}`;
|
|
271
271
|
}
|
|
272
|
-
|
|
273
|
-
* Builds an NQL (Nested Query Language) string based on the provided Nql object.
|
|
274
|
-
*
|
|
275
|
-
* @param {Nql} nql - The Nql object containing the clauses for building the NQL string.
|
|
276
|
-
* @returns {string} - The constructed NQL string.
|
|
277
|
-
* @throws {Error} - Throws an error if the provided Nql object is invalid.
|
|
278
|
-
*/
|
|
279
|
-
export function buildNql(nql) {
|
|
272
|
+
export function buildNql(nql, strict = true) {
|
|
280
273
|
// Validate our object
|
|
281
|
-
|
|
274
|
+
if (strict) {
|
|
275
|
+
NqlObj.parse(nql);
|
|
276
|
+
}
|
|
282
277
|
// Build the NQL string from each of the clasues
|
|
283
|
-
|
|
284
|
-
|
|
278
|
+
let explain = '';
|
|
279
|
+
if ('explain' in nql && strict) {
|
|
280
|
+
explain = buildPreSelect(nql.explain);
|
|
281
|
+
}
|
|
282
|
+
let limit = '';
|
|
283
|
+
if ('limit' in nql && strict) {
|
|
284
|
+
limit = buildLimit(nql.limit);
|
|
285
|
+
}
|
|
285
286
|
const select = buildSelect(nql.select);
|
|
286
287
|
const from = buildFrom(nql.from[0]);
|
|
287
288
|
const where = buildWhere(nql.where);
|
|
@@ -318,18 +319,44 @@ export function buildField(field) {
|
|
|
318
319
|
return String(field);
|
|
319
320
|
}
|
|
320
321
|
}
|
|
321
|
-
|
|
322
|
-
* Parses the given NQL string and returns an Nql object.
|
|
323
|
-
*
|
|
324
|
-
* @param {string} nql - The NQL string to parse.
|
|
325
|
-
* @returns {Nql} - The parsed Nql object.
|
|
326
|
-
* @throws {Error} - If the provided NQL string is invalid.
|
|
327
|
-
*/
|
|
328
|
-
export function getNqlObject(nql) {
|
|
322
|
+
export function getNqlObject(nql, type) {
|
|
329
323
|
const explain = getPreSelect(nql);
|
|
330
324
|
const limit = getLimit(nql);
|
|
331
325
|
const where = getWhere(nql);
|
|
332
326
|
const from = getFrom(nql);
|
|
333
327
|
const select = getSelect(nql);
|
|
334
|
-
|
|
328
|
+
const parsed = NqlObj.parse({ where, from, explain, select, limit });
|
|
329
|
+
if (type === undefined || type === 'full') {
|
|
330
|
+
return parsed;
|
|
331
|
+
}
|
|
332
|
+
else {
|
|
333
|
+
return {
|
|
334
|
+
where: createSemanticWhere(parsed.where),
|
|
335
|
+
from: parsed.from,
|
|
336
|
+
select: parsed.select
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
export function createSemanticWhere(expressionContainer) {
|
|
341
|
+
const copy = JSON.parse(JSON.stringify(expressionContainer)); // deep copy
|
|
342
|
+
let index = 0;
|
|
343
|
+
while (index < copy.filterExpressions.length) {
|
|
344
|
+
const filterExp = copy.filterExpressions[index];
|
|
345
|
+
const fieldObject = 'field' in filterExp ? filterExp.field : null;
|
|
346
|
+
const leftObject = 'left' in filterExp && typeof filterExp.left === 'object' ? filterExp.left : null;
|
|
347
|
+
const rightObject = 'right' in filterExp && typeof filterExp.right === 'object' ? filterExp.right : null;
|
|
348
|
+
if ((fieldObject?.field?.startsWith('_') ?? false) ||
|
|
349
|
+
(leftObject?.field?.startsWith('_') ?? false) ||
|
|
350
|
+
(rightObject?.field?.startsWith('_') ?? false)) {
|
|
351
|
+
copy.filterExpressions.splice(index, 1);
|
|
352
|
+
// Make sure the booleanExpressions array has an element to remove
|
|
353
|
+
if (index > 0 && copy.booleanExpressions.length >= index) {
|
|
354
|
+
copy.booleanExpressions.splice(index - 1, 1);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
else {
|
|
358
|
+
index++; // Only increment if no element was removed
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
return copy;
|
|
335
362
|
}
|