Check out a free preview of the full API Design in Node.js, v4 course:
The "Object Relational Mapper (ORM)" Lesson is part of the full, API Design in Node.js, v4 course featured in this preview video. Here's what you'd learn in this lesson:

Scott introduces ORMs, or Object Relational Mappers. ORMs provide an API for conducting database operations and eliminate the need to write SQL statements inside the application. A question about using JSON in Postgres vs. Mongo is also answered in this lesson.

Get Unlimited Access Now

Transcript from the "Object Relational Mapper (ORM)" Lesson

>> Okay, now, we're gonna start getting into actually building out the API. Now that we have our server, we're just gonna move fast and we're gonna hop right into modeling the data and getting everything organized when it comes to making sure our API can actually do things and it's protecting stuff.

[00:00:18] But before that, we kinda gotta talk about what an ORM is. So I would say most of you probably know what a database is. 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.

[00:00:33] You can think of a database as, yeah, it's like you saving stuff to a hard drive or an SSD, whereas the code that you write is saved in RAM and is temporary. So database is just an obstruction around how you interact with an SSD or hard drive through code.

[00:00:50] 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. So in oder to do that, we need something called an ORM. An ORM is basically, the way that I think about it, it's basically an STK for the database.

[00:01:10] That's basically what it is. It's 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.

[00:01:34] This is the SQL query you would have to write, right? It would literally look like this. So I don't know, if you're a SQL god and usually doing SQL your whole life, this is probably nothing. But for the new generation of folks that just haven't grown up on SQL, yeah, Brian Holt is the god.

[00:01:48] And this SQL course is pretty good actually to get because I was one of those people that didn't learn SQL when I was learning to code, so it's a really good course. And even with ORM, you still sometimes have to write SQL, but imagine having to write SQL.

[00:02:03] [LAUGH] And this is what it will look like, okay? This is cool, this is fine, right? Yeah, I didn't test that, I don't know, but yeah, imagine writing this, okay? This is fine, when in ORM, which is an SDK for interacting with database, you can turn this insert operation into this, a function call, okay?

[00:02:28] That's what an ORM does, it's an SDK for your database. So that's what we're gonna be using, 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.

[00:02:45] ORM stands for object-relation mapper. And I think it's described that way because it basically allows you to interact with the database based off an object-oriented approach. So everything is an object, for instance, this is a customer's object and I'm able to create a customer. So it's just a fancy way of saying it's OOP for databases, and it's just called object-relational mapper.

[00:03:11] But I think honestly, we should just start calling them database SDKs at this point cuz that's literally what they are. And we should just scrap the term ORM, it's kind of moot at this point. Cool, any questions on that? Yes.
>> You mentioned using PostgreSQL instead of Mongo, is there any advantage to Mongo over Postgres, especially since Postgres stores JSON?

>> 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?

[00:04:05] 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.

[00:04:21] 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 care what database you have, it actually works the same no matter what database you use. So it really doesn't matter at that point, and I really liked that.

[00:04:36] So there really isn't any advantage other than what you're comfortable with. And if you do get to the point where it does matter, again, different problems.