Skip to content

Commit

Permalink
improve geography type narrowing
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchwadair committed Jul 24, 2024
1 parent 9621d95 commit 4cd60b7
Showing 1 changed file with 33 additions and 16 deletions.
49 changes: 33 additions & 16 deletions drizzle-orm/src/singlestore-core/columns/geography.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,23 +69,18 @@ export class SingleStoreGeography<T extends ColumnBaseConfig<'array', 'SingleSto
}

override mapToDriverValue(value: GeographyPoint | GeographyLineString | GeographyPolygon) {
const test = value[0];
if (typeof test === 'number') {
const [lng, lat] = value as GeographyPoint;
if (_isPoint(value)) {
const [lng, lat] = value;
return sql`"POINT(${lng} ${lat})"`;
} else if (Array.isArray(test)) {
if (typeof test[0] === 'number') {
const linestring = value as GeographyLineString;
const points = linestring.map((ls) => sql`${ls[0]} ${ls[1]}`);
return sql`"LINESTRING(${sql.join(points, sql.raw(', '))})"`;
} else {
const polygon = value as GeographyPolygon;
const rings = polygon.map((ring) => {
const points = ring.map((point) => sql`${point[0]} ${point[1]}`);
return sql`(${sql.join(points, sql.raw(', '))})`;
});
return sql`"POLYGON(${sql.join(rings, sql.raw(', '))})"`;
}
} else if (_isLineString(value)) {
const points = value.map((ls) => sql`${ls[0]} ${ls[1]}`);
return sql`"LINESTRING(${sql.join(points, sql.raw(', '))})"`;
} else if (_isPolygon(value)) {
const rings = value.map((ring) => {
const points = ring.map((point) => sql`${point[0]} ${point[1]}`);
return sql`(${sql.join(points, sql.raw(', '))})`;
});
return sql`"POLYGON(${sql.join(rings, sql.raw(', '))})"`;
} else {
throw new Error('value is not Array');
}
Expand Down Expand Up @@ -129,3 +124,25 @@ export class SingleStoreGeography<T extends ColumnBaseConfig<'array', 'SingleSto
export function geography<TName extends string>(name: TName): SingleStoreGeographyBuilderInitial<TName> {
return new SingleStoreGeographyBuilder(name);
}

function _isPoint(value: GeographyPoint | GeographyLineString | GeographyPolygon): value is GeographyPoint {
return value.length === 2 && typeof value[0] === 'number';
}

function _isLineString(value: GeographyPoint | GeographyLineString | GeographyPolygon): value is GeographyLineString {
try {
const test = value as GeographyLineString;
return typeof test[0]![0] === 'number';
} catch {
return false;
}
}

function _isPolygon(value: GeographyPoint | GeographyLineString | GeographyPolygon): value is GeographyPolygon {
try {
const test = value as GeographyPolygon;
return typeof test[0]![0]![0] === 'number';
} catch {
return false;
}
}

0 comments on commit 4cd60b7

Please sign in to comment.