Decorators in Drizzle ORM schema files

51 views Asked by At

I am new to drizzle ORM and I am building a node application with type-graphql. I have to define a drizzle schema and a graphql schema. I would like to define both schemas in the same file, so I can keep a single model in one place. However, the drizzle-kit seems to dislike the usage of decorators and throws errors when it tries to generate migrations from the schema.

I have defined my drizzle and type-graphql schemas like so:

export const admins = mysqlTable('admins', {
  id: serial('id').primaryKey(),
  email: varchar('email', {length: 256}).unique().notNull(),
  password: varchar('password', {length: 256}).notNull(),
});

export type Admin = typeof admins.$inferSelect;

@ObjectType('Admin')
export class AdminGql implements Admin {

  @Field(()=> Int)
  id!: number

  @Field(() => String)
  email!: string;

  password!: string;

}

And I try to generate the migrations like so: drizzle-kit generate:mysql --schema=path/to/schema.ts

And I get the following error because of the decorators in the file:

@((0, import_type_graphql.ObjectType)("Admin"))
^

SyntaxError: Invalid or unexpected token
    at internalCompileFunction (node:internal/vm:77:18)
    at wrapSafe (node:internal/modules/cjs/loader:1288:20)
    at Module._compile (node:internal/modules/cjs/loader:1340:27)
    at Module._compile (api\node_modules\drizzle-kit\bin.cjs:8644:30)
    at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
    at Object.newLoader [as .ts] (api\node_modules\drizzle-kit\bin.cjs:8648:13)
    at Module.load (node:internal/modules/cjs/loader:1207:32)
    at Module._load (node:internal/modules/cjs/loader:1023:12)
    at Module.require (node:internal/modules/cjs/loader:1235:19)
    at require (node:internal/modules/helpers:176:18)

I also tried importing the table const into another file and exporting it there and using that file for the migrations, but that does not help, drizzle-kit still reads the entire file in which the table is defined and tries to compile it unsuccessfully due to the decorators. Again, my goal is to keep the table definition and the type-graphql definition in the same file.

0

There are 0 answers