Skip to content

Commit

Permalink
stringify BSON column
Browse files Browse the repository at this point in the history
  • Loading branch information
Rodriguespn committed Jul 25, 2024
1 parent 52963af commit a623dce
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 16 deletions.
121 changes: 121 additions & 0 deletions drizzle-orm/src/singlestore-core/columns/blob.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnConfig } from '~/column-builder';
import type { ColumnBaseConfig } from '~/column.ts';
import { entityKind } from '~/entity.ts';
import type { AnySingleStoreTable } from '~/singlestore-core/table';
import { sql } from '~/sql/sql.ts';
import type { Equal } from '~/utils';
import { SingleStoreColumn, SingleStoreColumnBuilder } from './common.ts';

export type SingleStoreBlobBuilderInitial<TName extends string> = SingleStoreBlobBuilder<{
name: TName;
dataType: 'string';
columnType: 'SingleStoreBlob';
data: Uint8Array;
driverParam: string;
enumValues: undefined;
generated: undefined;
}>;

export class SingleStoreBlobBuilder<T extends ColumnBuilderBaseConfig<'string', 'SingleStoreBlob'>>
extends SingleStoreColumnBuilder<T, SingleStoreBlobConfig>
{
static readonly [entityKind]: string = 'SingleStoreBlobBuilder';

constructor(name: T['name'], _config?: SingleStoreBlobConfig) {
super(name, 'string', 'SingleStoreBlob');
}

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

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

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

getSQLType(): string {
return 'binary(16)';
}

override mapToDriverValue(value: string) {
return sql`UNHEX(REPLACE(${value}, "-", ""))`;
}
}

export type SingleStoreBlobStringBuilderInitial<TName extends string> = SingleStoreBlobStringBuilder<{
name: TName;
dataType: 'string';
columnType: 'SingleStoreBlobString';
data: string;
driverParam: string;
enumValues: undefined;
generated: undefined;
}>;

export class SingleStoreBlobStringBuilder<T extends ColumnBuilderBaseConfig<'string', 'SingleStoreBlobString'>>
extends SingleStoreColumnBuilder<T, SingleStoreBlobConfig>
{
static readonly [entityKind]: string = 'SingleStoreBlobStringBuilder';

constructor(name: T['name'], _config?: SingleStoreBlobConfig) {
super(name, 'string', 'SingleStoreBlobString');
}

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

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

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

getSQLType(): string {
return 'binary(16)';
}

override mapToDriverValue(value: string) {
return sql`UNHEX(REPLACE(${value}, "-", ""))`;
}

override mapFromDriverValue(value: Uint8Array): string {
const hex = Buffer.from(value).toString('hex');
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
}
}

export interface SingleStoreBlobConfig<TMode extends 'string' | 'buffer' = 'string' | 'buffer'> {
mode?: TMode;
}

/**
* Creates a column with the data type `BINARY(16)`
*
* Use config `{ mode: "string" }` for a string representation of the Blob
*/
export function blob<TName extends string, TMode extends SingleStoreBlobConfig['mode'] & {}>(
name: TName,
config?: SingleStoreBlobConfig<TMode>,
): Equal<TMode, 'string'> extends true ? SingleStoreBlobStringBuilderInitial<TName>
: SingleStoreBlobBuilderInitial<TName>;
export function blob(name: string, config?: SingleStoreBlobConfig) {
if (config?.mode === 'string') {
return new SingleStoreBlobStringBuilder(name, config);
}
return new SingleStoreBlobBuilder(name, config);
}
6 changes: 4 additions & 2 deletions drizzle-orm/src/singlestore-core/columns/bson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnCon
import type { ColumnBaseConfig } from '~/column.ts';
import { entityKind } from '~/entity.ts';
import type { AnySingleStoreTable } from '~/singlestore-core/table.ts';
import { sql } from '~/sql/sql.ts';
import { SingleStoreColumn, SingleStoreColumnBuilder } from './common.ts';

export type SingleStoreBsonBuilderInitial<TName extends string> = SingleStoreBsonBuilder<{
Expand Down Expand Up @@ -41,8 +42,9 @@ export class SingleStoreBson<T extends ColumnBaseConfig<'json', 'SingleStoreBson
return 'bson';
}

override mapToDriverValue(value: T['data']): string {
return JSON.stringify(value);
override mapToDriverValue(value: T['data']) {
const json = JSON.stringify(value);
return sql`${json}:>BSON`;
}
}

