@opra/mongodb 1.21.0 → 1.22.1
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/package.json +12 -29
- package/{esm/services → services}/mongo-collection-service.js +6 -0
- package/{esm/services → services}/mongo-nested-service.js +24 -0
- package/{esm/services → services}/mongo-service.js +44 -2
- package/{esm/services → services}/mongo-singleton-service.js +5 -0
- package/cjs/adapter/mongo-adapter.js +0 -127
- package/cjs/adapter/mongo-patch-generator.js +0 -215
- package/cjs/adapter/prepare-filter.js +0 -190
- package/cjs/adapter/prepare-key-values.js +0 -21
- package/cjs/adapter/prepare-projection.js +0 -58
- package/cjs/adapter/prepare-sort.js +0 -17
- package/cjs/index.js +0 -11
- package/cjs/package.json +0 -3
- package/cjs/services/mongo-collection-service.js +0 -401
- package/cjs/services/mongo-entity-service.js +0 -578
- package/cjs/services/mongo-nested-service.js +0 -913
- package/cjs/services/mongo-service.js +0 -282
- package/cjs/services/mongo-singleton-service.js +0 -213
- package/cjs/types.js +0 -2
- package/esm/package.json +0 -3
- package/types/index.d.cts +0 -8
- /package/{types/adapter → adapter}/mongo-adapter.d.ts +0 -0
- /package/{esm/adapter → adapter}/mongo-adapter.js +0 -0
- /package/{types/adapter → adapter}/mongo-patch-generator.d.ts +0 -0
- /package/{esm/adapter → adapter}/mongo-patch-generator.js +0 -0
- /package/{types/adapter → adapter}/prepare-filter.d.ts +0 -0
- /package/{esm/adapter → adapter}/prepare-filter.js +0 -0
- /package/{types/adapter → adapter}/prepare-key-values.d.ts +0 -0
- /package/{esm/adapter → adapter}/prepare-key-values.js +0 -0
- /package/{types/adapter → adapter}/prepare-projection.d.ts +0 -0
- /package/{esm/adapter → adapter}/prepare-projection.js +0 -0
- /package/{types/adapter → adapter}/prepare-sort.d.ts +0 -0
- /package/{esm/adapter → adapter}/prepare-sort.js +0 -0
- /package/{types/index.d.ts → index.d.ts} +0 -0
- /package/{esm/index.js → index.js} +0 -0
- /package/{types/services → services}/mongo-collection-service.d.ts +0 -0
- /package/{types/services → services}/mongo-entity-service.d.ts +0 -0
- /package/{esm/services → services}/mongo-entity-service.js +0 -0
- /package/{types/services → services}/mongo-nested-service.d.ts +0 -0
- /package/{types/services → services}/mongo-service.d.ts +0 -0
- /package/{types/services → services}/mongo-singleton-service.d.ts +0 -0
- /package/{types/types.d.ts → types.d.ts} +0 -0
- /package/{esm/types.js → types.js} +0 -0
|
@@ -1,190 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.default = prepareFilter;
|
|
4
|
-
const common_1 = require("@opra/common");
|
|
5
|
-
const opMap = {
|
|
6
|
-
'=': '$eq',
|
|
7
|
-
'!=': '$ne',
|
|
8
|
-
'>': '$gt',
|
|
9
|
-
'>=': '$gte',
|
|
10
|
-
'<': '$lt',
|
|
11
|
-
'<=': '$lte',
|
|
12
|
-
in: '$in',
|
|
13
|
-
'!in': '$nin',
|
|
14
|
-
};
|
|
15
|
-
/**
|
|
16
|
-
* Prepare the MongoDB filter based on the provided filters and options.
|
|
17
|
-
*
|
|
18
|
-
* @param {FilterInput|FilterInput[]} filters - The filter(s) to be applied.
|
|
19
|
-
* @param {Object} [options] - Additional options.
|
|
20
|
-
* @param {string} [options.fieldPrefix] - The prefix to be added to field names.
|
|
21
|
-
*
|
|
22
|
-
* @returns {mongodb.Filter<any>} - The prepared MongoDB filter.
|
|
23
|
-
*/
|
|
24
|
-
function prepareFilter(filters, options) {
|
|
25
|
-
const filtersArray = Array.isArray(filters) ? filters : [filters];
|
|
26
|
-
if (!filtersArray.length)
|
|
27
|
-
return {};
|
|
28
|
-
let i = 0;
|
|
29
|
-
const out = {};
|
|
30
|
-
for (const filter of filtersArray) {
|
|
31
|
-
if (!filter)
|
|
32
|
-
continue;
|
|
33
|
-
let x;
|
|
34
|
-
if (typeof filter === 'string')
|
|
35
|
-
x = prepareFilterAst(common_1.OpraFilter.parse(filter));
|
|
36
|
-
else if (filter instanceof common_1.OpraFilter.Expression)
|
|
37
|
-
x = prepareFilterAst(filter);
|
|
38
|
-
else
|
|
39
|
-
x = filter;
|
|
40
|
-
if (Array.isArray(x))
|
|
41
|
-
x = { $and: out };
|
|
42
|
-
// Merge $and arrays
|
|
43
|
-
if (x.$and) {
|
|
44
|
-
out.$and = out.$and || [];
|
|
45
|
-
out.$and.push(...x.$and);
|
|
46
|
-
delete x.$and;
|
|
47
|
-
}
|
|
48
|
-
// Merge $or arrays
|
|
49
|
-
if (x.$or) {
|
|
50
|
-
out.$or = out.$or || [];
|
|
51
|
-
out.$or.push(...x.$or);
|
|
52
|
-
delete x.$or;
|
|
53
|
-
}
|
|
54
|
-
for (const k of Object.keys(x)) {
|
|
55
|
-
// If result object has filter field we convert it to $and
|
|
56
|
-
if (out[k]) {
|
|
57
|
-
out.$and = out.$and || [];
|
|
58
|
-
out.$and.push({ [k]: out[k] });
|
|
59
|
-
out.$and.push({ [k]: x[k] });
|
|
60
|
-
delete out[k];
|
|
61
|
-
continue;
|
|
62
|
-
}
|
|
63
|
-
out[k] = x[k];
|
|
64
|
-
}
|
|
65
|
-
i++;
|
|
66
|
-
}
|
|
67
|
-
return i
|
|
68
|
-
? options?.fieldPrefix
|
|
69
|
-
? addPrefix(out, options.fieldPrefix)
|
|
70
|
-
: out
|
|
71
|
-
: undefined;
|
|
72
|
-
}
|
|
73
|
-
function addPrefix(source, prefix) {
|
|
74
|
-
if (typeof source !== 'object')
|
|
75
|
-
return source;
|
|
76
|
-
if (Array.isArray(source))
|
|
77
|
-
return source.map(v => addPrefix(v, prefix));
|
|
78
|
-
const out = {};
|
|
79
|
-
for (const [k, v] of Object.entries(source)) {
|
|
80
|
-
if (k.startsWith('$'))
|
|
81
|
-
out[k] = addPrefix(v, prefix);
|
|
82
|
-
else
|
|
83
|
-
out[prefix + k] = addPrefix(v, prefix);
|
|
84
|
-
}
|
|
85
|
-
return out;
|
|
86
|
-
}
|
|
87
|
-
function prepareFilterAst(ast, negative) {
|
|
88
|
-
if (!ast)
|
|
89
|
-
return;
|
|
90
|
-
if (ast instanceof common_1.OpraFilter.ArrayExpression) {
|
|
91
|
-
return ast.items.map(x => prepareFilterAst(x, negative));
|
|
92
|
-
}
|
|
93
|
-
if (ast instanceof common_1.OpraFilter.NegativeExpression) {
|
|
94
|
-
return prepareFilterAst(ast.expression, !negative);
|
|
95
|
-
}
|
|
96
|
-
if (ast instanceof common_1.OpraFilter.LogicalExpression) {
|
|
97
|
-
const items = ast.items
|
|
98
|
-
.map(x => prepareFilterAst(x, negative))
|
|
99
|
-
.filter(x => x != null);
|
|
100
|
-
if (ast.op === 'or')
|
|
101
|
-
return { $or: items };
|
|
102
|
-
return { $and: items };
|
|
103
|
-
}
|
|
104
|
-
if (ast instanceof common_1.OpraFilter.ParenthesizedExpression) {
|
|
105
|
-
return prepareFilterAst(ast.expression, negative);
|
|
106
|
-
}
|
|
107
|
-
if (ast instanceof common_1.OpraFilter.ComparisonExpression) {
|
|
108
|
-
const left = prepareFilterAst(ast.left, negative);
|
|
109
|
-
if (ast.right instanceof common_1.OpraFilter.QualifiedIdentifier) {
|
|
110
|
-
const op = opMap[ast.op];
|
|
111
|
-
if (op)
|
|
112
|
-
return { $expr: { [op]: ['$' + left, '$' + ast.right.value] } };
|
|
113
|
-
/* istanbul ignore next */
|
|
114
|
-
throw new Error(`Invalid filter query.`);
|
|
115
|
-
}
|
|
116
|
-
let right = prepareFilterAst(ast.right);
|
|
117
|
-
if (right == null) {
|
|
118
|
-
const op = ast.op === '=' ? (negative ? '!=' : '=') : negative ? '=' : '!=';
|
|
119
|
-
if (op === '=')
|
|
120
|
-
return { $or: [{ [left]: null }, { [left]: { $exists: false } }] };
|
|
121
|
-
if (op === '!=')
|
|
122
|
-
return {
|
|
123
|
-
$and: [{ $ne: { [left]: null } }, { [left]: { $exists: true } }],
|
|
124
|
-
};
|
|
125
|
-
}
|
|
126
|
-
if (ast.prepare) {
|
|
127
|
-
const x = ast.prepare({
|
|
128
|
-
left,
|
|
129
|
-
right,
|
|
130
|
-
op: ast.op,
|
|
131
|
-
adapter: 'mongodb',
|
|
132
|
-
});
|
|
133
|
-
if (x)
|
|
134
|
-
return x;
|
|
135
|
-
}
|
|
136
|
-
const mngOp = opMap[ast.op];
|
|
137
|
-
if (mngOp) {
|
|
138
|
-
if (ast.op === 'in' || ast.op === '!in')
|
|
139
|
-
right = Array.isArray(right) ? right : [right];
|
|
140
|
-
if (ast.op === '=' && !negative)
|
|
141
|
-
return { [left]: right };
|
|
142
|
-
return { [left]: wrapNot({ [mngOp]: right }, negative) };
|
|
143
|
-
}
|
|
144
|
-
switch (ast.op) {
|
|
145
|
-
case 'like':
|
|
146
|
-
return {
|
|
147
|
-
[left]: wrapNot({
|
|
148
|
-
$text: {
|
|
149
|
-
$search: '\\"' + right.replace(/\\"/, '"') + '\\"',
|
|
150
|
-
$caseSensitive: true,
|
|
151
|
-
},
|
|
152
|
-
}, negative),
|
|
153
|
-
};
|
|
154
|
-
case 'ilike':
|
|
155
|
-
return {
|
|
156
|
-
[left]: wrapNot({
|
|
157
|
-
$text: {
|
|
158
|
-
$search: '\\"' + right.replace(/\\"/, '"') + '\\"',
|
|
159
|
-
},
|
|
160
|
-
}, negative),
|
|
161
|
-
};
|
|
162
|
-
case '!like':
|
|
163
|
-
return {
|
|
164
|
-
[left]: wrapNot({
|
|
165
|
-
$text: {
|
|
166
|
-
$search: '\\"' + right.replace(/\\"/, '"') + '\\"',
|
|
167
|
-
$caseSensitive: true,
|
|
168
|
-
},
|
|
169
|
-
}, !negative),
|
|
170
|
-
};
|
|
171
|
-
case '!ilike':
|
|
172
|
-
return {
|
|
173
|
-
[left]: wrapNot({
|
|
174
|
-
$text: {
|
|
175
|
-
$search: '\\"' + right.replace(/\\"/, '"') + '\\"',
|
|
176
|
-
},
|
|
177
|
-
}, !negative),
|
|
178
|
-
};
|
|
179
|
-
default:
|
|
180
|
-
break;
|
|
181
|
-
}
|
|
182
|
-
throw new Error(`Unimplemented ComparisonExpression operation (right side is ${ast.right.kind})`);
|
|
183
|
-
}
|
|
184
|
-
if (ast instanceof common_1.OpraFilter.QualifiedIdentifier ||
|
|
185
|
-
ast instanceof common_1.OpraFilter.Literal) {
|
|
186
|
-
return ast.value;
|
|
187
|
-
}
|
|
188
|
-
throw new Error(`${ast.kind} is not implemented yet`);
|
|
189
|
-
}
|
|
190
|
-
const wrapNot = (o, negative) => (negative ? { $not: o } : o);
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.default = prepareKeyValues;
|
|
4
|
-
const objects_1 = require("@jsopen/objects");
|
|
5
|
-
const defaultPrimaryKey = ['_id'];
|
|
6
|
-
function prepareKeyValues(keyValue, primaryKey) {
|
|
7
|
-
primaryKey = primaryKey || defaultPrimaryKey;
|
|
8
|
-
const b = (0, objects_1.isPlainObject)(keyValue);
|
|
9
|
-
if (primaryKey.length > 1 && !b) {
|
|
10
|
-
throw new TypeError(`Argument "keyValue" must be an object that contains all key values`);
|
|
11
|
-
}
|
|
12
|
-
if (primaryKey.length > 1 || b) {
|
|
13
|
-
return primaryKey.reduce((o, k) => {
|
|
14
|
-
o[k] = keyValue[k];
|
|
15
|
-
if (o[k] == null)
|
|
16
|
-
throw new Error(`Value of key "${k}" is required`);
|
|
17
|
-
return o;
|
|
18
|
-
}, {});
|
|
19
|
-
}
|
|
20
|
-
return { [primaryKey[0]]: keyValue };
|
|
21
|
-
}
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.default = prepareProjection;
|
|
4
|
-
exports.prepare = prepare;
|
|
5
|
-
const common_1 = require("@opra/common");
|
|
6
|
-
function prepareProjection(dataType, projection, scope) {
|
|
7
|
-
if (projection === '*')
|
|
8
|
-
return undefined;
|
|
9
|
-
if (projection &&
|
|
10
|
-
typeof projection === 'object' &&
|
|
11
|
-
!Array.isArray(projection))
|
|
12
|
-
return projection;
|
|
13
|
-
const out = {};
|
|
14
|
-
const projection_ = typeof projection === 'string' || Array.isArray(projection)
|
|
15
|
-
? (0, common_1.parseFieldsProjection)(projection)
|
|
16
|
-
: projection;
|
|
17
|
-
prepare(dataType, out, projection_, scope);
|
|
18
|
-
return Object.keys(out).length ? out : undefined;
|
|
19
|
-
}
|
|
20
|
-
function prepare(dataType, target, projection, scope) {
|
|
21
|
-
const defaultFields = !projection || !Object.values(projection).find(p => !p.sign);
|
|
22
|
-
const projectionKeys = projection && Object.keys(projection);
|
|
23
|
-
const projectionKeysSet = new Set(projectionKeys);
|
|
24
|
-
let fieldName;
|
|
25
|
-
let field;
|
|
26
|
-
let k;
|
|
27
|
-
/** Add fields from data type */
|
|
28
|
-
for (field of dataType.fields(scope)) {
|
|
29
|
-
fieldName = field.name;
|
|
30
|
-
k = fieldName.toLowerCase();
|
|
31
|
-
projectionKeysSet.delete(k);
|
|
32
|
-
const p = projection?.[k];
|
|
33
|
-
if (
|
|
34
|
-
/** Ignore if field is omitted */
|
|
35
|
-
p?.sign === '-' ||
|
|
36
|
-
/** Ignore if defaultFields is false and field is not in projection */
|
|
37
|
-
(!defaultFields && !p) ||
|
|
38
|
-
/** Ignore if defaultFields is true and fields is exclusive */
|
|
39
|
-
(defaultFields && field.exclusive && !p)) {
|
|
40
|
-
continue;
|
|
41
|
-
}
|
|
42
|
-
if (field.type instanceof common_1.ComplexType &&
|
|
43
|
-
typeof p?.projection === 'object') {
|
|
44
|
-
target[fieldName] = {};
|
|
45
|
-
prepare(field.type, target[fieldName], p.projection);
|
|
46
|
-
continue;
|
|
47
|
-
}
|
|
48
|
-
target[fieldName] = 1;
|
|
49
|
-
}
|
|
50
|
-
/** Add additional fields */
|
|
51
|
-
if (dataType.additionalFields) {
|
|
52
|
-
for (k of projectionKeysSet.values()) {
|
|
53
|
-
const n = projection?.[k];
|
|
54
|
-
if (n?.sign !== '-')
|
|
55
|
-
target[k] = 1;
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.default = prepareSort;
|
|
4
|
-
function prepareSort(sort) {
|
|
5
|
-
if (!(sort && sort.length))
|
|
6
|
-
return;
|
|
7
|
-
const out = {};
|
|
8
|
-
sort.forEach(k => {
|
|
9
|
-
if (k.startsWith('-'))
|
|
10
|
-
out[k.substring(1)] = -1;
|
|
11
|
-
else if (k.startsWith('+'))
|
|
12
|
-
out[k.substring(1)] = 1;
|
|
13
|
-
else
|
|
14
|
-
out[k] = 1;
|
|
15
|
-
});
|
|
16
|
-
return out;
|
|
17
|
-
}
|
package/cjs/index.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const tslib_1 = require("tslib");
|
|
4
|
-
tslib_1.__exportStar(require("./adapter/mongo-adapter.js"), exports);
|
|
5
|
-
tslib_1.__exportStar(require("./adapter/mongo-patch-generator.js"), exports);
|
|
6
|
-
tslib_1.__exportStar(require("./services/mongo-collection-service.js"), exports);
|
|
7
|
-
tslib_1.__exportStar(require("./services/mongo-entity-service.js"), exports);
|
|
8
|
-
tslib_1.__exportStar(require("./services/mongo-nested-service.js"), exports);
|
|
9
|
-
tslib_1.__exportStar(require("./services/mongo-service.js"), exports);
|
|
10
|
-
tslib_1.__exportStar(require("./services/mongo-singleton-service.js"), exports);
|
|
11
|
-
tslib_1.__exportStar(require("./types.js"), exports);
|
package/cjs/package.json
DELETED