How do I access config in fromDriver and toDriver when defining a custom type using customType?
None of the examples do this so it's not clear how/if this is possible?
9 Replies
@johtso the config parameter is for supplying additional parameters to the database data type string representation. For example:
dataType(config) {
return typeof config.length !== 'undefined' ? ``varchar(${config.length})`` : ``varchar``;
}
would allow a column definition: varchar('fullName', { length: 256})
resulting in a database type: varchar(256)
Can you share more information on what you are trying to achieve?I was hoping to do something like this, except for a custom type that would store the time as an ISO string https://github.com/drizzle-team/drizzle-orm/blob/0ddab656ed99f80f7ca2a6f02e96d739f20ccf8b/drizzle-orm/src/sqlite-core/columns/integer.ts#L142-L155
@johtso Is this what you are thinking?
Yep, but was going to generate a date or a datetime string based on config
@johtso the function method of defining a custom type doesn't support accessing the config in the toDriver and fromDriver.
You could try using the underlying classes:
hmm.. this doesn't work when you have a column like this:
ends up being given something like this instead of a Date, I guess the sql query for the default value:
GitHub
Release 0.30.5 · drizzle-team/drizzle-orm
New Features
🎉 $onUpdate functionality for PostgreSQL, MySQL and SQLite
Adds a dynamic update value to the column.
The function will be called when the row is updated, and the returned value will b...
@johtso what about
timestamp('last_updated').$onUpdate(() => new Date()).notNull()
Yeah, should be able to switch to that.. although
Date
is kind of dodgy on cloudflare workers