-
Hey everyone, recently saw another ORM (https://github.com/drizzle-team/drizzle-orm) I wanted to try out with my TsED projects. I'm curious if anyone knows if this is a good idea to use this with TsED since TsED uses Classes for validation and type checks and Drizzle ORM uses TS Types. Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hello @LefanTan For example, drizzle display a showcase with postgre-sql: import {integer, pgTable, serial, text, timestamp, varchar} from "drizzle-orm/pg-core";
import {InferModel} from "drizzle-orm";
export const users = pgTable("users", {
id: serial("id").primaryKey(),
fullName: text("full_name").notNull(),
phone: varchar("phone", {length: 20}).notNull(),
role: text("role", {enum: ["user", "admin"]}).default("user").notNull(),
cityId: integer("city_id").references(() => cities.id),
createdAt: timestamp("created_at").defaultNow().notNull(),
updatedAt: timestamp("updated_at").defaultNow().notNull()
});
export type User = InferModel<typeof users>;
export type NewUser = InferModel<typeof users, "insert">; Here is the same things with Ts.ED: import {JsonEntityFn, MaxLength, Name, Enum, Required, JsonEntityStore, Groups} from "@tsed/schema";
import {Type, useDecorators, StoreSet, nameOf} from "@tsed/core";
import {deserialize} from "@tsed/json-mapper";
import {deserialize} from "@tsed/json-mapper";
const DRIZZLE_SCHEMA = Symbol.for("drizzleSchema");
const DRIZZLE_TABLE = Symbol.for("drizzleTable");
function Model(model: string, options: any = {}) {
return StoreSet(DRIZZLE_TABLE, {model, options});
}
function Column(drizzleSchema: any) {
return JsonEntityFn((entity) => {
entity.store(DRIZZLE_SCHEMA, drizzleSchema);
});
}
function PrimaryColumn(field: string) {
return useDecorators(
Name(field),
Column(serial(field).primaryKey())
);
}
function getDrizzleSchema(klass: Type<any>) {
const schema = [...JsonEntityStore.from(klass).children.entries()].reduce((schema, [key, entity]) => {
return {
...schema,
[key]: entity.store.get(DRIZZLE_SCHEMA)
}
}, {})
const {model} = JsonEntityStore.from(klass).store.get(DRIZZLE_TABLE) || {name: nameOf(klass)}
return pgTable(model.name, schema);
}
enum UserRole {
USER = "USER",
ADMIN = "ADMIN"
}
@Model("users")
class User {
@PrimaryColumn("id")
@Required()
@Groups("!insert")
id: number;
@Column(text("full_name").notNull()) // you can create a TextColumn decorator to simplify: @TextColumn("full_name")
@Required()
@Name("full_name")
fullName: string;
@Column(varchar("phone", {length: 20}).notNull())
@MaxLength(20)
@Required()
phone: string;
@Column(text("role", {enum: ["user", "admin"]}).default("user").notNull())
@Enum(UserRole)
@Required()
role: UserRole;
static table() {
return getDrizzleSchema(User);
}
}
const users = User.table();
type NewUser = Omit<User, "id">;
// emulate a data deserialized from a request
const newUser = deserialize<NewUser>({
fullName: "John Doe",
phone: "1234567890",
role: UserRole.USER
}, {type: User, groups: ["insert"]});
const insertedUsers = deserialize(await db.insert(users).values(newUser).returning(), {
type: User,
collectionType: Array
}) Seems to be good, no ? ;) |
Beta Was this translation helpful? Give feedback.
Hello @LefanTan
I thinks isn't really a problem. It will depend how far you want to go in integrating ORM features with Ts.ED.
For example, drizzle display a showcase with postgre-sql: