datascript

Andrew Carlile 2023-06-20T04:42:44.851539Z

Hello Wizards, What would be the best way to represent a many-to-one relationship in datascript schema? say if I had number of garages, and in each garage were a number of cars, would it be best practice to: define "car" with a field that references the particular garage it's in? or define "garage" with a multi-cardinal field 'cars'? in either case, would it be better to represent the referred datum with its uuid or with a keyword that represents the thing itself? eg:

(def car-schema
  {:car/name {:db/cardinality :db.cardinality/one :db/valueType :db.type/string}
   :car/color {:db/cardinality :db.cardinality/one :db/valueType :db.type/string}
   :car/year {:db/cardinality :db.cardinality/one :db/valueType :db.type/string}
   :car/make {:db/cardinality :db.cardinality/one :db/valueType :db.type/string}
   :car/model {:db/cardinality :db.cardinality/one :db/valueType :db.type/string}
   :car/garage {:db/cardinality :db.cardinality/one 
     :db/valueType :db.type/uuid
     :db/valueType :db.type/Garage
}})

(def garage-schema
 {:garage/name 
  {:db/cardinality :db.cardinality/one}
  :garage/cars
  {:db/cardinality :db.cardinality/many
   :db/valueType :db.type/uuid ;; to represent the car's uuid
   :db/valueType :db.type/Car ;; to represent the car itself
}})

Niki 2023-06-20T16:59:50.752709Z

Either way works. I’d prefer :car/garage of :cardinality/one probably, because reverse :car/_garage (which is automatic) is always :cardinality/many re: types, it’s best to use :db.type/ref . It will enable you to traverse both directions with pull and entity APIs