Brand ids?

Hi, it's possible to mapped a primary key to a branded type from effect?
export type UserId = string & Brand.Brand<"UserId">
export const UserId = Brand.nominal<UserId>()

export const usersTable = pgTable("users", {
// Possible?
id: uuid().primaryKey().transform((value) => UserId(value)),
createdAt: timestamp().notNull().defaultNow(),
givenName: varchar({ length: 64 }).notNull(),
familyName: varchar({ length: 64 }).notNull(),
})
export type UserId = string & Brand.Brand<"UserId">
export const UserId = Brand.nominal<UserId>()

export const usersTable = pgTable("users", {
// Possible?
id: uuid().primaryKey().transform((value) => UserId(value)),
createdAt: timestamp().notNull().defaultNow(),
givenName: varchar({ length: 64 }).notNull(),
familyName: varchar({ length: 64 }).notNull(),
})
5 Replies
Gary, el Pingüino Artefacto
Also could something like
export const usersTable = pgTable("users", {
// Possible?
id: uuid().primaryKey().transform((value) => `users_${value}`),
createdAt: timestamp().notNull().defaultNow(),
givenName: varchar({ length: 64 }).notNull(),
familyName: varchar({ length: 64 }).notNull(),
})
export const usersTable = pgTable("users", {
// Possible?
id: uuid().primaryKey().transform((value) => `users_${value}`),
createdAt: timestamp().notNull().defaultNow(),
givenName: varchar({ length: 64 }).notNull(),
familyName: varchar({ length: 64 }).notNull(),
})
Angelelz
Angelelz4d ago
There is not transform function to my knowledge, but you can create your own custom types and make it return whatever you want: https://orm.drizzle.team/docs/custom-types
Drizzle ORM - Custom types
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
CMarker
CMarker4d ago
I guess you can use the .mapWith((value) => {...}) function. Regular: .mapWith((value) => UserId(value)) Curried: .mapWith(UserId)
Gary, el Pingüino Artefacto
I tried this way but it doesn't work with the query API I tried mapping with extras props and the sql tag, but I read on github that there is a bug there the .mapWith is not called on extra props
CMarker
CMarker3d ago
I have never tried the Query API, and are not familiar with this. Can you try this? edit... i just testet to add mapWith to a uuid in one of my applications. And it looks like this function is only defined on aggregate-functions like sum, avg etc.. not the uuid type. So i guess that what you are trying to do is not possible with drizzle, and should be done using typescript casting or mapping over the result and concatinating the id column. export const usersTable = pgTable("users", { id: uuid().primaryKey(), createdAt: timestamp().notNull().defaultNow(), givenName: varchar({ length: 64 }).notNull(), familyName: varchar({ length: 64 }).notNull(), }) const result = await db.select({ id: usersTable.id.mapWith((value) => UserId(value)), }).from(usersTable); note the removal of .transform() on the ID property on the table defenition. Just tested this raw SQL query. SELECT 'users_' || users.id AS id FROM users and it gave me the concatinated string in return
Want results from more Discord servers?
Add your server