Reputation: 883
I am new to clojure. I want to fetch x records with fields from database and want to insert records into database. Which once should I use between defrecord
and defschema
in this scenario?
Are those the same?
Upvotes: 1
Views: 635
Reputation: 1372
defschema
and defrecord
do not refer to database schema ("shape of database") nor to records (i.e., rows in relational DBs).
Schema is a library for describing the shape of your data, and validating whether some data conforms to this shape. It is similar to the more recent clojure.spec. Clojure Records are custom datatypes, which look a bit like Java-classes.
It is easy to be tempted to write "Object Oriented" DB communication with Records for each entity. However, all database contains is data, which is just lists, maps, sets, and some basic data types. I suggest you keep your data in built-in Clojure data structures, ready at hand, and don't hide it in unnecessary abstractions. (Side note: your DB component, instead of DB entity, may very well be a Clojure Record. For example, lifecycle management with Component uses Records.)
A good place to start would be Honey SQL, which allows you to build SQL queries as Clojure data structures. You get back data and can operate on that data with the full might of Clojure.
Then, when you are comfortable with "laying all your data open (without encapsulation)", go and describe the shape of your data, what is valid and what is not. clojure.spec
is a powerful tool for that.
Upvotes: 2