Skip to content

Commit

Permalink
fix(pg): PostGIS extenstion - correctly handle srid in geometry colum…
Browse files Browse the repository at this point in the history
…n definition
  • Loading branch information
robinsimonklein committed Sep 29, 2024
1 parent b62b3f1 commit bd65abd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
24 changes: 14 additions & 10 deletions drizzle-orm/src/pg-core/columns/postgis_extension/geometry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ export type PgGeometryBuilderInitial<TName extends string> = PgGeometryBuilder<{
generated: undefined;
}>;

export class PgGeometryBuilder<T extends ColumnBuilderBaseConfig<'array', 'PgGeometry'>> extends PgColumnBuilder<T> {
export class PgGeometryBuilder<T extends ColumnBuilderBaseConfig<'array', 'PgGeometry'>> extends PgColumnBuilder<T, PgGeometryConfig> {
static readonly [entityKind]: string = 'PgGeometryBuilder';

constructor(name: T['name']) {
constructor(name: T['name'], config: PgGeometryConfig = {}) {
super(name, 'array', 'PgGeometry');
this.config.srid = config.srid;
}

/** @internal */
Expand All @@ -35,11 +36,12 @@ export class PgGeometryBuilder<T extends ColumnBuilderBaseConfig<'array', 'PgGeo
}
}

export class PgGeometry<T extends ColumnBaseConfig<'array', 'PgGeometry'>> extends PgColumn<T> {
export class PgGeometry<T extends ColumnBaseConfig<'array', 'PgGeometry'>> extends PgColumn<T, PgGeometryConfig> {
static readonly [entityKind]: string = 'PgGeometry';

getSQLType(): string {
return 'geometry(point)';
const { srid } = this.config;
return `geometry(point${srid ? `,${srid}` : ''})`;
}

override mapFromDriverValue(value: string): [number, number] {
Expand All @@ -62,12 +64,13 @@ export type PgGeometryObjectBuilderInitial<TName extends string> = PgGeometryObj
}>;

export class PgGeometryObjectBuilder<T extends ColumnBuilderBaseConfig<'json', 'PgGeometryObject'>>
extends PgColumnBuilder<T>
extends PgColumnBuilder<T, PgGeometryConfig>
{
static readonly [entityKind]: string = 'PgGeometryObjectBuilder';

constructor(name: T['name']) {
constructor(name: T['name'], config: PgGeometryConfig = {}) {
super(name, 'json', 'PgGeometryObject');
this.config.srid = config.srid;
}

/** @internal */
Expand All @@ -81,11 +84,12 @@ export class PgGeometryObjectBuilder<T extends ColumnBuilderBaseConfig<'json', '
}
}

export class PgGeometryObject<T extends ColumnBaseConfig<'json', 'PgGeometryObject'>> extends PgColumn<T> {
export class PgGeometryObject<T extends ColumnBaseConfig<'json', 'PgGeometryObject'>> extends PgColumn<T, PgGeometryConfig> {
static readonly [entityKind]: string = 'PgGeometryObject';

getSQLType(): string {
return 'geometry(point)';
const { srid } = this.config;
return `geometry(point${srid ? `,${srid}` : ''})`;
}

override mapFromDriverValue(value: string): { x: number; y: number } {
Expand All @@ -111,7 +115,7 @@ export function geometry<TName extends string, TMode extends PgGeometryConfig['m
: PgGeometryBuilderInitial<TName>;
export function geometry(name: string, config?: PgGeometryConfig) {
if (!config?.mode || config.mode === 'tuple') {
return new PgGeometryBuilder(name);
return new PgGeometryBuilder(name, config);
}
return new PgGeometryObjectBuilder(name);
return new PgGeometryObjectBuilder(name, config);
}
4 changes: 2 additions & 2 deletions integration-tests/tests/extensions/postgis/pg.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ const items = pgTable('items', {
lineObj: line('line_abc', { mode: 'abc' }),
geometry: geometry('geometry', { type: 'point' }),
geometryObj: geometry('geometry_obj', { type: 'point', mode: 'xy' }),
geometrySrid: geometry('geometry_options', { type: 'point', mode: 'xy', srid: 4000 }),
geometrySrid: geometry('geometry_options', { type: 'point', mode: 'xy', srid: 4326 }),
geography: geometry('geography', { type: 'point' }),
geographyObj: geography('geography_obj', { type: 'point', mode: 'json' }),
});
Expand All @@ -103,7 +103,7 @@ beforeEach(async () => {
"line_abc" line,
"geometry" geometry(point),
"geometry_obj" geometry(point),
"geometry_options" geometry(point,4000),
"geometry_options" geometry(point,4326),
"geography" geography(point),
"geography_obj" geography(point)
);
Expand Down
4 changes: 2 additions & 2 deletions integration-tests/tests/extensions/postgis/postgres.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ const items = pgTable('items', {
lineObj: line('line_abc', { mode: 'abc' }),
geometry: geometry('geometry', { type: 'point' }),
geometryObj: geometry('geometry_obj', { type: 'point', mode: 'xy' }),
geometrySrid: geometry('geometry_options', { type: 'point', mode: 'xy', srid: 4000 }),
geometrySrid: geometry('geometry_options', { type: 'point', mode: 'xy', srid: 4326 }),
geography: geometry('geography', { type: 'point' }),
geographyObj: geography('geography_obj', { type: 'point', mode: 'json' }),
});
Expand All @@ -105,7 +105,7 @@ beforeEach(async () => {
"line_abc" line,
"geometry" geometry(point),
"geometry_obj" geometry(point),
"geometry_options" geometry(point,4000),
"geometry_options" geometry(point,4326),
"geography" geography(point),
"geography_obj" geography(point)
);
Expand Down

0 comments on commit bd65abd

Please sign in to comment.