Last active
October 29, 2024 16:58
-
-
Save suhascv/822e48ab035f59ca00a71c23806b96db to your computer and use it in GitHub Desktop.
Defining custom type with drizzle-orm to support MySQL geospatial POINT data type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { sql, type SQL } from 'drizzle-orm'; | |
import { customType, mysqlTable } from 'drizzle-orm/mysql-core'; | |
const pointType = < | |
TData extends { | |
x: number; | |
y: number; | |
}, | |
>( | |
name: string | |
) => | |
customType<{ data: TData; driverData: string }>({ | |
dataType() { | |
return 'point'; | |
}, | |
toDriver(value: TData): SQL { | |
return sql`ST_GeomFromText('POINT(${value.x} ${value.y})')`; | |
}, | |
})(name); | |
const posts = mysqlTable( | |
'Posts', | |
{ | |
postId: varchar('PostId', { length: 36 }).notNull(), | |
geoCoordinates: pointType<{ x: number; y: number }>('GeoCoordinates'), | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment