Skip to content

Commit a152ae2

Browse files
committed
better docs on the schema
1 parent 5207729 commit a152ae2

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

doc/can-query-logic.md

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,10 @@ format. It supports a variety of operators and options. It looks like:
7777
@param {function|can-reflect/schema} schemaOrType Defines the behavior of
7878
keys on a [can-query-logic/query]. This is done with either:
7979

80-
- A constructor function that supports the `can.getSchema` symbol. Currently, [can-define/map/map] supports the `can.getSchema` symbol:
80+
- A constructor function that supports [can-reflect.getSchema can-reflect.getSchema]. Currently, [can-define/map/map] supports the `can.getSchema` symbol:
8181
```js
82+
import {DefineMap} from "can";
83+
8284
const Todo = DefineMap.extend({
8385
id: {
8486
identity: true,
@@ -87,10 +89,11 @@ keys on a [can-query-logic/query]. This is done with either:
8789
name: "string",
8890
complete: "boolean"
8991
});
90-
new Query(Todo);
92+
new QueryLogic(Todo);
9193
```
92-
- A schema object that looks like:
94+
- A [can-reflect.getSchema schema object] that looks like the following:
9395
```js
96+
import {MaybeNumber, MaybeString, MaybeBoolean} from "can"
9497
new QueryLogic({
9598
// keys that uniquely represent this type
9699
identity: ["id"],
@@ -102,8 +105,33 @@ keys on a [can-query-logic/query]. This is done with either:
102105
})
103106
```
104107

108+
Note that if a key type (ex: `name: MaybeString`) is __not__ provided, filtering by that
109+
key will still work, but there won't be any type coercion. For example, the following
110+
might not produce the desired results:
111+
112+
```js
113+
var queryLogic = new QueryLogic({identity: ["id"]});
114+
queryLogic.union(
115+
{filter: {age: 7}},
116+
{filter: {age: "07"}}) //-> {filter: {age: {$in: [7,"07"]}}}
117+
```
118+
Use types like [can-data-types/maybe-number/maybe-number] if you want to add basic
119+
type coercion:
120+
121+
```js
122+
var queryLogic = new QueryLogic({
123+
identity: ["id"],
124+
keys: {age: MaybeNumber}
125+
});
126+
queryLogic.union(
127+
{filter: {age: 7}},
128+
{filter: {age: "07"}}) //-> {filter: {age: 7}}
129+
```
130+
131+
If you need even more special key behavior, read [defining properties with special logic](#Definingfilterpropertieswithspeciallogic).
132+
105133
By default, filter properties like `status` in `{filter: {status: "complete"}}`
106-
are used to create to one of the [can-query/types/comparisons comparison instances] like
134+
are used to create to one of the [can-query-logic/comparison-operators] like
107135
`GreaterThan`. A matching schema key will overwrite this behavior. How this
108136
works is explained in the [Defining filter properties with special logic](#Definingfilterpropertieswithspeciallogic) section below.
109137

0 commit comments

Comments
 (0)