@opra/sqb 1.26.4 → 1.27.0
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.
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import '@opra/core';
|
|
2
2
|
import { OpraFilter } from '@opra/common';
|
|
3
|
-
import
|
|
3
|
+
import { Operators, sql } from '@sqb/builder';
|
|
4
4
|
import { vg } from 'valgen';
|
|
5
5
|
/**
|
|
6
6
|
* Prepares the SQB filter based on the provided filters and options.
|
|
@@ -26,7 +26,7 @@ export default function prepareFilter(filters) {
|
|
|
26
26
|
if (ast)
|
|
27
27
|
arr.push(ast);
|
|
28
28
|
}
|
|
29
|
-
return arr.length > 1 ?
|
|
29
|
+
return arr.length > 1 ? sql.And(...arr) : arr[0];
|
|
30
30
|
}
|
|
31
31
|
const _isDate = vg.isDate({ trim: 'day' });
|
|
32
32
|
const _isDateTime = vg.isDate();
|
|
@@ -43,12 +43,12 @@ function prepareFilterAst(ast) {
|
|
|
43
43
|
return ast.items.map(prepareFilterAst);
|
|
44
44
|
}
|
|
45
45
|
if (ast instanceof OpraFilter.NegativeExpression) {
|
|
46
|
-
return
|
|
46
|
+
return sql.Not(prepareFilterAst(ast.expression));
|
|
47
47
|
}
|
|
48
48
|
if (ast instanceof OpraFilter.LogicalExpression) {
|
|
49
49
|
if (ast.op === 'or')
|
|
50
|
-
return
|
|
51
|
-
return
|
|
50
|
+
return sql.Or(...ast.items.map(prepareFilterAst));
|
|
51
|
+
return sql.And(...ast.items.map(prepareFilterAst));
|
|
52
52
|
}
|
|
53
53
|
if (ast instanceof OpraFilter.ParenthesizedExpression) {
|
|
54
54
|
return prepareFilterAst(ast.expression);
|
|
@@ -67,33 +67,19 @@ function prepareFilterAst(ast) {
|
|
|
67
67
|
return x;
|
|
68
68
|
}
|
|
69
69
|
switch (ast.op) {
|
|
70
|
-
case '=':
|
|
71
|
-
return sqb.Eq(left, right);
|
|
72
|
-
case '!=':
|
|
73
|
-
return sqb.Ne(left, right);
|
|
74
|
-
case '>':
|
|
75
|
-
return sqb.Gt(left, right);
|
|
76
|
-
case '>=':
|
|
77
|
-
return sqb.Gte(left, right);
|
|
78
|
-
case '<':
|
|
79
|
-
return sqb.Lt(left, right);
|
|
80
|
-
case '<=':
|
|
81
|
-
return sqb.Lte(left, right);
|
|
82
|
-
case 'in':
|
|
83
|
-
return sqb.In(left, right);
|
|
84
|
-
case '!in':
|
|
85
|
-
return sqb.Nin(left, right);
|
|
86
70
|
case 'like':
|
|
87
|
-
return
|
|
71
|
+
return sql.Like(left, String(right).replace(/\*/g, '%'));
|
|
88
72
|
case 'ilike':
|
|
89
|
-
return
|
|
73
|
+
return sql.ILike(left, String(right).replace(/\*/g, '%'));
|
|
90
74
|
case '!like':
|
|
91
|
-
return
|
|
75
|
+
return sql.NotLike(left, String(right).replace(/\*/g, '%'));
|
|
92
76
|
case '!ilike':
|
|
93
|
-
return
|
|
94
|
-
default:
|
|
95
|
-
throw new Error(`ComparisonExpression operator (${ast.op}) not implemented yet`);
|
|
77
|
+
return sql.NotILike(left, String(right).replace(/\*/g, '%'));
|
|
96
78
|
}
|
|
79
|
+
const fn = Operators[ast.op];
|
|
80
|
+
if (fn)
|
|
81
|
+
return fn(left, right);
|
|
82
|
+
throw new Error(`ComparisonExpression operator (${ast.op}) not implemented yet`);
|
|
97
83
|
}
|
|
98
84
|
if (ast instanceof OpraFilter.QualifiedIdentifier ||
|
|
99
85
|
ast instanceof OpraFilter.Literal) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opra/sqb",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.27.0",
|
|
4
4
|
"description": "Opra SQB adapter package",
|
|
5
5
|
"author": "Panates",
|
|
6
6
|
"license": "MIT",
|
|
@@ -10,8 +10,8 @@
|
|
|
10
10
|
"valgen": "^6.0.3"
|
|
11
11
|
},
|
|
12
12
|
"peerDependencies": {
|
|
13
|
-
"@opra/core": "^1.
|
|
14
|
-
"@opra/http": "^1.
|
|
13
|
+
"@opra/core": "^1.27.0",
|
|
14
|
+
"@opra/http": "^1.27.0",
|
|
15
15
|
"@sqb/builder": ">4.0.0 <5",
|
|
16
16
|
"@sqb/connect": ">4.0.0 <5"
|
|
17
17
|
},
|
package/sqb-entity-service.d.ts
CHANGED
|
@@ -163,9 +163,10 @@ export declare class SqbEntityService<T extends object = object> extends SqbServ
|
|
|
163
163
|
* Called whenever a command throws. Useful for logging or transforming errors.
|
|
164
164
|
*
|
|
165
165
|
* @param error - The thrown error.
|
|
166
|
+
* @param command - The service command during which the error was thrown.
|
|
166
167
|
* @param _this - The service instance.
|
|
167
168
|
*/
|
|
168
|
-
onError?: (error: unknown, _this: any) => void | Promise<void>;
|
|
169
|
+
onError?: (error: unknown, command: SqbEntityService.CommandInfo, _this: any) => void | Promise<void>;
|
|
169
170
|
/**
|
|
170
171
|
* Constructs a new instance.
|
|
171
172
|
*
|
package/sqb-entity-service.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { InternalServerError } from '@opra/common';
|
|
2
|
-
import {
|
|
2
|
+
import { sql } from '@sqb/builder';
|
|
3
3
|
import { EntityMetadata } from '@sqb/connect';
|
|
4
4
|
import { isNotNullish } from 'valgen';
|
|
5
5
|
import { SQBAdapter } from './sqb-adapter.js';
|
|
@@ -35,6 +35,7 @@ export class SqbEntityService extends SqbServiceBase {
|
|
|
35
35
|
* Called whenever a command throws. Useful for logging or transforming errors.
|
|
36
36
|
*
|
|
37
37
|
* @param error - The thrown error.
|
|
38
|
+
* @param command - The service command during which the error was thrown.
|
|
38
39
|
* @param _this - The service instance.
|
|
39
40
|
*/
|
|
40
41
|
onError;
|
|
@@ -121,6 +122,7 @@ export class SqbEntityService extends SqbServiceBase {
|
|
|
121
122
|
* @param operation - The operation name.
|
|
122
123
|
*/
|
|
123
124
|
getInputCodec(operation) {
|
|
125
|
+
const dataType = this.dataType;
|
|
124
126
|
const cacheKey = operation + (this._dataTypeScope ? ':' + this._dataTypeScope : '');
|
|
125
127
|
let validator = this._inputCodecs[cacheKey];
|
|
126
128
|
if (validator)
|
|
@@ -131,7 +133,6 @@ export class SqbEntityService extends SqbServiceBase {
|
|
|
131
133
|
};
|
|
132
134
|
if (operation === 'update')
|
|
133
135
|
options.partial = 'deep';
|
|
134
|
-
const dataType = this.dataType;
|
|
135
136
|
validator = dataType.generateCodec('decode', options);
|
|
136
137
|
this._inputCodecs[cacheKey] = validator;
|
|
137
138
|
return validator;
|
|
@@ -142,6 +143,7 @@ export class SqbEntityService extends SqbServiceBase {
|
|
|
142
143
|
* @param operation - The operation name.
|
|
143
144
|
*/
|
|
144
145
|
getOutputCodec(operation) {
|
|
146
|
+
const dataType = this.dataType;
|
|
145
147
|
const cacheKey = operation + (this._dataTypeScope ? ':' + this._dataTypeScope : '');
|
|
146
148
|
let validator = this._outputCodecs[cacheKey];
|
|
147
149
|
if (validator)
|
|
@@ -151,7 +153,6 @@ export class SqbEntityService extends SqbServiceBase {
|
|
|
151
153
|
partial: 'deep',
|
|
152
154
|
scope: this._dataTypeScope,
|
|
153
155
|
};
|
|
154
|
-
const dataType = this.dataType;
|
|
155
156
|
validator = dataType.generateCodec('decode', options);
|
|
156
157
|
this._outputCodecs[cacheKey] = validator;
|
|
157
158
|
return validator;
|
|
@@ -540,7 +541,7 @@ export class SqbEntityService extends SqbServiceBase {
|
|
|
540
541
|
? this.commonFilter
|
|
541
542
|
: [this.commonFilter];
|
|
542
543
|
const mapped = commonFilter.map(f => typeof f === 'function' ? f(command, this) : f);
|
|
543
|
-
return mapped.length > 1 ?
|
|
544
|
+
return mapped.length > 1 ? sql.And(...mapped) : mapped[0];
|
|
544
545
|
}
|
|
545
546
|
async _executeCommand(command, commandFn) {
|
|
546
547
|
let proto;
|
|
@@ -594,7 +595,7 @@ export class SqbEntityService extends SqbServiceBase {
|
|
|
594
595
|
}
|
|
595
596
|
catch (e) {
|
|
596
597
|
Error.captureStackTrace(e, this._executeCommand);
|
|
597
|
-
await this.onError?.(e, this);
|
|
598
|
+
await this.onError?.(e, command, this);
|
|
598
599
|
throw e;
|
|
599
600
|
}
|
|
600
601
|
}
|