cry-synced-db-client 0.1.150 → 0.1.151
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/dist/index.js +29 -16
- package/dist/src/utils/localQuery.d.ts +4 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -69,16 +69,13 @@ function matchesQuery(item, query) {
|
|
|
69
69
|
function matchesCondition(item, key, condition) {
|
|
70
70
|
const value = getNestedValue(item, key);
|
|
71
71
|
if (condition === null || typeof condition !== "object") {
|
|
72
|
-
return value
|
|
72
|
+
return scalarMatches(value, condition);
|
|
73
73
|
}
|
|
74
74
|
if (condition instanceof Date) {
|
|
75
|
-
|
|
76
|
-
return value.getTime() === condition.getTime();
|
|
77
|
-
}
|
|
78
|
-
return false;
|
|
75
|
+
return scalarMatches(value, condition);
|
|
79
76
|
}
|
|
80
77
|
if (Array.isArray(condition)) {
|
|
81
|
-
return condition.
|
|
78
|
+
return condition.some((c) => scalarMatches(value, c));
|
|
82
79
|
}
|
|
83
80
|
for (const [op, opValue] of Object.entries(condition)) {
|
|
84
81
|
if (!matchesOperator(value, op, opValue)) {
|
|
@@ -90,29 +87,32 @@ function matchesCondition(item, key, condition) {
|
|
|
90
87
|
function matchesOperator(value, operator, operand) {
|
|
91
88
|
switch (operator) {
|
|
92
89
|
case "$eq":
|
|
93
|
-
return
|
|
90
|
+
return scalarMatches(value, operand);
|
|
94
91
|
case "$ne":
|
|
95
|
-
return !
|
|
92
|
+
return !scalarMatches(value, operand);
|
|
96
93
|
case "$gt":
|
|
97
|
-
return value > operand;
|
|
94
|
+
return compareScalarOrArray(value, (v) => v > operand);
|
|
98
95
|
case "$gte":
|
|
99
|
-
return value >= operand;
|
|
96
|
+
return compareScalarOrArray(value, (v) => v >= operand);
|
|
100
97
|
case "$lt":
|
|
101
|
-
return value < operand;
|
|
98
|
+
return compareScalarOrArray(value, (v) => v < operand);
|
|
102
99
|
case "$lte":
|
|
103
|
-
return value <= operand;
|
|
100
|
+
return compareScalarOrArray(value, (v) => v <= operand);
|
|
104
101
|
case "$in":
|
|
105
102
|
if (!Array.isArray(operand)) return false;
|
|
106
|
-
return operand.some((item) =>
|
|
103
|
+
return operand.some((item) => scalarMatches(value, item));
|
|
107
104
|
case "$nin":
|
|
108
105
|
if (!Array.isArray(operand)) return true;
|
|
109
|
-
return !operand.some((item) =>
|
|
106
|
+
return !operand.some((item) => scalarMatches(value, item));
|
|
110
107
|
case "$exists":
|
|
111
108
|
return operand ? value !== void 0 : value === void 0;
|
|
112
109
|
case "$regex": {
|
|
113
|
-
if (typeof value !== "string") return false;
|
|
114
110
|
const regex = operand instanceof RegExp ? operand : new RegExp(operand);
|
|
115
|
-
return regex.test(value);
|
|
111
|
+
if (typeof value === "string") return regex.test(value);
|
|
112
|
+
if (Array.isArray(value)) {
|
|
113
|
+
return value.some((v) => typeof v === "string" && regex.test(v));
|
|
114
|
+
}
|
|
115
|
+
return false;
|
|
116
116
|
}
|
|
117
117
|
case "$elemMatch":
|
|
118
118
|
if (!Array.isArray(value)) return false;
|
|
@@ -152,6 +152,19 @@ function getNestedValue(obj, path) {
|
|
|
152
152
|
}
|
|
153
153
|
return current;
|
|
154
154
|
}
|
|
155
|
+
function scalarMatches(value, scalar) {
|
|
156
|
+
if (equals(value, scalar)) return true;
|
|
157
|
+
if (Array.isArray(value)) {
|
|
158
|
+
return value.some((elem) => equals(elem, scalar));
|
|
159
|
+
}
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
function compareScalarOrArray(value, predicate) {
|
|
163
|
+
if (Array.isArray(value)) {
|
|
164
|
+
return value.some((v) => predicate(v));
|
|
165
|
+
}
|
|
166
|
+
return predicate(value);
|
|
167
|
+
}
|
|
155
168
|
function equals(a, b) {
|
|
156
169
|
var _a, _b;
|
|
157
170
|
if (a === b) return true;
|
|
@@ -5,6 +5,10 @@ import type { DbEntity } from "../types/DbEntity";
|
|
|
5
5
|
* Podpira polje-operatorje: $eq, $ne, $gt, $gte, $lt, $lte, $in, $nin,
|
|
6
6
|
* $exists, $regex, $elemMatch, $size, $all — in logične operatorje
|
|
7
7
|
* $and, $or, $nor na top-levelu.
|
|
8
|
+
*
|
|
9
|
+
* Skalarni operandi se ujemajo tudi z array-polji (MongoDB semantika):
|
|
10
|
+
* `{ a: "ok" }` matcha tako `{ a: "ok" }` kot `{ a: ["ok", "more"] }`.
|
|
11
|
+
* Velja za neposredno enakost, $eq, $ne, $in, $nin, $gt/$gte/$lt/$lte in $regex.
|
|
8
12
|
*/
|
|
9
13
|
export declare function matchesQuery<T extends DbEntity>(item: T, query: QuerySpec<T>): boolean;
|
|
10
14
|
/**
|