Skip to content

Instantly share code, notes, and snippets.

@amatiasq
Last active December 20, 2024 09:47
Show Gist options
  • Save amatiasq/8a698a922d86935c72723062e610e3a1 to your computer and use it in GitHub Desktop.
Save amatiasq/8a698a922d86935c72723062e610e3a1 to your computer and use it in GitHub Desktop.

Sources

State of DB

Drizzle has the best rate of "If you were starting a new project, would you use them?"

  • Sequelize: 12% yes 30% maybe
  • TypeORM: 14% yes 37% maybe
  • Prisma: 52% yes 32% maybe
  • Drizzle: 56% yes 39% maybe

Medium

https://medium.com/@tamartwena/node-js-data-access-layer-tools-which-orm-to-choose-40473bc09ad0

Compared to Sequelize, Drizzle supports cursor-based pagination natively

Reddit

https://www.reddit.com/r/node/comments/198uugu/what_orm_do_you_use_in_production/

As a Sequelize user, I've found it to be stable, well documented and it's supported by the Open Collective. I do find that it tends to over complicate some queries when there are multiple joins.

Sequelize library was not made for TypeScript. There is an additional library called Sequelize-Typescript that tries to bridge the gap

Recently migrated from Prisma to Drizzle and I would recommend Drizzle to everyone

I like drizzle and I think it can be considered an ORM since it does a lot, it doesn't fit the technical definition of an ORM but it's also not just a query builder

Drizzle. Ticked all the boxes I had for ORM requirements, including programmatic triggering of schema migrations (although it's just an "up" migration currently). The devs and community are super helpful.

https://www.reddit.com/r/node/comments/176zyyh/pick_an_orm_for_2024_and_explain_the_good_the_bad/

I have been working with Drizzle, adding it to Payload over the last ~4 months and it has been absolutely beautiful. Very happy with the choice.

I used Sequelize in a NestJS app and though it was great. I used Prisma alone and thought it was great. I used Prisma+Kysley to address performance issues and though it was great. I used Drizzle and thought it was great All of these are fantastic choices and anyone reading this can’t go wrong with any of these. But going forward I’m using Drizzle. The DX and performance is unrivaled.

Transactions

They are quite similar

// sequelize
await sequelize.transaction(async (t) => {
  await User.create({ a: 1 }, { transaction: t })
});
// drizzle
await db.transaction(async (t) => {
  await t.insert(users).values({ a: 1 });
});

Other points in favour of Drizzle:

  • native Zod integration for runtime schema validation: https://orm.drizzle.team/docs/zod

  • native type generation for devtime code validation (no need for manual .d.ts)

    import { type InferSelectModel, type InferInsertModel } from 'drizzle-orm'
    
    const users = mysqlTable('users', {
      id: int('id').primaryKey(),
      name: text('name').notNull(),
    });
    
    type SelectUser = typeof users.$inferSelect;
    type InsertUser = typeof users.$inferInsert;
    // or
    type SelectUser = InferSelectModel<typeof users>;
    type InsertUser = InferInsertModel<typeof users>;
  • sql template string allows for interpolation

    const user = sql`SELECT * FROM ${users} WHERE ${users.id} = ${id}`
  • get all columns except

    import { getTableColumns } from "drizzle-orm";
    import { user } from "./schema";
    
    const { password, role, ...rest } = getTableColumns(user);
    await db.select({ ...rest }).from(users);
  • get table config

    const {
      columns,
      indexes,
      foreignKeys,
      checks,
      primaryKeys,
      name,
      schema,
    } = getTableConfig(table);
  • native mocking

    import { drizzle } from "drizzle-orm/node-mysql";
    import * as schema from "./schema"
    
    const db = drizzle.mock({ schema });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment