-
-
Notifications
You must be signed in to change notification settings - Fork 499
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): allow querying by JSON properties
We can query by JSON object properties easily: ```ts const b = await em.findOne(Book, { meta: { valid: true, nested: { foo: '123', bar: 321, deep: { baz: 59, qux: false, }, }, }, }); ``` Will produce following query (in postgres): ```sql select "e0".* from "book" as "e0" where ("meta"->>'valid')::bool = true and "meta"->'nested'->>'foo' = '123' and ("meta"->'nested'->>'bar')::float8 = 321 and ("meta"->'nested'->'deep'->>'baz')::float8 = 59 and ("meta"->'nested'->'deep'->>'qux')::bool = false limit 1 ``` > All drivers are currently supported (including sqlite and mongo). In postgres we > also try to cast the value if we detect number or boolean on the right-hand side. Closes #1359 Related: #1261
- Loading branch information
Showing
18 changed files
with
263 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
--- | ||
title: Using JSON properties | ||
--- | ||
|
||
## Defining JSON properties | ||
|
||
Each database driver behaves a bit differently when it comes to JSON properties. | ||
MikroORM tries to unify the experience via [JsonType](custom-types.md#jsontype). | ||
This type will be also used if you specify `type: 'json'`. | ||
|
||
```ts | ||
@Entity() | ||
export class Book { | ||
|
||
@Property({ type: 'json', nullable: true }) | ||
meta?: { foo: string; bar: number }; | ||
|
||
} | ||
``` | ||
|
||
## Querying by JSON object properties | ||
|
||
> Support for querying by JSON object properties was added in v4.4.2 | ||
We can query by JSON object properties easily: | ||
|
||
```ts | ||
const b = await em.findOne(Book, { | ||
meta: { | ||
valid: true, | ||
nested: { | ||
foo: '123', | ||
bar: 321, | ||
deep: { | ||
baz: 59, | ||
qux: false, | ||
}, | ||
}, | ||
}, | ||
}); | ||
``` | ||
|
||
Will produce following query (in postgres): | ||
|
||
```sql | ||
select "e0".* | ||
from "book" as "e0" | ||
where ("meta"->>'valid')::bool = true | ||
and "meta"->'nested'->>'foo' = '123' | ||
and ("meta"->'nested'->>'bar')::float8 = 321 | ||
and ("meta"->'nested'->'deep'->>'baz')::float8 = 59 | ||
and ("meta"->'nested'->'deep'->>'qux')::bool = false | ||
limit 1 | ||
``` | ||
|
||
> All drivers are currently supported (including sqlite and mongo). In postgres we | ||
> also try to cast the value if we detect number or boolean on the right-hand side. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
--- | ||
title: Using JSON properties | ||
--- | ||
|
||
## Defining JSON properties | ||
|
||
Each database driver behaves a bit differently when it comes to JSON properties. | ||
MikroORM tries to unify the experience via [JsonType](custom-types.md#jsontype). | ||
This type will be also used if you specify `type: 'json'`. | ||
|
||
```ts | ||
@Entity() | ||
export class Book { | ||
|
||
@Property({ type: 'json', nullable: true }) | ||
meta?: { foo: string; bar: number }; | ||
|
||
} | ||
``` | ||
|
||
## Querying by JSON object properties | ||
|
||
> Support for querying by JSON object properties was added in v4.4.2 | ||
We can query by JSON object properties easily: | ||
|
||
```ts | ||
const b = await em.findOne(Book, { | ||
meta: { | ||
valid: true, | ||
nested: { | ||
foo: '123', | ||
bar: 321, | ||
deep: { | ||
baz: 59, | ||
qux: false, | ||
}, | ||
}, | ||
}, | ||
}); | ||
``` | ||
|
||
Will produce following query (in postgres): | ||
|
||
```sql | ||
select "e0".* | ||
from "book" as "e0" | ||
where ("meta"->>'valid')::bool = true | ||
and "meta"->'nested'->>'foo' = '123' | ||
and ("meta"->'nested'->>'bar')::float8 = 321 | ||
and ("meta"->'nested'->'deep'->>'baz')::float8 = 59 | ||
and ("meta"->'nested'->'deep'->>'qux')::bool = false | ||
limit 1 | ||
``` | ||
|
||
> All drivers are currently supported (including sqlite and mongo). In postgres we | ||
> also try to cast the value if we detect number or boolean on the right-hand side. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.