Expand Down
8 changes: 4 additions & 4 deletions drizzle-orm/src/singlestore-core/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ import type { DrizzleTypeError } from '~/utils.ts';
import type { SingleStoreDialect } from './dialect.ts';
import { SingleStoreAttachBase } from './query-builders/attach.ts';
import { SingleStoreBranchBase } from './query-builders/branch.ts';
import { SingleStoreCreateMilestoneBase } from './query-builders/createMilestone.ts';
import { SingleStoreDetachBase } from './query-builders/detach.ts';
import { SingleStoreDropMilestoneBase } from './query-builders/dropMilestone.ts';
import {
QueryBuilder,
SingleStoreDeleteBase,
SingleStoreInsertBuilder,
SingleStoreSelectBuilder,
SingleStoreUpdateBuilder,
} from './query-builders/index.ts';
import { SingleStoreOptimizeTableBase } from './query-builders/optimizeTable.ts';
import type { OptimizeTableArgument } from './query-builders/optimizeTable.types.ts';
import { RelationalQueryBuilder } from './query-builders/query.ts';
import type { SelectedFields } from './query-builders/select.types.ts';
import type {
Expand All @@ -30,10 +34,6 @@ import type {
} from './session.ts';
import type { WithSubqueryWithSelection } from './subquery.ts';
import type { SingleStoreTable } from './table.ts';
import { SingleStoreCreateMilestoneBase } from './query-builders/createMilestone.ts';
import { SingleStoreDropMilestoneBase } from './query-builders/dropMilestone.ts';
import { SingleStoreOptimizeTableBase } from './query-builders/optimizeTable.ts';
import type { OptimizeTableArgument } from './query-builders/optimizeTable.types.ts';

export class SingleStoreDatabase<
TQueryResult extends SingleStoreQueryResultHKT,
Expand Down
14 changes: 9 additions & 5 deletions drizzle-orm/src/singlestore-core/dialect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ import { ViewBaseConfig } from '~/view-common.ts';
import { SingleStoreColumn } from './columns/common.ts';
import type { SingleStoreAttachConfig } from './query-builders/attach.ts';
import type { SingleStoreBranchConfig } from './query-builders/branch.ts';
import type { SingleStoreCreateMilestoneConfig } from './query-builders/createMilestone.ts';
import type { SingleStoreDeleteConfig } from './query-builders/delete.ts';
import type { SingleStoreDetachConfig } from './query-builders/detach.ts';
import type { SingleStoreDropMilestoneConfig } from './query-builders/dropMilestone.ts';
import type { SingleStoreInsertConfig } from './query-builders/insert.ts';
import type { SingleStoreOptimizeTableConfig } from './query-builders/optimizeTable.ts';
import type {
SelectedFieldsOrdered,
SingleStoreSelectConfig,
Expand All @@ -37,9 +40,6 @@ import type { SingleStoreUpdateConfig } from './query-builders/update.ts';
import type { SingleStoreSession } from './session.ts';
import { SingleStoreTable } from './table.ts';
import { SingleStoreViewBase } from './view-base.ts';
import type { SingleStoreCreateMilestoneConfig } from './query-builders/createMilestone.ts';
import type { SingleStoreDropMilestoneConfig } from './query-builders/dropMilestone.ts';
import type { SingleStoreOptimizeTableConfig } from './query-builders/optimizeTable.ts';

export class SingleStoreDialect {
static readonly [entityKind]: string = 'SingleStoreDialect';
Expand Down Expand Up @@ -163,8 +163,12 @@ export class SingleStoreDialect {

let warmBlobCacheForColumnSql = undefined;
if (selection) {
const selectionField = selection.map((column) => { return { path: [], field: column } });
warmBlobCacheForColumnSql = sql` warm blob cache for column ${this.buildSelection(selectionField, { isSingleTable: true })}`;
const selectionField = selection.map((column) => {
return { path: [], field: column };
});
warmBlobCacheForColumnSql = sql` warm blob cache for column ${
this.buildSelection(selectionField, { isSingleTable: true })
}`;
}

return sql`optimize table ${table}${argSql}${warmBlobCacheForColumnSql}`;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { entityKind } from '~/entity.ts';
import type { ColumnBaseConfig, ColumnDataType } from '~/index.ts';
import { QueryPromise } from '~/query-promise.ts';
import type { SingleStoreDialect } from '~/singlestore-core/dialect.ts';
import type {
Expand All @@ -11,10 +12,9 @@ import type {
SingleStoreSession,
} from '~/singlestore-core/session.ts';
import type { Query, SQL, SQLWrapper } from '~/sql/sql.ts';
import type { SingleStoreColumn } from '../columns/common.ts';
import type { SingleStoreTable } from '../table.ts';
import type { OptimizeTableArgument } from './optimizeTable.types.ts';
import type { SingleStoreColumn } from '../columns/common.ts';
import type { ColumnBaseConfig, ColumnDataType } from '~/index.ts';

export type SingleStoreOptimizeTableWithout<
T extends AnySingleStoreOptimizeTableBase,
Expand Down Expand Up @@ -107,7 +107,9 @@ export class SingleStoreOptimizeTableBase<
}

// TODO(singlestore): docs
warmBlobCacheForColumn(...selection: SingleStoreColumn<ColumnBaseConfig<ColumnDataType, string>, object>[]): SingleStoreOptimizeTableWithout<this, TDynamic, 'warmBlobCacheForColumn'> {
warmBlobCacheForColumn(
...selection: SingleStoreColumn<ColumnBaseConfig<ColumnDataType, string>, object>[]
): SingleStoreOptimizeTableWithout<this, TDynamic, 'warmBlobCacheForColumn'> {
if (this.config.arg) {
throw new Error('Cannot call warmBlobCacheForColumn with an argument');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ export type OptimizeTableArgument =
| 'FULL'
| 'FLUSH'
| 'FIX_ALTER'
| 'INDEX'

| 'INDEX';

0 comments on commit a623dce

Please sign in to comment.