Skip to content

Commit 2d1f767

Browse files
committed
docs(types): document type-safe querying
1 parent 43c7aae commit 2d1f767

File tree

8 files changed

+105
-34
lines changed

8 files changed

+105
-34
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# 🔍 ArrayQuery
22

3-
ArrayQuery is a powerful TypeScript package that brings ORM-like querying capabilities to local arrays. It allows you to manipulate and retrieve data from arrays with ease, using a familiar and intuitive API.
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.
44

55
## ✨ Features
66

bun.lockb

0 Bytes
Binary file not shown.

docs/.vitepress/config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export default defineConfig({
5555
{
5656
text: 'Features',
5757
items: [
58+
{ text: 'Type-Safe Queries', link: '/features/type-safety' },
5859
{ text: 'Pagination', link: '/features/pagination' },
5960
{ text: 'Sorting', link: '/features/sorting' },
6061
{ text: 'Searching', link: '/features/searching' },

docs/features/sorting.md

-29
Original file line numberDiff line numberDiff line change
@@ -212,32 +212,3 @@ Result:
212212
{ id: 3, date: '2023-12-01' }
213213
]
214214
```
215-
216-
4. Sorting by a computed value:
217-
218-
```ts twoslash
219-
import { query } from '@chronicstone/array-query'
220-
221-
const data = [
222-
{ id: 1, firstName: 'John', lastName: 'Doe' },
223-
{ id: 2, firstName: 'Jane', lastName: 'Smith' },
224-
{ id: 3, firstName: 'Alice', lastName: 'Johnson' }
225-
]
226-
227-
const result = query(data, {
228-
sort: {
229-
key: 'firstName',
230-
dir: 'asc',
231-
parser: (value, item) => `${item.lastName}, ${item.firstName}`
232-
}
233-
})
234-
```
235-
236-
Result:
237-
```ts twoslash
238-
[
239-
{ id: 3, firstName: 'Alice', lastName: 'Johnson' },
240-
{ id: 2, firstName: 'Jane', lastName: 'Smith' },
241-
{ id: 1, firstName: 'John', lastName: 'Doe' }
242-
]
243-
```

docs/features/type-safety.md

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
## Type-Safe Queries
2+
3+
ArrayQuery provides type safety for query parameters, ensuring that you can only use valid keys for sorting, searching, and filtering. This feature helps catch errors at compile-time and provides excellent autocompletion support in your IDE.
4+
5+
### Sorting
6+
7+
When specifying the `key` for sorting, you'll get type-safe suggestions based on the properties of your data:
8+
9+
```ts twoslash
10+
// @noErrors
11+
import { query } from '@chronicstone/array-query'
12+
13+
const users = [
14+
{ id: 1, name: 'Alice', age: 30, email: '[email protected]' },
15+
{ id: 2, name: 'Bob', age: 25, email: '[email protected]' },
16+
]
17+
18+
const result = query(users, {
19+
sort: { key: '' }
20+
// ^|
21+
})
22+
```
23+
24+
Hovering over the empty string will show a popover with valid options: `"id" | "name" | "age" | "email"`.
25+
26+
### Searching
27+
28+
The `keys` array in the search options is also type-safe:
29+
30+
```ts twoslash
31+
// @noErrors
32+
import { query } from '@chronicstone/array-query'
33+
34+
const products = [
35+
{ id: 1, name: 'Laptop', price: 1000, description: 'Powerful laptop' },
36+
{ id: 2, name: 'Phone', price: 500, description: 'Smart phone' },
37+
]
38+
39+
const result = query(products, {
40+
search: {
41+
value: 'laptop',
42+
keys: ['']
43+
// ^|
44+
}
45+
})
46+
```
47+
48+
The popover for the empty string will show: `"id" | "name" | "price" | "description"`.
49+
50+
### Filtering
51+
52+
Filter keys are also type-safe, including for nested properties:
53+
54+
```ts twoslash
55+
// @noErrors
56+
import { query } from '@chronicstone/array-query'
57+
58+
const employees = [
59+
{ id: 1, name: 'Alice', department: { name: 'IT', location: 'New York' } },
60+
{ id: 2, name: 'Bob', department: { name: 'HR', location: 'London' } },
61+
]
62+
63+
const result = query(employees, {
64+
filter: [
65+
{ key: '' }
66+
// ^|
67+
]
68+
})
69+
```
70+
71+
### Complex Nested Structures
72+
73+
ArrayQuery handles complex nested structures with ease:
74+
75+
```typescript twoslash
76+
// @noErrors
77+
import { query } from '@chronicstone/array-query'
78+
79+
const data = [
80+
{
81+
id: 1,
82+
info: {
83+
personal: { name: 'Alice', age: 30 },
84+
professional: { title: 'Developer', skills: ['JavaScript', 'TypeScript'] }
85+
},
86+
contacts: [
87+
{ type: 'email', value: '[email protected]' },
88+
{ type: 'phone', value: '123-456-7890' }
89+
]
90+
}
91+
]
92+
93+
const result = query(data, {
94+
sort: { key: '', dir: 'asc' },
95+
// ^|
96+
})
97+
```
98+
99+
These type-safe queries ensure that you're always using valid property paths, reducing errors and improving the developer experience when working with ArrayQuery.

docs/getting-started/introduction.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Introduction to ArrayQuery
22

3-
ArrayQuery is a powerful TypeScript library designed to bring ORM-like querying capabilities to local arrays. It provides an intuitive and efficient way to manipulate and retrieve data from arrays, offering functionality similar to database querying but for in-memory JavaScript/TypeScript arrays.
3+
ArrayQuery is a powerful TypeScript library designed to bring Type-Safe ORM-like querying capabilities to local arrays. It provides an intuitive and efficient way to manipulate and retrieve data from arrays, offering functionality similar to database querying but for in-memory JavaScript/TypeScript arrays.
44

55
## What is ArrayQuery?
66

docs/index.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
---
22
layout: home
33

4-
title: ORM-like Querying for Local Arrays
4+
title: Type-Safe ORM-like Querying for Local Arrays
55
hero:
66
name: "ArrayQuery"
7-
text: "ORM-like Querying for Local Arrays"
7+
text: "Type-Safe ORM-like Querying for Arrays"
88
tagline: "Manipulate and retrieve data from arrays with ease using a familiar and intuitive API."
99
image:
1010
src: /images/logo.svg

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
"@antfu/eslint-config": "^2.22.0-beta.2",
5353
"@antfu/ni": "^0.21.12",
5454
"@antfu/utils": "^0.7.10",
55-
"@chronicstone/array-query": "0.2.3",
55+
"@chronicstone/array-query": "0.2.4",
5656
"@faker-js/faker": "^8.4.1",
5757
"@shikijs/vitepress-twoslash": "^1.10.3",
5858
"@types/node": "^20.14.10",

0 commit comments

Comments
 (0)