Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request to add a cast configuration option #359

Open
IanMitchell opened this issue Apr 16, 2024 · 0 comments
Open

Request to add a cast configuration option #359

IanMitchell opened this issue Apr 16, 2024 · 0 comments

Comments

@IanMitchell
Copy link

I wanted to file an issue for the discussion we had on Twitter 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:

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).

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant