Skip to content

Instantly share code, notes, and snippets.

@suhascv
Last active October 29, 2024 16:58
Show Gist options
  • Save suhascv/822e48ab035f59ca00a71c23806b96db to your computer and use it in GitHub Desktop.
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
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