Fork me on GitHub
#datascript
<
2017-04-24
>
dmitriid12:04:41

I have another noob question šŸ™‚

dmitriid12:04:02

Do you use :db.type/ref in your actual code? Or do you just look up by properties?

dmitriid12:04:30

The reason Iā€™m asking is that Iā€™m currently playing with an existing API to our app

dmitriid12:04:47

We have the following entities:

A city: {cityId: xxx, name: "Something"}
A venue: {venueId: yyy, name: "Something", cityId: xxx}
An event: {eventId: zzz, name: "Something", venueId: yyy}

Niki12:04:55

How do you mean? We use refs in schema. Do you mean do we dynamically analyse our schema?

dmitriid12:04:29

Letā€™s say I want to have an event referencing a venue referencing a city

dmitriid12:04:19

I tried to do that by setting :db/id to *Id, but, obviously, venue with :db/id set to 1 would get overwritten by event with :db/id set to 1

Niki12:04:27

I donā€™t see a problem in your data

Niki12:04:47

cityId and venueId could totally be refs

Niki12:04:51

they should be refs

dmitriid12:04:29

I just realized how to solve this šŸ˜„ I think.

dmitriid12:04:56

- If we display a list of cities, we know their entities. - Click on city, load venues for that city. - On load, set :venue/city (or whatever) to the currently (:db/id (currently-selected-city))

Niki12:04:29

are you sure youā€™re talking about datascript?

dmitriid12:04:35

Yes šŸ™‚

dmitriid12:04:08

The thing is: the API is an existing API, so itā€™s not Datascript-optimized.

Niki12:04:26

so you want to keep original ids?

dmitriid12:04:17

So what I get from the server is:

Cities: [{cityId: 1, name: "Stockholm"}, {cityId: 2, "Copenhagen"}]
// click Stokcholm get:
Venues: [{venueId: 1, name: "Berns", cityId: 1}, {venueId: 2, "Ambassador", cityId: 1}}]
// Click Berns get
Events: [{eventId: 2, name: "Party!", venueId: 1} ...]

dmitriid12:04:44

So itā€™s kinda ā€œI want to keep original ids and use refsā€ šŸ™‚

Niki12:04:48

youā€™ll need to use ā€œexternal idā€ attributes for those

Niki12:04:10

but for refs youā€™ll need to use datascript ids

Niki12:04:32

[{ :cityId 1, :name "Stockholm"}
 { :cityId 2, :name "Copenhagen"}
 { :venueId 1, :name "Berns", :city [:cityId 1] }]

Niki12:04:05

and youā€™ll need to mark :cityId :venueId as :db.unique/identity and :city as ref

dmitriid12:04:21

Iā€™ll have to try this šŸ™‚

Niki12:04:44

then [:cityId 1] will be resolved to datascript id of entity that has :cityId attribute set to 1

Niki12:04:22

yeah. Feature is called ā€œlookup refsā€

Niki12:04:50

basically itā€™s nothing more but a nice syntax to find entity id by attribute and value

dmitriid12:04:14

Iā€™ll still need to massage incoming data a little, but itā€™s still awesome!

dmitriid12:04:33

So little time, so much to learn šŸ™‚