@medyll/idae-query 0.45.0 → 0.57.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,8 +1,9 @@
|
|
|
1
|
-
import
|
|
1
|
+
import type { ResultSet } from "../resultSet/resultset.js";
|
|
2
|
+
import { type Operator, type OperatorType, type Where } from "../types.js";
|
|
2
3
|
export declare class Operators {
|
|
3
4
|
#private;
|
|
4
5
|
static operators: (keyof OperatorType)[];
|
|
5
|
-
static parse<T extends object>(data: T[]
|
|
6
|
+
static parse<T extends object | undefined>(data: T[] | ResultSet<T>, qy: Where<T>): T[];
|
|
6
7
|
static filters<F extends object>(fieldName: keyof F | Operator, operator: keyof OperatorType, value: OperatorType[typeof operator], data: F[]): F[];
|
|
7
8
|
static addCustomOperator(operatorName: string, operatorFunction: <T extends object>(fieldName: keyof T, value: any, data: T) => boolean): void;
|
|
8
9
|
}
|
|
@@ -1,19 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
import {} from '../types.js';
|
|
1
|
+
import {} from "../types.js";
|
|
3
2
|
export class Operators {
|
|
4
3
|
static operators = [
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
4
|
+
"eq",
|
|
5
|
+
"gt",
|
|
6
|
+
"gte",
|
|
7
|
+
"lt",
|
|
8
|
+
"lte",
|
|
9
|
+
"ne",
|
|
10
|
+
"in",
|
|
11
|
+
"nin",
|
|
12
|
+
"contains",
|
|
13
|
+
"startsWith",
|
|
14
|
+
"endsWith",
|
|
15
|
+
"btw",
|
|
17
16
|
];
|
|
18
17
|
static #operatorsFunctions = {
|
|
19
18
|
eq: this.#equalityComparison,
|
|
@@ -27,20 +26,20 @@ export class Operators {
|
|
|
27
26
|
contains: this.#containsComparison,
|
|
28
27
|
startsWith: this.#startsWithComparison,
|
|
29
28
|
endsWith: this.#endsWithComparison,
|
|
30
|
-
btw: this.#betweenComparison
|
|
29
|
+
btw: this.#betweenComparison,
|
|
31
30
|
};
|
|
32
31
|
static parse(data, qy) {
|
|
33
32
|
return data.filter((dt) => {
|
|
34
33
|
for (const fieldName in qy) {
|
|
35
34
|
const query = qy[fieldName];
|
|
36
|
-
if (typeof query ===
|
|
35
|
+
if (typeof query === "object" &&
|
|
37
36
|
Operators.operators.includes(Object.keys(query)[0])) {
|
|
38
37
|
for (const key in query) {
|
|
39
38
|
if (Operators.operators.includes(key)) {
|
|
40
39
|
const operator = key;
|
|
41
40
|
let value = query[key];
|
|
42
41
|
// Convertir en Set pour 'in' et 'nin'
|
|
43
|
-
if (operator ===
|
|
42
|
+
if (operator === "in" || operator === "nin") {
|
|
44
43
|
value = new Set(value);
|
|
45
44
|
}
|
|
46
45
|
return this.#operatorsFunctions[operator](fieldName, value, dt);
|
|
@@ -55,7 +54,7 @@ export class Operators {
|
|
|
55
54
|
}
|
|
56
55
|
static filters(fieldName, operator, value, data) {
|
|
57
56
|
// Utilisation de Set pour 'in' et 'nin'
|
|
58
|
-
if (operator ===
|
|
57
|
+
if (operator === "in" || operator === "nin") {
|
|
59
58
|
value = new Set(value);
|
|
60
59
|
}
|
|
61
60
|
// Gestion des cas où fieldName est un opérateur (pour les nouvelles formes de requête)
|
|
@@ -78,10 +77,10 @@ export class Operators {
|
|
|
78
77
|
return false;
|
|
79
78
|
}
|
|
80
79
|
const dataValue = data[fieldName];
|
|
81
|
-
if (typeof dataValue ===
|
|
80
|
+
if (typeof dataValue === "string" && typeof value === "string") {
|
|
82
81
|
const valueStr = value.toString();
|
|
83
|
-
const startsWith = valueStr.startsWith(
|
|
84
|
-
const endsWith = valueStr.endsWith(
|
|
82
|
+
const startsWith = valueStr.startsWith("*");
|
|
83
|
+
const endsWith = valueStr.endsWith("*");
|
|
85
84
|
if (startsWith && endsWith) {
|
|
86
85
|
return dataValue.includes(valueStr.slice(1, -1));
|
|
87
86
|
}
|
|
@@ -151,7 +150,7 @@ export class Operators {
|
|
|
151
150
|
return false;
|
|
152
151
|
}
|
|
153
152
|
const fieldValue = data[fieldName];
|
|
154
|
-
if (typeof fieldValue !==
|
|
153
|
+
if (typeof fieldValue !== "number") {
|
|
155
154
|
console.warn(`Field "${String(fieldName)}" is not a number`);
|
|
156
155
|
return false;
|
|
157
156
|
}
|
|
@@ -1,4 +1,72 @@
|
|
|
1
1
|
import { dotPath } from "../path/pathResolver.js";
|
|
2
|
+
export const getResultsetProxy = (data) => {
|
|
3
|
+
return new Proxy(data, {
|
|
4
|
+
get: (target, prop) => {
|
|
5
|
+
if (prop === "setOptions") {
|
|
6
|
+
return (options) => {
|
|
7
|
+
if (options.sort) {
|
|
8
|
+
target.sortBy(options.sort);
|
|
9
|
+
}
|
|
10
|
+
if (options.groupBy) {
|
|
11
|
+
target.groupBy(options.groupBy);
|
|
12
|
+
}
|
|
13
|
+
return target;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
if (prop === "sortBy") {
|
|
17
|
+
return (args) => {
|
|
18
|
+
const keys = Object.keys(args);
|
|
19
|
+
const values = Object.values(args);
|
|
20
|
+
target.sort((a, b) => {
|
|
21
|
+
let i = 0;
|
|
22
|
+
let result = 0;
|
|
23
|
+
while (i < keys.length && result === 0) {
|
|
24
|
+
let value = keys[i];
|
|
25
|
+
result =
|
|
26
|
+
values[i] === "asc"
|
|
27
|
+
? Number(dotPath(a, value)) - Number(dotPath(b, value))
|
|
28
|
+
: Number(dotPath(b, value)) - Number(dotPath(a, value));
|
|
29
|
+
i++;
|
|
30
|
+
}
|
|
31
|
+
return result;
|
|
32
|
+
});
|
|
33
|
+
delete target?.sortBy;
|
|
34
|
+
return target;
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
if (prop === "groupBy") {
|
|
38
|
+
return (fieldName, transform) => {
|
|
39
|
+
const finalFieldName = typeof fieldName === "string" ? [fieldName] : fieldName;
|
|
40
|
+
return target.reduce((acc, curr) => {
|
|
41
|
+
finalFieldName.forEach((f) => {
|
|
42
|
+
acc += dotPath(curr, f);
|
|
43
|
+
});
|
|
44
|
+
if (!acc[key]) {
|
|
45
|
+
acc[key] = [];
|
|
46
|
+
}
|
|
47
|
+
acc[key].push(curr);
|
|
48
|
+
return acc;
|
|
49
|
+
}, {});
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
if (prop === "getPage") {
|
|
53
|
+
return (page, size) => {
|
|
54
|
+
const start = (page - 1) * size;
|
|
55
|
+
const end = page * size;
|
|
56
|
+
return target.slice(start, end);
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
/* if (prop === "toObject") {
|
|
60
|
+
return (dotPath) => {
|
|
61
|
+
return target.map((item) => {
|
|
62
|
+
return dotPath(item);
|
|
63
|
+
});
|
|
64
|
+
};
|
|
65
|
+
} */
|
|
66
|
+
return target[prop];
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
};
|
|
2
70
|
/**
|
|
3
71
|
* Generates a ResultSet based on the provided data array and defines additional properties like setOptions, sortBy, groupBy, and getPage for customization and manipulation.
|
|
4
72
|
*
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@medyll/idae-query",
|
|
3
3
|
"scope": "@medyll",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.57.0",
|
|
5
5
|
"description": "A powerful and flexible IndexedDB query library for TypeScript and JavaScript applications, featuring a MongoDB-like query interface, strong TypeScript support, reactive state management, and easy integration with front-end frameworks like Svelte.",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"dev": "vite dev",
|
|
@@ -33,24 +33,24 @@
|
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"@medyll/idae-prettier-config": "^1.1.0",
|
|
36
|
-
"@sveltejs/adapter-auto": "^
|
|
37
|
-
"@sveltejs/kit": "^2.
|
|
38
|
-
"@sveltejs/package": "^2.3.
|
|
39
|
-
"@sveltejs/vite-plugin-svelte": "^
|
|
40
|
-
"@types/eslint": "^9.6.
|
|
41
|
-
"eslint": "^9.
|
|
42
|
-
"eslint-config-prettier": "^
|
|
43
|
-
"eslint-plugin-svelte": "^
|
|
44
|
-
"globals": "^
|
|
45
|
-
"prettier": "^3.
|
|
46
|
-
"prettier-plugin-svelte": "^3.
|
|
47
|
-
"svelte": "^5.
|
|
48
|
-
"svelte-check": "^
|
|
49
|
-
"tslib": "^2.
|
|
50
|
-
"typescript": "^5.
|
|
51
|
-
"typescript-eslint": "^8.
|
|
52
|
-
"vite": "^
|
|
53
|
-
"vitest": "^
|
|
36
|
+
"@sveltejs/adapter-auto": "^4.0.0",
|
|
37
|
+
"@sveltejs/kit": "^2.17.3",
|
|
38
|
+
"@sveltejs/package": "^2.3.10",
|
|
39
|
+
"@sveltejs/vite-plugin-svelte": "^5.0.3",
|
|
40
|
+
"@types/eslint": "^9.6.1",
|
|
41
|
+
"eslint": "^9.21.0",
|
|
42
|
+
"eslint-config-prettier": "^10.0.2",
|
|
43
|
+
"eslint-plugin-svelte": "^3.0.2",
|
|
44
|
+
"globals": "^16.0.0",
|
|
45
|
+
"prettier": "^3.5.3",
|
|
46
|
+
"prettier-plugin-svelte": "^3.3.3",
|
|
47
|
+
"svelte": "^5.20.5",
|
|
48
|
+
"svelte-check": "^4.1.4",
|
|
49
|
+
"tslib": "^2.8.1",
|
|
50
|
+
"typescript": "^5.8.2",
|
|
51
|
+
"typescript-eslint": "^8.25.0",
|
|
52
|
+
"vite": "^6.2.0",
|
|
53
|
+
"vitest": "^3.0.7"
|
|
54
54
|
},
|
|
55
55
|
"svelte": "./dist/index.js",
|
|
56
56
|
"types": "./dist/index.d.ts",
|