Serial
import { customType } from 'drizzle-orm/pg-core' ;
const customSerial = customType <{ data : number ; notNull : true ; default : true }>(
{
dataType () {
return 'serial' ;
} ,
} ,
);
Text
import { customType } from 'drizzle-orm/pg-core' ;
const customText = customType <{ data : string }>({
dataType () {
return 'text' ;
} ,
});
Boolean
import { customType } from 'drizzle-orm/pg-core' ;
const customBoolean = customType <{ data : boolean }>({
dataType () {
return 'boolean' ;
} ,
});
Jsonb
import { customType } from 'drizzle-orm/pg-core' ;
const customJsonb = < TData >(name : string ) =>
customType <{ data : TData ; driverData : string }>({
dataType () {
return 'jsonb' ;
} ,
toDriver (value : TData ) : string {
return JSON .stringify (value);
} ,
})(name);
Timestamp
import { customType } from 'drizzle-orm/pg-core' ;
const customTimestamp = customType <
{
data : Date;
driverData : string;
config : { withTimezone : boolean; precision ?: number };
}
> ({
dataType (config) {
const precision = typeof config .precision !== 'undefined'
? ` ( ${ config .precision } )`
: '' ;
return `timestamp ${ precision }${
config .withTimezone ? ' with time zone' : ''
} ` ;
} ,
fromDriver (value : string ) : Date {
return new Date (value);
} ,
});
Usage for all types will be same as defined functions in Drizzle ORM. For example:
const usersTable = pgTable ( 'users' , {
id : customSerial ( 'id' ) .primaryKey () ,
name : customText ( 'name' ) .notNull () ,
verified : customBoolean ( 'verified' ) .notNull () .default ( false ) ,
jsonb : customJsonb < string []>( 'jsonb' ) ,
createdAt : customTimestamp ( 'created_at' , { withTimezone : true }) .notNull ()
.default ( sql `now()` ) ,
});
Serial
import { customType } from 'drizzle-orm/mysql-core' ;
const customInt = customType <{ data : number ; notNull : false ; default : false }>(
{
dataType () {
return 'int' ;
} ,
} ,
);
Text
import { customType } from 'drizzle-orm/mysql-core' ;
const customText = customType <{ data : string }>({
dataType () {
return 'text' ;
} ,
});
Boolean
import { customType } from 'drizzle-orm/mysql-core' ;
const customBoolean = customType <{ data : boolean }>({
dataType () {
return 'boolean' ;
} ,
fromDriver (value) {
if ( typeof value === 'boolean' ) {
return value;
}
return value === 1 ;
} ,
});
Json
import { customType } from 'drizzle-orm/mysql-core' ;
const customJson = < TData >(name : string ) =>
customType <{ data : TData ; driverData : string }>({
dataType () {
return 'json' ;
} ,
toDriver (value : TData ) : string {
return JSON .stringify (value);
} ,
})(name);
Timestamp
import { customType } from 'drizzle-orm/mysql-core' ;
const customTimestamp = customType <
{ data : Date; driverData : string; config : { fsp : number } }
> ({
dataType (config) {
const precision = typeof config .fsp !== 'undefined'
? ` ( ${ config .fsp } )`
: '' ;
return `timestamp ${ precision } ` ;
} ,
fromDriver (value : string ) : Date {
return new Date (value);
} ,
});
Usage for all types will be same as defined functions in Drizzle ORM. For example:
const usersTable = mysqlTable ( 'userstest' , {
id : customInt ( 'id' ) .primaryKey () ,
name : customText ( 'name' ) .notNull () ,
verified : customBoolean ( 'verified' ) .notNull () .default ( false ) ,
jsonb : customJson < string []>( 'jsonb' ) ,
createdAt : customTimestamp ( 'created_at' , { fsp : 2 }) .notNull () .default (
sql `now()` ,
) ,
});