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: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 🙂