|
1 | | -# 🔍 ArrayQuery |
| 1 | +# Array-Query |
2 | 2 |
|
3 | | -ArrayQuery is a powerful TypeScript package that brings type-safe querying capabilities to local arrays. It allows you to manipulate and retrieve data from arrays with ease, using a familiar and intuitive API. |
| 3 | +[![npm version][npm-version-src]][npm-version-href] |
| 4 | +[![npm downloads][npm-downloads-src]][npm-downloads-href] |
| 5 | +[![bundle][bundle-src]][bundle-href] |
| 6 | +[![JSDocs][jsdocs-src]][jsdocs-href] |
| 7 | +[![License][license-src]][license-href] |
4 | 8 |
|
5 | | -## ✨ Features |
| 9 | +> ### **Query JavaScript arrays with ORM-like syntax, benefiting from excellent type-safety & developer experience.** |
6 | 10 |
|
7 | | -- 📄 **Pagination**: Easily paginate through large datasets |
8 | | -- 🔎 **Searching**: Perform full-text search across multiple fields |
9 | | -- 🧭 **Filtering**: Apply complex filters with various match modes |
10 | | -- 🔢 **Sorting**: Order results based on any field |
| 11 | +## Key Features: |
11 | 12 |
|
12 | | -## 📦 Installation |
| 13 | +- 🛠 **Type-safe Querying:** Design your queries with strong typing (typed sort / search / filter keys & output) |
| 14 | +- 📄 **Pagination:** Easily paginate through large datasets with built-in support. |
| 15 | +- 🔎 **Full-text Search:** Perform comprehensive searches across multiple fields in your data. |
| 16 | +- 🧭 **Advanced Filtering:** Apply complex filters with various match modes for precise data retrieval. Supports logical grouping, nested conditions, and array matching. |
| 17 | +- 🔢 **Flexible Sorting:** Order results based on any field, with support for multiple sort criteria. |
| 18 | +- 🚀 **Lightweight and Fast:** Queries stay super fast, even with large datasets. |
13 | 19 |
|
14 | | -```bash |
15 | | -npm install array-query |
16 | | -``` |
| 20 | +## INSTALLATION |
17 | 21 |
|
18 | | -## 🚀 Quick Start |
| 22 | +\```bash |
| 23 | +npm install @chronicstone/array-query |
| 24 | +yarn add @chronicstone/array-query |
| 25 | +pnpm add @chronicstone/array-query |
| 26 | +bun add @chronicstone/array-query |
| 27 | +\``` |
19 | 28 |
|
20 | | -```typescript |
21 | | -import { query } from 'array-query' |
| 29 | +## USAGE EXAMPLE |
| 30 | + |
| 31 | +\```ts |
| 32 | +import { query } from '@chronicstone/array-query' |
22 | 33 |
|
23 | 34 | const users = [ |
24 | | - { id: 1, fullName: 'John Doe', email: 'john@example.com', status: 'active', createdAt: '2023-01-01' }, |
25 | | - { id: 2, fullName: 'Jane Smith', email: 'jane@example.com', status: 'inactive', createdAt: '2023-02-15' }, |
| 35 | + { id: 1, fullName: 'John Doe', email: 'john@example.com', age: 30, status: 'active', roles: ['admin'], createdAt: '2023-01-01' }, |
| 36 | + { id: 2, fullName: 'Jane Smith', email: 'jane@example.com', age: 28, status: 'inactive', roles: ['user'], createdAt: '2023-02-15' }, |
| 37 | + { id: 3, fullName: 'Bob Johnson', email: 'bob@example.com', age: 35, status: 'active', roles: ['user', 'manager'], createdAt: '2023-03-20' }, |
26 | 38 | // ... more users |
27 | 39 | ] |
28 | 40 |
|
29 | | -const result = query(users, { |
30 | | - limit: 10, |
| 41 | +const { totalRows, totalPages, rows } = query(users, { |
| 42 | + // Pagination |
31 | 43 | page: 1, |
32 | | - search: { |
33 | | - value: 'John', |
34 | | - keys: ['fullName', 'email'], |
35 | | - }, |
36 | | - sort: { |
37 | | - key: 'createdAt', |
38 | | - order: 'desc', |
39 | | - }, |
40 | | - filter: [ |
41 | | - { |
42 | | - key: 'status', |
43 | | - matchMode: 'equals', |
44 | | - value: 'active', |
45 | | - }, |
46 | | - ], |
47 | | -}) |
48 | | - |
49 | | -console.log(result.data) // Filtered and paginated user data |
50 | | -console.log(result.totalPages) // Total number of pages |
51 | | -console.log(result.totalRows) // Total number of matching rows |
52 | | -``` |
53 | | - |
54 | | -## 📘 API Reference |
55 | | - |
56 | | -### `query(data: T[], params: QueryParams): QueryResult<T>` |
57 | | - |
58 | | -The main function to query your array data. |
59 | | - |
60 | | -#### QueryParams |
61 | | - |
62 | | -- `page: number`: The page number to retrieve |
63 | | -- `limit: number`: The number of items per page |
64 | | -- `sort?: { key: string, order?: 'asc' | 'desc' }`: Sorting configuration |
65 | | -- `search?: { value: string, keys: string[] }`: Search configuration |
66 | | -- `filter?: QueryFilter[]`: Array of filters to apply |
67 | | - |
68 | | -#### QueryFilter |
69 | | - |
70 | | -- `key: string`: The property to filter on |
71 | | -- `value: any`: The value to compare against |
72 | | -- `matchMode: FilterMatchMode`: The type of comparison to make |
73 | | -- `operator?: Operator` (`'AND' | 'OR' | (() => 'AND' | 'OR')`): How to combine multiple matches in arrays |
74 | | -- `params?: MatcherParams`: Additional parameters for the match mode |
75 | | - |
76 | | -### FilterMatchMode |
77 | | - |
78 | | -- `'contains'`: Check if the value contains the filter value |
79 | | -- `'between'`: Check if the value is between two values (for numbers or dates) |
80 | | -- `'equals'`: Exact match |
81 | | -- `'notEquals'`: Inverse of exact match |
82 | | -- `'greaterThan'`: Greater than comparison |
83 | | -- `'greaterThanOrEqual'`: Greater than or equal comparison |
84 | | -- `'lessThan'`: Less than comparison |
85 | | -- `'lessThanOrEqual'`: Less than or equal comparison |
86 | | -- `'exists'`: Check if the property exists |
87 | | -- `'objectStringMap'`: Complex object mapping |
88 | | -- `'arrayLength'`: Check the length of an array |
89 | | -- `'objectMatch'`: Complex object matching |
90 | | - |
91 | | -## 🌟 Advanced Usage |
92 | | - |
93 | | -### Complex Filtering |
| 44 | + limit: 10, |
94 | 45 |
|
95 | | -```typescript |
96 | | -const result = query(users, { |
97 | | - filter: [ |
98 | | - { |
99 | | - key: 'account.type', |
100 | | - matchMode: 'equals', |
101 | | - value: ['admin', 'manager'], |
102 | | - }, |
103 | | - { |
104 | | - key: 'lastLogin', |
105 | | - matchMode: 'greaterThan', |
106 | | - value: '2023-01-01', |
107 | | - params: { dateMode: true }, |
108 | | - }, |
109 | | - { |
110 | | - key: 'tags', |
111 | | - matchMode: 'arrayLength', |
112 | | - value: 3, |
113 | | - }, |
| 46 | + // Sorting |
| 47 | + sort: [ |
| 48 | + { key: 'age', dir: 'desc' }, |
| 49 | + { key: 'fullName', dir: 'asc' } |
114 | 50 | ], |
115 | | -}) |
116 | | -``` |
117 | 51 |
|
118 | | -### Object Map Filtering |
| 52 | + // Searching |
| 53 | + search: { |
| 54 | + value: 'john', |
| 55 | + keys: ['fullName', 'email'] |
| 56 | + }, |
119 | 57 |
|
120 | | -```typescript |
121 | | -const result = query(users, { |
| 58 | + // Filtering |
122 | 59 | filter: [ |
123 | | - { |
124 | | - key: 'account', |
125 | | - matchMode: 'objectMatch', |
126 | | - params: { |
127 | | - properties: [{ key: 'type', matchMode: 'equals' }, { key: 'id', matchMode: 'equals' }], |
128 | | - operator: 'AND', |
129 | | - }, |
130 | | - value: { |
131 | | - type: 'admin', |
132 | | - id: 1, |
133 | | - }, |
134 | | - }, |
135 | | - ], |
| 60 | + { key: 'status', matchMode: 'equals', value: 'active' }, |
| 61 | + { key: 'age', matchMode: 'greaterThan', value: 25 }, |
| 62 | + ] |
136 | 63 | }) |
137 | | -``` |
| 64 | +\``` |
| 65 | + |
| 66 | +This example demonstrates pagination, multi-field sorting, full-text searching, and complex filtering with nested conditions and array field matching. |
138 | 67 |
|
139 | | -## 🤝 Contributing |
| 68 | +## License |
140 | 69 |
|
141 | | -Contributions, issues, and feature requests are welcome! Feel free to check [issues page](https://github.com/yourusername/array-query/issues). |
| 70 | +[MIT](./LICENSE) License © 2023-PRESENT [Cyprien THAO](https://github.com/ChronicStone) |
142 | 71 |
|
143 | | -## 📄 License |
| 72 | +<!-- Badges --> |
144 | 73 |
|
145 | | -This project is [MIT](https://opensource.org/licenses/MIT) licensed. |
| 74 | +[npm-version-src]: https://img.shields.io/npm/v/@chronicstone/array-query?style=flat&colorA=080f12&colorB=1fa669 |
| 75 | +[npm-version-href]: https://npmjs.com/package/@chronicstone/array-query |
| 76 | +[npm-downloads-src]: https://img.shields.io/npm/dm/@chronicstone/array-query?style=flat&colorA=080f12&colorB=1fa669 |
| 77 | +[npm-downloads-href]: https://npmjs.com/package/@chronicstone/array-query |
| 78 | +[bundle-src]: https://img.shields.io/bundlephobia/minzip/@chronicstone/array-query?style=flat&colorA=080f12&colorB=1fa669&label=minzip |
| 79 | +[bundle-href]: https://bundlephobia.com/result?p=@chronicstone/array-query |
| 80 | +[license-src]: https://img.shields.io/github/license/ChronicStone/array-query.svg?style=flat&colorA=080f12&colorB=1fa669 |
| 81 | +[license-href]: https://github.com/ChronicStone/array-query/blob/main/LICENSE |
| 82 | +[jsdocs-src]: https://img.shields.io/badge/jsdocs-reference-080f12?style=flat&colorA=080f12&colorB=1fa669 |
| 83 | +[jsdocs-href]: https://www.jsdocs.io/package/@chronicstone/array-query |
0 commit comments