Skip to content

Commit

Permalink
project and alternatives
Browse files Browse the repository at this point in the history
  • Loading branch information
btholt committed Aug 12, 2024
1 parent 0696fe5 commit c3b130d
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lessons/05-build-a-project-with-nodejs-and-sqlite/C-project.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
You are going to write the invoice.js API route for our invoice viewer app.

[Click here][file] to see the file you'll edit.

This is a Fastify app but that shouldn't really matter. You should really only need to write SQLite code.

All of the frontend and Fastify code should just work with no need for you to modify it, but feel free to. It's written in HTMX.

This doesn't need to be the most optimized code. You can nest callbacks if you want to, you can make multiple queries, or you can try to optimize the hell out of it.

I installed sqlite3 for you already, but if you're so inclined, feel free to use other SQLite libraries.

The point here is to learn and experiment.

The version I wrote (which is definitely _not_ the optimal solution) is [here][solution].

[file]: https://github.com/btholt/sqlite-app/blob/main/invoice.js
[solution]: https://github.com/btholt/sqlite-app/blob/main/invoice-complete.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,47 @@
---
title: "Alternatives to sqlite3"
---

Now that you've experienced the OG SQLite client for Node.js, let's look at a few of the alternatives.

## Node.js's built-in client

[⛓️ Link][nodejs]

This is the one we'll all eventually use in the future. [Bun][bun] started shipping their own builtin clients so Node.js followed suit. This is unstable and under active development so expect to be able to use it some time in the future.

## better-sqlite3

[⛓️ Link][better-sqlite3]

As it states, it is an attempt to be sqlite3 but better. This SDK adds support for features like a synchronous API, better transaction support, better performance, and other things that are not supported in sqlite3.

## promised-sqlite3

[⛓️ Link][promised-sqlite3]

There's a few different versions of this that people have done, but generally speaking they take sqlite3 and make it easier to use with promises. You can also do this yourself with [Node.js's promisify function][promisify] and the normal sqlite3 library (this is normally what I do.)

## Prisma, Drizzle, Sequelize

- [⛓️ Prisma][prisma]
- [⛓️ Drizzle][drizzle]
- [⛓️ Sequelize][sequelize]

All of these are what you would call an ORM, object relational mapping. It is a library that abstracts away the actual writing of SQL. You call various functions and methods in the library and the library will generate and send SQL to your database for you.

I have a love/hate relationship with ORMs (having mostly used a lot of [Django's ORM][django] when I worked at Reddit). On one hand, it makes writing code frequently very easy when you're doing straightforward stuff. They also frequently can handle things like data migrations for you which can be nice.

However, I have found that ORMs can frequently make things harder too. Once you want to do something that ORM doesn't have or doesn't want you to do, it makes everything harder. It can sometimes be slower.

That said, these three ORMs are quite popular right now and I think they've made great strides in making them more easy to use and more performant. While I typically don't use them myself, I think it's a much more defendable thing to do than it used to be.

[nodejs]: https://nodejs.org/api/sqlite.html
[bun]: https://bun.sh/docs/api/sqlite
[better-sqlite3]: https://github.com/WiseLibs/better-sqlite3
[promised-sqlite3]: https://github.com/tguichaoua/promised-sqlite3
[promisify]: https://nodejs.org/api/util.html#utilpromisifyoriginal
[drizzle]: https://orm.drizzle.team/docs/get-started-sqlite
[prisma]: https://www.prisma.io/docs/getting-started/quickstart
[sequelize]: https://sequelize.org/docs/v7/databases/sqlite/
[django]: https://docs.djangoproject.com/en/5.0/topics/db/queries/

0 comments on commit c3b130d

Please sign in to comment.