drizzle-team / drizzle-team/drizzle-orm

[FEATURE]: Add `fromDatabase` callback in customType

Open
#2,159 2 comments 2 reactions 0 assignees View on GitHub
enhancement qb/crud
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

### Describe what you want

Hello,

would be nice that `customType` have option `fromDatabase` or some similar name. It would be working the same as `toDriver` but for selects before they are queried.

My example here is for mysql and uuid support. They have `BIN_TO_UUID()` and `UUID_TO_BIN()` functions. I know i can use `sql` in select statements and i already have function for it.
```ts
const uuid = (column: MySqlColumn) => sql`BIN_TO_UUID(${column})`;
```

But the issue here is, that im also using the third party lib, which makes the select to database thru drizzle, like this:
```js
this.db
.select({
user: this.userTable,
session: this.sessionTable
})
```

As you can see, i have no control about how my custom column will be selected from db.

The proposal for customType would be like this:
```ts
const uuidColumn = customType<{ data: string }>({
dataType: () => 'binary(16)',
toDriver: (value) => sql`UUID_TO_BIN(${value})`,
fromDatabase: (value) => sql`BIN_TO_UUID(${value})`
});
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.