Skip to content

Commit

Permalink
add geographypoint type
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchwadair authored and drodrigues4 committed Jul 23, 2024
1 parent f7068dc commit ab00e2a
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions drizzle-orm/src/singlestore-core/columns/geographypoint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import type { ColumnBaseConfig } from '~/column';
import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnConfig } from '~/column-builder';
import { entityKind } from '~/entity.ts';
import type { AnySingleStoreTable } from '~/singlestore-core/table';
import { sql } from '~/sql/sql.ts';
import { SingleStoreColumn, SingleStoreColumnBuilder } from './common.ts';

type GeographyPoint = [number, number];

export type SingleStoreGeographyPointBuilderInitial<TName extends string> = SingleStoreGeographyPointBuilder<{
name: TName;
dataType: 'array';
columnType: 'SingleStoreGeographyPoint';
data: GeographyPoint;
driverParam: string;
enumValues: undefined;
generated: undefined;
}>;

export class SingleStoreGeographyPointBuilder<T extends ColumnBuilderBaseConfig<'array', 'SingleStoreGeographyPoint'>>
extends SingleStoreColumnBuilder<T>
{
static readonly [entityKind]: string = 'SingleStoreGeographyPointBuilder';

constructor(name: T['name']) {
super(name, 'array', 'SingleStoreGeographyPoint');
}

/** @internal */
override build<TTableName extends string>(
table: AnySingleStoreTable<{ name: TTableName }>,
): SingleStoreGeographyPoint<MakeColumnConfig<T, TTableName>> {
return new SingleStoreGeographyPoint(table, this.config as ColumnBuilderRuntimeConfig<any, any>);
}
}

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

constructor(
table: AnySingleStoreTable<{ name: T['tableName'] }>,
config: SingleStoreGeographyPointBuilder<T>['config'],
) {
super(table, config);
}

getSQLType(): string {
return 'geographypoint';
}

override mapToDriverValue([lon, lat]: GeographyPoint) {
return sql`"POINT(${lon} ${lat})"`;
}

override mapFromDriverValue(value: string): GeographyPoint {
const numbers = value.slice(value.indexOf('(') + 1, -1);
return numbers.split(' ').map(Number) as GeographyPoint; // driver value will look like `POINT(lon lat)`
}
}

export function geographypoint<TName extends string>(name: TName): SingleStoreGeographyPointBuilderInitial<TName> {
return new SingleStoreGeographyPointBuilder(name);
}

0 comments on commit ab00e2a

Please sign in to comment.