Skip to content

Instantly share code, notes, and snippets.

@domdoescode
Created October 22, 2024 14:33
Show Gist options
  • Select an option

  • Save domdoescode/6a129e6fdfcb3ec98bc860dddba7d05e to your computer and use it in GitHub Desktop.

Select an option

Save domdoescode/6a129e6fdfcb3ec98bc860dddba7d05e to your computer and use it in GitHub Desktop.
Compare Drizzle, TypeORM, and Prisma. I want to use it as both an ORM and migration tool in GitLab CI. Also show some examples of queries including selects with joins, inserts, updates, and deletes

Overview of Drizzle, TypeORM, and Prisma

Each of these tools offers ORM (Object-Relational Mapping) functionality, as well as migration capabilities, but they differ in their philosophy, performance, and how they handle migrations. Below is a comparison tailored for a GitLab CI environment:

1. Drizzle ORM

  • Philosophy: Drizzle ORM focuses on simplicity and type-safety with a minimalistic approach. It is written in TypeScript and offers a SQL-like querying experience.

  • Migration: Drizzle has built-in migration capabilities, allowing you to create and run migrations programmatically. It is highly flexible and fits well into CI/CD pipelines.

  • Performance: Lightweight with a focus on being fast and minimal.

  • Pros:

    • Type-safe and small footprint.
    • SQL-like query experience.
    • Great TypeScript support.
    • Easy integration with CI.
  • Cons:

    • Still relatively new compared to TypeORM and Prisma.
    • Less ecosystem support compared to more mature tools.
  • Example Queries (Drizzle):

    // Select with join
    const result = db.select(users).leftJoin(posts, posts.userId.equals(users.id));
    
    // Insert
    const insert = db.insert(users).values({
      username: 'john_doe',
      email: 'john@example.com',
    }).returning();
    
    // Update
    const update = db.update(users).set({
      username: 'john_updated',
    }).where(users.id.equals(1));
    
    // Delete
    const remove = db.delete(users).where(users.id.equals(1));

2. TypeORM

  • Philosophy: TypeORM is a mature ORM that provides robust functionality, including entity mapping, migrations, and various database support. It’s a full-featured ORM for Node.js and TypeScript.

  • Migration: Built-in migration tool that works with both automatic and manual migrations. Migration scripts can be executed easily in CI/CD pipelines.

  • Performance: It is heavier than Drizzle but more feature-rich.

  • Pros:

    • Extensive feature set, including advanced relationships and lazy loading.
    • Support for a wide range of databases (PostgreSQL, MySQL, SQLite, etc.).
    • Strong community and ecosystem support.
  • Cons:

    • Can be slower than Prisma in some cases.
    • More complex setup compared to Drizzle.
  • Example Queries (TypeORM):

    // Select with join
    const result = await userRepository
      .createQueryBuilder('user')
      .leftJoinAndSelect('user.posts', 'post')
      .getMany();
    
    // Insert
    const user = userRepository.create({ username: 'john_doe', email: 'john@example.com' });
    await userRepository.save(user);
    
    // Update
    await userRepository.update(1, { username: 'john_updated' });
    
    // Delete
    await userRepository.delete(1);

3. Prisma

  • Philosophy: Prisma is a modern ORM focused on developer experience, performance, and easy integration with TypeScript. It has excellent type safety and works with GraphQL and REST backends.

  • Migration: Prisma has a powerful migration engine called Prisma Migrate that handles schema changes with ease. Its declarative schema is ideal for CI pipelines, where you need to track migrations automatically.

  • Performance: Prisma is highly optimized, but its query engine adds some overhead compared to Drizzle or raw SQL solutions.

  • Pros:

    • Excellent type safety with auto-generated types.
    • Declarative schema for managing data models.
    • Very strong community and support.
    • Great developer experience.
  • Cons:

    • Requires running a query engine, which adds some operational overhead.
    • Migration management is relatively less customizable than in TypeORM.
  • Example Queries (Prisma):

    // Select with join
    const usersWithPosts = await prisma.user.findMany({
      include: { posts: true },
    });
    
    // Insert
    const newUser = await prisma.user.create({
      data: {
        username: 'john_doe',
        email: 'john@example.com',
      },
    });
    
    // Update
    const updatedUser = await prisma.user.update({
      where: { id: 1 },
      data: { username: 'john_updated' },
    });
    
    // Delete
    const deletedUser = await prisma.user.delete({
      where: { id: 1 },
    });

Comparison of Drizzle, TypeORM, and Prisma

Feature Drizzle TypeORM Prisma
Ease of Setup Easy, but requires manual setup Moderate, requires entities setup Easy, uses declarative schema
Type Safety Excellent Good, requires manual effort Excellent, auto-generated
Migration Support Programmatic, fits CI well Automatic & manual migrations Prisma Migrate, ideal for CI
Database Support Limited but growing Extensive (SQL and NoSQL) SQL Databases (Postgres, MySQL)
Performance Lightweight and fast Moderate, feature-rich Optimized, but heavier engine
Maturity Newer, less community Mature, large ecosystem Modern, growing fast
Learning Curve Simple Moderate Easy, especially for TypeScript
Community Support Small Large Growing fast

Which to Choose for GitLab CI?

  • Prisma: If your focus is on ease of use, type safety, and seamless migrations in a TypeScript project, Prisma is the best choice. It integrates easily with CI/CD pipelines.
  • TypeORM: If you need a more feature-rich solution with support for more complex queries and relationships, TypeORM is more suited, although it can be a bit heavier to set up in a CI environment.
  • Drizzle: If you are looking for something more lightweight and type-safe with a minimal footprint in CI/CD, Drizzle is a good option, especially if you prefer more control over migrations and queries.

Each of these tools will work in a GitLab CI environment, but Prisma and Drizzle might offer easier, more automated integration with their respective migration tools, while TypeORM might require more configuration.

Let me know if you need more examples or further details on using these tools in GitLab CI!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment