@medyll/idae-query 0.146.0 → 0.148.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.
- package/README.md +109 -2
- package/dist/query/query.js +3 -3
- package/dist/resultSet/resultset.d.ts +5 -1
- package/dist/resultSet/resultset.js +7 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -20,7 +20,7 @@ npm install @medyll/idae-query
|
|
|
20
20
|
Here's a quick example to get you started:
|
|
21
21
|
|
|
22
22
|
```typescript
|
|
23
|
-
import {
|
|
23
|
+
import { getResultSet } from '@medyll/idae-query';
|
|
24
24
|
|
|
25
25
|
const data = [
|
|
26
26
|
{ id: 1, name: 'John', age: 25, metadata: { order: 1 } },
|
|
@@ -29,7 +29,7 @@ const data = [
|
|
|
29
29
|
{ id: 4, name: 'Alice', age: 40, metadata: { order: 4 } },
|
|
30
30
|
];
|
|
31
31
|
|
|
32
|
-
const resultSet =
|
|
32
|
+
const resultSet = getResultSet(data);
|
|
33
33
|
|
|
34
34
|
// Sorting
|
|
35
35
|
const sortedData = resultSet.sortBy({ age: 'asc' });
|
|
@@ -47,6 +47,113 @@ console.log(pageData);
|
|
|
47
47
|
|
|
48
48
|
## API
|
|
49
49
|
|
|
50
|
+
## Operators Demo
|
|
51
|
+
|
|
52
|
+
## Query Operators Usage
|
|
53
|
+
|
|
54
|
+
## Advanced Usage
|
|
55
|
+
|
|
56
|
+
### Multi-criteria Sorting
|
|
57
|
+
```typescript
|
|
58
|
+
const resultSet = getResultSet(data);
|
|
59
|
+
const sorted = resultSet.sortBy({ age: 'asc', name: 'desc' });
|
|
60
|
+
// Tri d'abord par age croissant, puis par name décroissant
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Grouping by Nested Property
|
|
64
|
+
```typescript
|
|
65
|
+
const resultSet = getResultSet(data);
|
|
66
|
+
const grouped = resultSet.groupBy('metadata.order');
|
|
67
|
+
// Regroupe par la propriété imbriquée metadata.order
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Pagination
|
|
71
|
+
```typescript
|
|
72
|
+
const resultSet = getResultSet(data);
|
|
73
|
+
const page1 = resultSet.getPage(1, 2); // Page 1, 2 éléments
|
|
74
|
+
const page2 = resultSet.getPage(2, 2); // Page 2, 2 éléments
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### Combined Example
|
|
78
|
+
```typescript
|
|
79
|
+
const resultSet = getResultSet(data)
|
|
80
|
+
.sortBy({ age: 'asc' })
|
|
81
|
+
.groupBy('age');
|
|
82
|
+
const page = resultSet.getPage(1, 2);
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
All operators are used via the main API (`getResultSet` or `Query`).
|
|
86
|
+
|
|
87
|
+
### Example with getResultSet
|
|
88
|
+
```typescript
|
|
89
|
+
import { getResultSet } from '@medyll/idae-query';
|
|
90
|
+
|
|
91
|
+
const data = [
|
|
92
|
+
{ id: 1, name: 'John', age: 25 },
|
|
93
|
+
{ id: 2, name: 'Jane', age: 30 },
|
|
94
|
+
{ id: 3, name: 'Bob', age: 35 },
|
|
95
|
+
];
|
|
96
|
+
|
|
97
|
+
const resultSet = getResultSet(data);
|
|
98
|
+
|
|
99
|
+
// eq
|
|
100
|
+
const eqResult = resultSet.where({ age: { eq: 30 } }); // [{ id: 2, name: 'Jane', age: 30 }]
|
|
101
|
+
|
|
102
|
+
// gt
|
|
103
|
+
const gtResult = resultSet.where({ age: { gt: 25 } }); // [{ id: 2, name: 'Jane', age: 30 }, { id: 3, name: 'Bob', age: 35 }]
|
|
104
|
+
|
|
105
|
+
// gte
|
|
106
|
+
const gteResult = resultSet.where({ age: { gte: 30 } }); // [{ id: 2, name: 'Jane', age: 30 }, { id: 3, name: 'Bob', age: 35 }]
|
|
107
|
+
|
|
108
|
+
// lt
|
|
109
|
+
const ltResult = resultSet.where({ age: { lt: 30 } }); // [{ id: 1, name: 'John', age: 25 }]
|
|
110
|
+
|
|
111
|
+
// lte
|
|
112
|
+
const lteResult = resultSet.where({ age: { lte: 30 } }); // [{ id: 1, name: 'John', age: 25 }, { id: 2, name: 'Jane', age: 30 }]
|
|
113
|
+
|
|
114
|
+
// ne
|
|
115
|
+
const neResult = resultSet.where({ age: { ne: 25 } }); // [{ id: 2, name: 'Jane', age: 30 }, { id: 3, name: 'Bob', age: 35 }]
|
|
116
|
+
|
|
117
|
+
// in
|
|
118
|
+
const inResult = resultSet.where({ age: { in: [25, 35] } }); // [{ id: 1, name: 'John', age: 25 }, { id: 3, name: 'Bob', age: 35 }]
|
|
119
|
+
|
|
120
|
+
// nin
|
|
121
|
+
const ninResult = resultSet.where({ age: { nin: [25, 35] } }); // [{ id: 2, name: 'Jane', age: 30 }]
|
|
122
|
+
|
|
123
|
+
// contains
|
|
124
|
+
const containsResult = resultSet.where({ name: { contains: 'an' } }); // [{ id: 2, name: 'Jane', age: 30 }]
|
|
125
|
+
|
|
126
|
+
// startsWith
|
|
127
|
+
const startsWithResult = resultSet.where({ name: { startsWith: 'Ja' } }); // [{ id: 2, name: 'Jane', age: 30 }]
|
|
128
|
+
|
|
129
|
+
// endsWith
|
|
130
|
+
const endsWithResult = resultSet.where({ name: { endsWith: 'hn' } }); // [{ id: 1, name: 'John', age: 25 }]
|
|
131
|
+
|
|
132
|
+
// btw
|
|
133
|
+
const btwResult = resultSet.where({ age: { btw: [25, 35] } }); // [{ id: 1, name: 'John', age: 25 }, { id: 2, name: 'Jane', age: 30 }, { id: 3, name: 'Bob', age: 35 }]
|
|
134
|
+
|
|
135
|
+
// Custom operator
|
|
136
|
+
import { Operators } from '@medyll/idae-query';
|
|
137
|
+
Operators.addCustomOperator('isEven', (field, value, data) => data[field] % 2 === 0);
|
|
138
|
+
const customResult = resultSet.where({ age: { isEven: true } }); // [{ id: 2, name: 'Jane', age: 30 }]
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### Example with Query class
|
|
142
|
+
```typescript
|
|
143
|
+
import { Query } from '@medyll/idae-query';
|
|
144
|
+
|
|
145
|
+
const data = [
|
|
146
|
+
{ id: 1, value: 10 },
|
|
147
|
+
{ id: 2, value: 20 },
|
|
148
|
+
{ id: 3, value: 30 },
|
|
149
|
+
];
|
|
150
|
+
|
|
151
|
+
const query = new Query(data);
|
|
152
|
+
const result = query.where({ value: { gt: 15, lt: 30 } }); // [{ id: 2, value: 20 }]
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
## API
|
|
156
|
+
|
|
50
157
|
### `getResultset(data: any[]): ResultSet`
|
|
51
158
|
|
|
52
159
|
Creates a new result set from the provided data.
|
package/dist/query/query.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
/* src\lib\scripts\query\query.ts */
|
|
2
2
|
import { Operators } from '../operators/operators.js';
|
|
3
|
-
import {
|
|
3
|
+
import { getResultSet } from '../resultset/resultset.js';
|
|
4
4
|
export class Query {
|
|
5
5
|
data;
|
|
6
6
|
constructor(data) {
|
|
7
7
|
this.data = data;
|
|
8
|
-
|
|
8
|
+
getResultSet(this.data ?? []);
|
|
9
9
|
}
|
|
10
10
|
where(qy) {
|
|
11
11
|
this.data = this.data.filter((item) => this.matchesQuery(item, qy));
|
|
12
|
-
return
|
|
12
|
+
return getResultSet(this.data);
|
|
13
13
|
}
|
|
14
14
|
matchesQuery(item, query) {
|
|
15
15
|
if (typeof query !== 'object' || query === null) {
|
|
@@ -29,10 +29,14 @@ export type ResultSet<T> = T[] & {
|
|
|
29
29
|
getPage: (page: number, size: number) => ResultSet<T>;
|
|
30
30
|
toObject: (dotPath: DotPath<T>) => T[];
|
|
31
31
|
};
|
|
32
|
+
/**
|
|
33
|
+
* @deprecated Use getResultSet instead. Will be removed in future versions.
|
|
34
|
+
*/
|
|
35
|
+
export declare function getResultset<T = (typeof arguments)[0]>(data: T[]): ResultSet<T>;
|
|
32
36
|
/**
|
|
33
37
|
* Generates a ResultSet based on the provided data array and defines additional properties like setOptions, sortBy, groupBy, and getPage for customization and manipulation.
|
|
34
38
|
*
|
|
35
39
|
* @param {T[]} data - The array of data to generate the ResultSet from.
|
|
36
40
|
* @return {ResultSet<T>} The generated ResultSet with additional properties for customization.
|
|
37
41
|
*/
|
|
38
|
-
export declare function
|
|
42
|
+
export declare function getResultSet<T = (typeof arguments)[0]>(data: T[]): ResultSet<T>;
|
|
@@ -1,13 +1,18 @@
|
|
|
1
1
|
import { dotPath } from "../path/pathResolver.js";
|
|
2
2
|
import { dataOp } from "@medyll/idae-engine";
|
|
3
|
+
/**
|
|
4
|
+
* @deprecated Use getResultSet instead. Will be removed in future versions.
|
|
5
|
+
*/
|
|
6
|
+
export function getResultset(data) {
|
|
7
|
+
return getResultSet(data);
|
|
8
|
+
}
|
|
3
9
|
/**
|
|
4
10
|
* Generates a ResultSet based on the provided data array and defines additional properties like setOptions, sortBy, groupBy, and getPage for customization and manipulation.
|
|
5
11
|
*
|
|
6
12
|
* @param {T[]} data - The array of data to generate the ResultSet from.
|
|
7
13
|
* @return {ResultSet<T>} The generated ResultSet with additional properties for customization.
|
|
8
14
|
*/
|
|
9
|
-
export function
|
|
10
|
-
// : ResultSet<T>
|
|
15
|
+
export function getResultSet(data) {
|
|
11
16
|
Object.defineProperties(data, {
|
|
12
17
|
setOptions: {
|
|
13
18
|
value: function (options = {}) {
|
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.148.0",
|
|
5
5
|
"description": "A powerful and flexible query library for TypeScript and JavaScript applications, featuring a MongoDB-like query interface, strong TypeScript support, and easy integration with front-end frameworks.",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"dev": "vite dev",
|