How do I access config in fromDriver and toDriver when defining a custom type using customType?

Jjohtso3/31/2024
None of the examples do this so it's not clear how/if this is possible?
AAaroned4/1/2024
@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?
Jjohtso4/1/2024
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
AAaroned4/2/2024
@johtso Is this what you are thinking?
const dateISOString = customType<{ data: Date, driverData: string}>({
dataType() {
return 'text'
},
toDriver(value : Date) {
return value.toISOString()
},
fromDriver(value: string) {
return new Date(value)
}
})
const dateISOString = customType<{ data: Date, driverData: string}>({
dataType() {
return 'text'
},
toDriver(value : Date) {
return value.toISOString()
},
fromDriver(value: string) {
return new Date(value)
}
})
Jjohtso4/2/2024
Yep, but was going to generate a date or a datetime string based on config
AAaroned4/2/2024
@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:
export class SQLiteTest<T extends ColumnBaseConfig<'custom', 'SQLiteCustomColumn'>>
extends SQLiteColumn<T, { mode: 'ISOString' | 'DateString' }>
{
static readonly [entityKind]: string = 'SQLiteCustomColumn';

readonly mode: 'ISOString' | 'DateString' = this.config.mode;

getSQLType(): string {
return 'text'
}

override mapFromDriverValue(value: string): Date {
return new Date(value);
}

override mapToDriverValue(value: Date): string {
if (this.config.mode === 'DateString') {
return value.toDateString();
}
return value.toISOString(); //default to ISOString
}
}
export class SQLiteTest<T extends ColumnBaseConfig<'custom', 'SQLiteCustomColumn'>>
extends SQLiteColumn<T, { mode: 'ISOString' | 'DateString' }>
{
static readonly [entityKind]: string = 'SQLiteCustomColumn';

readonly mode: 'ISOString' | 'DateString' = this.config.mode;

getSQLType(): string {
return 'text'
}

override mapFromDriverValue(value: string): Date {
return new Date(value);
}

override mapToDriverValue(value: Date): string {
if (this.config.mode === 'DateString') {
return value.toDateString();
}
return value.toISOString(); //default to ISOString
}
}
Jjohtso4/3/2024
hmm.. this doesn't work when you have a column like this:
timestamp('last_updated').default(sql`(CURRENT_TIMESTAMP)`).$onUpdate(() => sql`(CURRENT_TIMESTAMP)`).notNull(),
timestamp('last_updated').default(sql`(CURRENT_TIMESTAMP)`).$onUpdate(() => sql`(CURRENT_TIMESTAMP)`).notNull(),
ends up being given something like this instead of a Date, I guess the sql query for the default value:
{\"decoder\":{},\"shouldInlineParams\":false,\"queryChunks\":[{\"value\":[\"(CURRENT_TIMESTAMP)\"]}]}
{\"decoder\":{},\"shouldInlineParams\":false,\"queryChunks\":[{\"value\":[\"(CURRENT_TIMESTAMP)\"]}]}
AAaroned4/3/2024
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...
AAaroned4/3/2024
@johtso what about timestamp('last_updated').$onUpdate(() => new Date()).notNull()
Jjohtso4/3/2024
Yeah, should be able to switch to that.. although Date is kind of dodgy on cloudflare workers

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
Cannot get user roleI'm using Next.js with next auth and the drizzle adapter. I must be doing something wrong because th`drizzle-kit generate:sqlite` doesn't work`lib/drizzle.ts` ```ts import { text, blob, sqliteTable } from "drizzle-orm/sqlite-core"; export coStudio not enough info to infer relationI'm getting this error: ``` throw new Error( ^ Error: There is not enough information toTurso DB reads (alot)Using turso for a project with drizzle ( I usually use mysql ), and i'm getting way more database reAppending to array OnConflictUpdate()? (Postgres)**full question on stack overflow: **https://stackoverflow.com/questions/78246191/error-appending-toTypeError: Cannot read properties of undefined (reading 'compositePrimaryKeys')I'm getting this error when I want to rename a table (there are no other changes). Old table: ``` ebetter way to check truthy / falsey from selectHey all, currently checking if the length > 0 and <1 to check truthy / falsey is there a better way Relation join conditionsHey - let's say I have the following SQL: ```sql SELECT blocks.id, blocks.type FROM inHow do I use the sql operator with better-sqlite3?Hello. I'm trying to build a custom query with the sql operator in drizzle-orm, and to run it againsIs there a way in drizzle to return the count before limit and offset?i have a table that i need to limit and offset that also needs to return the count. is there a way torder by dynamic columi have a table and i want the user to be able to order by different columns. how do i write the drizDynamic query columns projectionHey, i'm trying to do a dynamic selection of columns in my queries, having before hand an array of tUnable to use drizzle-orm migrator with drizzle-kitSo. I'm running into something pretty wild. I've got a bunch of migrations generated by dizzle kit, How to use findMany?I'm using supabase. I'm going to show a sample which doesnt necessarily need findMany to work but I How can I convert a null value to integer in a select?I am doing a left join so joined table may contain null fields. In this case I have a player.ranking[object Object] on custom typeUsing Turso I have this column definition: `information: text('information').$type<MeritInformationQuerying User Data Response FormatCurrently, I'm querying user data using the following code: ```javascript const res = await db .sCase sensitivity issue with planetscaleHey all, apologies if this is already addressed. I am migrating from prisma+planetscale to drizzleIssue with CURRENT_TIMESTAMP bun:sqliteI have the following table: ` export const token = sqliteTable("token", { token: text("token").setting up AWS with DrizzleHello I’m trying to configure drizzle to connect to my AWS RDS. The docs state: `` import { drizzl