Brand ids?
Hi, it's possible to mapped a primary key to a branded type from effect?
5 Replies
Also could something like
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.
I guess you can use the
.mapWith((value) => {...})
function.
Regular: .mapWith((value) => UserId(value))
Curried: .mapWith(UserId)
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
I have never tried the Query API, and are not familiar with this. Can you try this?
edit... i just testet to add 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
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.
.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