Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions pull_data.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
## Pull

Datascript and Datomic both come with a special *Pull API* for pulling entity data at a more coarse grained level. The Pull API can be seen as a sort of *simple query* that is more batch oriented.
Datascript and Datomic both come with a special *Pull API* for pulling entity data at a more coarse-grained level. The Pull API can be seen as a sort of *simple query* that is more batch oriented.

The Pull API comes in two variants:
- Pull One
- Pull Many

### Select patterns

The Pull API uses patterns for selecing attributes.
The Pull API uses patterns for selecting attributes.
The special pattern `'*'` fetches *all* attributes.

### Limits
Expand Down Expand Up @@ -42,7 +42,7 @@ SQL: `["SELECT * FROM People WHERE People.id = ?", maksim-id]`

### Fetch entity by ID

`entity(db eid)`
`(d/entity db eid)`

### Pull and Query combined

Expand All @@ -51,21 +51,21 @@ Let's say we want to extract all entity data of all entities.

`(d/pull-many db '[*] (d/q '[:find ?e :where [?e]]))`

The `d/q` will match all entity ids in the db
Then `pull-many` pulls everything for all of them
The `d/q` will match all entity ids in the db.
Then `pull-many` pulls everything for all of them.

We can f.ex also use a pull to find attributes to be used in a `:find` clause.
As another example, we can use a pull to find attributes to be used in a `:find` clause.
Here we use `pull` on the `:aliases` attribute value `?a` in the `:where` clause to pull only `:name` and `:order` values, which will be used as the attributes to `find`.

```clj
;; query
(->>
(d/q '[:find [(pull ?a [:name :order]) ...]
:in $ ?e
(d/q '[:find [(pull ?a [:name :order]) ...]
:in $ ?e
:where [?e :aliases ?a]
db eid)
(sort-by :order)
(map :name))
```

We can then sort and map the query result independently (and/or include limit/paging logic).
We can then sort and map the query result independently (or include limiting or paging logic).