|
1 | 1 | # can-query-logic |
2 | 2 |
|
3 | | -[](https://travis-ci.org/canjs/can-query-logic) |
| 3 | +[](https://www.bitovi.com/community/slack?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) |
| 4 | +[](https://forums.bitovi.com/?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) |
| 5 | +[](https://github.com/canjs/can-query-logic/blob/master/LICENSE) |
| 6 | +[](https://www.npmjs.com/package/can-query-logic) |
| 7 | +[](https://travis-ci.org/canjs/can-query-logic) |
| 8 | +[](https://greenkeeper.io/) |
4 | 9 |
|
5 | | -## Purpose |
| 10 | +Perform data queries and compare queries against each other. Provides logic useful for data caching and real-time behavior. |
6 | 11 |
|
7 | | -`can-query-logic` is used to give CanJS an _understanding_ of what __the parameters used to |
8 | | -retrieve a list of data__ represent. This awareness helps other libraries like |
9 | | -[can-connect] and [can-fixture] provide real-time, caching and other behaviors. |
| 12 | +## Documentation |
10 | 13 |
|
11 | | -__The parameters used to retrieve a list of data?__ |
| 14 | +Read the [can-query-logic API docs on CanJS.com](https://canjs.com/doc/can-query-logic.html). |
12 | 15 |
|
13 | | -In many applications, you request a list of data by making a `fetch` or `XMLHTTPRequest` |
14 | | -to a url like: |
| 16 | +## Changelog |
15 | 17 |
|
16 | | -``` |
17 | | -/api/todos?filter[complete]=true&sort=name |
18 | | -``` |
| 18 | +See the [latest releases on GitHub](https://github.com/canjs/can-query-logic/releases). |
19 | 19 |
|
20 | | -The values after the `?` are used to control the data that comes back. Those values are |
21 | | -deserialized into |
22 | | -a query object look like this: |
| 20 | +## Contributing |
23 | 21 |
|
24 | | -```js |
25 | | -{ |
26 | | - filter: {complete: true}, |
27 | | - sort: "name" |
28 | | -} |
29 | | -``` |
| 22 | +The [contribution guide](https://github.com/canjs/can-query-logic/blob/master/CONTRIBUTING.md) has information on getting help, reporting bugs, developing locally, and more. |
30 | 23 |
|
31 | | -This object represents a `Query`. This specific query is for requesting completed todos and have the todos sorted by their _name_. |
| 24 | +## License |
32 | 25 |
|
33 | | -A `QueryLogic` instance _understands_ what a `Query` represents. For example, it can filter records |
34 | | -that match a particular query: |
35 | | - |
36 | | -```js |
37 | | -var todos = [ |
38 | | - { id: 1, name: "learn CanJS", complete: true }, |
39 | | - { id: 2, name: "wash the car", complete: false }, |
40 | | - { id: 3, name: "do the dishes", complete: true } |
41 | | -] |
42 | | - |
43 | | -var queryLogic = new QueryLogic(); |
44 | | - |
45 | | -var result = queryLogic.filterMembers({ |
46 | | - filter: {complete: true} |
47 | | -}, todos); |
48 | | - |
49 | | -result //-> [ |
50 | | -// { id: 3, name: "do the dishes", complete: true }, |
51 | | -// { id: 1, name: "learn CanJS", complete: true } |
52 | | -//] |
53 | | -``` |
54 | | - |
55 | | -The `filterMembers` method allows `QueryLogic` to be used similar to a database. `QueryLogic` instances methods help solve other problems too: |
56 | | - |
57 | | -- __real-time__ - `.isMember` returns if a particular item |
58 | | -belongs to a query and `.index` returns the location where that item belongs. |
59 | | -- __caching__ - `.isSubset` can tell you if you've already loaded |
60 | | - data you are looking for. `.difference` can tell you what data |
61 | | - you need to load that already isn't in your cache. |
62 | | - |
63 | | -In fact, `can-query-logic`'s most unique ability is to be able to directly compare |
64 | | -queries that represent sets of data instead of having to compare |
65 | | -the data itself. For example, if you already loaded all completed todos, |
66 | | -`can-query-logic` can tell you how to get all remaining todos: |
67 | | - |
68 | | -```js |
69 | | -var completedTodosQuery = {filter: {complete: false}}; |
70 | | -var allTodosQuery = {}; |
71 | | -var remainingTodosQuery = queryLogic.difference(allTodosQuery, completedTodosQuery); |
72 | | - |
73 | | -remainingTodosQuery //-> {filter: {complete: {$ne: false}}} |
74 | | -``` |
75 | | - |
76 | | - |
77 | | -### Matching the default query structure |
78 | | - |
79 | | -By default, `can-query-logic` assumes your service layer will match a default query structure |
80 | | -that looks like: |
81 | | - |
82 | | -```js |
83 | | -{ |
84 | | - // Selects only the todos that match. |
85 | | - filter: { |
86 | | - complete: {$in: [false, null]} |
87 | | - }, |
88 | | - // Sort the results of the selection |
89 | | - sort: "-name", |
90 | | - // Selects a range of the sorted result |
91 | | - page: {start: 0, end: 19} |
92 | | -} |
93 | | -``` |
94 | | - |
95 | | -This structures follows the [Fetching Data JSONAPI specification](http://jsonapi.org/format/#fetching). |
96 | | - |
97 | | -There's: |
98 | | - |
99 | | -- a [filter](http://jsonapi.org/format/#fetching-filtering) property for filtering records, |
100 | | -- a [sort](http://jsonapi.org/format/#fetching-sorting) property for specifying the order to sort records, and |
101 | | -- a [page](http://jsonapi.org/format/#fetching-pagination) property that selects a range of the sorted result. _The range indexes are inclusive_. |
102 | | - |
103 | | - |
104 | | -If you control the service layer, we __encourage__ you to make it match the default |
105 | | -query structure. The default query structure also supports the following comparison operators: `$eq`, `$gt`, `$gte`, `$in`, `$lt`, `$lte`, `$ne`, `$nin`. |
| 26 | +[MIT](https://github.com/canjs/can-query-logic/blob/master/LICENSE) |
0 commit comments