Skip to main content

Object Relational Mapper (ORM)

When it comes to choosing a DB for your API, there are many variables at play. For the most part, you'll end up with a Relational (SQL) DB or a NoSql DB (Document Store).

What is a Database?

  • It's basically just a mechanism for you to store things on disk.
  • You can think of a database as, yeah, it's like you saving stuff to a hard drive oran SSD, whereas the code that you write is saved in RAM and is temporary.
tip

We're not going to get into what is the "best" DB because that's impossible to answer and changes as your product's needs change.We are probably not database experts.
But I think if you don't know what a database is,it's basically just a mechanism for you to store things on disk.

So database is just an obstruction around how you interact with an SSD or hard drive through code.That's basically what it is, and there's so many of those databases.But how you interact with the database, that's where things start to get funky.

  • However, no matter the DB, how you interact with the DB matters.
  • What good is the perfect DB that is painfull to interact with the DB.
info

Object-Relational Mapper (ORM) is a term used to describe a technique that allows you to interact with a DB using an object-oriented approach.
When most people say ORM, they're actually talking about an ORM library, which is really just and SDK for your DB.
For example, without and ORM, you can only interact with a SQL DB using SQL.

  • ORM is the library that you install that allows you to interact with the database so you don't have to do any lower level operations.

For instance, if you were to use a SQL database like SQL or SQLite,imagine trying to insert some customers into that database,it will look something like this.This is the SQL query you would have to write, right?It would literally look like this.So I

INSERT INTO Customers (
CustomerName,
ContactName,
Address,
City,
PostalCode,
Country
)
VALUES
('Cardinal',
'Tom B. Erichsen',
'Skagen 21',
'Stavanger',
'4006',
'Norway'
);

Using an ORM, depending on which one, your DB interaction for the same logic might look like this.

db.customers.create({
customerName: 'Cardinal',
contactName: 'Tom B. Erichsen',
address: 'Skagen 21',
....
})
ORM
  • ORM is an SDK for interacting with database,you can turn this insert operation into this, a function call.
  • we're gonna be using ORM to interact with our database.We're also gonna be using that same ORM to create schemas, create models,everything to do with the database, we'll use this SDK, and that's called an ORM.
  • ORM stands for object-relation mapper.. And I think it's described that way because it basically allows you tointeract with the database based off an object-oriented approach.
  • So everything is an object, for instance, this is a customer's object andI'm able to create a customer.So it's just a fancy way of saying it's OOP for databases, andit's just called object-relational mapper.

Is there any advantage using Mongo over Postgres,especially since Postgres stores JSON?

That advantage is gonna come down to how efficient your multi $100 million business is and how often they need to scale.The difference between the two and what needs to happen is so irrelevant early on that it probably just comes down to what are you most comfortable with? What tools do you like the most?What level of support are you getting?Who's giving you a better price versus what it can do,because Mongo is completely acid at this point?
They have transactions now, whereas before, they didn't.So I might have said, Postgres has transactions, but so does Mongo now.

So actually, I mean, you could pretty much do whatever you want.The reason I'm using and also the ORM that we're gonna be using actually doesn't carewhat database you have, it actually works the same no matter what database you use.