What is an ORM? An introduction for novice-programmers (to be updated)
ORM is the abbreviation for object rational mapping. It is used to convert type system data into object-oriented models, which becomes useful to programmers when dealing with databases. This just means it’s the way a programmer treats the data that they are given.
Looking under the hood of an ORM starts with data modeling. The relationships that data have allows ORM’s to be useful. For example, let’s say you have a set of class information of attributes such as Artist, and Songs. How would you relate these two? (note: A class only carries data about itself, you will not find the song name inside of the Artist class, and you will not find the Artist name under the Song class.)
Artist’s has many Songs, and Song’s has many Artists. Songs belong to artist, and Artist belong to song? This doesn’t sound right at all.
Let us talk this out, before we think about any code. Speaking these topics out generally should makes sense: an artist has many songs, and songs has many artists.
But, how are they related programming wise? The Artist class would only have information about the Artists, for example:
· … etc.
The Song class would only have information about the Songs, for example:
(note: Artist class has no information on the song, and Song class has no information of the artist. It only has that information when they are created, and then you can relate the two somehow. 🤔)
If you are not a programmer, you would think that an artist name is individualized and that is the link to the song. But since the class only holds information about itself, you need to figure out how to link the two classes together. This is where objects become useful, and a joiner method becomes useful.
What is a joiner method? It’s the link between our Artist and Song classes. If you’re scratching your head thinking how these are going to related, the answer is the Album class. Oh yeah, a song belongs to an album, and an album belongs to an artist, also an artist has many albums, and an album has many songs! There is a connection! This connection is called a joiner class.
(note: Artist_object and Song_object are represented as objects for the Song and Artist classes. These objects hold all the information from the Artist and Song class, allowing you to match the datat to one another.)
As you can see in Image_two, the Album holds the information about the artist and the song via the Song_id, and Artist_id. You can also now relate the artist and song classes by saying Artists has many Songs through Albums, or vice versa, Songs has many Artists through Albums. Also, you are not feeding in just the attribute of Song_id and Artist_id into the Album class, you are treating the Artist and Song class as objects and comparing the data.
One example of an ORM is Ruby on Rails! Now there is a lot of things that I am not going into details about. One of them is Metaprogramming. According to Wikipedia:
“Metaprogramming is a programming technique in which computer programs have the ability to treat other programs as their data. It means that a program can be designed to read, generate, analyze or transform other programs, and even modify itself while running. In some cases, this allows programmers to minimize the number of lines of code to express a solution, in turn reducing development time.”
Essentially, Metaprogramming allows you to build resources to make your programming life easier. With this, coders can develop libraries for others to use. Ruby on Rails is not a program; it is a library which was developed specifically for Ruby. Image_two is using Ruby code in order to map out how the objects are going to be treated. Rails writes the relationships for us without having to write hundreds of lines of code. Rails will abstract most of the repetitive coding for us, meaning you won’t have to create everything from scratch.