forked from drizzle-team/drizzle-orm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2783479
commit 5bce7e5
Showing
5 changed files
with
199 additions
and
213 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,111 +1,123 @@ | ||
import { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnConfig } from "~/column-builder"; | ||
import { entityKind } from "~/entity"; | ||
import { SingleStoreColumn, SingleStoreColumnBuilder } from "./common"; | ||
import { AnySingleStoreTable } from "~/singlestore-core/table"; | ||
import { ColumnBaseConfig } from "~/column"; | ||
import { sql } from "~/sql/sql"; | ||
import { Equal } from "~/utils"; | ||
|
||
import { ColumnBaseConfig } from '~/column'; | ||
import { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnConfig } from '~/column-builder'; | ||
import { entityKind } from '~/entity'; | ||
import { AnySingleStoreTable } from '~/singlestore-core/table'; | ||
import { sql } from '~/sql/sql'; | ||
import { Equal } from '~/utils'; | ||
import { SingleStoreColumn, SingleStoreColumnBuilder } from './common'; | ||
|
||
export type SingleStoreGUIDBuilderInitial<TName extends string> = SingleStoreGUIDBuilder<{ | ||
name: TName; | ||
dataType: "buffer"; | ||
columnType: "SingleStoreGUID"; | ||
data: Uint8Array; | ||
driverParam: string; | ||
enumValues: undefined; | ||
generated: undefined; | ||
}> | ||
|
||
export class SingleStoreGUIDBuilder<T extends ColumnBuilderBaseConfig<"buffer", "SingleStoreGUID">> extends SingleStoreColumnBuilder<T, SingleStoreGUIDConfig> { | ||
static readonly [entityKind]: string = "SingleStoreGUIDBuilder" | ||
|
||
constructor(name: T["name"], config?: SingleStoreGUIDConfig) { | ||
super(name, "buffer", "SingleStoreGUID") | ||
} | ||
|
||
/** @internal */ | ||
override build<TTableName extends string>(table: AnySingleStoreTable<{name: TTableName}>): SingleStoreGUID<MakeColumnConfig<T, TTableName>> { | ||
return new SingleStoreGUID(table, this.config as ColumnBuilderRuntimeConfig<any, any>); | ||
} | ||
name: TName; | ||
dataType: 'buffer'; | ||
columnType: 'SingleStoreGUID'; | ||
data: Uint8Array; | ||
driverParam: string; | ||
enumValues: undefined; | ||
generated: undefined; | ||
}>; | ||
|
||
export class SingleStoreGUIDBuilder<T extends ColumnBuilderBaseConfig<'buffer', 'SingleStoreGUID'>> | ||
extends SingleStoreColumnBuilder<T, SingleStoreGUIDConfig> | ||
{ | ||
static readonly [entityKind]: string = 'SingleStoreGUIDBuilder'; | ||
|
||
constructor(name: T['name'], config?: SingleStoreGUIDConfig) { | ||
super(name, 'buffer', 'SingleStoreGUID'); | ||
} | ||
|
||
/** @internal */ | ||
override build<TTableName extends string>( | ||
table: AnySingleStoreTable<{ name: TTableName }>, | ||
): SingleStoreGUID<MakeColumnConfig<T, TTableName>> { | ||
return new SingleStoreGUID(table, this.config as ColumnBuilderRuntimeConfig<any, any>); | ||
} | ||
} | ||
|
||
export class SingleStoreGUID<T extends ColumnBaseConfig<"buffer", "SingleStoreGUID">> extends SingleStoreColumn<T> { | ||
static readonly [entityKind]: string = "SingleStoreGUID" | ||
export class SingleStoreGUID<T extends ColumnBaseConfig<'buffer', 'SingleStoreGUID'>> extends SingleStoreColumn<T> { | ||
static readonly [entityKind]: string = 'SingleStoreGUID'; | ||
|
||
constructor(table: AnySingleStoreTable<{name: T["tableName"]}>, config: SingleStoreGUIDBuilder<T>["config"]) { | ||
super(table, config) | ||
} | ||
constructor(table: AnySingleStoreTable<{ name: T['tableName'] }>, config: SingleStoreGUIDBuilder<T>['config']) { | ||
super(table, config); | ||
} | ||
|
||
getSQLType(): string { | ||
return "binary(16)" | ||
} | ||
getSQLType(): string { | ||
return 'binary(16)'; | ||
} | ||
|
||
override mapToDriverValue(value: string) { | ||
return sql`UNHEX(REPLACE(${value}, "-", ""))` | ||
} | ||
override mapToDriverValue(value: string) { | ||
return sql`UNHEX(REPLACE(${value}, "-", ""))`; | ||
} | ||
} | ||
|
||
export type SingleStoreGUIDStringBuilderInitial<TName extends string> = SingleStoreGUIDStringBuilder<{ | ||
name: TName; | ||
dataType: "string"; | ||
columnType: "SingleStoreGUIDString"; | ||
data: string; | ||
driverParam: string; | ||
enumValues: undefined; | ||
generated: undefined; | ||
}> | ||
|
||
export class SingleStoreGUIDStringBuilder<T extends ColumnBuilderBaseConfig<"string", "SingleStoreGUIDString">> extends SingleStoreColumnBuilder<T, SingleStoreGUIDConfig> { | ||
static readonly [entityKind]: string = "SingleStoreGUIDStringBuilder" | ||
|
||
constructor(name: T["name"], config?: SingleStoreGUIDConfig) { | ||
super(name, "string", "SingleStoreGUIDString") | ||
} | ||
|
||
/** @internal */ | ||
override build<TTableName extends string>(table: AnySingleStoreTable<{name: TTableName}>): SingleStoreGUIDString<MakeColumnConfig<T, TTableName>> { | ||
return new SingleStoreGUIDString(table, this.config as ColumnBuilderRuntimeConfig<any, any>); | ||
} | ||
name: TName; | ||
dataType: 'string'; | ||
columnType: 'SingleStoreGUIDString'; | ||
data: string; | ||
driverParam: string; | ||
enumValues: undefined; | ||
generated: undefined; | ||
}>; | ||
|
||
export class SingleStoreGUIDStringBuilder<T extends ColumnBuilderBaseConfig<'string', 'SingleStoreGUIDString'>> | ||
extends SingleStoreColumnBuilder<T, SingleStoreGUIDConfig> | ||
{ | ||
static readonly [entityKind]: string = 'SingleStoreGUIDStringBuilder'; | ||
|
||
constructor(name: T['name'], config?: SingleStoreGUIDConfig) { | ||
super(name, 'string', 'SingleStoreGUIDString'); | ||
} | ||
|
||
/** @internal */ | ||
override build<TTableName extends string>( | ||
table: AnySingleStoreTable<{ name: TTableName }>, | ||
): SingleStoreGUIDString<MakeColumnConfig<T, TTableName>> { | ||
return new SingleStoreGUIDString(table, this.config as ColumnBuilderRuntimeConfig<any, any>); | ||
} | ||
} | ||
|
||
export class SingleStoreGUIDString<T extends ColumnBaseConfig<"string", "SingleStoreGUIDString">> extends SingleStoreColumn<T> { | ||
static readonly [entityKind]: string = "SingleStoreGUIDString" | ||
|
||
constructor(table: AnySingleStoreTable<{name: T["tableName"]}>, config: SingleStoreGUIDStringBuilder<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.substring(0, 8)}-${hex.substring(8, 12)}-${hex.substring(12, 16)}-${hex.substring(16, 20)}-${hex.substring(20)}`; | ||
} | ||
export class SingleStoreGUIDString<T extends ColumnBaseConfig<'string', 'SingleStoreGUIDString'>> | ||
extends SingleStoreColumn<T> | ||
{ | ||
static readonly [entityKind]: string = 'SingleStoreGUIDString'; | ||
|
||
constructor(table: AnySingleStoreTable<{ name: T['tableName'] }>, config: SingleStoreGUIDStringBuilder<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.substring(0, 8)}-${hex.substring(8, 12)}-${hex.substring(12, 16)}-${hex.substring(16, 20)}-${ | ||
hex.substring(20) | ||
}`; | ||
} | ||
} | ||
|
||
export interface SingleStoreGUIDConfig<TMode extends "string" | "buffer" = "string" | "buffer"> { | ||
mode?: TMode; | ||
export interface SingleStoreGUIDConfig<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 GUID | ||
*/ | ||
export function guid<TName extends string, TMode extends SingleStoreGUIDConfig["mode"] & {}>( | ||
name: TName, | ||
config?: SingleStoreGUIDConfig<TMode> | ||
): Equal<TMode, "string"> extends true ? SingleStoreGUIDStringBuilderInitial<TName> : SingleStoreGUIDBuilderInitial<TName>; | ||
export function guid<TName extends string, TMode extends SingleStoreGUIDConfig['mode'] & {}>( | ||
name: TName, | ||
config?: SingleStoreGUIDConfig<TMode>, | ||
): Equal<TMode, 'string'> extends true ? SingleStoreGUIDStringBuilderInitial<TName> | ||
: SingleStoreGUIDBuilderInitial<TName>; | ||
export function guid(name: string, config?: SingleStoreGUIDConfig) { | ||
if (config?.mode === "string") { | ||
return new SingleStoreGUIDStringBuilder(name, config) | ||
} | ||
return new SingleStoreGUIDBuilder(name, config) | ||
} | ||
if (config?.mode === 'string') { | ||
return new SingleStoreGUIDStringBuilder(name, config); | ||
} | ||
return new SingleStoreGUIDBuilder(name, config); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,48 @@ | ||
import { ColumnBaseConfig } from "~/column"; | ||
import { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnConfig } from "~/column-builder"; | ||
import { entityKind } from "~/entity"; | ||
import { SingleStoreColumn, SingleStoreColumnBuilder } from "./common"; | ||
import { AnySingleStoreTable } from "~/singlestore-core/table"; | ||
|
||
import { ColumnBaseConfig } from '~/column'; | ||
import { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnConfig } from '~/column-builder'; | ||
import { entityKind } from '~/entity'; | ||
import { AnySingleStoreTable } from '~/singlestore-core/table'; | ||
import { SingleStoreColumn, SingleStoreColumnBuilder } from './common'; | ||
|
||
export type SingleStoreUUIDBuilderInitial<TName extends string> = SingleStoreUUIDBuilder<{ | ||
name: TName; | ||
dataType: "string"; | ||
columnType: "SingleStoreUUID"; | ||
data: string; | ||
driverParam: string; | ||
enumValues: undefined; | ||
generated: undefined; | ||
}> | ||
|
||
export class SingleStoreUUIDBuilder<T extends ColumnBuilderBaseConfig<"string", "SingleStoreUUID">> extends SingleStoreColumnBuilder<T> { | ||
static readonly [entityKind]: string = "SingleStoreUUIDBuilder" | ||
|
||
constructor(name: T["name"]) { | ||
super(name, "string", "SingleStoreUUID") | ||
} | ||
|
||
/** @internal */ | ||
override build<TTableName extends string>(table: AnySingleStoreTable<{name: TTableName}>): SingleStoreUUID<MakeColumnConfig<T, TTableName>> { | ||
return new SingleStoreUUID(table, this.config as ColumnBuilderRuntimeConfig<any, any>); | ||
} | ||
name: TName; | ||
dataType: 'string'; | ||
columnType: 'SingleStoreUUID'; | ||
data: string; | ||
driverParam: string; | ||
enumValues: undefined; | ||
generated: undefined; | ||
}>; | ||
|
||
export class SingleStoreUUIDBuilder<T extends ColumnBuilderBaseConfig<'string', 'SingleStoreUUID'>> | ||
extends SingleStoreColumnBuilder<T> | ||
{ | ||
static readonly [entityKind]: string = 'SingleStoreUUIDBuilder'; | ||
|
||
constructor(name: T['name']) { | ||
super(name, 'string', 'SingleStoreUUID'); | ||
} | ||
|
||
/** @internal */ | ||
override build<TTableName extends string>( | ||
table: AnySingleStoreTable<{ name: TTableName }>, | ||
): SingleStoreUUID<MakeColumnConfig<T, TTableName>> { | ||
return new SingleStoreUUID(table, this.config as ColumnBuilderRuntimeConfig<any, any>); | ||
} | ||
} | ||
|
||
export class SingleStoreUUID<T extends ColumnBaseConfig<"string", "SingleStoreUUID">> extends SingleStoreColumn<T> { | ||
static readonly [entityKind]: string = "SingleStoreUUID" | ||
export class SingleStoreUUID<T extends ColumnBaseConfig<'string', 'SingleStoreUUID'>> extends SingleStoreColumn<T> { | ||
static readonly [entityKind]: string = 'SingleStoreUUID'; | ||
|
||
constructor(table: AnySingleStoreTable<{name: T["tableName"]}>, config: SingleStoreUUIDBuilder<T>["config"]) { | ||
super(table, config) | ||
} | ||
constructor(table: AnySingleStoreTable<{ name: T['tableName'] }>, config: SingleStoreUUIDBuilder<T>['config']) { | ||
super(table, config); | ||
} | ||
|
||
getSQLType(): string { | ||
return "varchar(36)" | ||
} | ||
getSQLType(): string { | ||
return 'varchar(36)'; | ||
} | ||
} | ||
|
||
export function uuid<TName extends string>(name: TName): SingleStoreUUIDBuilderInitial<TName> { | ||
return new SingleStoreUUIDBuilder(name) | ||
} | ||
return new SingleStoreUUIDBuilder(name); | ||
} |
Oops, something went wrong.