Home > front end >  Clojure assoc/assoc-in
Clojure assoc/assoc-in

Time:01-27

(def db-sample 
{
:person [{:person/id 9 :name "rich" :surname "hickey" :join-date "04.04.2016" :experience :experience/lead :loyality-level :loyality-level/more-than-seven-years :work-type :work-type/tenure :work-time :work-time/part-time}]

:employees/developer-team [{:frontend  [[:person/id 1] [:person/id 2] [:person/id 3] [:person/id 4]]
}

hello everyone, I making practice on assoc functions so I wanted to create a sample database just using assoc functions to do the practice.

I checked it's quick docs but there is no explanation about how can I create a vector and put data into it. I left an example on top, my question is how can I create db-sample data by using assoc functions? (or maybe easier better options)

CodePudding user response:

In practice if you wanted to simulate a db it would first have to be an atom so you can update in place. And your "adding a person" would be something like:

(def db-sample (atom {:person [] :employees/developer-team []})

(swap! db-sample update :person #(conj % new-person))

Things can get tricky when your database is too nested - there are libraries for this such as specter. But keeping databases relatively flat is also good practice IMHO.

  • Related