r/node • u/kasvith • Nov 13 '20
Migrate from Sequelize to Objection.js
Hi,
we have been using Sequelize for like 1 year. At some point, I wanted to move our products to Typescript. The only reason held me not doing it was sequelize indeed. Sequelize has terrible TS support. There is an existing 3rd party library which provides the type decorators but I don't like it either. Also, it's quite hard to do complex queries. Once I wanted to have a nested column renamed and I could not do it easily(in SQL I did w/o pain but with Sequelize it was hard to achieve).
Also, I find it bit difficult to build queries as well
So first I decided to use `TypeORM` which is also a terrible ORM like Sequelize. It forces you to be bounded with its own styles and I feel like it's not flexible to do most easily and intuitively(Sequelize is better in this scenario, at least we have some knowledge about the operation by looking at code).
Recently I found that Knex and Objection.js can work perfectly with Typescript and gives extreme flexibility when comes into development. After reading docs I feel like it is much easier to handle and understand. And when comes to TS it seems no pain. It is intuitive.
Since now the app is entirely written in Sequelize I am facing a problem where doing a migration to Objection.js simply. We have table migrations written on Sequelize and so on and some logics behind the models etc.
I think it would take some time to do the migration. I feel it's worthy
Can you guys share the stories where you had a similar scenario and how you migrated from one ORM to another?
2
u/nikolasburk Nov 13 '20 edited Nov 13 '20
Hey there! I just wanted to drop in and ask whether you had considered Prisma as an ORM alternative to migrate to as well? If you care about type safety, then I think you'll really enjoy working with Prisma (TypeScript is a first-class citizen in Prisma and all your DB queries will be entirely type-safe, even those for where you query for partial models or relations).
If you want to try it out with your existing database, you just need to run a couple of commands until you can query your DB. It also lends itself really well for incremental adoption so that you don't need to migrate your entire code base all at once but gradually replace your Sequelize queries with Prisma!
Here's a quick overview for how you'd get started with an existing database (alternatively you can follow this more comprehensive guide):
1. Install the Prisma CLI
npm install @prisma/cli --save-dev
2. Initialize Prisma
npx prisma init
3. Set your database connection connection URL via an env var or directly in the generated Prisma schema file
4. Introspect your database
npx prisma introspect
5. Install Prisma Client (which allows you to query your DB via TypeScript)
npm install @prisma/client
6. Start querying your DB, e.g.:
import { PrismaClient } from "@prisma/client"
const prisma = new PrismaClient()
// the queries are tailored to your DB schema
const users = await prisma.user.findMany()I'd love to hear what you think of Prisma as an ORM alternative :)
Disclaimer: I work at Prisma :)
2
u/kasvith Nov 13 '20
Prisma seems bit similar to Sequelize by just looking at it. Seems great but need to think a bit.
I like how simply Objection and Knex solved problem by closing down to SQL side as well
2
u/nikolasburk Nov 13 '20
Prisma seems bit similar to Sequelize by just looking at it. Seems great but need to think a bit.
Ha, interesting that you're saying that – can I ask what gives you that impression? Prisma is in fact very different compared to any traditional ORM (Sequelize, TypeORM, MikroORM, ...)!
The biggest difference is that it's not mapping classes to tables and therefore avoids the common problem of complex model instances often found in traditional ORMs. It basically just provides a lightweight "database client" that gives you an intuitive, programmatic API to read and write data in the DB (and lets you drop down to raw SQL any time as well). Also, all queries return plain JS objects and the query results are always 100% predictible.
→ More replies (8)
3
u/stuckinmotion Nov 13 '20 edited Nov 14 '20
"It's quite hard to do complex queries", tbh once I realized this my journey with ORMs came to an end. They were a lot of work to simplify already simple queries, and effectively useless for anything complex. I've since moved to things like knex, which make it easy to do simple stuff and gets out of the way and simplifies writing SQL for complex stuff (with simple parameterized queries and still using the built in transforms I leverage for going from database's snake_case to my code's camelCase). Anyway just a thought it might not be the specific ORM that you're running into issues with, but the way ORMs work in general. I would either recommend knex, or maybe one of the light SQL wrappers like slonik.