drizzle-team / drizzle-team/drizzle-orm

Request to add a `cast` configuration option

Open
#3,288 0 comments 0 reactions 0 assignees View on GitHub
db/mysql driver/planetscale drizzle/kit priority
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

I wanted to file an issue for the [discussion we had on Twitter](https://twitter.com/IanMitchel1/status/1779559528058823010) about this problem!

When using the bigint type with PlanetScale or other HTTP drivers there needs to be a translation to convert a bigint into a JSON serializable value. With `drizzle-orm`, this is accomplished with the `cast` optional config parameter:

```ts
function inflate(field: Field, value: string | null) {
if (field.type === "INT64" || field.type === "UINT64") {
return BigInt(value ?? 0);
}

return cast(field, value);
}

const client = new Client({
host: process.env.DATABASE_HOST,
username: process.env.DATABASE_USERNAME,
password: process.env.DATABASE_PASSWORD,
cast: inflate,
});
```

However when using `drizzle-kit`, this option doesn't appear to exist. This results in errors when trying to create a table migration with a bigint column that has a default value set on it (JavaScript doesn't know how to serialize the value into JSON).

```ts
flags: bigint("flags", { mode: "bigint", unsigned: true }).notNull().default(0n)
```

You can kind of work around this via `.$defaultFn(() => 0n)`, but you lose out on your database schema enforcing a non-null value. Ideally a cast option would be added to the drizzle-kit configuration options that would allow me to pass in the same `inflate` function!

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.