| name | drizzle-orm |
| description | Drizzle ORM patterns for type branding and custom types. Use when working with Drizzle column definitions, branded types, or custom type conversions. |
Drizzle ORM Guidelines
Use $type() for Branded Strings, Not customType
When you need a column with a branded TypeScript type but no actual data transformation, use $type<T>() instead of customType.
The Rule
If toDriver and fromDriver would be identity functions (x) => x, use $type<T>() instead.
Why
Even with identity functions, customType still invokes mapFromDriverValue on every row:
const rawValue = row[columnIndex]!;
const value = rawValue === null ? null : decoder.mapFromDriverValue(rawValue);
Query 1000 rows with 3 date columns = 3000 function calls doing nothing.
Bad Pattern
customType<{ data: DateTimeString; driverParam: DateTimeString }>({
dataType: () => "text",
toDriver: (value) => value,
fromDriver: (value) => value,
});
Good Pattern
text().$type<DateTimeString>();
$type<T>() is a compile-time-only type override:
$type<TType>(): $Type<this, TType> {
return this as $Type<this, TType>;
}
When to Use customType
Only when data genuinely transforms between app and database:
customType<{ data: UserPrefs; driverParam: string }>({
toDriver: (value) => JSON.stringify(value),
fromDriver: (value) => JSON.parse(value),
});
Keep Data in Intermediate Representation
Prefer keeping data serialized (strings) through the system, parsing only at the edges (UI components).
The principle: If data enters serialized and leaves serialized, keep it serialized in the middle. Parse at the edges where you actually need the rich representation.
Example: DateTimeString
Instead of parsing DateTimeString into Temporal.ZonedDateTime at the database layer:
customType<{ data: Temporal.ZonedDateTime; driverParam: string }>({
fromDriver: (value) => fromDateTimeString(value),
});
Keep it as a string until the UI actually needs it:
text().$type<DateTimeString>();
const temporal = fromDateTimeString(row.createdAt);
const updated = toDateTimeString(temporal);