drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
21.52k stars 487 forks source link

[FEATURE]: Is there a plan to support defining data models using decorators, similar to TypeORM? #2218

Open izhouteng opened 2 months ago

izhouteng commented 2 months ago

Describe what you want

The concept of this ORM framework is great, and I really like it, but I find the way of defining models not as convenient as TypeORM.

The current approach is not very convenient when used with the Nest.js framework.

Especially when defining relationship queries, it's quite unintuitive.

Drizzle ORM:

import { pgTable, serial, text, integer } from 'drizzle-orm/pg-core';
import { relations } from 'drizzle-orm';

export const users = pgTable('users', {
  id: serial('id').primaryKey(),
  name: text('name'),
});

export const usersRelations = relations(users, ({ many }) => ({
  posts: many(posts),
}));

export const posts = pgTable('posts', {
  id: serial('id').primaryKey(),
  content: text('content'),
  authorId: integer('author_id'),
});

export const postsRelations = relations(posts, ({ one }) => ({
  author: one(users, {
    fields: [posts.authorId],
    references: [users.id],
  }),
}));

TypeORM:

import { Entity, Column, PrimaryGeneratedColumn, OneToMany } from 'typeorm';
import { Photo } from '../photos/photo.entity';

@Entity()
export class User {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  firstName: string;

  @Column()
  lastName: string;

  @Column({ default: true })
  isActive: boolean;

  @OneToMany(type => Photo, photo => photo.user)
  photos: Photo[];
}

Clearly, using decorators to define data models is much clearer and straightforward.

incompletude commented 2 months ago

I strongly dislike this pattern. Please do not make this a requirement.