@opra/sqb 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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Panates
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # @opra/I18n
2
+
3
+ OPRA i18n package.
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.convertFilter = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const url_1 = require("@opra/url");
6
+ const sqb = tslib_1.__importStar(require("@sqb/builder"));
7
+ function convertFilter(str) {
8
+ const ast = typeof str === 'string' ? (0, url_1.parseFilter)(str) : str;
9
+ if (!ast)
10
+ return;
11
+ if (ast instanceof url_1.ComparisonExpression) {
12
+ const left = convertFilter(ast.left);
13
+ const right = convertFilter(ast.right);
14
+ switch (ast.op) {
15
+ case '=':
16
+ return sqb.Eq(left, right);
17
+ case '!=':
18
+ return sqb.Ne(left, right);
19
+ case '>':
20
+ return sqb.Gt(left, right);
21
+ case '>=':
22
+ return sqb.Gte(left, right);
23
+ case '<':
24
+ return sqb.Lt(left, right);
25
+ case '<=':
26
+ return sqb.Lte(left, right);
27
+ case 'in':
28
+ return sqb.In(left, right);
29
+ case '!in':
30
+ return sqb.Nin(left, right);
31
+ case 'like':
32
+ return sqb.Like(left, right);
33
+ case 'ilike':
34
+ return sqb.Ilike(left, right);
35
+ case '!like':
36
+ return sqb.NotLike(left, right);
37
+ case '!ilike':
38
+ return sqb.NotILike(left, right);
39
+ default:
40
+ throw new Error(`ComparisonExpression operator (${ast.op}) not implemented yet`);
41
+ }
42
+ }
43
+ if (ast instanceof url_1.QualifiedIdentifier) {
44
+ return sqb.Field(ast.value);
45
+ }
46
+ if (ast instanceof url_1.NumberLiteral ||
47
+ ast instanceof url_1.StringLiteral ||
48
+ ast instanceof url_1.BooleanLiteral ||
49
+ ast instanceof url_1.NullLiteral ||
50
+ ast instanceof url_1.DateLiteral ||
51
+ ast instanceof url_1.TimeLiteral) {
52
+ return ast.value;
53
+ }
54
+ if (ast instanceof url_1.ArrayExpression) {
55
+ return ast.items.map(convertFilter);
56
+ }
57
+ if (ast instanceof url_1.LogicalExpression) {
58
+ if (ast.op === 'or')
59
+ return sqb.Or(...ast.items.map(convertFilter));
60
+ return sqb.And(...ast.items.map(convertFilter));
61
+ }
62
+ if (ast instanceof url_1.ArrayExpression) {
63
+ return ast.items.map(convertFilter);
64
+ }
65
+ if (ast instanceof url_1.ParenthesesExpression) {
66
+ return convertFilter(ast.expression);
67
+ }
68
+ throw new Error(`${ast.type} is not implemented yet`);
69
+ }
70
+ exports.convertFilter = convertFilter;
package/cjs/index.js ADDED
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ tslib_1.__exportStar(require("./sqb-adapter.js"), exports);
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }
@@ -0,0 +1,131 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SQBAdapter = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const lodash_1 = tslib_1.__importDefault(require("lodash"));
6
+ const convert_filter_js_1 = require("./convert-filter.js");
7
+ var SQBAdapter;
8
+ (function (SQBAdapter) {
9
+ function prepare(request) {
10
+ const { query } = request;
11
+ switch (query.queryType) {
12
+ case 'create': {
13
+ const options = lodash_1.default.omitBy({
14
+ pick: query.pick?.length ? query.pick : undefined,
15
+ omit: query.omit?.length ? query.omit : undefined,
16
+ include: query.include?.length ? query.include : undefined,
17
+ }, lodash_1.default.isNil);
18
+ const { data } = query;
19
+ return {
20
+ method: 'create',
21
+ values: data,
22
+ options,
23
+ args: [data, options]
24
+ };
25
+ }
26
+ case 'read': {
27
+ const options = lodash_1.default.omitBy({
28
+ pick: query.pick?.length ? query.pick : undefined,
29
+ omit: query.omit?.length ? query.omit : undefined,
30
+ include: query.include?.length ? query.include : undefined,
31
+ }, lodash_1.default.isNil);
32
+ const keyValue = query.keyValue;
33
+ return {
34
+ method: 'findByPk',
35
+ keyValue,
36
+ options,
37
+ args: [keyValue, options]
38
+ };
39
+ }
40
+ case 'search': {
41
+ const options = lodash_1.default.omitBy({
42
+ pick: query.pick?.length ? query.pick : undefined,
43
+ omit: query.omit?.length ? query.omit : undefined,
44
+ include: query.include?.length ? query.include : undefined,
45
+ sort: query.sort?.length ? query.sort : undefined,
46
+ limit: query.limit,
47
+ offset: query.skip,
48
+ distinct: query.distinct,
49
+ total: query.total,
50
+ filter: (0, convert_filter_js_1.convertFilter)(query.filter)
51
+ }, lodash_1.default.isNil);
52
+ return {
53
+ method: 'findAll',
54
+ options,
55
+ args: [options]
56
+ };
57
+ }
58
+ case 'update': {
59
+ const options = lodash_1.default.omitBy({
60
+ pick: query.pick?.length ? query.pick : undefined,
61
+ omit: query.omit?.length ? query.omit : undefined,
62
+ include: query.include?.length ? query.include : undefined,
63
+ }, lodash_1.default.isNil);
64
+ const { data } = query;
65
+ const keyValue = query.keyValue;
66
+ return {
67
+ method: 'update',
68
+ keyValue: query.keyValue,
69
+ values: data,
70
+ options,
71
+ args: [keyValue, data, options]
72
+ };
73
+ }
74
+ case 'updateMany': {
75
+ const options = lodash_1.default.omitBy({
76
+ filter: (0, convert_filter_js_1.convertFilter)(query.filter)
77
+ }, lodash_1.default.isNil);
78
+ const { data } = query;
79
+ return {
80
+ method: 'updateAll',
81
+ options,
82
+ args: [data, options]
83
+ };
84
+ }
85
+ case 'delete': {
86
+ const options = {};
87
+ const keyValue = query.keyValue;
88
+ return {
89
+ method: 'destroy',
90
+ keyValue,
91
+ options,
92
+ args: [keyValue, options]
93
+ };
94
+ }
95
+ case 'deleteMany': {
96
+ const options = lodash_1.default.omitBy({
97
+ filter: (0, convert_filter_js_1.convertFilter)(query.filter)
98
+ }, lodash_1.default.isNil);
99
+ return {
100
+ method: 'destroyAll',
101
+ options,
102
+ args: [options]
103
+ };
104
+ }
105
+ default:
106
+ throw new Error(`Unimplemented query type "${query.queryType}"`);
107
+ }
108
+ }
109
+ SQBAdapter.prepare = prepare;
110
+ async function execute(connection, request) {
111
+ const { query } = request;
112
+ const repo = connection.getRepository(query.resource.dataType.ctor);
113
+ const prepared = prepare(request);
114
+ const out = {};
115
+ // @ts-ignore
116
+ const value = await repo[prepared.method](...prepared.args);
117
+ if (value && typeof value === 'object')
118
+ out.value = value;
119
+ if (query.queryType === 'search') {
120
+ if (query.total) {
121
+ out.count = await repo.count(prepared.options);
122
+ }
123
+ }
124
+ if (prepared.method === 'destroy')
125
+ out.affected = value ? 1 : 0;
126
+ else if (prepared.method === 'destroyAll' || prepared.method === 'updateAll')
127
+ out.affected = value;
128
+ return out;
129
+ }
130
+ SQBAdapter.execute = execute;
131
+ })(SQBAdapter = exports.SQBAdapter || (exports.SQBAdapter = {}));
@@ -0,0 +1,65 @@
1
+ import { ArrayExpression, BooleanLiteral, ComparisonExpression, DateLiteral, LogicalExpression, NullLiteral, NumberLiteral, ParenthesesExpression, parseFilter, QualifiedIdentifier, StringLiteral, TimeLiteral } from '@opra/url';
2
+ import * as sqb from '@sqb/builder';
3
+ export function convertFilter(str) {
4
+ const ast = typeof str === 'string' ? parseFilter(str) : str;
5
+ if (!ast)
6
+ return;
7
+ if (ast instanceof ComparisonExpression) {
8
+ const left = convertFilter(ast.left);
9
+ const right = convertFilter(ast.right);
10
+ switch (ast.op) {
11
+ case '=':
12
+ return sqb.Eq(left, right);
13
+ case '!=':
14
+ return sqb.Ne(left, right);
15
+ case '>':
16
+ return sqb.Gt(left, right);
17
+ case '>=':
18
+ return sqb.Gte(left, right);
19
+ case '<':
20
+ return sqb.Lt(left, right);
21
+ case '<=':
22
+ return sqb.Lte(left, right);
23
+ case 'in':
24
+ return sqb.In(left, right);
25
+ case '!in':
26
+ return sqb.Nin(left, right);
27
+ case 'like':
28
+ return sqb.Like(left, right);
29
+ case 'ilike':
30
+ return sqb.Ilike(left, right);
31
+ case '!like':
32
+ return sqb.NotLike(left, right);
33
+ case '!ilike':
34
+ return sqb.NotILike(left, right);
35
+ default:
36
+ throw new Error(`ComparisonExpression operator (${ast.op}) not implemented yet`);
37
+ }
38
+ }
39
+ if (ast instanceof QualifiedIdentifier) {
40
+ return sqb.Field(ast.value);
41
+ }
42
+ if (ast instanceof NumberLiteral ||
43
+ ast instanceof StringLiteral ||
44
+ ast instanceof BooleanLiteral ||
45
+ ast instanceof NullLiteral ||
46
+ ast instanceof DateLiteral ||
47
+ ast instanceof TimeLiteral) {
48
+ return ast.value;
49
+ }
50
+ if (ast instanceof ArrayExpression) {
51
+ return ast.items.map(convertFilter);
52
+ }
53
+ if (ast instanceof LogicalExpression) {
54
+ if (ast.op === 'or')
55
+ return sqb.Or(...ast.items.map(convertFilter));
56
+ return sqb.And(...ast.items.map(convertFilter));
57
+ }
58
+ if (ast instanceof ArrayExpression) {
59
+ return ast.items.map(convertFilter);
60
+ }
61
+ if (ast instanceof ParenthesesExpression) {
62
+ return convertFilter(ast.expression);
63
+ }
64
+ throw new Error(`${ast.type} is not implemented yet`);
65
+ }
package/esm/index.js ADDED
@@ -0,0 +1 @@
1
+ export * from './sqb-adapter.js';
@@ -0,0 +1,127 @@
1
+ import _ from 'lodash';
2
+ import { convertFilter } from './convert-filter.js';
3
+ export var SQBAdapter;
4
+ (function (SQBAdapter) {
5
+ function prepare(request) {
6
+ const { query } = request;
7
+ switch (query.queryType) {
8
+ case 'create': {
9
+ const options = _.omitBy({
10
+ pick: query.pick?.length ? query.pick : undefined,
11
+ omit: query.omit?.length ? query.omit : undefined,
12
+ include: query.include?.length ? query.include : undefined,
13
+ }, _.isNil);
14
+ const { data } = query;
15
+ return {
16
+ method: 'create',
17
+ values: data,
18
+ options,
19
+ args: [data, options]
20
+ };
21
+ }
22
+ case 'read': {
23
+ const options = _.omitBy({
24
+ pick: query.pick?.length ? query.pick : undefined,
25
+ omit: query.omit?.length ? query.omit : undefined,
26
+ include: query.include?.length ? query.include : undefined,
27
+ }, _.isNil);
28
+ const keyValue = query.keyValue;
29
+ return {
30
+ method: 'findByPk',
31
+ keyValue,
32
+ options,
33
+ args: [keyValue, options]
34
+ };
35
+ }
36
+ case 'search': {
37
+ const options = _.omitBy({
38
+ pick: query.pick?.length ? query.pick : undefined,
39
+ omit: query.omit?.length ? query.omit : undefined,
40
+ include: query.include?.length ? query.include : undefined,
41
+ sort: query.sort?.length ? query.sort : undefined,
42
+ limit: query.limit,
43
+ offset: query.skip,
44
+ distinct: query.distinct,
45
+ total: query.total,
46
+ filter: convertFilter(query.filter)
47
+ }, _.isNil);
48
+ return {
49
+ method: 'findAll',
50
+ options,
51
+ args: [options]
52
+ };
53
+ }
54
+ case 'update': {
55
+ const options = _.omitBy({
56
+ pick: query.pick?.length ? query.pick : undefined,
57
+ omit: query.omit?.length ? query.omit : undefined,
58
+ include: query.include?.length ? query.include : undefined,
59
+ }, _.isNil);
60
+ const { data } = query;
61
+ const keyValue = query.keyValue;
62
+ return {
63
+ method: 'update',
64
+ keyValue: query.keyValue,
65
+ values: data,
66
+ options,
67
+ args: [keyValue, data, options]
68
+ };
69
+ }
70
+ case 'updateMany': {
71
+ const options = _.omitBy({
72
+ filter: convertFilter(query.filter)
73
+ }, _.isNil);
74
+ const { data } = query;
75
+ return {
76
+ method: 'updateAll',
77
+ options,
78
+ args: [data, options]
79
+ };
80
+ }
81
+ case 'delete': {
82
+ const options = {};
83
+ const keyValue = query.keyValue;
84
+ return {
85
+ method: 'destroy',
86
+ keyValue,
87
+ options,
88
+ args: [keyValue, options]
89
+ };
90
+ }
91
+ case 'deleteMany': {
92
+ const options = _.omitBy({
93
+ filter: convertFilter(query.filter)
94
+ }, _.isNil);
95
+ return {
96
+ method: 'destroyAll',
97
+ options,
98
+ args: [options]
99
+ };
100
+ }
101
+ default:
102
+ throw new Error(`Unimplemented query type "${query.queryType}"`);
103
+ }
104
+ }
105
+ SQBAdapter.prepare = prepare;
106
+ async function execute(connection, request) {
107
+ const { query } = request;
108
+ const repo = connection.getRepository(query.resource.dataType.ctor);
109
+ const prepared = prepare(request);
110
+ const out = {};
111
+ // @ts-ignore
112
+ const value = await repo[prepared.method](...prepared.args);
113
+ if (value && typeof value === 'object')
114
+ out.value = value;
115
+ if (query.queryType === 'search') {
116
+ if (query.total) {
117
+ out.count = await repo.count(prepared.options);
118
+ }
119
+ }
120
+ if (prepared.method === 'destroy')
121
+ out.affected = value ? 1 : 0;
122
+ else if (prepared.method === 'destroyAll' || prepared.method === 'updateAll')
123
+ out.affected = value;
124
+ return out;
125
+ }
126
+ SQBAdapter.execute = execute;
127
+ })(SQBAdapter || (SQBAdapter = {}));
package/package.json ADDED
@@ -0,0 +1,69 @@
1
+ {
2
+ "name": "@opra/sqb",
3
+ "version": "0.0.5",
4
+ "description": "Opra SQB adapter package",
5
+ "author": "Panates",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/panates/opra.git",
10
+ "directory": "packages/sqb"
11
+ },
12
+ "scripts": {
13
+ "compile": "tsc",
14
+ "prebuild": "npm run lint && npm run clean",
15
+ "build": "npm run build:cjs && npm run build:esm",
16
+ "build:cjs": "tsc -b tsconfig-build-cjs.json",
17
+ "build:esm": "tsc -b tsconfig-build-esm.json",
18
+ "postbuild": "cp README.md package.json ../../LICENSE ../../build/sqb && cp ../../package.cjs.json ../../build/sqb/cjs/package.json",
19
+ "lint": "eslint .",
20
+ "test": "jest",
21
+ "cover": "jest --collect-coverage",
22
+ "clean": "npm run clean:src && npm run clean:dist && npm run clean:cover",
23
+ "clean:src": "ts-cleanup -s src --all",
24
+ "clean:dist": "rimraf ../../build/sqb",
25
+ "clean:cover": "rimraf ../../coverage/sqb"
26
+ },
27
+ "dependencies": {
28
+ "lodash": "^4.17.x",
29
+ "ts-gems": "^2.2.0"
30
+ },
31
+ "devDependencies": {
32
+ "@sqb/connect": "^4.5.0"
33
+ },
34
+ "peerDependencies": {
35
+ "@opra/core": "0.x.x",
36
+ "@opra/url": "0.x.x",
37
+ "@opra/optionals": "0.x.x",
38
+ "@sqb/connect": ">= 4.5.0",
39
+ "@sqb/sqljs": ">= 4.5.0"
40
+ },
41
+ "type": "module",
42
+ "main": "cjs/index.js",
43
+ "module": "esm/index.js",
44
+ "types": "esm/index.d.ts",
45
+ "exports": {
46
+ ".": {
47
+ "require": "./cjs/index.js",
48
+ "default": "./esm/index.js"
49
+ },
50
+ "./cjs": "./cjs/index.js",
51
+ "./esm": "./esm/index.js"
52
+ },
53
+ "engines": {
54
+ "node": ">=16.0",
55
+ "npm": ">=7.0.0"
56
+ },
57
+ "files": [
58
+ "bin/",
59
+ "cjs/",
60
+ "esm/",
61
+ "LICENSE",
62
+ "README.md"
63
+ ],
64
+ "keywords": [
65
+ "opra",
66
+ "sqb",
67
+ "adapter"
68
+ ]
69
+ }