Dhan
Dhan
DTDrizzle Team
Created by volks on 5/6/2023 in #help
Custom Type interpreted as String
I hope this looks good, please let me know if I missed something.
73 replies
DTDrizzle Team
Created by volks on 5/6/2023 in #help
Custom Type interpreted as String
I added SQL<Point> as the selectPoint return type, else selectedPlace would not be typed correctly.
73 replies
DTDrizzle Team
Created by volks on 5/6/2023 in #help
Custom Type interpreted as String
Hi folks, the above info was really useful, though it was quite difficult for me to navigate. Heres a complete example in case someone else will find it useful.
import { sql, type DriverValueMapper, type SQL } from 'drizzle-orm';

export type Point = {
lat: number;
lng: number;
};

export const pointType = customType<{ data: Point; driverData: string }>({
dataType() {
return 'geometry(Point,4326)';
},
toDriver(value: Point): string {
return `SRID=4326;POINT(${value.lng} ${value.lat})`;
},
fromDriver(value: string) {
const matches = value.match(/POINT\((?<lng>[\d.-]+) (?<lat>[\d.-]+)\)/);
const { lat, lng } = matches?.groups ?? {};

if (!matches) {
console.warn(
`Could not parse point value in function pointType.fromDriver
Currently returning() is not supported and select must use a
custom select like so: db.select({ geo: selectPoint('geo', place.geo) })`,
value,
);
}

return { lat: parseFloat(String(lat)), lng: parseFloat(String(lng)) };
},
});

export const selectPoint = (column: string, decoder: DriverValueMapper<any, any>): SQL<Point> => {
return sql<Point>`st_astext(${sql.identifier(column)})`.mapWith(decoder);
};

// Schema
export const place = pgTable('place', {
id: uuid('id')
// drizzle-kit support for pointType is not 100% yet - edit the generated sql file like so:
// "geometry(Point,4326)" -> geometry(Point,4326)
geo: pointType('geo').notNull(),
});

// Usage
const selectedPlace = await db.select({ geo: selectPoint('geo', place.geo) }).from(place);
import { sql, type DriverValueMapper, type SQL } from 'drizzle-orm';

export type Point = {
lat: number;
lng: number;
};

export const pointType = customType<{ data: Point; driverData: string }>({
dataType() {
return 'geometry(Point,4326)';
},
toDriver(value: Point): string {
return `SRID=4326;POINT(${value.lng} ${value.lat})`;
},
fromDriver(value: string) {
const matches = value.match(/POINT\((?<lng>[\d.-]+) (?<lat>[\d.-]+)\)/);
const { lat, lng } = matches?.groups ?? {};

if (!matches) {
console.warn(
`Could not parse point value in function pointType.fromDriver
Currently returning() is not supported and select must use a
custom select like so: db.select({ geo: selectPoint('geo', place.geo) })`,
value,
);
}

return { lat: parseFloat(String(lat)), lng: parseFloat(String(lng)) };
},
});

export const selectPoint = (column: string, decoder: DriverValueMapper<any, any>): SQL<Point> => {
return sql<Point>`st_astext(${sql.identifier(column)})`.mapWith(decoder);
};

// Schema
export const place = pgTable('place', {
id: uuid('id')
// drizzle-kit support for pointType is not 100% yet - edit the generated sql file like so:
// "geometry(Point,4326)" -> geometry(Point,4326)
geo: pointType('geo').notNull(),
});

// Usage
const selectedPlace = await db.select({ geo: selectPoint('geo', place.geo) }).from(place);
73 replies
DTDrizzle Team
Created by rubberduckies on 7/27/2023 in #help
Postgres: install plugin during migration
4 replies
DTDrizzle Team
Created by rubberduckies on 7/27/2023 in #help
Postgres: install plugin during migration
Hey, I saw this question here: https://www.answeroverflow.com/m/1134215299139383466 I found the answer... run generate with the --custom flag e.g. drizzle-kit generate:pg --custom. It will generate empty SQL for custom migration.
4 replies