Skip to content
MasterDuke17 edited this page Sep 7, 2019 · 3 revisions

Red is an ORM.

Each table you want to use should be mapped using a model. On that, you define each column and the relationship between models.

Every model's meta-class has a method called all (or aliased to rs) that represents a ResultSeq of all rows on the table that the model represents. You can treat it as a usual Seq and call methods like map, grep, classify, head, pick, etc. and most of them will return new ResultSeqs without going to the database. When you try to iterate over it (explicitly or implicitly calling .Seq) then it creates the AST that represents that "intention" of a query.

Then Red gets this AST that represents the query and ask the driver you are using to run that. The driver has a method called translate that receives an AST and returns a string of SQL and a list of binds. Each driver can do the translation the way it wants. Then it sends the query to the connected database and gets its response. It's the driver's responsibility to transform the return into a lazy list of hashes where each key is one of the attribute's names in the model.

Than Red creates an object for each returned value (lazily) and returns it as a "real" Seq.