Skip to content

Commit dea725b

Browse files
committed
Add ORM section.
1 parent f54cbbf commit dea725b

File tree

1 file changed

+54
-6
lines changed

1 file changed

+54
-6
lines changed

module2-database-technologies/README.md

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -288,17 +288,65 @@ know how to query both in command-line and graphically.
288288
through the command-line or JavaScript)
289289

290290
## ORMs
291+
ORM stands for **object-relational mapping**. Don't let the terminology scare
292+
you: an ORM is simply a library that makes it easier to communicate with a
293+
database in code. For example, some popular JavaScript ORMs are
294+
[Sequelize](https://sequelize.org/), [TypeORM](https://typeorm.io/#/), etc.
295+
These are all libraries that help you query the database from the code.
296+
297+
Why is an ORM useful? Consider the case of a SQL database. Writing raw SQL
298+
queries in the code is quite unsafe -- there is no type-checking on raw query
299+
strings, and a single typo could cause the query to fail. Additionally,
300+
substituting variable values into a query (such as `SELECT * FROM customer WHERE
301+
first_name = $myVariable`) can get quite cumbersome if you are manipulating the
302+
strings yourself.
303+
304+
ORMs often simply the process of fetching relations as well. For example, if you
305+
want to get all the posts for a user, you don't have to think so hard about
306+
concepts such as joins. The same goes at insertion time: you don't have to think
307+
too hard about foreign keys.
308+
309+
Here is an example from another Node ORM, Prisma:
310+
```
311+
const createCategory = await prisma.post.create({
312+
data: {
313+
title: 'How to be Bob',
314+
categories: {
315+
create: [
316+
{
317+
assignedBy: 'Bob',
318+
assignedAt: new Date(),
319+
category: {
320+
create: {
321+
name: 'New category',
322+
},
323+
},
324+
},
325+
],
326+
},
327+
},
328+
});
329+
```
330+
331+
Notice how this conveniently creates rows in two tables in a single function
332+
call -- it creates a post, but it also creates a category for that post, and
333+
finally, it assigns that category to the post. This happens all in a single
334+
function call, in a way that feels natural to a JavaScript programmer (using
335+
objects, functions, and so on, rather than SQL queries).
291336

292-
### ORM use cases
293337

294-
Discuss why ORMs exist, drawbacks, etc
295-
Code along tutorial: Install sequelize/mongoose and do basic queries to see the benefits of an ORM
296-
Describe what happens when you need to update a database. Most ORMs have some concept of migrations so it is a bit important.
338+
### Drawbacks of ORMs
339+
For most use cases, ORMs are extremely convenient and should be used so that the
340+
code is more organized. However, ORMs can be inconvenient in the case of
341+
extremely complex queries, and it's possible that you may run into performance
342+
problems with certain queries that may be difficult to optimize.
343+
344+
However, almost all ORMs allow you to use raw queries as a last resort (e.g.,
345+
type your own SQL and pass it to a function call) to handle these cases.
346+
297347
### ORM lab
298348
TODO
299349

300-
## Database migrations
301-
302350
## Other popular databases
303351

304352
### Elasticsearch

0 commit comments

Comments
 (0)