From e67fe4cc6760fcf2adb5bbb3415660165e0b2d18 Mon Sep 17 00:00:00 2001 From: Gabriel Cipriano Date: Tue, 4 Mar 2025 16:08:38 +0100 Subject: [PATCH 1/3] feat: googlesql columns --- drizzle-orm/src/googlesql/columns/all.ts | 61 +++----- drizzle-orm/src/googlesql/columns/bigint.ts | 118 --------------- drizzle-orm/src/googlesql/columns/binary.ts | 69 --------- drizzle-orm/src/googlesql/columns/boolean.ts | 2 + drizzle-orm/src/googlesql/columns/bytes.ts | 69 +++++++++ drizzle-orm/src/googlesql/columns/char.ts | 85 ----------- drizzle-orm/src/googlesql/columns/common.ts | 68 ++++----- drizzle-orm/src/googlesql/columns/custom.ts | 1 + .../src/googlesql/columns/date.common.ts | 42 ------ drizzle-orm/src/googlesql/columns/date.ts | 71 ++------- drizzle-orm/src/googlesql/columns/datetime.ts | 139 ------------------ drizzle-orm/src/googlesql/columns/decimal.ts | 80 ---------- drizzle-orm/src/googlesql/columns/double.ts | 79 ---------- drizzle-orm/src/googlesql/columns/enum.ts | 69 --------- drizzle-orm/src/googlesql/columns/float.ts | 79 ---------- drizzle-orm/src/googlesql/columns/float32.ts | 52 +++++++ drizzle-orm/src/googlesql/columns/float64.ts | 52 +++++++ drizzle-orm/src/googlesql/columns/index.ts | 28 +--- drizzle-orm/src/googlesql/columns/int.ts | 70 --------- drizzle-orm/src/googlesql/columns/int64.ts | 59 ++++++++ .../src/googlesql/columns/mediumint.ts | 67 --------- drizzle-orm/src/googlesql/columns/numeric.ts | 54 +++++++ drizzle-orm/src/googlesql/columns/real.ts | 80 ---------- drizzle-orm/src/googlesql/columns/serial.ts | 75 ---------- drizzle-orm/src/googlesql/columns/smallint.ts | 67 --------- drizzle-orm/src/googlesql/columns/string.ts | 87 +++++++++++ drizzle-orm/src/googlesql/columns/text.ts | 115 --------------- drizzle-orm/src/googlesql/columns/time.ts | 72 --------- .../src/googlesql/columns/timestamp.ts | 87 ++--------- drizzle-orm/src/googlesql/columns/tinyint.ts | 67 --------- .../src/googlesql/columns/varbinary.ts | 65 -------- drizzle-orm/src/googlesql/columns/varchar.ts | 84 ----------- drizzle-orm/src/googlesql/columns/year.ts | 50 ------- 33 files changed, 460 insertions(+), 1803 deletions(-) delete mode 100644 drizzle-orm/src/googlesql/columns/bigint.ts delete mode 100644 drizzle-orm/src/googlesql/columns/binary.ts create mode 100644 drizzle-orm/src/googlesql/columns/bytes.ts delete mode 100644 drizzle-orm/src/googlesql/columns/char.ts delete mode 100644 drizzle-orm/src/googlesql/columns/date.common.ts delete mode 100644 drizzle-orm/src/googlesql/columns/datetime.ts delete mode 100644 drizzle-orm/src/googlesql/columns/decimal.ts delete mode 100644 drizzle-orm/src/googlesql/columns/double.ts delete mode 100644 drizzle-orm/src/googlesql/columns/enum.ts delete mode 100644 drizzle-orm/src/googlesql/columns/float.ts create mode 100644 drizzle-orm/src/googlesql/columns/float32.ts create mode 100644 drizzle-orm/src/googlesql/columns/float64.ts delete mode 100644 drizzle-orm/src/googlesql/columns/int.ts create mode 100644 drizzle-orm/src/googlesql/columns/int64.ts delete mode 100644 drizzle-orm/src/googlesql/columns/mediumint.ts create mode 100644 drizzle-orm/src/googlesql/columns/numeric.ts delete mode 100644 drizzle-orm/src/googlesql/columns/real.ts delete mode 100644 drizzle-orm/src/googlesql/columns/serial.ts delete mode 100644 drizzle-orm/src/googlesql/columns/smallint.ts create mode 100644 drizzle-orm/src/googlesql/columns/string.ts delete mode 100644 drizzle-orm/src/googlesql/columns/text.ts delete mode 100644 drizzle-orm/src/googlesql/columns/time.ts delete mode 100644 drizzle-orm/src/googlesql/columns/tinyint.ts delete mode 100644 drizzle-orm/src/googlesql/columns/varbinary.ts delete mode 100644 drizzle-orm/src/googlesql/columns/varchar.ts delete mode 100644 drizzle-orm/src/googlesql/columns/year.ts diff --git a/drizzle-orm/src/googlesql/columns/all.ts b/drizzle-orm/src/googlesql/columns/all.ts index cf14d2e97c..9edbebcf86 100644 --- a/drizzle-orm/src/googlesql/columns/all.ts +++ b/drizzle-orm/src/googlesql/columns/all.ts @@ -1,57 +1,32 @@ -import { bigint } from './bigint.ts'; -import { binary } from './binary.ts'; +// import { bigint } from './bigint.ts'; +import { bytes } from './bytes.ts'; import { boolean } from './boolean.ts'; -import { char } from './char.ts'; -import { customType } from './custom.ts'; +import { string } from './string.ts'; +// import { customType } from './custom.ts'; import { date } from './date.ts'; -import { datetime } from './datetime.ts'; -import { decimal } from './decimal.ts'; -import { double } from './double.ts'; -import { googlesqlEnum } from './enum.ts'; -import { float } from './float.ts'; -import { int } from './int.ts'; +import { numeric } from './numeric.ts'; +import { float32 } from './float32.ts'; +import { int64 } from './int64.ts'; import { json } from './json.ts'; -import { mediumint } from './mediumint.ts'; -import { real } from './real.ts'; -import { serial } from './serial.ts'; -import { smallint } from './smallint.ts'; -import { longtext, mediumtext, text, tinytext } from './text.ts'; -import { time } from './time.ts'; import { timestamp } from './timestamp.ts'; -import { tinyint } from './tinyint.ts'; -import { varbinary } from './varbinary.ts'; -import { varchar } from './varchar.ts'; -import { year } from './year.ts'; +import { float64 } from './float64.ts'; export function getGoogleSqlColumnBuilders() { return { - bigint, - binary, + bytes, boolean, - char, - customType, + string, date, - datetime, - decimal, - double, - googlesqlEnum, - float, - int, + numeric, + float32, + float64, + int64, json, - mediumint, - real, - serial, - smallint, - text, - time, timestamp, - tinyint, - varbinary, - varchar, - year, - longtext, - mediumtext, - tinytext, + // TODO: suppport for more types: + // array, + // proto, + // customType, }; } diff --git a/drizzle-orm/src/googlesql/columns/bigint.ts b/drizzle-orm/src/googlesql/columns/bigint.ts deleted file mode 100644 index 8e21469ccb..0000000000 --- a/drizzle-orm/src/googlesql/columns/bigint.ts +++ /dev/null @@ -1,118 +0,0 @@ -import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnConfig } from '~/column-builder.ts'; -import type { ColumnBaseConfig } from '~/column.ts'; -import { entityKind } from '~/entity.ts'; -import type { AnyGoogleSqlTable } from '~/googlesql/table.ts'; -import { getColumnNameAndConfig } from '~/utils.ts'; -import { GoogleSqlColumnBuilderWithAutoIncrement, GoogleSqlColumnWithAutoIncrement } from './common.ts'; - -export type GoogleSqlBigInt53BuilderInitial = GoogleSqlBigInt53Builder<{ - name: TName; - dataType: 'number'; - columnType: 'GoogleSqlBigInt53'; - data: number; - driverParam: number | string; - enumValues: undefined; -}>; - -export class GoogleSqlBigInt53Builder> - extends GoogleSqlColumnBuilderWithAutoIncrement -{ - static override readonly [entityKind]: string = 'GoogleSqlBigInt53Builder'; - - constructor(name: T['name'], unsigned: boolean = false) { - super(name, 'number', 'GoogleSqlBigInt53'); - this.config.unsigned = unsigned; - } - - /** @internal */ - override build( - table: AnyGoogleSqlTable<{ name: TTableName }>, - ): GoogleSqlBigInt53> { - return new GoogleSqlBigInt53>( - table, - this.config as ColumnBuilderRuntimeConfig, - ); - } -} - -export class GoogleSqlBigInt53> - extends GoogleSqlColumnWithAutoIncrement -{ - static override readonly [entityKind]: string = 'GoogleSqlBigInt53'; - - getSQLType(): string { - return `bigint${this.config.unsigned ? ' unsigned' : ''}`; - } - - override mapFromDriverValue(value: number | string): number { - if (typeof value === 'number') { - return value; - } - return Number(value); - } -} - -export type GoogleSqlBigInt64BuilderInitial = GoogleSqlBigInt64Builder<{ - name: TName; - dataType: 'bigint'; - columnType: 'GoogleSqlBigInt64'; - data: bigint; - driverParam: string; - enumValues: undefined; -}>; - -export class GoogleSqlBigInt64Builder> - extends GoogleSqlColumnBuilderWithAutoIncrement -{ - static override readonly [entityKind]: string = 'GoogleSqlBigInt64Builder'; - - constructor(name: T['name'], unsigned: boolean = false) { - super(name, 'bigint', 'GoogleSqlBigInt64'); - this.config.unsigned = unsigned; - } - - /** @internal */ - override build( - table: AnyGoogleSqlTable<{ name: TTableName }>, - ): GoogleSqlBigInt64> { - return new GoogleSqlBigInt64>( - table, - this.config as ColumnBuilderRuntimeConfig, - ); - } -} - -export class GoogleSqlBigInt64> - extends GoogleSqlColumnWithAutoIncrement -{ - static override readonly [entityKind]: string = 'GoogleSqlBigInt64'; - - getSQLType(): string { - return `bigint${this.config.unsigned ? ' unsigned' : ''}`; - } - - // eslint-disable-next-line unicorn/prefer-native-coercion-functions - override mapFromDriverValue(value: string): bigint { - return BigInt(value); - } -} - -export interface GoogleSqlBigIntConfig { - mode: T; - unsigned?: boolean; -} - -export function bigint( - config: GoogleSqlBigIntConfig, -): TMode extends 'number' ? GoogleSqlBigInt53BuilderInitial<''> : GoogleSqlBigInt64BuilderInitial<''>; -export function bigint( - name: TName, - config: GoogleSqlBigIntConfig, -): TMode extends 'number' ? GoogleSqlBigInt53BuilderInitial : GoogleSqlBigInt64BuilderInitial; -export function bigint(a?: string | GoogleSqlBigIntConfig, b?: GoogleSqlBigIntConfig) { - const { name, config } = getColumnNameAndConfig(a, b); - if (config.mode === 'number') { - return new GoogleSqlBigInt53Builder(name, config.unsigned); - } - return new GoogleSqlBigInt64Builder(name, config.unsigned); -} diff --git a/drizzle-orm/src/googlesql/columns/binary.ts b/drizzle-orm/src/googlesql/columns/binary.ts deleted file mode 100644 index b0f7001ea7..0000000000 --- a/drizzle-orm/src/googlesql/columns/binary.ts +++ /dev/null @@ -1,69 +0,0 @@ -import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnConfig } from '~/column-builder.ts'; -import type { ColumnBaseConfig } from '~/column.ts'; -import { entityKind } from '~/entity.ts'; -import type { AnyGoogleSqlTable } from '~/googlesql/table.ts'; -import { getColumnNameAndConfig } from '~/utils.ts'; -import { GoogleSqlColumn, GoogleSqlColumnBuilder } from './common.ts'; - -export type GoogleSqlBinaryBuilderInitial = GoogleSqlBinaryBuilder<{ - name: TName; - dataType: 'string'; - columnType: 'GoogleSqlBinary'; - data: string; - driverParam: string; - enumValues: undefined; -}>; - -export class GoogleSqlBinaryBuilder> - extends GoogleSqlColumnBuilder< - T, - GoogleSqlBinaryConfig - > -{ - static override readonly [entityKind]: string = 'GoogleSqlBinaryBuilder'; - - constructor(name: T['name'], length: number | undefined) { - super(name, 'string', 'GoogleSqlBinary'); - this.config.length = length; - } - - /** @internal */ - override build( - table: AnyGoogleSqlTable<{ name: TTableName }>, - ): GoogleSqlBinary> { - return new GoogleSqlBinary>( - table, - this.config as ColumnBuilderRuntimeConfig, - ); - } -} - -export class GoogleSqlBinary> extends GoogleSqlColumn< - T, - GoogleSqlBinaryConfig -> { - static override readonly [entityKind]: string = 'GoogleSqlBinary'; - - length: number | undefined = this.config.length; - - getSQLType(): string { - return this.length === undefined ? `binary` : `binary(${this.length})`; - } -} - -export interface GoogleSqlBinaryConfig { - length?: number; -} - -export function binary(): GoogleSqlBinaryBuilderInitial<''>; -export function binary( - config?: GoogleSqlBinaryConfig, -): GoogleSqlBinaryBuilderInitial<''>; -export function binary( - name: TName, - config?: GoogleSqlBinaryConfig, -): GoogleSqlBinaryBuilderInitial; -export function binary(a?: string | GoogleSqlBinaryConfig, b: GoogleSqlBinaryConfig = {}) { - const { name, config } = getColumnNameAndConfig(a, b); - return new GoogleSqlBinaryBuilder(name, config.length); -} diff --git a/drizzle-orm/src/googlesql/columns/boolean.ts b/drizzle-orm/src/googlesql/columns/boolean.ts index 7f75796ff9..c2261d98a4 100644 --- a/drizzle-orm/src/googlesql/columns/boolean.ts +++ b/drizzle-orm/src/googlesql/columns/boolean.ts @@ -44,6 +44,8 @@ export class GoogleSqlBoolean = GoogleSqlBytesBuilder<{ + name: TName; + dataType: 'buffer'; + columnType: 'GoogleSqlBytes'; + data: Buffer; + driverParam: Buffer; + enumValues: undefined; +}>; + +export class GoogleSqlBytesBuilder> + extends GoogleSqlColumnBuilder< + T, + GoogleSqlBytesConfig + > +{ + static override readonly [entityKind]: string = 'GoogleSqlBytesBuilder'; + + constructor(name: T['name'], length: number | "MAX" | undefined) { + super(name, 'buffer', 'GoogleSqlBytes'); + this.config.length = length; + } + + /** @internal */ + override build( + table: AnyGoogleSqlTable<{ name: TTableName }>, + ): GoogleSqlBytes> { + return new GoogleSqlBytes>( + table, + this.config as ColumnBuilderRuntimeConfig, + ); + } +} + +export class GoogleSqlBytes> extends GoogleSqlColumn< + T, + GoogleSqlBytesConfig +> { + static override readonly [entityKind]: string = 'GoogleSqlBytes'; + + length: number | "MAX" | undefined = this.config.length; + + getSQLType(): string { + return `bytes(${this.length === undefined ? "MAX" : this.length})`; + } +} + +export interface GoogleSqlBytesConfig { + length?: number | "MAX"; +} + +export function bytes(): GoogleSqlBytesBuilderInitial<''>; +export function bytes( + config?: GoogleSqlBytesConfig, +): GoogleSqlBytesBuilderInitial<''>; +export function bytes( + name: TName, + config?: GoogleSqlBytesConfig, +): GoogleSqlBytesBuilderInitial; +export function bytes(a?: string | GoogleSqlBytesConfig, b: GoogleSqlBytesConfig = {}) { + const { name, config } = getColumnNameAndConfig(a, b); + return new GoogleSqlBytesBuilder(name, config.length); +} diff --git a/drizzle-orm/src/googlesql/columns/char.ts b/drizzle-orm/src/googlesql/columns/char.ts deleted file mode 100644 index 54749c9ff6..0000000000 --- a/drizzle-orm/src/googlesql/columns/char.ts +++ /dev/null @@ -1,85 +0,0 @@ -import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnConfig } from '~/column-builder.ts'; -import type { ColumnBaseConfig } from '~/column.ts'; -import { entityKind } from '~/entity.ts'; -import type { AnyGoogleSqlTable } from '~/googlesql/table.ts'; -import { getColumnNameAndConfig, type Writable } from '~/utils.ts'; -import { GoogleSqlColumn, GoogleSqlColumnBuilder } from './common.ts'; - -export type GoogleSqlCharBuilderInitial< - TName extends string, - TEnum extends [string, ...string[]], - TLength extends number | undefined, -> = GoogleSqlCharBuilder<{ - name: TName; - dataType: 'string'; - columnType: 'GoogleSqlChar'; - data: TEnum[number]; - driverParam: number | string; - enumValues: TEnum; - length: TLength; -}>; - -export class GoogleSqlCharBuilder< - T extends ColumnBuilderBaseConfig<'string', 'GoogleSqlChar'> & { length?: number | undefined }, -> extends GoogleSqlColumnBuilder< - T, - GoogleSqlCharConfig, - { length: T['length'] } -> { - static override readonly [entityKind]: string = 'GoogleSqlCharBuilder'; - - constructor(name: T['name'], config: GoogleSqlCharConfig) { - super(name, 'string', 'GoogleSqlChar'); - this.config.length = config.length; - this.config.enum = config.enum; - } - - /** @internal */ - override build( - table: AnyGoogleSqlTable<{ name: TTableName }>, - ): GoogleSqlChar & { length: T['length']; enumValues: T['enumValues'] }> { - return new GoogleSqlChar & { length: T['length']; enumValues: T['enumValues'] }>( - table, - this.config as ColumnBuilderRuntimeConfig, - ); - } -} - -export class GoogleSqlChar & { length?: number | undefined }> - extends GoogleSqlColumn, { length: T['length'] }> -{ - static override readonly [entityKind]: string = 'GoogleSqlChar'; - - readonly length: T['length'] = this.config.length; - override readonly enumValues = this.config.enum; - - getSQLType(): string { - return this.length === undefined ? `char` : `char(${this.length})`; - } -} - -export interface GoogleSqlCharConfig< - TEnum extends readonly string[] | string[] | undefined = readonly string[] | string[] | undefined, - TLength extends number | undefined = number | undefined, -> { - enum?: TEnum; - length?: TLength; -} - -export function char(): GoogleSqlCharBuilderInitial<'', [string, ...string[]], undefined>; -export function char, L extends number | undefined>( - config?: GoogleSqlCharConfig, L>, -): GoogleSqlCharBuilderInitial<'', Writable, L>; -export function char< - TName extends string, - U extends string, - T extends Readonly<[U, ...U[]]>, - L extends number | undefined, ->( - name: TName, - config?: GoogleSqlCharConfig, L>, -): GoogleSqlCharBuilderInitial, L>; -export function char(a?: string | GoogleSqlCharConfig, b: GoogleSqlCharConfig = {}): any { - const { name, config } = getColumnNameAndConfig(a, b); - return new GoogleSqlCharBuilder(name, config as any); -} diff --git a/drizzle-orm/src/googlesql/columns/common.ts b/drizzle-orm/src/googlesql/columns/common.ts index eedcba8979..d76c63e0e3 100644 --- a/drizzle-orm/src/googlesql/columns/common.ts +++ b/drizzle-orm/src/googlesql/columns/common.ts @@ -5,9 +5,9 @@ import type { ColumnBuilderExtraConfig, ColumnBuilderRuntimeConfig, ColumnDataType, - HasDefault, + // HasDefault, HasGenerated, - IsAutoincrement, + // IsAutoincrement, MakeColumnConfig, } from '~/column-builder.ts'; import type { ColumnBaseConfig } from '~/column.ts'; @@ -122,34 +122,36 @@ export type AnyGoogleSqlColumn, TPartial>> >; -export interface GoogleSqlColumnWithAutoIncrementConfig { - autoIncrement: boolean; -} - -export abstract class GoogleSqlColumnBuilderWithAutoIncrement< - T extends ColumnBuilderBaseConfig = ColumnBuilderBaseConfig, - TRuntimeConfig extends object = object, - TExtraConfig extends ColumnBuilderExtraConfig = ColumnBuilderExtraConfig, -> extends GoogleSqlColumnBuilder { - static override readonly [entityKind]: string = 'GoogleSqlColumnBuilderWithAutoIncrement'; - - constructor(name: NonNullable, dataType: T['dataType'], columnType: T['columnType']) { - super(name, dataType, columnType); - this.config.autoIncrement = false; - } - - autoincrement(): IsAutoincrement> { - this.config.autoIncrement = true; - this.config.hasDefault = true; - return this as IsAutoincrement>; - } -} - -export abstract class GoogleSqlColumnWithAutoIncrement< - T extends ColumnBaseConfig = ColumnBaseConfig, - TRuntimeConfig extends object = object, -> extends GoogleSqlColumn { - static override readonly [entityKind]: string = 'GoogleSqlColumnWithAutoIncrement'; - - readonly autoIncrement: boolean = this.config.autoIncrement; -} +// export interface GoogleSqlColumnWithAutoIncrementConfig { +// autoIncrement: boolean; +// } + + +// TODO - future work: add bit_reversed_positive autoGenerated column type +// export abstract class GoogleSqlColumnBuilderWithAutoIncrement< +// T extends ColumnBuilderBaseConfig = ColumnBuilderBaseConfig, +// TRuntimeConfig extends object = object, +// TExtraConfig extends ColumnBuilderExtraConfig = ColumnBuilderExtraConfig, +// > extends GoogleSqlColumnBuilder { +// static override readonly [entityKind]: string = 'GoogleSqlColumnBuilderWithAutoIncrement'; + +// constructor(name: NonNullable, dataType: T['dataType'], columnType: T['columnType']) { +// super(name, dataType, columnType); +// this.config.autoIncrement = false; +// } + +// autoincrement(): IsAutoincrement> { +// this.config.autoIncrement = true; +// this.config.hasDefault = true; +// return this as IsAutoincrement>; +// } +// } + +// export abstract class GoogleSqlColumnWithAutoIncrement< +// T extends ColumnBaseConfig = ColumnBaseConfig, +// TRuntimeConfig extends object = object, +// > extends GoogleSqlColumn { +// static override readonly [entityKind]: string = 'GoogleSqlColumnWithAutoIncrement'; + +// readonly autoIncrement: boolean = this.config.autoIncrement; +// } diff --git a/drizzle-orm/src/googlesql/columns/custom.ts b/drizzle-orm/src/googlesql/columns/custom.ts index 8252bc7d73..9721a8d53f 100644 --- a/drizzle-orm/src/googlesql/columns/custom.ts +++ b/drizzle-orm/src/googlesql/columns/custom.ts @@ -6,6 +6,7 @@ import type { SQL } from '~/sql/sql.ts'; import { type Equal, getColumnNameAndConfig } from '~/utils.ts'; import { GoogleSqlColumn, GoogleSqlColumnBuilder } from './common.ts'; +// TODO - SPANNER: Low priority, make sure it works. export type ConvertCustomConfig> = & { name: TName; diff --git a/drizzle-orm/src/googlesql/columns/date.common.ts b/drizzle-orm/src/googlesql/columns/date.common.ts deleted file mode 100644 index 26397bb4bd..0000000000 --- a/drizzle-orm/src/googlesql/columns/date.common.ts +++ /dev/null @@ -1,42 +0,0 @@ -import type { - ColumnBuilderBaseConfig, - ColumnBuilderExtraConfig, - ColumnDataType, - HasDefault, -} from '~/column-builder.ts'; -import type { ColumnBaseConfig } from '~/column.ts'; -import { entityKind } from '~/entity.ts'; -import { sql } from '~/sql/sql.ts'; -import { GoogleSqlColumn, GoogleSqlColumnBuilder } from './common.ts'; - -export interface GoogleSqlDateColumnBaseConfig { - hasOnUpdateNow: boolean; -} - -export abstract class GoogleSqlDateColumnBaseBuilder< - T extends ColumnBuilderBaseConfig, - TRuntimeConfig extends object = object, - TExtraConfig extends ColumnBuilderExtraConfig = ColumnBuilderExtraConfig, -> extends GoogleSqlColumnBuilder { - static override readonly [entityKind]: string = 'GoogleSqlDateColumnBuilder'; - - defaultNow() { - return this.default(sql`(now())`); - } - - // "on update now" also adds an implicit default value to the column - https://dev.mysql.com/doc/refman/8.0/en/timestamp-initialization.html - onUpdateNow(): HasDefault { - this.config.hasOnUpdateNow = true; - this.config.hasDefault = true; - return this as HasDefault; - } -} - -export abstract class GoogleSqlDateBaseColumn< - T extends ColumnBaseConfig, - TRuntimeConfig extends object = object, -> extends GoogleSqlColumn { - static override readonly [entityKind]: string = 'GoogleSqlDateColumn'; - - readonly hasOnUpdateNow: boolean = this.config.hasOnUpdateNow; -} diff --git a/drizzle-orm/src/googlesql/columns/date.ts b/drizzle-orm/src/googlesql/columns/date.ts index 1bffecaeb0..32f464686b 100644 --- a/drizzle-orm/src/googlesql/columns/date.ts +++ b/drizzle-orm/src/googlesql/columns/date.ts @@ -2,7 +2,6 @@ import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnCon import type { ColumnBaseConfig } from '~/column.ts'; import { entityKind } from '~/entity.ts'; import type { AnyGoogleSqlTable } from '~/googlesql/table.ts'; -import { type Equal, getColumnNameAndConfig } from '~/utils.ts'; import { GoogleSqlColumn, GoogleSqlColumnBuilder } from './common.ts'; export type GoogleSqlDateBuilderInitial = GoogleSqlDateBuilder<{ @@ -34,6 +33,8 @@ export class GoogleSqlDateBuilder> extends GoogleSqlColumn { static override readonly [entityKind]: string = 'GoogleSqlDate'; @@ -51,70 +52,16 @@ export class GoogleSqlDate> override mapFromDriverValue(value: string): Date { return new Date(value); } -} - -export type GoogleSqlDateStringBuilderInitial = GoogleSqlDateStringBuilder<{ - name: TName; - dataType: 'string'; - columnType: 'GoogleSqlDateString'; - data: string; - driverParam: string | number; - enumValues: undefined; -}>; - -export class GoogleSqlDateStringBuilder> - extends GoogleSqlColumnBuilder -{ - static override readonly [entityKind]: string = 'GoogleSqlDateStringBuilder'; - constructor(name: T['name']) { - super(name, 'string', 'GoogleSqlDateString'); - } - - /** @internal */ - override build( - table: AnyGoogleSqlTable<{ name: TTableName }>, - ): GoogleSqlDateString> { - return new GoogleSqlDateString>( - table, - this.config as ColumnBuilderRuntimeConfig, - ); + override mapToDriverValue(value: Date): string { + return value.toISOString().split('T')[0]!; } } -export class GoogleSqlDateString> - extends GoogleSqlColumn -{ - static override readonly [entityKind]: string = 'GoogleSqlDateString'; - - constructor( - table: AnyGoogleSqlTable<{ name: T['tableName'] }>, - config: GoogleSqlDateStringBuilder['config'], - ) { - super(table, config); - } - - getSQLType(): string { - return `date`; - } -} - -export interface GoogleSqlDateConfig { - mode?: TMode; -} - export function date(): GoogleSqlDateBuilderInitial<''>; -export function date( - config?: GoogleSqlDateConfig, -): Equal extends true ? GoogleSqlDateStringBuilderInitial<''> : GoogleSqlDateBuilderInitial<''>; -export function date( - name: TName, - config?: GoogleSqlDateConfig, -): Equal extends true ? GoogleSqlDateStringBuilderInitial : GoogleSqlDateBuilderInitial; -export function date(a?: string | GoogleSqlDateConfig, b?: GoogleSqlDateConfig) { - const { name, config } = getColumnNameAndConfig(a, b); - if (config?.mode === 'string') { - return new GoogleSqlDateStringBuilder(name); - } - return new GoogleSqlDateBuilder(name); +export function date( + name: TName +): GoogleSqlDateBuilderInitial; +export function date(name?: string) { + return new GoogleSqlDateBuilder(name ?? ''); } diff --git a/drizzle-orm/src/googlesql/columns/datetime.ts b/drizzle-orm/src/googlesql/columns/datetime.ts deleted file mode 100644 index 81281beeee..0000000000 --- a/drizzle-orm/src/googlesql/columns/datetime.ts +++ /dev/null @@ -1,139 +0,0 @@ -import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnConfig } from '~/column-builder.ts'; -import type { ColumnBaseConfig } from '~/column.ts'; -import { entityKind } from '~/entity.ts'; -import type { AnyGoogleSqlTable } from '~/googlesql/table.ts'; -import { type Equal, getColumnNameAndConfig } from '~/utils.ts'; -import { GoogleSqlColumn, GoogleSqlColumnBuilder } from './common.ts'; - -export type GoogleSqlDateTimeBuilderInitial = GoogleSqlDateTimeBuilder<{ - name: TName; - dataType: 'date'; - columnType: 'GoogleSqlDateTime'; - data: Date; - driverParam: string | number; - enumValues: undefined; -}>; - -export class GoogleSqlDateTimeBuilder> - extends GoogleSqlColumnBuilder -{ - static override readonly [entityKind]: string = 'GoogleSqlDateTimeBuilder'; - - constructor(name: T['name'], config: GoogleSqlDatetimeConfig | undefined) { - super(name, 'date', 'GoogleSqlDateTime'); - this.config.fsp = config?.fsp; - } - - /** @internal */ - override build( - table: AnyGoogleSqlTable<{ name: TTableName }>, - ): GoogleSqlDateTime> { - return new GoogleSqlDateTime>( - table, - this.config as ColumnBuilderRuntimeConfig, - ); - } -} - -export class GoogleSqlDateTime> extends GoogleSqlColumn { - static override readonly [entityKind]: string = 'GoogleSqlDateTime'; - - readonly fsp: number | undefined; - - constructor( - table: AnyGoogleSqlTable<{ name: T['tableName'] }>, - config: GoogleSqlDateTimeBuilder['config'], - ) { - super(table, config); - this.fsp = config.fsp; - } - - getSQLType(): string { - const precision = this.fsp === undefined ? '' : `(${this.fsp})`; - return `datetime${precision}`; - } - - override mapToDriverValue(value: Date): unknown { - return value.toISOString().replace('T', ' ').replace('Z', ''); - } - - override mapFromDriverValue(value: string): Date { - return new Date(value.replace(' ', 'T') + 'Z'); - } -} - -export type GoogleSqlDateTimeStringBuilderInitial = GoogleSqlDateTimeStringBuilder<{ - name: TName; - dataType: 'string'; - columnType: 'GoogleSqlDateTimeString'; - data: string; - driverParam: string | number; - enumValues: undefined; -}>; - -export class GoogleSqlDateTimeStringBuilder> - extends GoogleSqlColumnBuilder -{ - static override readonly [entityKind]: string = 'GoogleSqlDateTimeStringBuilder'; - - constructor(name: T['name'], config: GoogleSqlDatetimeConfig | undefined) { - super(name, 'string', 'GoogleSqlDateTimeString'); - this.config.fsp = config?.fsp; - } - - /** @internal */ - override build( - table: AnyGoogleSqlTable<{ name: TTableName }>, - ): GoogleSqlDateTimeString> { - return new GoogleSqlDateTimeString>( - table, - this.config as ColumnBuilderRuntimeConfig, - ); - } -} - -export class GoogleSqlDateTimeString> - extends GoogleSqlColumn -{ - static override readonly [entityKind]: string = 'GoogleSqlDateTimeString'; - - readonly fsp: number | undefined; - - constructor( - table: AnyGoogleSqlTable<{ name: T['tableName'] }>, - config: GoogleSqlDateTimeStringBuilder['config'], - ) { - super(table, config); - this.fsp = config.fsp; - } - - getSQLType(): string { - const precision = this.fsp === undefined ? '' : `(${this.fsp})`; - return `datetime${precision}`; - } -} - -export type DatetimeFsp = 0 | 1 | 2 | 3 | 4 | 5 | 6; - -export interface GoogleSqlDatetimeConfig { - mode?: TMode; - fsp?: DatetimeFsp; -} - -export function datetime(): GoogleSqlDateTimeBuilderInitial<''>; -export function datetime( - config?: GoogleSqlDatetimeConfig, -): Equal extends true ? GoogleSqlDateTimeStringBuilderInitial<''> - : GoogleSqlDateTimeBuilderInitial<''>; -export function datetime( - name: TName, - config?: GoogleSqlDatetimeConfig, -): Equal extends true ? GoogleSqlDateTimeStringBuilderInitial - : GoogleSqlDateTimeBuilderInitial; -export function datetime(a?: string | GoogleSqlDatetimeConfig, b?: GoogleSqlDatetimeConfig) { - const { name, config } = getColumnNameAndConfig(a, b); - if (config?.mode === 'string') { - return new GoogleSqlDateTimeStringBuilder(name, config); - } - return new GoogleSqlDateTimeBuilder(name, config); -} diff --git a/drizzle-orm/src/googlesql/columns/decimal.ts b/drizzle-orm/src/googlesql/columns/decimal.ts deleted file mode 100644 index 0738a3afc9..0000000000 --- a/drizzle-orm/src/googlesql/columns/decimal.ts +++ /dev/null @@ -1,80 +0,0 @@ -import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnConfig } from '~/column-builder.ts'; -import type { ColumnBaseConfig } from '~/column.ts'; -import { entityKind } from '~/entity.ts'; -import type { AnyGoogleSqlTable } from '~/googlesql/table.ts'; -import { getColumnNameAndConfig } from '~/utils.ts'; -import { GoogleSqlColumnBuilderWithAutoIncrement, GoogleSqlColumnWithAutoIncrement } from './common.ts'; - -export type GoogleSqlDecimalBuilderInitial = GoogleSqlDecimalBuilder<{ - name: TName; - dataType: 'string'; - columnType: 'GoogleSqlDecimal'; - data: string; - driverParam: string; - enumValues: undefined; -}>; - -export class GoogleSqlDecimalBuilder< - T extends ColumnBuilderBaseConfig<'string', 'GoogleSqlDecimal'>, -> extends GoogleSqlColumnBuilderWithAutoIncrement { - static override readonly [entityKind]: string = 'GoogleSqlDecimalBuilder'; - - constructor(name: T['name'], config: GoogleSqlDecimalConfig | undefined) { - super(name, 'string', 'GoogleSqlDecimal'); - this.config.precision = config?.precision; - this.config.scale = config?.scale; - this.config.unsigned = config?.unsigned; - } - - /** @internal */ - override build( - table: AnyGoogleSqlTable<{ name: TTableName }>, - ): GoogleSqlDecimal> { - return new GoogleSqlDecimal>( - table, - this.config as ColumnBuilderRuntimeConfig, - ); - } -} - -export class GoogleSqlDecimal> - extends GoogleSqlColumnWithAutoIncrement -{ - static override readonly [entityKind]: string = 'GoogleSqlDecimal'; - - readonly precision: number | undefined = this.config.precision; - readonly scale: number | undefined = this.config.scale; - readonly unsigned: boolean | undefined = this.config.unsigned; - - getSQLType(): string { - let type = ''; - if (this.precision !== undefined && this.scale !== undefined) { - type += `decimal(${this.precision},${this.scale})`; - } else if (this.precision === undefined) { - type += 'decimal'; - } else { - type += `decimal(${this.precision})`; - } - type = type === 'decimal(10,0)' || type === 'decimal(10)' ? 'decimal' : type; - return this.unsigned ? `${type} unsigned` : type; - } -} - -export interface GoogleSqlDecimalConfig { - precision?: number; - scale?: number; - unsigned?: boolean; -} - -export function decimal(): GoogleSqlDecimalBuilderInitial<''>; -export function decimal( - config: GoogleSqlDecimalConfig, -): GoogleSqlDecimalBuilderInitial<''>; -export function decimal( - name: TName, - config?: GoogleSqlDecimalConfig, -): GoogleSqlDecimalBuilderInitial; -export function decimal(a?: string | GoogleSqlDecimalConfig, b: GoogleSqlDecimalConfig = {}) { - const { name, config } = getColumnNameAndConfig(a, b); - return new GoogleSqlDecimalBuilder(name, config); -} diff --git a/drizzle-orm/src/googlesql/columns/double.ts b/drizzle-orm/src/googlesql/columns/double.ts deleted file mode 100644 index 01d4f372df..0000000000 --- a/drizzle-orm/src/googlesql/columns/double.ts +++ /dev/null @@ -1,79 +0,0 @@ -import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnConfig } from '~/column-builder.ts'; -import type { ColumnBaseConfig } from '~/column.ts'; -import { entityKind } from '~/entity.ts'; -import type { AnyGoogleSqlTable } from '~/googlesql/table.ts'; -import { getColumnNameAndConfig } from '~/utils.ts'; -import { GoogleSqlColumnBuilderWithAutoIncrement, GoogleSqlColumnWithAutoIncrement } from './common.ts'; - -export type GoogleSqlDoubleBuilderInitial = GoogleSqlDoubleBuilder<{ - name: TName; - dataType: 'number'; - columnType: 'GoogleSqlDouble'; - data: number; - driverParam: number | string; - enumValues: undefined; -}>; - -export class GoogleSqlDoubleBuilder> - extends GoogleSqlColumnBuilderWithAutoIncrement -{ - static override readonly [entityKind]: string = 'GoogleSqlDoubleBuilder'; - - constructor(name: T['name'], config: GoogleSqlDoubleConfig | undefined) { - super(name, 'number', 'GoogleSqlDouble'); - this.config.precision = config?.precision; - this.config.scale = config?.scale; - this.config.unsigned = config?.unsigned; - } - - /** @internal */ - override build( - table: AnyGoogleSqlTable<{ name: TTableName }>, - ): GoogleSqlDouble> { - return new GoogleSqlDouble>( - table, - this.config as ColumnBuilderRuntimeConfig, - ); - } -} - -export class GoogleSqlDouble> - extends GoogleSqlColumnWithAutoIncrement -{ - static override readonly [entityKind]: string = 'GoogleSqlDouble'; - - readonly precision: number | undefined = this.config.precision; - readonly scale: number | undefined = this.config.scale; - readonly unsigned: boolean | undefined = this.config.unsigned; - - getSQLType(): string { - let type = ''; - if (this.precision !== undefined && this.scale !== undefined) { - type += `double(${this.precision},${this.scale})`; - } else if (this.precision === undefined) { - type += 'double'; - } else { - type += `double(${this.precision})`; - } - return this.unsigned ? `${type} unsigned` : type; - } -} - -export interface GoogleSqlDoubleConfig { - precision?: number; - scale?: number; - unsigned?: boolean; -} - -export function double(): GoogleSqlDoubleBuilderInitial<''>; -export function double( - config?: GoogleSqlDoubleConfig, -): GoogleSqlDoubleBuilderInitial<''>; -export function double( - name: TName, - config?: GoogleSqlDoubleConfig, -): GoogleSqlDoubleBuilderInitial; -export function double(a?: string | GoogleSqlDoubleConfig, b?: GoogleSqlDoubleConfig) { - const { name, config } = getColumnNameAndConfig(a, b); - return new GoogleSqlDoubleBuilder(name, config); -} diff --git a/drizzle-orm/src/googlesql/columns/enum.ts b/drizzle-orm/src/googlesql/columns/enum.ts deleted file mode 100644 index 126a62fe53..0000000000 --- a/drizzle-orm/src/googlesql/columns/enum.ts +++ /dev/null @@ -1,69 +0,0 @@ -import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnConfig } from '~/column-builder.ts'; -import type { ColumnBaseConfig } from '~/column.ts'; -import { entityKind } from '~/entity.ts'; -import type { AnyGoogleSqlTable } from '~/googlesql/table.ts'; -import { getColumnNameAndConfig, type Writable } from '~/utils.ts'; -import { GoogleSqlColumn, GoogleSqlColumnBuilder } from './common.ts'; - -export type GoogleSqlEnumColumnBuilderInitial = - GoogleSqlEnumColumnBuilder<{ - name: TName; - dataType: 'string'; - columnType: 'GoogleSqlEnumColumn'; - data: TEnum[number]; - driverParam: string; - enumValues: TEnum; - }>; - -export class GoogleSqlEnumColumnBuilder> - extends GoogleSqlColumnBuilder -{ - static override readonly [entityKind]: string = 'GoogleSqlEnumColumnBuilder'; - - constructor(name: T['name'], values: T['enumValues']) { - super(name, 'string', 'GoogleSqlEnumColumn'); - this.config.enumValues = values; - } - - /** @internal */ - override build( - table: AnyGoogleSqlTable<{ name: TTableName }>, - ): GoogleSqlEnumColumn & { enumValues: T['enumValues'] }> { - return new GoogleSqlEnumColumn & { enumValues: T['enumValues'] }>( - table, - this.config as ColumnBuilderRuntimeConfig, - ); - } -} - -export class GoogleSqlEnumColumn> - extends GoogleSqlColumn -{ - static override readonly [entityKind]: string = 'GoogleSqlEnumColumn'; - - override readonly enumValues = this.config.enumValues; - - getSQLType(): string { - return `enum(${this.enumValues!.map((value) => `'${value}'`).join(',')})`; - } -} - -export function googlesqlEnum>( - values: T | Writable, -): GoogleSqlEnumColumnBuilderInitial<'', Writable>; -export function googlesqlEnum>( - name: TName, - values: T | Writable, -): GoogleSqlEnumColumnBuilderInitial>; -export function googlesqlEnum( - a?: string | readonly [string, ...string[]] | [string, ...string[]], - b?: readonly [string, ...string[]] | [string, ...string[]], -): any { - const { name, config: values } = getColumnNameAndConfig(a, b); - - if (values.length === 0) { - throw new Error(`You have an empty array for "${name}" enum values`); - } - - return new GoogleSqlEnumColumnBuilder(name, values as any); -} diff --git a/drizzle-orm/src/googlesql/columns/float.ts b/drizzle-orm/src/googlesql/columns/float.ts deleted file mode 100644 index a37e10cd0c..0000000000 --- a/drizzle-orm/src/googlesql/columns/float.ts +++ /dev/null @@ -1,79 +0,0 @@ -import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnConfig } from '~/column-builder.ts'; -import type { ColumnBaseConfig } from '~/column.ts'; -import { entityKind } from '~/entity.ts'; -import type { AnyGoogleSqlTable } from '~/googlesql/table.ts'; -import { getColumnNameAndConfig } from '~/utils.ts'; -import { GoogleSqlColumnBuilderWithAutoIncrement, GoogleSqlColumnWithAutoIncrement } from './common.ts'; - -export type GoogleSqlFloatBuilderInitial = GoogleSqlFloatBuilder<{ - name: TName; - dataType: 'number'; - columnType: 'GoogleSqlFloat'; - data: number; - driverParam: number | string; - enumValues: undefined; -}>; - -export class GoogleSqlFloatBuilder> - extends GoogleSqlColumnBuilderWithAutoIncrement -{ - static override readonly [entityKind]: string = 'GoogleSqlFloatBuilder'; - - constructor(name: T['name'], config: GoogleSqlFloatConfig | undefined) { - super(name, 'number', 'GoogleSqlFloat'); - this.config.precision = config?.precision; - this.config.scale = config?.scale; - this.config.unsigned = config?.unsigned; - } - - /** @internal */ - override build( - table: AnyGoogleSqlTable<{ name: TTableName }>, - ): GoogleSqlFloat> { - return new GoogleSqlFloat>( - table, - this.config as ColumnBuilderRuntimeConfig, - ); - } -} - -export class GoogleSqlFloat> - extends GoogleSqlColumnWithAutoIncrement -{ - static override readonly [entityKind]: string = 'GoogleSqlFloat'; - - readonly precision: number | undefined = this.config.precision; - readonly scale: number | undefined = this.config.scale; - readonly unsigned: boolean | undefined = this.config.unsigned; - - getSQLType(): string { - let type = ''; - if (this.precision !== undefined && this.scale !== undefined) { - type += `float(${this.precision},${this.scale})`; - } else if (this.precision === undefined) { - type += 'float'; - } else { - type += `float(${this.precision})`; - } - return this.unsigned ? `${type} unsigned` : type; - } -} - -export interface GoogleSqlFloatConfig { - precision?: number; - scale?: number; - unsigned?: boolean; -} - -export function float(): GoogleSqlFloatBuilderInitial<''>; -export function float( - config?: GoogleSqlFloatConfig, -): GoogleSqlFloatBuilderInitial<''>; -export function float( - name: TName, - config?: GoogleSqlFloatConfig, -): GoogleSqlFloatBuilderInitial; -export function float(a?: string | GoogleSqlFloatConfig, b?: GoogleSqlFloatConfig) { - const { name, config } = getColumnNameAndConfig(a, b); - return new GoogleSqlFloatBuilder(name, config); -} diff --git a/drizzle-orm/src/googlesql/columns/float32.ts b/drizzle-orm/src/googlesql/columns/float32.ts new file mode 100644 index 0000000000..01be056bfd --- /dev/null +++ b/drizzle-orm/src/googlesql/columns/float32.ts @@ -0,0 +1,52 @@ +import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnConfig } from '~/column-builder.ts'; +import type { ColumnBaseConfig } from '~/column.ts'; +import { entityKind } from '~/entity.ts'; +import type { AnyGoogleSqlTable } from '~/googlesql/table.ts'; +import { GoogleSqlColumnBuilder, GoogleSqlColumn } from './common.ts'; + +export type GoogleSqlFloat32BuilderInitial = GoogleSqlFloat32Builder<{ + name: TName; + dataType: 'number'; + columnType: 'GoogleSqlFloat32'; + data: number; + driverParam: number | string; + enumValues: undefined; +}>; + +export class GoogleSqlFloat32Builder> + extends GoogleSqlColumnBuilder +{ + static override readonly [entityKind]: string = 'GoogleSqlFloat32Builder'; + + constructor(name: T['name']) { + super(name, 'number', 'GoogleSqlFloat32'); + } + + /** @internal */ + override build( + table: AnyGoogleSqlTable<{ name: TTableName }>, + ): GoogleSqlFloat32> { + return new GoogleSqlFloat32>( + table, + this.config as ColumnBuilderRuntimeConfig, + ); + } +} + +export class GoogleSqlFloat32> + extends GoogleSqlColumn +{ + static override readonly [entityKind]: string = 'GoogleSqlFloat32'; + + getSQLType(): string { + return 'float32'; + } +} + +export function float32(): GoogleSqlFloat32BuilderInitial<''>; +export function float32( + name: TName, +): GoogleSqlFloat32BuilderInitial; +export function float32(name?: string) { + return new GoogleSqlFloat32Builder(name ?? ''); +} diff --git a/drizzle-orm/src/googlesql/columns/float64.ts b/drizzle-orm/src/googlesql/columns/float64.ts new file mode 100644 index 0000000000..260e201261 --- /dev/null +++ b/drizzle-orm/src/googlesql/columns/float64.ts @@ -0,0 +1,52 @@ +import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnConfig } from '~/column-builder.ts'; +import type { ColumnBaseConfig } from '~/column.ts'; +import { entityKind } from '~/entity.ts'; +import type { AnyGoogleSqlTable } from '~/googlesql/table.ts'; +import { GoogleSqlColumnBuilder, GoogleSqlColumn } from './common.ts'; + +export type GoogleSqlFloat64BuilderInitial = GoogleSqlFloat64Builder<{ + name: TName; + dataType: 'number'; + columnType: 'GoogleSqlFloat64'; + data: number; + driverParam: number | string; + enumValues: undefined; +}>; + +export class GoogleSqlFloat64Builder> + extends GoogleSqlColumnBuilder +{ + static override readonly [entityKind]: string = 'GoogleSqlFloat64Builder'; + + constructor(name: T['name']) { + super(name, 'number', 'GoogleSqlFloat64'); + } + + /** @internal */ + override build( + table: AnyGoogleSqlTable<{ name: TTableName }>, + ): GoogleSqlFloat64> { + return new GoogleSqlFloat64>( + table, + this.config as ColumnBuilderRuntimeConfig, + ); + } +} + +export class GoogleSqlFloat64> + extends GoogleSqlColumn +{ + static override readonly [entityKind]: string = 'GoogleSqlFloat64'; + + getSQLType(): string { + return 'float64'; + } +} + +export function float64(): GoogleSqlFloat64BuilderInitial<''>; +export function float64( + name: TName, +): GoogleSqlFloat64BuilderInitial; +export function float64(name?: string) { + return new GoogleSqlFloat64Builder(name ?? ''); +} diff --git a/drizzle-orm/src/googlesql/columns/index.ts b/drizzle-orm/src/googlesql/columns/index.ts index b51f0fac48..67bdc26947 100644 --- a/drizzle-orm/src/googlesql/columns/index.ts +++ b/drizzle-orm/src/googlesql/columns/index.ts @@ -1,25 +1,13 @@ -export * from './bigint.ts'; -export * from './binary.ts'; +// export * from './bigint.ts'; export * from './boolean.ts'; -export * from './char.ts'; +export * from './bytes.ts'; export * from './common.ts'; -export * from './custom.ts'; +// export * from './custom.ts'; export * from './date.ts'; -export * from './datetime.ts'; -export * from './decimal.ts'; -export * from './double.ts'; -export * from './enum.ts'; -export * from './float.ts'; -export * from './int.ts'; +export * from './float32.ts'; +export * from './float64.ts'; +export * from './int64.ts'; export * from './json.ts'; -export * from './mediumint.ts'; -export * from './real.ts'; -export * from './serial.ts'; -export * from './smallint.ts'; -export * from './text.ts'; -export * from './time.ts'; +export * from './numeric.ts'; +export * from './string.ts'; export * from './timestamp.ts'; -export * from './tinyint.ts'; -export * from './varbinary.ts'; -export * from './varchar.ts'; -export * from './year.ts'; diff --git a/drizzle-orm/src/googlesql/columns/int.ts b/drizzle-orm/src/googlesql/columns/int.ts deleted file mode 100644 index fe143c8d34..0000000000 --- a/drizzle-orm/src/googlesql/columns/int.ts +++ /dev/null @@ -1,70 +0,0 @@ -import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnConfig } from '~/column-builder.ts'; -import type { ColumnBaseConfig } from '~/column.ts'; -import { entityKind } from '~/entity.ts'; -import type { AnyGoogleSqlTable } from '~/googlesql/table.ts'; -import { getColumnNameAndConfig } from '~/utils.ts'; -import { GoogleSqlColumnBuilderWithAutoIncrement, GoogleSqlColumnWithAutoIncrement } from './common.ts'; - -export type GoogleSqlIntBuilderInitial = GoogleSqlIntBuilder<{ - name: TName; - dataType: 'number'; - columnType: 'GoogleSqlInt'; - data: number; - driverParam: number | string; - enumValues: undefined; -}>; - -export class GoogleSqlIntBuilder> - extends GoogleSqlColumnBuilderWithAutoIncrement -{ - static override readonly [entityKind]: string = 'GoogleSqlIntBuilder'; - - constructor(name: T['name'], config?: GoogleSqlIntConfig) { - super(name, 'number', 'GoogleSqlInt'); - this.config.unsigned = config ? config.unsigned : false; - } - - /** @internal */ - override build( - table: AnyGoogleSqlTable<{ name: TTableName }>, - ): GoogleSqlInt> { - return new GoogleSqlInt>( - table, - this.config as ColumnBuilderRuntimeConfig, - ); - } -} - -export class GoogleSqlInt> - extends GoogleSqlColumnWithAutoIncrement -{ - static override readonly [entityKind]: string = 'GoogleSqlInt'; - - getSQLType(): string { - return `int${this.config.unsigned ? ' unsigned' : ''}`; - } - - override mapFromDriverValue(value: number | string): number { - if (typeof value === 'string') { - return Number(value); - } - return value; - } -} - -export interface GoogleSqlIntConfig { - unsigned?: boolean; -} - -export function int(): GoogleSqlIntBuilderInitial<''>; -export function int( - config?: GoogleSqlIntConfig, -): GoogleSqlIntBuilderInitial<''>; -export function int( - name: TName, - config?: GoogleSqlIntConfig, -): GoogleSqlIntBuilderInitial; -export function int(a?: string | GoogleSqlIntConfig, b?: GoogleSqlIntConfig) { - const { name, config } = getColumnNameAndConfig(a, b); - return new GoogleSqlIntBuilder(name, config); -} diff --git a/drizzle-orm/src/googlesql/columns/int64.ts b/drizzle-orm/src/googlesql/columns/int64.ts new file mode 100644 index 0000000000..7d6f3373c5 --- /dev/null +++ b/drizzle-orm/src/googlesql/columns/int64.ts @@ -0,0 +1,59 @@ +import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnConfig } from '~/column-builder.ts'; +import type { ColumnBaseConfig } from '~/column.ts'; +import { entityKind } from '~/entity.ts'; +import type { AnyGoogleSqlTable } from '~/googlesql/table.ts'; +import { GoogleSqlColumn, GoogleSqlColumnBuilder } from './common.ts'; + +export type GoogleSqlInt64BuilderInitial = GoogleSqlInt64Builder<{ + name: TName; + dataType: 'number'; + columnType: 'GoogleSqlInt64'; + data: number; + driverParam: number | string; + enumValues: undefined; +}>; + +export class GoogleSqlInt64Builder> + extends GoogleSqlColumnBuilder +{ + static override readonly [entityKind]: string = 'GoogleSqlInt64Builder'; + + constructor(name: T['name']) { + super(name, 'number', 'GoogleSqlInt64'); + } + + /** @internal */ + override build( + table: AnyGoogleSqlTable<{ name: TTableName }>, + ): GoogleSqlInt64> { + return new GoogleSqlInt64>( + table, + this.config as ColumnBuilderRuntimeConfig, + ); + } +} + +export class GoogleSqlInt64> + extends GoogleSqlColumn +{ + static override readonly [entityKind]: string = 'GoogleSqlInt64'; + + getSQLType(): string { + return `int64`; + } + + override mapFromDriverValue(value: number | string): number { + if (typeof value === 'string') { + return Number(value); + } + return value; + } +} + +export function int64(): GoogleSqlInt64BuilderInitial<''>; +export function int64( + name: TName +): GoogleSqlInt64BuilderInitial; +export function int64(name?: string) { + return new GoogleSqlInt64Builder(name ?? ''); +} diff --git a/drizzle-orm/src/googlesql/columns/mediumint.ts b/drizzle-orm/src/googlesql/columns/mediumint.ts deleted file mode 100644 index 915eb47916..0000000000 --- a/drizzle-orm/src/googlesql/columns/mediumint.ts +++ /dev/null @@ -1,67 +0,0 @@ -import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnConfig } from '~/column-builder.ts'; -import type { ColumnBaseConfig } from '~/column.ts'; -import { entityKind } from '~/entity.ts'; -import type { AnyGoogleSqlTable } from '~/googlesql/table.ts'; -import { getColumnNameAndConfig } from '~/utils.ts'; -import { GoogleSqlColumnBuilderWithAutoIncrement, GoogleSqlColumnWithAutoIncrement } from './common.ts'; -import type { GoogleSqlIntConfig } from './int.ts'; - -export type GoogleSqlMediumIntBuilderInitial = GoogleSqlMediumIntBuilder<{ - name: TName; - dataType: 'number'; - columnType: 'GoogleSqlMediumInt'; - data: number; - driverParam: number | string; - enumValues: undefined; -}>; - -export class GoogleSqlMediumIntBuilder> - extends GoogleSqlColumnBuilderWithAutoIncrement -{ - static override readonly [entityKind]: string = 'GoogleSqlMediumIntBuilder'; - - constructor(name: T['name'], config?: GoogleSqlIntConfig) { - super(name, 'number', 'GoogleSqlMediumInt'); - this.config.unsigned = config ? config.unsigned : false; - } - - /** @internal */ - override build( - table: AnyGoogleSqlTable<{ name: TTableName }>, - ): GoogleSqlMediumInt> { - return new GoogleSqlMediumInt>( - table, - this.config as ColumnBuilderRuntimeConfig, - ); - } -} - -export class GoogleSqlMediumInt> - extends GoogleSqlColumnWithAutoIncrement -{ - static override readonly [entityKind]: string = 'GoogleSqlMediumInt'; - - getSQLType(): string { - return `mediumint${this.config.unsigned ? ' unsigned' : ''}`; - } - - override mapFromDriverValue(value: number | string): number { - if (typeof value === 'string') { - return Number(value); - } - return value; - } -} - -export function mediumint(): GoogleSqlMediumIntBuilderInitial<''>; -export function mediumint( - config?: GoogleSqlIntConfig, -): GoogleSqlMediumIntBuilderInitial<''>; -export function mediumint( - name: TName, - config?: GoogleSqlIntConfig, -): GoogleSqlMediumIntBuilderInitial; -export function mediumint(a?: string | GoogleSqlIntConfig, b?: GoogleSqlIntConfig) { - const { name, config } = getColumnNameAndConfig(a, b); - return new GoogleSqlMediumIntBuilder(name, config); -} diff --git a/drizzle-orm/src/googlesql/columns/numeric.ts b/drizzle-orm/src/googlesql/columns/numeric.ts new file mode 100644 index 0000000000..a13d173326 --- /dev/null +++ b/drizzle-orm/src/googlesql/columns/numeric.ts @@ -0,0 +1,54 @@ +import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnConfig } from '~/column-builder.ts'; +import type { ColumnBaseConfig } from '~/column.ts'; +import { entityKind } from '~/entity.ts'; +import type { AnyGoogleSqlTable } from '~/googlesql/table.ts'; +import { GoogleSqlColumn, GoogleSqlColumnBuilder} from './common.ts'; + +export type GoogleSqlNumericBuilderInitial = GoogleSqlNumericBuilder<{ + name: TName; + dataType: 'string'; + columnType: 'GoogleSqlNumeric'; + data: string; + driverParam: string; + enumValues: undefined; +}>; + +export class GoogleSqlNumericBuilder< + T extends ColumnBuilderBaseConfig<'string', 'GoogleSqlNumeric'>, +> extends GoogleSqlColumnBuilder { + static override readonly [entityKind]: string = 'GoogleSqlNumericBuilder'; + + constructor(name: T['name']) { + super(name, 'string', 'GoogleSqlNumeric'); + } + + /** @internal */ + override build( + table: AnyGoogleSqlTable<{ name: TTableName }>, + ): GoogleSqlNumeric> { + return new GoogleSqlNumeric>( + table, + this.config as ColumnBuilderRuntimeConfig, + ); + } +} + +export class GoogleSqlNumeric> + extends GoogleSqlColumn +{ + static override readonly [entityKind]: string = 'GoogleSqlNumeric'; + + getSQLType(): string { + return 'numeric'; + } +} + + +export function numeric(): GoogleSqlNumericBuilderInitial<''>; + +export function numeric( + name: TName, +): GoogleSqlNumericBuilderInitial; +export function numeric(name?: string) { + return new GoogleSqlNumericBuilder(name ?? ''); +} diff --git a/drizzle-orm/src/googlesql/columns/real.ts b/drizzle-orm/src/googlesql/columns/real.ts deleted file mode 100644 index 87443b07a3..0000000000 --- a/drizzle-orm/src/googlesql/columns/real.ts +++ /dev/null @@ -1,80 +0,0 @@ -import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnConfig } from '~/column-builder.ts'; -import type { ColumnBaseConfig } from '~/column.ts'; -import { entityKind } from '~/entity.ts'; -import type { AnyGoogleSqlTable } from '~/googlesql/table.ts'; -import { getColumnNameAndConfig } from '~/utils.ts'; -import { GoogleSqlColumnBuilderWithAutoIncrement, GoogleSqlColumnWithAutoIncrement } from './common.ts'; - -export type GoogleSqlRealBuilderInitial = GoogleSqlRealBuilder<{ - name: TName; - dataType: 'number'; - columnType: 'GoogleSqlReal'; - data: number; - driverParam: number | string; - enumValues: undefined; -}>; - -export class GoogleSqlRealBuilder> - extends GoogleSqlColumnBuilderWithAutoIncrement< - T, - GoogleSqlRealConfig - > -{ - static override readonly [entityKind]: string = 'GoogleSqlRealBuilder'; - - constructor(name: T['name'], config: GoogleSqlRealConfig | undefined) { - super(name, 'number', 'GoogleSqlReal'); - this.config.precision = config?.precision; - this.config.scale = config?.scale; - } - - /** @internal */ - override build( - table: AnyGoogleSqlTable<{ name: TTableName }>, - ): GoogleSqlReal> { - return new GoogleSqlReal>( - table, - this.config as ColumnBuilderRuntimeConfig, - ); - } -} - -export class GoogleSqlReal> - extends GoogleSqlColumnWithAutoIncrement< - T, - GoogleSqlRealConfig - > -{ - static override readonly [entityKind]: string = 'GoogleSqlReal'; - - precision: number | undefined = this.config.precision; - scale: number | undefined = this.config.scale; - - getSQLType(): string { - if (this.precision !== undefined && this.scale !== undefined) { - return `real(${this.precision}, ${this.scale})`; - } else if (this.precision === undefined) { - return 'real'; - } else { - return `real(${this.precision})`; - } - } -} - -export interface GoogleSqlRealConfig { - precision?: number; - scale?: number; -} - -export function real(): GoogleSqlRealBuilderInitial<''>; -export function real( - config?: GoogleSqlRealConfig, -): GoogleSqlRealBuilderInitial<''>; -export function real( - name: TName, - config?: GoogleSqlRealConfig, -): GoogleSqlRealBuilderInitial; -export function real(a?: string | GoogleSqlRealConfig, b: GoogleSqlRealConfig = {}) { - const { name, config } = getColumnNameAndConfig(a, b); - return new GoogleSqlRealBuilder(name, config); -} diff --git a/drizzle-orm/src/googlesql/columns/serial.ts b/drizzle-orm/src/googlesql/columns/serial.ts deleted file mode 100644 index 7371b68c56..0000000000 --- a/drizzle-orm/src/googlesql/columns/serial.ts +++ /dev/null @@ -1,75 +0,0 @@ -import type { - ColumnBuilderBaseConfig, - ColumnBuilderRuntimeConfig, - HasDefault, - IsAutoincrement, - IsPrimaryKey, - MakeColumnConfig, - NotNull, -} from '~/column-builder.ts'; -import type { ColumnBaseConfig } from '~/column.ts'; -import { entityKind } from '~/entity.ts'; -import type { AnyGoogleSqlTable } from '~/googlesql/table.ts'; -import { GoogleSqlColumnBuilderWithAutoIncrement, GoogleSqlColumnWithAutoIncrement } from './common.ts'; - -export type GoogleSqlSerialBuilderInitial = IsAutoincrement< - IsPrimaryKey< - NotNull< - HasDefault< - GoogleSqlSerialBuilder<{ - name: TName; - dataType: 'number'; - columnType: 'GoogleSqlSerial'; - data: number; - driverParam: number; - enumValues: undefined; - }> - > - > - > ->; - -export class GoogleSqlSerialBuilder> - extends GoogleSqlColumnBuilderWithAutoIncrement -{ - static override readonly [entityKind]: string = 'GoogleSqlSerialBuilder'; - - constructor(name: T['name']) { - super(name, 'number', 'GoogleSqlSerial'); - this.config.hasDefault = true; - this.config.autoIncrement = true; - } - - /** @internal */ - override build( - table: AnyGoogleSqlTable<{ name: TTableName }>, - ): GoogleSqlSerial> { - return new GoogleSqlSerial>( - table, - this.config as ColumnBuilderRuntimeConfig, - ); - } -} - -export class GoogleSqlSerial< - T extends ColumnBaseConfig<'number', 'GoogleSqlSerial'>, -> extends GoogleSqlColumnWithAutoIncrement { - static override readonly [entityKind]: string = 'GoogleSqlSerial'; - - getSQLType(): string { - return 'serial'; - } - - override mapFromDriverValue(value: number | string): number { - if (typeof value === 'string') { - return Number(value); - } - return value; - } -} - -export function serial(): GoogleSqlSerialBuilderInitial<''>; -export function serial(name: TName): GoogleSqlSerialBuilderInitial; -export function serial(name?: string) { - return new GoogleSqlSerialBuilder(name ?? ''); -} diff --git a/drizzle-orm/src/googlesql/columns/smallint.ts b/drizzle-orm/src/googlesql/columns/smallint.ts deleted file mode 100644 index 81516580f1..0000000000 --- a/drizzle-orm/src/googlesql/columns/smallint.ts +++ /dev/null @@ -1,67 +0,0 @@ -import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnConfig } from '~/column-builder.ts'; -import type { ColumnBaseConfig } from '~/column.ts'; -import { entityKind } from '~/entity.ts'; -import type { AnyGoogleSqlTable } from '~/googlesql/table.ts'; -import { getColumnNameAndConfig } from '~/utils.ts'; -import { GoogleSqlColumnBuilderWithAutoIncrement, GoogleSqlColumnWithAutoIncrement } from './common.ts'; -import type { GoogleSqlIntConfig } from './int.ts'; - -export type GoogleSqlSmallIntBuilderInitial = GoogleSqlSmallIntBuilder<{ - name: TName; - dataType: 'number'; - columnType: 'GoogleSqlSmallInt'; - data: number; - driverParam: number | string; - enumValues: undefined; -}>; - -export class GoogleSqlSmallIntBuilder> - extends GoogleSqlColumnBuilderWithAutoIncrement -{ - static override readonly [entityKind]: string = 'GoogleSqlSmallIntBuilder'; - - constructor(name: T['name'], config?: GoogleSqlIntConfig) { - super(name, 'number', 'GoogleSqlSmallInt'); - this.config.unsigned = config ? config.unsigned : false; - } - - /** @internal */ - override build( - table: AnyGoogleSqlTable<{ name: TTableName }>, - ): GoogleSqlSmallInt> { - return new GoogleSqlSmallInt>( - table, - this.config as ColumnBuilderRuntimeConfig, - ); - } -} - -export class GoogleSqlSmallInt> - extends GoogleSqlColumnWithAutoIncrement -{ - static override readonly [entityKind]: string = 'GoogleSqlSmallInt'; - - getSQLType(): string { - return `smallint${this.config.unsigned ? ' unsigned' : ''}`; - } - - override mapFromDriverValue(value: number | string): number { - if (typeof value === 'string') { - return Number(value); - } - return value; - } -} - -export function smallint(): GoogleSqlSmallIntBuilderInitial<''>; -export function smallint( - config?: GoogleSqlIntConfig, -): GoogleSqlSmallIntBuilderInitial<''>; -export function smallint( - name: TName, - config?: GoogleSqlIntConfig, -): GoogleSqlSmallIntBuilderInitial; -export function smallint(a?: string | GoogleSqlIntConfig, b?: GoogleSqlIntConfig) { - const { name, config } = getColumnNameAndConfig(a, b); - return new GoogleSqlSmallIntBuilder(name, config); -} diff --git a/drizzle-orm/src/googlesql/columns/string.ts b/drizzle-orm/src/googlesql/columns/string.ts new file mode 100644 index 0000000000..f087f2d5f0 --- /dev/null +++ b/drizzle-orm/src/googlesql/columns/string.ts @@ -0,0 +1,87 @@ +import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnConfig } from '~/column-builder.ts'; +import type { ColumnBaseConfig } from '~/column.ts'; +import { entityKind } from '~/entity.ts'; +import type { AnyGoogleSqlTable } from '~/googlesql/table.ts'; +import { getColumnNameAndConfig, type Writable } from '~/utils.ts'; +import { GoogleSqlColumn, GoogleSqlColumnBuilder } from './common.ts'; + +export type GoogleSqlStringBuilderInitial< + TName extends string, + TEnum extends [string, ...string[]], + TLength extends number | undefined | "MAX", +> = GoogleSqlStringBuilder<{ + name: TName; + dataType: 'string'; + columnType: 'GoogleSqlString'; + data: TEnum[number]; + driverParam: number | string; + enumValues: TEnum; + length: TLength; +}>; + + +// TODO: SPANNER - check how those "enum" work +export class GoogleSqlStringBuilder< + T extends ColumnBuilderBaseConfig<'string', 'GoogleSqlString'> & { length?: number | undefined | "MAX" }, +> extends GoogleSqlColumnBuilder< + T, + GoogleSqlStringConfig, + { length: T['length'] } +> { + static override readonly [entityKind]: string = 'GoogleSqlStringBuilder'; + + constructor(name: T['name'], config: GoogleSqlStringConfig) { + super(name, 'string', 'GoogleSqlString'); + this.config.length = config.length; + this.config.enum = config.enum; + } + + /** @internal */ + override build( + table: AnyGoogleSqlTable<{ name: TTableName }>, + ): GoogleSqlString & { length: T['length']; enumValues: T['enumValues'] }> { + return new GoogleSqlString & { length: T['length']; enumValues: T['enumValues'] }>( + table, + this.config as ColumnBuilderRuntimeConfig, + ); + } +} + +export class GoogleSqlString & { length?: number | undefined | "MAX" }> + extends GoogleSqlColumn, { length: T['length'] }> +{ + static override readonly [entityKind]: string = 'GoogleSqlString'; + + readonly length: T['length'] = this.config.length; + override readonly enumValues = this.config.enum; + + getSQLType(): string { + return this.length === undefined ? `string(MAX)` : `string(${this.length})`; + } +} + +export interface GoogleSqlStringConfig< + TEnum extends readonly string[] | string[] | undefined = readonly string[] | string[] | undefined, + TLength extends number | undefined | "MAX" = number | undefined | "MAX", +> { + enum?: TEnum; + length?: TLength; +} + +export function string(): GoogleSqlStringBuilderInitial<'', [string, ...string[]], undefined>; +export function string, L extends number | undefined>( + config?: GoogleSqlStringConfig, L>, +): GoogleSqlStringBuilderInitial<'', Writable, L>; +export function string< + TName extends string, + U extends string, + T extends Readonly<[U, ...U[]]>, + L extends number | undefined, +>( + name: TName, + config?: GoogleSqlStringConfig, L>, +): GoogleSqlStringBuilderInitial, L>; +export function string(a?: string | GoogleSqlStringConfig, b: GoogleSqlStringConfig = {}): any { + const { name, config } = getColumnNameAndConfig(a, b); + return new GoogleSqlStringBuilder(name, config as any); +} diff --git a/drizzle-orm/src/googlesql/columns/text.ts b/drizzle-orm/src/googlesql/columns/text.ts deleted file mode 100644 index bb4d83a704..0000000000 --- a/drizzle-orm/src/googlesql/columns/text.ts +++ /dev/null @@ -1,115 +0,0 @@ -import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnConfig } from '~/column-builder.ts'; -import type { ColumnBaseConfig } from '~/column.ts'; -import { entityKind } from '~/entity.ts'; -import type { AnyGoogleSqlTable } from '~/googlesql/table.ts'; -import { getColumnNameAndConfig, type Writable } from '~/utils.ts'; -import { GoogleSqlColumn, GoogleSqlColumnBuilder } from './common.ts'; - -export type GoogleSqlTextColumnType = 'tinytext' | 'text' | 'mediumtext' | 'longtext'; - -export type GoogleSqlTextBuilderInitial = - GoogleSqlTextBuilder<{ - name: TName; - dataType: 'string'; - columnType: 'GoogleSqlText'; - data: TEnum[number]; - driverParam: string; - enumValues: TEnum; - }>; - -export class GoogleSqlTextBuilder> - extends GoogleSqlColumnBuilder< - T, - { textType: GoogleSqlTextColumnType; enumValues: T['enumValues'] } - > -{ - static override readonly [entityKind]: string = 'GoogleSqlTextBuilder'; - - constructor(name: T['name'], textType: GoogleSqlTextColumnType, config: GoogleSqlTextConfig) { - super(name, 'string', 'GoogleSqlText'); - this.config.textType = textType; - this.config.enumValues = config.enum; - } - - /** @internal */ - override build( - table: AnyGoogleSqlTable<{ name: TTableName }>, - ): GoogleSqlText> { - return new GoogleSqlText>( - table, - this.config as ColumnBuilderRuntimeConfig, - ); - } -} - -export class GoogleSqlText> - extends GoogleSqlColumn -{ - static override readonly [entityKind]: string = 'GoogleSqlText'; - - readonly textType: GoogleSqlTextColumnType = this.config.textType; - - override readonly enumValues = this.config.enumValues; - - getSQLType(): string { - return this.textType; - } -} - -export interface GoogleSqlTextConfig< - TEnum extends readonly string[] | string[] | undefined = readonly string[] | string[] | undefined, -> { - enum?: TEnum; -} - -export function text(): GoogleSqlTextBuilderInitial<'', [string, ...string[]]>; -export function text>( - config?: GoogleSqlTextConfig>, -): GoogleSqlTextBuilderInitial<'', Writable>; -export function text>( - name: TName, - config?: GoogleSqlTextConfig>, -): GoogleSqlTextBuilderInitial>; -export function text(a?: string | GoogleSqlTextConfig, b: GoogleSqlTextConfig = {}): any { - const { name, config } = getColumnNameAndConfig(a, b); - return new GoogleSqlTextBuilder(name, 'text', config as any); -} - -export function tinytext(): GoogleSqlTextBuilderInitial<'', [string, ...string[]]>; -export function tinytext>( - config?: GoogleSqlTextConfig>, -): GoogleSqlTextBuilderInitial<'', Writable>; -export function tinytext>( - name: TName, - config?: GoogleSqlTextConfig>, -): GoogleSqlTextBuilderInitial>; -export function tinytext(a?: string | GoogleSqlTextConfig, b: GoogleSqlTextConfig = {}): any { - const { name, config } = getColumnNameAndConfig(a, b); - return new GoogleSqlTextBuilder(name, 'tinytext', config as any); -} - -export function mediumtext(): GoogleSqlTextBuilderInitial<'', [string, ...string[]]>; -export function mediumtext>( - config?: GoogleSqlTextConfig>, -): GoogleSqlTextBuilderInitial<'', Writable>; -export function mediumtext>( - name: TName, - config?: GoogleSqlTextConfig>, -): GoogleSqlTextBuilderInitial>; -export function mediumtext(a?: string | GoogleSqlTextConfig, b: GoogleSqlTextConfig = {}): any { - const { name, config } = getColumnNameAndConfig(a, b); - return new GoogleSqlTextBuilder(name, 'mediumtext', config as any); -} - -export function longtext(): GoogleSqlTextBuilderInitial<'', [string, ...string[]]>; -export function longtext>( - config?: GoogleSqlTextConfig>, -): GoogleSqlTextBuilderInitial<'', Writable>; -export function longtext>( - name: TName, - config?: GoogleSqlTextConfig>, -): GoogleSqlTextBuilderInitial>; -export function longtext(a?: string | GoogleSqlTextConfig, b: GoogleSqlTextConfig = {}): any { - const { name, config } = getColumnNameAndConfig(a, b); - return new GoogleSqlTextBuilder(name, 'longtext', config as any); -} diff --git a/drizzle-orm/src/googlesql/columns/time.ts b/drizzle-orm/src/googlesql/columns/time.ts deleted file mode 100644 index 89f22e51d4..0000000000 --- a/drizzle-orm/src/googlesql/columns/time.ts +++ /dev/null @@ -1,72 +0,0 @@ -import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnConfig } from '~/column-builder.ts'; -import type { ColumnBaseConfig } from '~/column.ts'; -import { entityKind } from '~/entity.ts'; -import type { AnyGoogleSqlTable } from '~/googlesql/table.ts'; -import { getColumnNameAndConfig } from '~/utils.ts'; -import { GoogleSqlColumn, GoogleSqlColumnBuilder } from './common.ts'; - -export type GoogleSqlTimeBuilderInitial = GoogleSqlTimeBuilder<{ - name: TName; - dataType: 'string'; - columnType: 'GoogleSqlTime'; - data: string; - driverParam: string | number; - enumValues: undefined; -}>; - -export class GoogleSqlTimeBuilder> - extends GoogleSqlColumnBuilder< - T, - TimeConfig - > -{ - static override readonly [entityKind]: string = 'GoogleSqlTimeBuilder'; - - constructor( - name: T['name'], - config: TimeConfig | undefined, - ) { - super(name, 'string', 'GoogleSqlTime'); - this.config.fsp = config?.fsp; - } - - /** @internal */ - override build( - table: AnyGoogleSqlTable<{ name: TTableName }>, - ): GoogleSqlTime> { - return new GoogleSqlTime>( - table, - this.config as ColumnBuilderRuntimeConfig, - ); - } -} - -export class GoogleSqlTime< - T extends ColumnBaseConfig<'string', 'GoogleSqlTime'>, -> extends GoogleSqlColumn { - static override readonly [entityKind]: string = 'GoogleSqlTime'; - - readonly fsp: number | undefined = this.config.fsp; - - getSQLType(): string { - const precision = this.fsp === undefined ? '' : `(${this.fsp})`; - return `time${precision}`; - } -} - -export type TimeConfig = { - fsp?: 0 | 1 | 2 | 3 | 4 | 5 | 6; -}; - -export function time(): GoogleSqlTimeBuilderInitial<''>; -export function time( - config?: TimeConfig, -): GoogleSqlTimeBuilderInitial<''>; -export function time( - name: TName, - config?: TimeConfig, -): GoogleSqlTimeBuilderInitial; -export function time(a?: string | TimeConfig, b?: TimeConfig) { - const { name, config } = getColumnNameAndConfig(a, b); - return new GoogleSqlTimeBuilder(name, config); -} diff --git a/drizzle-orm/src/googlesql/columns/timestamp.ts b/drizzle-orm/src/googlesql/columns/timestamp.ts index e8a4795aa0..230309dfb7 100644 --- a/drizzle-orm/src/googlesql/columns/timestamp.ts +++ b/drizzle-orm/src/googlesql/columns/timestamp.ts @@ -2,8 +2,7 @@ import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnCon import type { ColumnBaseConfig } from '~/column.ts'; import { entityKind } from '~/entity.ts'; import type { AnyGoogleSqlTable } from '~/googlesql/table.ts'; -import { type Equal, getColumnNameAndConfig } from '~/utils.ts'; -import { GoogleSqlDateBaseColumn, GoogleSqlDateColumnBaseBuilder } from './date.common.ts'; +import { GoogleSqlColumn, GoogleSqlColumnBuilder } from './common.ts'; export type GoogleSqlTimestampBuilderInitial = GoogleSqlTimestampBuilder<{ name: TName; @@ -15,13 +14,12 @@ export type GoogleSqlTimestampBuilderInitial = GoogleSqlTi }>; export class GoogleSqlTimestampBuilder> - extends GoogleSqlDateColumnBaseBuilder + extends GoogleSqlColumnBuilder { static override readonly [entityKind]: string = 'GoogleSqlTimestampBuilder'; - constructor(name: T['name'], config: GoogleSqlTimestampConfig | undefined) { + constructor(name: T['name']) { super(name, 'date', 'GoogleSqlTimestamp'); - this.config.fsp = config?.fsp; } /** @internal */ @@ -35,16 +33,14 @@ export class GoogleSqlTimestampBuilder> - extends GoogleSqlDateBaseColumn + extends GoogleSqlColumn { static override readonly [entityKind]: string = 'GoogleSqlTimestamp'; - readonly fsp: number | undefined = this.config.fsp; - getSQLType(): string { - const precision = this.fsp === undefined ? '' : `(${this.fsp})`; - return `timestamp${precision}`; + return `timestamp`; } override mapFromDriverValue(value: string): Date { @@ -56,70 +52,15 @@ export class GoogleSqlTimestamp = GoogleSqlTimestampStringBuilder<{ - name: TName; - dataType: 'string'; - columnType: 'GoogleSqlTimestampString'; - data: string; - driverParam: string | number; - enumValues: undefined; -}>; - -export class GoogleSqlTimestampStringBuilder> - extends GoogleSqlDateColumnBaseBuilder -{ - static override readonly [entityKind]: string = 'GoogleSqlTimestampStringBuilder'; - - constructor(name: T['name'], config: GoogleSqlTimestampConfig | undefined) { - super(name, 'string', 'GoogleSqlTimestampString'); - this.config.fsp = config?.fsp; - } - - /** @internal */ - override build( - table: AnyGoogleSqlTable<{ name: TTableName }>, - ): GoogleSqlTimestampString> { - return new GoogleSqlTimestampString>( - table, - this.config as ColumnBuilderRuntimeConfig, - ); - } -} - -export class GoogleSqlTimestampString> - extends GoogleSqlDateBaseColumn -{ - static override readonly [entityKind]: string = 'GoogleSqlTimestampString'; - - readonly fsp: number | undefined = this.config.fsp; - - getSQLType(): string { - const precision = this.fsp === undefined ? '' : `(${this.fsp})`; - return `timestamp${precision}`; - } -} - -export type TimestampFsp = 0 | 1 | 2 | 3 | 4 | 5 | 6; - -export interface GoogleSqlTimestampConfig { - mode?: TMode; - fsp?: TimestampFsp; -} +// TODO: SPANNER - add support for allowCommitTimestamp https://cloud.google.com/spanner/docs/commit-timestamp#overview +// export interface GoogleSqlTimestampConfig { +// allowCommitTimestamp?: AllowAC; +// } export function timestamp(): GoogleSqlTimestampBuilderInitial<''>; -export function timestamp( - config?: GoogleSqlTimestampConfig, -): Equal extends true ? GoogleSqlTimestampStringBuilderInitial<''> - : GoogleSqlTimestampBuilderInitial<''>; -export function timestamp( +export function timestamp( name: TName, - config?: GoogleSqlTimestampConfig, -): Equal extends true ? GoogleSqlTimestampStringBuilderInitial - : GoogleSqlTimestampBuilderInitial; -export function timestamp(a?: string | GoogleSqlTimestampConfig, b: GoogleSqlTimestampConfig = {}) { - const { name, config } = getColumnNameAndConfig(a, b); - if (config?.mode === 'string') { - return new GoogleSqlTimestampStringBuilder(name, config); - } - return new GoogleSqlTimestampBuilder(name, config); +): GoogleSqlTimestampBuilderInitial; +export function timestamp(name?: string) { + return new GoogleSqlTimestampBuilder(name ?? ''); } diff --git a/drizzle-orm/src/googlesql/columns/tinyint.ts b/drizzle-orm/src/googlesql/columns/tinyint.ts deleted file mode 100644 index 471b5bf842..0000000000 --- a/drizzle-orm/src/googlesql/columns/tinyint.ts +++ /dev/null @@ -1,67 +0,0 @@ -import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnConfig } from '~/column-builder.ts'; -import type { ColumnBaseConfig } from '~/column.ts'; -import { entityKind } from '~/entity.ts'; -import type { AnyGoogleSqlTable } from '~/googlesql/table.ts'; -import { getColumnNameAndConfig } from '~/utils.ts'; -import { GoogleSqlColumnBuilderWithAutoIncrement, GoogleSqlColumnWithAutoIncrement } from './common.ts'; -import type { GoogleSqlIntConfig } from './int.ts'; - -export type GoogleSqlTinyIntBuilderInitial = GoogleSqlTinyIntBuilder<{ - name: TName; - dataType: 'number'; - columnType: 'GoogleSqlTinyInt'; - data: number; - driverParam: number | string; - enumValues: undefined; -}>; - -export class GoogleSqlTinyIntBuilder> - extends GoogleSqlColumnBuilderWithAutoIncrement -{ - static override readonly [entityKind]: string = 'GoogleSqlTinyIntBuilder'; - - constructor(name: T['name'], config?: GoogleSqlIntConfig) { - super(name, 'number', 'GoogleSqlTinyInt'); - this.config.unsigned = config ? config.unsigned : false; - } - - /** @internal */ - override build( - table: AnyGoogleSqlTable<{ name: TTableName }>, - ): GoogleSqlTinyInt> { - return new GoogleSqlTinyInt>( - table, - this.config as ColumnBuilderRuntimeConfig, - ); - } -} - -export class GoogleSqlTinyInt> - extends GoogleSqlColumnWithAutoIncrement -{ - static override readonly [entityKind]: string = 'GoogleSqlTinyInt'; - - getSQLType(): string { - return `tinyint${this.config.unsigned ? ' unsigned' : ''}`; - } - - override mapFromDriverValue(value: number | string): number { - if (typeof value === 'string') { - return Number(value); - } - return value; - } -} - -export function tinyint(): GoogleSqlTinyIntBuilderInitial<''>; -export function tinyint( - config?: GoogleSqlIntConfig, -): GoogleSqlTinyIntBuilderInitial<''>; -export function tinyint( - name: TName, - config?: GoogleSqlIntConfig, -): GoogleSqlTinyIntBuilderInitial; -export function tinyint(a?: string | GoogleSqlIntConfig, b?: GoogleSqlIntConfig) { - const { name, config } = getColumnNameAndConfig(a, b); - return new GoogleSqlTinyIntBuilder(name, config); -} diff --git a/drizzle-orm/src/googlesql/columns/varbinary.ts b/drizzle-orm/src/googlesql/columns/varbinary.ts deleted file mode 100644 index 03533eb6d8..0000000000 --- a/drizzle-orm/src/googlesql/columns/varbinary.ts +++ /dev/null @@ -1,65 +0,0 @@ -import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnConfig } from '~/column-builder.ts'; -import type { ColumnBaseConfig } from '~/column.ts'; -import { entityKind } from '~/entity.ts'; -import type { AnyGoogleSqlTable } from '~/googlesql/table.ts'; -import { getColumnNameAndConfig } from '~/utils.ts'; -import { GoogleSqlColumn, GoogleSqlColumnBuilder } from './common.ts'; - -export type GoogleSqlVarBinaryBuilderInitial = GoogleSqlVarBinaryBuilder<{ - name: TName; - dataType: 'string'; - columnType: 'GoogleSqlVarBinary'; - data: string; - driverParam: string; - enumValues: undefined; -}>; - -export class GoogleSqlVarBinaryBuilder> - extends GoogleSqlColumnBuilder -{ - static override readonly [entityKind]: string = 'GoogleSqlVarBinaryBuilder'; - - /** @internal */ - constructor(name: T['name'], config: GoogleSqlVarbinaryOptions) { - super(name, 'string', 'GoogleSqlVarBinary'); - this.config.length = config?.length; - } - - /** @internal */ - override build( - table: AnyGoogleSqlTable<{ name: TTableName }>, - ): GoogleSqlVarBinary> { - return new GoogleSqlVarBinary>( - table, - this.config as ColumnBuilderRuntimeConfig, - ); - } -} - -export class GoogleSqlVarBinary< - T extends ColumnBaseConfig<'string', 'GoogleSqlVarBinary'>, -> extends GoogleSqlColumn { - static override readonly [entityKind]: string = 'GoogleSqlVarBinary'; - - length: number | undefined = this.config.length; - - getSQLType(): string { - return this.length === undefined ? `varbinary` : `varbinary(${this.length})`; - } -} - -export interface GoogleSqlVarbinaryOptions { - length: number; -} - -export function varbinary( - config: GoogleSqlVarbinaryOptions, -): GoogleSqlVarBinaryBuilderInitial<''>; -export function varbinary( - name: TName, - config: GoogleSqlVarbinaryOptions, -): GoogleSqlVarBinaryBuilderInitial; -export function varbinary(a?: string | GoogleSqlVarbinaryOptions, b?: GoogleSqlVarbinaryOptions) { - const { name, config } = getColumnNameAndConfig(a, b); - return new GoogleSqlVarBinaryBuilder(name, config); -} diff --git a/drizzle-orm/src/googlesql/columns/varchar.ts b/drizzle-orm/src/googlesql/columns/varchar.ts deleted file mode 100644 index 9fd9b0a313..0000000000 --- a/drizzle-orm/src/googlesql/columns/varchar.ts +++ /dev/null @@ -1,84 +0,0 @@ -import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnConfig } from '~/column-builder.ts'; -import type { ColumnBaseConfig } from '~/column.ts'; -import { entityKind } from '~/entity.ts'; -import type { AnyGoogleSqlTable } from '~/googlesql/table.ts'; -import { getColumnNameAndConfig, type Writable } from '~/utils.ts'; -import { GoogleSqlColumn, GoogleSqlColumnBuilder } from './common.ts'; - -export type GoogleSqlVarCharBuilderInitial< - TName extends string, - TEnum extends [string, ...string[]], - TLength extends number | undefined, -> = GoogleSqlVarCharBuilder< - { - name: TName; - dataType: 'string'; - columnType: 'GoogleSqlVarChar'; - data: TEnum[number]; - driverParam: number | string; - enumValues: TEnum; - length: TLength; - } ->; - -export class GoogleSqlVarCharBuilder< - T extends ColumnBuilderBaseConfig<'string', 'GoogleSqlVarChar'> & { length?: number | undefined }, -> extends GoogleSqlColumnBuilder> { - static override readonly [entityKind]: string = 'GoogleSqlVarCharBuilder'; - - /** @internal */ - constructor(name: T['name'], config: GoogleSqlVarCharConfig) { - super(name, 'string', 'GoogleSqlVarChar'); - this.config.length = config.length; - this.config.enum = config.enum; - } - - /** @internal */ - override build( - table: AnyGoogleSqlTable<{ name: TTableName }>, - ): GoogleSqlVarChar & { length: T['length']; enumValues: T['enumValues'] }> { - return new GoogleSqlVarChar & { length: T['length']; enumValues: T['enumValues'] }>( - table, - this.config as ColumnBuilderRuntimeConfig, - ); - } -} - -export class GoogleSqlVarChar< - T extends ColumnBaseConfig<'string', 'GoogleSqlVarChar'> & { length?: number | undefined }, -> extends GoogleSqlColumn, { length: T['length'] }> { - static override readonly [entityKind]: string = 'GoogleSqlVarChar'; - - readonly length: number | undefined = this.config.length; - - override readonly enumValues = this.config.enum; - - getSQLType(): string { - return this.length === undefined ? `varchar` : `varchar(${this.length})`; - } -} - -export interface GoogleSqlVarCharConfig< - TEnum extends string[] | readonly string[] | undefined = string[] | readonly string[] | undefined, - TLength extends number | undefined = number | undefined, -> { - enum?: TEnum; - length?: TLength; -} - -export function varchar, L extends number | undefined>( - config: GoogleSqlVarCharConfig, L>, -): GoogleSqlVarCharBuilderInitial<'', Writable, L>; -export function varchar< - TName extends string, - U extends string, - T extends Readonly<[U, ...U[]]>, - L extends number | undefined, ->( - name: TName, - config: GoogleSqlVarCharConfig, L>, -): GoogleSqlVarCharBuilderInitial, L>; -export function varchar(a?: string | GoogleSqlVarCharConfig, b?: GoogleSqlVarCharConfig): any { - const { name, config } = getColumnNameAndConfig(a, b); - return new GoogleSqlVarCharBuilder(name, config as any); -} diff --git a/drizzle-orm/src/googlesql/columns/year.ts b/drizzle-orm/src/googlesql/columns/year.ts deleted file mode 100644 index 83d7947ba5..0000000000 --- a/drizzle-orm/src/googlesql/columns/year.ts +++ /dev/null @@ -1,50 +0,0 @@ -import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnConfig } from '~/column-builder.ts'; -import type { ColumnBaseConfig } from '~/column.ts'; -import { entityKind } from '~/entity.ts'; -import type { AnyGoogleSqlTable } from '~/googlesql/table.ts'; -import { GoogleSqlColumn, GoogleSqlColumnBuilder } from './common.ts'; - -export type GoogleSqlYearBuilderInitial = GoogleSqlYearBuilder<{ - name: TName; - dataType: 'number'; - columnType: 'GoogleSqlYear'; - data: number; - driverParam: number; - enumValues: undefined; -}>; - -export class GoogleSqlYearBuilder> - extends GoogleSqlColumnBuilder -{ - static override readonly [entityKind]: string = 'GoogleSqlYearBuilder'; - - constructor(name: T['name']) { - super(name, 'number', 'GoogleSqlYear'); - } - - /** @internal */ - override build( - table: AnyGoogleSqlTable<{ name: TTableName }>, - ): GoogleSqlYear> { - return new GoogleSqlYear>( - table, - this.config as ColumnBuilderRuntimeConfig, - ); - } -} - -export class GoogleSqlYear< - T extends ColumnBaseConfig<'number', 'GoogleSqlYear'>, -> extends GoogleSqlColumn { - static override readonly [entityKind]: string = 'GoogleSqlYear'; - - getSQLType(): string { - return `year`; - } -} - -export function year(): GoogleSqlYearBuilderInitial<''>; -export function year(name: TName): GoogleSqlYearBuilderInitial; -export function year(name?: string) { - return new GoogleSqlYearBuilder(name ?? ''); -} From dc4836ea81c897f0803745548be7b2f2c1eb5476 Mon Sep 17 00:00:00 2001 From: Gabriel Cipriano Date: Fri, 7 Mar 2025 11:08:33 +0100 Subject: [PATCH 2/3] feat: googlesql dialect specifics --- drizzle-orm/src/googlesql/columns/common.ts | 8 +- drizzle-orm/src/googlesql/dialect.ts | 73 ++++++++++--------- drizzle-orm/src/googlesql/foreign-keys.ts | 22 +++--- drizzle-orm/src/googlesql/indexes.ts | 36 +++------ .../src/googlesql/query-builders/delete.ts | 47 ++---------- .../src/googlesql/query-builders/insert.ts | 36 +++++---- .../src/googlesql/query-builders/query.ts | 48 +++++++----- .../src/googlesql/query-builders/select.ts | 44 +++-------- .../googlesql/query-builders/select.types.ts | 4 +- .../src/googlesql/query-builders/update.ts | 53 +++----------- 10 files changed, 141 insertions(+), 230 deletions(-) diff --git a/drizzle-orm/src/googlesql/columns/common.ts b/drizzle-orm/src/googlesql/columns/common.ts index d76c63e0e3..25113cfc02 100644 --- a/drizzle-orm/src/googlesql/columns/common.ts +++ b/drizzle-orm/src/googlesql/columns/common.ts @@ -23,7 +23,7 @@ import { uniqueKeyName } from '../unique-constraint.ts'; export interface ReferenceConfig { ref: () => GoogleSqlColumn; actions: { - onUpdate?: UpdateDeleteAction; + // onUpdate?: UpdateDeleteAction; onDelete?: UpdateDeleteAction; }; } @@ -81,9 +81,9 @@ export abstract class GoogleSqlColumnBuilder< const foreignColumn = ref(); return { columns: [column], foreignColumns: [foreignColumn] }; }); - if (actions.onUpdate) { - builder.onUpdate(actions.onUpdate); - } + // if (actions.onUpdate) { + // builder.onUpdate(actions.onUpdate); + // } if (actions.onDelete) { builder.onDelete(actions.onDelete); } diff --git a/drizzle-orm/src/googlesql/dialect.ts b/drizzle-orm/src/googlesql/dialect.ts index bb046b7dc2..679d351540 100644 --- a/drizzle-orm/src/googlesql/dialect.ts +++ b/drizzle-orm/src/googlesql/dialect.ts @@ -56,10 +56,10 @@ export class GoogleSqlDialect { session: GoogleSqlSession, config: Omit, ): Promise { - const migrationsTable = config.migrationsTable ?? '__drizzle_migrations'; + const migrationsTable = config.migrationsTable ?? 'drizzle_migrations'; const migrationTableCreate = sql` create table if not exists ${sql.identifier(migrationsTable)} ( - id serial primary key, + id STRING(36) DEFAULT (GENERATE_UUID()), hash text not null, created_at bigint ) @@ -96,11 +96,11 @@ export class GoogleSqlDialect { } escapeParam(_num: number): string { - return `?`; + return `@p${_num}`; } escapeString(str: string): string { - return `'${str.replace(/'/g, "''")}'`; + return `'${str.replace(/'/g, "\\'")}'`; } private buildWithCTE(queries: Subquery[] | undefined): SQL | undefined { @@ -117,20 +117,17 @@ export class GoogleSqlDialect { return sql.join(withSqlChunks); } - buildDeleteQuery({ table, where, returning, withList, limit, orderBy }: GoogleSqlDeleteConfig): SQL { - const withSql = this.buildWithCTE(withList); + buildDeleteQuery({ table, where, returning }: GoogleSqlDeleteConfig): SQL { + // TODO - SPANNER: verify if WITH on delete clause is supported in Spanner. By the docs, it is not supported. https://cloud.google.com/spanner/docs/reference/standard-sql/dml-syntax#delete-statement + // const withSql = this.buildWithCTE(withList); const returningSql = returning - ? sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` + ? sql` then return ${this.buildSelection(returning, { isSingleTable: true })}` : undefined; const whereSql = where ? sql` where ${where}` : undefined; - const orderBySql = this.buildOrderBy(orderBy); - - const limitSql = this.buildLimit(limit); - - return sql`${withSql}delete from ${table}${whereSql}${orderBySql}${limitSql}${returningSql}`; + return sql`delete from ${table}${whereSql}${returningSql}`; } buildUpdateSet(table: GoogleSqlTable, set: UpdateSet): SQL { @@ -154,22 +151,25 @@ export class GoogleSqlDialect { })); } - buildUpdateQuery({ table, set, where, returning, withList, limit, orderBy }: GoogleSqlUpdateConfig): SQL { - const withSql = this.buildWithCTE(withList); + buildUpdateQuery({ table, set, where, returning }: GoogleSqlUpdateConfig): SQL { + // TODO - SPANNER: verify if this is supported. By the docs, it is not. https://cloud.google.com/spanner/docs/reference/standard-sql/dml-syntax#update-statement + // const withSql = this.buildWithCTE(withList); const setSql = this.buildUpdateSet(table, set); const returningSql = returning - ? sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` + ? sql` then return ${this.buildSelection(returning, { isSingleTable: true })}` : undefined; const whereSql = where ? sql` where ${where}` : undefined; - const orderBySql = this.buildOrderBy(orderBy); + // TODO - SPANNER: verify if this is supported. By the docs, it is not. https://cloud.google.com/spanner/docs/reference/standard-sql/dml-syntax#update-statement + // const orderBySql = this.buildOrderBy(orderBy); - const limitSql = this.buildLimit(limit); + // TODO - SPANNER: verify if this is supported. By the docs, it is not. https://cloud.google.com/spanner/docs/reference/standard-sql/dml-syntax#update-statement + // const limitSql = this.buildLimit(limit); - return sql`${withSql}update ${table} set ${setSql}${whereSql}${orderBySql}${limitSql}${returningSql}`; + return sql`update ${table} set ${setSql}${whereSql}${returningSql}`; } /** @@ -249,10 +249,10 @@ export class GoogleSqlDialect { indexFor, }: { indexes: string[] | undefined; - indexFor: 'USE' | 'FORCE' | 'IGNORE'; + indexFor: 'FORCE'; }): SQL | undefined { return indexes && indexes.length > 0 - ? sql` ${sql.raw(indexFor)} INDEX (${sql.raw(indexes.join(`, `))})` + ? sql`@{${sql.raw(indexFor)}_INDEX=${sql.raw(indexes.join(`, `))}}` : undefined; } @@ -272,9 +272,9 @@ export class GoogleSqlDialect { lockingClause, distinct, setOperators, - useIndex, forceIndex, - ignoreIndex, + // useIndex, + // ignoreIndex, }: GoogleSqlSelectConfig, ): SQL { const fieldsList = fieldsFlat ?? orderSelectedFields(fields); @@ -327,20 +327,23 @@ export class GoogleSqlDialect { joinsArray.push(sql` `); } const table = joinMeta.table; + + // TODO: SPANNER - spanner calls "lateral" as "correlated joins". https://cloud.google.com/spanner/docs/reference/standard-sql/query-syntax#correlated_join const lateralSql = joinMeta.lateral ? sql` lateral` : undefined; + if (lateralSql) { + throw new Error('GoogleSql does not support lateral joins. Correlated joins to be implemented.'); + } if (is(table, GoogleSqlTable)) { const tableName = table[GoogleSqlTable.Symbol.Name]; const tableSchema = table[GoogleSqlTable.Symbol.Schema]; const origTableName = table[GoogleSqlTable.Symbol.OriginalName]; const alias = tableName === origTableName ? undefined : joinMeta.alias; - const useIndexSql = this.buildIndex({ indexes: joinMeta.useIndex, indexFor: 'USE' }); const forceIndexSql = this.buildIndex({ indexes: joinMeta.forceIndex, indexFor: 'FORCE' }); - const ignoreIndexSql = this.buildIndex({ indexes: joinMeta.ignoreIndex, indexFor: 'IGNORE' }); joinsArray.push( sql`${sql.raw(joinMeta.joinType)} join${lateralSql} ${ tableSchema ? sql`${sql.identifier(tableSchema)}.` : undefined - }${sql.identifier(origTableName)}${useIndexSql}${forceIndexSql}${ignoreIndexSql}${ + }${sql.identifier(origTableName)}${forceIndexSql}${ alias && sql` ${sql.identifier(alias)}` } on ${joinMeta.on}`, ); @@ -379,11 +382,9 @@ export class GoogleSqlDialect { const offsetSql = offset ? sql` offset ${offset}` : undefined; - const useIndexSql = this.buildIndex({ indexes: useIndex, indexFor: 'USE' }); - const forceIndexSql = this.buildIndex({ indexes: forceIndex, indexFor: 'FORCE' }); - - const ignoreIndexSql = this.buildIndex({ indexes: ignoreIndex, indexFor: 'IGNORE' }); + // const useIndexSql = this.buildIndex({ indexes: useIndex, indexFor: 'USE' }); + // const ignoreIndexSql = this.buildIndex({ indexes: ignoreIndex, indexFor: 'IGNORE' }); let lockingClausesSql; if (lockingClause) { @@ -397,7 +398,7 @@ export class GoogleSqlDialect { } const finalQuery = - sql`${withSql}select${distinctSql} ${selection} from ${tableSql}${useIndexSql}${forceIndexSql}${ignoreIndexSql}${joinsSql}${whereSql}${groupBySql}${havingSql}${orderBySql}${limitSql}${offsetSql}${lockingClausesSql}`; + sql`${withSql}select${distinctSql} ${selection} from ${tableSql}${forceIndexSql}${joinsSql}${whereSql}${groupBySql}${havingSql}${orderBySql}${limitSql}${offsetSql}${lockingClausesSql}`; if (setOperators.length > 0) { return this.buildSetOperations(finalQuery, setOperators); @@ -470,8 +471,12 @@ export class GoogleSqlDialect { } buildInsertQuery( - { table, values: valuesOrSelect, ignore, onConflict, select }: GoogleSqlInsertConfig, + { table, values: valuesOrSelect, ignore, update, select }: GoogleSqlInsertConfig, ): { sql: SQL; generatedIds: Record[] } { + if (update && ignore) { + throw new Error('Cannot use "ignore" and "update" at the same time'); + } + // const isSingleValue = values.length === 1; const valuesSqlList: ((SQLChunk | SQL)[] | SQL)[] = []; const columns: Record = table[Table.Symbol.Columns]; @@ -535,10 +540,12 @@ export class GoogleSqlDialect { const ignoreSql = ignore ? sql` ignore` : undefined; - const onConflictSql = onConflict ? sql` on duplicate key ${onConflict}` : undefined; + const updateSql = update ? sql` update` : undefined; + + // TODO: SPANNER - support "THEN RETURN ..." clause. https://cloud.google.com/spanner/docs/reference/standard-sql/dml-syntax#insert_with_then_return_examples return { - sql: sql`insert${ignoreSql} into ${table} ${insertOrder} ${valuesSql}${onConflictSql}`, + sql: sql`insert${ignoreSql}${updateSql} into ${table} ${insertOrder} ${valuesSql}`, generatedIds: generatedIdsResponse, }; } diff --git a/drizzle-orm/src/googlesql/foreign-keys.ts b/drizzle-orm/src/googlesql/foreign-keys.ts index 7d39372a65..95532c8b26 100644 --- a/drizzle-orm/src/googlesql/foreign-keys.ts +++ b/drizzle-orm/src/googlesql/foreign-keys.ts @@ -3,7 +3,7 @@ import { TableName } from '~/table.utils.ts'; import type { AnyGoogleSqlColumn, GoogleSqlColumn } from './columns/index.ts'; import type { GoogleSqlTable } from './table.ts'; -export type UpdateDeleteAction = 'cascade' | 'restrict' | 'no action' | 'set null' | 'set default'; +export type UpdateDeleteAction = 'cascade' | 'no action'; export type Reference = () => { readonly name?: string; @@ -18,8 +18,8 @@ export class ForeignKeyBuilder { /** @internal */ reference: Reference; - /** @internal */ - _onUpdate: UpdateDeleteAction | undefined; + // /** @internal */ + // _onUpdate: UpdateDeleteAction | undefined; /** @internal */ _onDelete: UpdateDeleteAction | undefined; @@ -31,7 +31,7 @@ export class ForeignKeyBuilder { foreignColumns: GoogleSqlColumn[]; }, actions?: { - onUpdate?: UpdateDeleteAction; + // onUpdate?: UpdateDeleteAction; onDelete?: UpdateDeleteAction; } | undefined, ) { @@ -40,15 +40,15 @@ export class ForeignKeyBuilder { return { name, columns, foreignTable: foreignColumns[0]!.table as GoogleSqlTable, foreignColumns }; }; if (actions) { - this._onUpdate = actions.onUpdate; + // this._onUpdate = actions.onUpdate; this._onDelete = actions.onDelete; } } - onUpdate(action: UpdateDeleteAction): this { - this._onUpdate = action; - return this; - } + // onUpdate(action: UpdateDeleteAction): this { + // this._onUpdate = action; + // return this; + // } onDelete(action: UpdateDeleteAction): this { this._onDelete = action; @@ -67,12 +67,12 @@ export class ForeignKey { static readonly [entityKind]: string = 'GoogleSqlForeignKey'; readonly reference: Reference; - readonly onUpdate: UpdateDeleteAction | undefined; + // readonly onUpdate: UpdateDeleteAction | undefined; readonly onDelete: UpdateDeleteAction | undefined; constructor(readonly table: GoogleSqlTable, builder: ForeignKeyBuilder) { this.reference = builder.reference; - this.onUpdate = builder._onUpdate; + // this.onUpdate = builder._onUpdate; this.onDelete = builder._onDelete; } diff --git a/drizzle-orm/src/googlesql/indexes.ts b/drizzle-orm/src/googlesql/indexes.ts index cb920ba814..7c3b5c8bd0 100644 --- a/drizzle-orm/src/googlesql/indexes.ts +++ b/drizzle-orm/src/googlesql/indexes.ts @@ -13,20 +13,17 @@ interface IndexConfig { */ unique?: boolean; - /** - * If set, the index will be created as `create index ... using { 'btree' | 'hash' }`. - */ - using?: 'btree' | 'hash'; + // TODO: SPANNER - add support for nullFiltered https://cloud.google.com/spanner/docs/reference/standard-sql/data-definition-language#parameters_12 + // nullFiltered?: boolean; - /** - * If set, the index will be created as `create index ... algorythm { 'default' | 'inplace' | 'copy' }`. - */ - algorythm?: 'default' | 'inplace' | 'copy'; + // TODO: SPANNER - add support for INTERLEAVE IN https://cloud.google.com/spanner/docs/reference/standard-sql/data-definition-language#create-index-interleave + // interleaveIn?: GoogleSqlTable; - /** - * If set, adds locks to the index creation. - */ - lock?: 'default' | 'none' | 'shared' | 'exclusive'; + // TODO: SPANNER - add support for stored columns https://cloud.google.com/spanner/docs/reference/standard-sql/data-definition-language#create-index + // storing?: GoogleSqlColumn[]; + + // TODO: SPANNER - add support for WHERE IS NOT NULL clause https://cloud.google.com/spanner/docs/reference/standard-sql/data-definition-language#create-index + // whereIsNotNull?: GoogleSqlColumn; } export type IndexColumn = GoogleSqlColumn | SQL; @@ -62,21 +59,6 @@ export class IndexBuilder implements AnyIndexBuilder { }; } - using(using: IndexConfig['using']): this { - this.config.using = using; - return this; - } - - algorythm(algorythm: IndexConfig['algorythm']): this { - this.config.algorythm = algorythm; - return this; - } - - lock(lock: IndexConfig['lock']): this { - this.config.lock = lock; - return this; - } - /** @internal */ build(table: GoogleSqlTable): Index { return new Index(this.config, table); diff --git a/drizzle-orm/src/googlesql/query-builders/delete.ts b/drizzle-orm/src/googlesql/query-builders/delete.ts index beb6027e37..dcd8fe1aef 100644 --- a/drizzle-orm/src/googlesql/query-builders/delete.ts +++ b/drizzle-orm/src/googlesql/query-builders/delete.ts @@ -11,12 +11,8 @@ import type { } from '~/googlesql/session.ts'; import type { GoogleSqlTable } from '~/googlesql/table.ts'; import { QueryPromise } from '~/query-promise.ts'; -import { SelectionProxyHandler } from '~/selection-proxy.ts'; -import type { Placeholder, Query, SQL, SQLWrapper } from '~/sql/sql.ts'; -import type { Subquery } from '~/subquery.ts'; -import { Table } from '~/table.ts'; -import type { ValueOrArray } from '~/utils.ts'; -import type { GoogleSqlColumn } from '../columns/common.ts'; +import type { Query, SQL, SQLWrapper } from '~/sql/sql.ts'; +// import type { Subquery } from '~/subquery.ts'; import type { SelectedFieldsOrdered } from './select.types.ts'; export type GoogleSqlDeleteWithout< @@ -43,11 +39,9 @@ export type GoogleSqlDelete< export interface GoogleSqlDeleteConfig { where?: SQL | undefined; - limit?: number | Placeholder; - orderBy?: (GoogleSqlColumn | SQL | SQL.Aliased)[]; table: GoogleSqlTable; returning?: SelectedFieldsOrdered; - withList?: Subquery[]; + // withList?: Subquery[]; } export type GoogleSqlDeletePrepare = PreparedQueryKind< @@ -100,10 +94,10 @@ export class GoogleSqlDeleteBase< private table: TTable, private session: GoogleSqlSession, private dialect: GoogleSqlDialect, - withList?: Subquery[], + // withList?: Subquery[], ) { super(); - this.config = { table, withList }; + this.config = { table }; } /** @@ -140,37 +134,6 @@ export class GoogleSqlDeleteBase< return this as any; } - orderBy( - builder: (deleteTable: TTable) => ValueOrArray, - ): GoogleSqlDeleteWithout; - orderBy(...columns: (GoogleSqlColumn | SQL | SQL.Aliased)[]): GoogleSqlDeleteWithout; - orderBy( - ...columns: - | [(deleteTable: TTable) => ValueOrArray] - | (GoogleSqlColumn | SQL | SQL.Aliased)[] - ): GoogleSqlDeleteWithout { - if (typeof columns[0] === 'function') { - const orderBy = columns[0]( - new Proxy( - this.config.table[Table.Symbol.Columns], - new SelectionProxyHandler({ sqlAliasedBehavior: 'alias', sqlBehavior: 'sql' }), - ) as any, - ); - - const orderByArray = Array.isArray(orderBy) ? orderBy : [orderBy]; - this.config.orderBy = orderByArray; - } else { - const orderByArray = columns as (GoogleSqlColumn | SQL | SQL.Aliased)[]; - this.config.orderBy = orderByArray; - } - return this as any; - } - - limit(limit: number | Placeholder): GoogleSqlDeleteWithout { - this.config.limit = limit; - return this as any; - } - /** @internal */ getSQL(): SQL { return this.dialect.buildDeleteQuery(this.config); diff --git a/drizzle-orm/src/googlesql/query-builders/insert.ts b/drizzle-orm/src/googlesql/query-builders/insert.ts index 8bd043e717..dde5bb53bd 100644 --- a/drizzle-orm/src/googlesql/query-builders/insert.ts +++ b/drizzle-orm/src/googlesql/query-builders/insert.ts @@ -14,20 +14,20 @@ import type { TypedQueryBuilder } from '~/query-builders/query-builder.ts'; import { QueryPromise } from '~/query-promise.ts'; import type { RunnableQuery } from '~/runnable-query.ts'; import type { Placeholder, Query, SQLWrapper } from '~/sql/sql.ts'; -import { Param, SQL, sql } from '~/sql/sql.ts'; +import { Param, SQL } from '~/sql/sql.ts'; import type { InferModelFromColumns } from '~/table.ts'; import { Columns, Table } from '~/table.ts'; -import { haveSameKeys, mapUpdateSet } from '~/utils.ts'; +import { haveSameKeys } from '~/utils.ts'; import type { AnyGoogleSqlColumn } from '../columns/common.ts'; import { QueryBuilder } from './query-builder.ts'; import type { SelectedFieldsOrdered } from './select.types.ts'; -import type { GoogleSqlUpdateSetSource } from './update.ts'; export interface GoogleSqlInsertConfig { table: TTable; values: Record[] | GoogleSqlInsertSelectQueryBuilder | SQL; ignore: boolean; - onConflict?: SQL; + update: boolean; + // onConflict?: SQL; returning?: SelectedFieldsOrdered; select?: boolean; } @@ -52,6 +52,7 @@ export class GoogleSqlInsertBuilder< static readonly [entityKind]: string = 'GoogleSqlInsertBuilder'; private shouldIgnore = false; + private shouldUpdate = false; constructor( private table: TTable, @@ -64,6 +65,11 @@ export class GoogleSqlInsertBuilder< return this; } + update(): this { + this.shouldUpdate = true; + return this; + } + values(value: GoogleSqlInsertValue): GoogleSqlInsertBase; values(values: GoogleSqlInsertValue[]): GoogleSqlInsertBase; values( @@ -83,7 +89,7 @@ export class GoogleSqlInsertBuilder< return result; }); - return new GoogleSqlInsertBase(this.table, mappedValues, this.shouldIgnore, this.session, this.dialect); + return new GoogleSqlInsertBase(this.table, mappedValues, this.shouldIgnore, this.shouldUpdate, this.session, this.dialect); } select( @@ -111,7 +117,7 @@ export class GoogleSqlInsertBuilder< ); } - return new GoogleSqlInsertBase(this.table, select, this.shouldIgnore, this.session, this.dialect, true); + return new GoogleSqlInsertBase(this.table, select, this.shouldIgnore,this.shouldUpdate, this.session, this.dialect, true); } } @@ -148,10 +154,6 @@ export type GoogleSqlInsertPrepare< true >; -export type GoogleSqlInsertOnDuplicateKeyUpdateConfig = { - set: GoogleSqlUpdateSetSource; -}; - export type GoogleSqlInsert< TTable extends GoogleSqlTable = GoogleSqlTable, TQueryResult extends GoogleSqlQueryResultHKT = AnyGoogleSqlQueryResultHKT, @@ -241,20 +243,24 @@ export class GoogleSqlInsertBase< table: TTable, values: GoogleSqlInsertConfig['values'], ignore: boolean, + update: boolean, private session: GoogleSqlSession, private dialect: GoogleSqlDialect, select?: boolean, ) { super(); - this.config = { table, values: values as any, select, ignore }; + this.config = { table, values: values as any, select, ignore, update }; } + // spanner does not support onConflict, we can use update() instead /** * Adds an `on duplicate key update` clause to the query. * * Calling this method will update the row if any unique index conflicts. MySQL will automatically determine the conflict target based on the primary key and unique indexes. * * See docs: {@link https://orm.drizzle.team/docs/insert#on-duplicate-key-update} + * + * @deprecated Use `update()` instead. * * @param config The `set` clause * @@ -276,13 +282,15 @@ export class GoogleSqlInsertBase< * ``` */ onDuplicateKeyUpdate( - config: GoogleSqlInsertOnDuplicateKeyUpdateConfig, + // config: GoogleSqlInsertOnDuplicateKeyUpdateConfig, ): GoogleSqlInsertWithout { - const setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set)); - this.config.onConflict = sql`update ${setSql}`; + // const setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set)); + // this.config.onConflict = sql`update ${setSql}`; + this.config.update = true; return this as any; } + // TODO: SPANNER - still not sure if it will be supported by spanner driver $returningId(): GoogleSqlInsertWithout< GoogleSqlInsertReturning, TDynamic, diff --git a/drizzle-orm/src/googlesql/query-builders/query.ts b/drizzle-orm/src/googlesql/query-builders/query.ts index 70363dd626..8c6fb19f79 100644 --- a/drizzle-orm/src/googlesql/query-builders/query.ts +++ b/drizzle-orm/src/googlesql/query-builders/query.ts @@ -112,25 +112,35 @@ export class GoogleSqlRelationalQuery< } private _getQuery() { - const query = this.mode === 'planetscale' - ? this.dialect.buildRelationalQueryWithoutLateralSubqueries({ - fullSchema: this.fullSchema, - schema: this.schema, - tableNamesMap: this.tableNamesMap, - table: this.table, - tableConfig: this.tableConfig, - queryConfig: this.config, - tableAlias: this.tableConfig.tsName, - }) - : this.dialect.buildRelationalQuery({ - fullSchema: this.fullSchema, - schema: this.schema, - tableNamesMap: this.tableNamesMap, - table: this.table, - tableConfig: this.tableConfig, - queryConfig: this.config, - tableAlias: this.tableConfig.tsName, - }); + // const query = this.mode === 'planetscale' + // ? this.dialect.buildRelationalQueryWithoutLateralSubqueries({ + // fullSchema: this.fullSchema, + // schema: this.schema, + // tableNamesMap: this.tableNamesMap, + // table: this.table, + // tableConfig: this.tableConfig, + // queryConfig: this.config, + // tableAlias: this.tableConfig.tsName, + // }) + // : this.dialect.buildRelationalQuery({ + // fullSchema: this.fullSchema, + // schema: this.schema, + // tableNamesMap: this.tableNamesMap, + // table: this.table, + // tableConfig: this.tableConfig, + // queryConfig: this.config, + // tableAlias: this.tableConfig.tsName, + // }); + + const query = this.dialect.buildRelationalQuery({ + fullSchema: this.fullSchema, + schema: this.schema, + tableNamesMap: this.tableNamesMap, + table: this.table, + tableConfig: this.tableConfig, + queryConfig: this.config, + tableAlias: this.tableConfig.tsName, + }); return query; } diff --git a/drizzle-orm/src/googlesql/query-builders/select.ts b/drizzle-orm/src/googlesql/query-builders/select.ts index b6ef7a1934..e67f7cb45d 100644 --- a/drizzle-orm/src/googlesql/query-builders/select.ts +++ b/drizzle-orm/src/googlesql/query-builders/select.ts @@ -50,9 +50,9 @@ import type { export type IndexForHint = IndexBuilder | string; export type IndexConfig = { - useIndex?: IndexForHint | IndexForHint[]; + // useIndex?: IndexForHint | IndexForHint[]; forceIndex?: IndexForHint | IndexForHint[]; - ignoreIndex?: IndexForHint | IndexForHint[]; + // ignoreIndex?: IndexForHint | IndexForHint[]; }; export class GoogleSqlSelectBuilder< @@ -117,19 +117,9 @@ export class GoogleSqlSelectBuilder< fields = getTableColumns(source); } - let useIndex: string[] = []; let forceIndex: string[] = []; - let ignoreIndex: string[] = []; - if (is(source, GoogleSqlTable) && onIndex && typeof onIndex !== 'string') { - if (onIndex.useIndex) { - useIndex = convertIndexToString(toArray(onIndex.useIndex)); - } - if (onIndex.forceIndex) { - forceIndex = convertIndexToString(toArray(onIndex.forceIndex)); - } - if (onIndex.ignoreIndex) { - ignoreIndex = convertIndexToString(toArray(onIndex.ignoreIndex)); - } + if (is(source, GoogleSqlTable) && onIndex && typeof onIndex !== 'string' && onIndex.forceIndex) { + forceIndex = convertIndexToString(toArray(onIndex.forceIndex)); } return new GoogleSqlSelectBase( @@ -141,9 +131,7 @@ export class GoogleSqlSelectBuilder< dialect: this.dialect, withList: this.withList, distinct: this.distinct, - useIndex, forceIndex, - ignoreIndex, }, ) as any; } @@ -186,7 +174,7 @@ export abstract class GoogleSqlSelectQueryBuilderBase< protected dialect: GoogleSqlDialect; constructor( - { table, fields, isPartialSelect, session, dialect, withList, distinct, useIndex, forceIndex, ignoreIndex }: { + { table, fields, isPartialSelect, session, dialect, withList, distinct, forceIndex }: { table: GoogleSqlSelectConfig['table']; fields: GoogleSqlSelectConfig['fields']; isPartialSelect: boolean; @@ -194,9 +182,7 @@ export abstract class GoogleSqlSelectQueryBuilderBase< dialect: GoogleSqlDialect; withList: Subquery[]; distinct: boolean | undefined; - useIndex?: string[]; forceIndex?: string[]; - ignoreIndex?: string[]; }, ) { super(); @@ -206,9 +192,9 @@ export abstract class GoogleSqlSelectQueryBuilderBase< fields: { ...fields }, distinct, setOperators: [], - useIndex, + // useIndex, forceIndex, - ignoreIndex, + // ignoreIndex, }; this.isPartialSelect = isPartialSelect; this.session = session; @@ -268,22 +254,12 @@ export abstract class GoogleSqlSelectQueryBuilderBase< this.config.joins = []; } - let useIndex: string[] = []; let forceIndex: string[] = []; - let ignoreIndex: string[] = []; - if (is(table, GoogleSqlTable) && onIndex && typeof onIndex !== 'string') { - if (onIndex.useIndex) { - useIndex = convertIndexToString(toArray(onIndex.useIndex)); - } - if (onIndex.forceIndex) { - forceIndex = convertIndexToString(toArray(onIndex.forceIndex)); - } - if (onIndex.ignoreIndex) { - ignoreIndex = convertIndexToString(toArray(onIndex.ignoreIndex)); - } + if (is(table, GoogleSqlTable) && onIndex && typeof onIndex !== 'string' && onIndex.forceIndex) { + forceIndex = convertIndexToString(toArray(onIndex.forceIndex)); } - this.config.joins.push({ on, table, joinType, alias: tableName, useIndex, forceIndex, ignoreIndex }); + this.config.joins.push({ on, table, joinType, alias: tableName, forceIndex }); if (typeof tableName === 'string') { switch (joinType) { diff --git a/drizzle-orm/src/googlesql/query-builders/select.types.ts b/drizzle-orm/src/googlesql/query-builders/select.types.ts index b25c7213c1..9fc6033533 100644 --- a/drizzle-orm/src/googlesql/query-builders/select.types.ts +++ b/drizzle-orm/src/googlesql/query-builders/select.types.ts @@ -77,9 +77,9 @@ export interface GoogleSqlSelectConfig { limit?: number | Placeholder; offset?: number | Placeholder; }[]; - useIndex?: string[]; + // useIndex?: string[]; forceIndex?: string[]; - ignoreIndex?: string[]; + // ignoreIndex?: string[]; } export type GoogleSqlJoin< diff --git a/drizzle-orm/src/googlesql/query-builders/update.ts b/drizzle-orm/src/googlesql/query-builders/update.ts index 7dce6470c6..95a278e9a8 100644 --- a/drizzle-orm/src/googlesql/query-builders/update.ts +++ b/drizzle-orm/src/googlesql/query-builders/update.ts @@ -12,22 +12,18 @@ import type { } from '~/googlesql/session.ts'; import type { GoogleSqlTable } from '~/googlesql/table.ts'; import { QueryPromise } from '~/query-promise.ts'; -import { SelectionProxyHandler } from '~/selection-proxy.ts'; -import type { Placeholder, Query, SQL, SQLWrapper } from '~/sql/sql.ts'; -import type { Subquery } from '~/subquery.ts'; -import { Table } from '~/table.ts'; -import { mapUpdateSet, type UpdateSet, type ValueOrArray } from '~/utils.ts'; -import type { GoogleSqlColumn } from '../columns/common.ts'; +import type { Query, SQL, SQLWrapper } from '~/sql/sql.ts'; +import { mapUpdateSet, type UpdateSet } from '~/utils.ts'; import type { SelectedFieldsOrdered } from './select.types.ts'; export interface GoogleSqlUpdateConfig { where?: SQL | undefined; - limit?: number | Placeholder; - orderBy?: (GoogleSqlColumn | SQL | SQL.Aliased)[]; + // limit?: number | Placeholder; + // orderBy?: (GoogleSqlColumn | SQL | SQL.Aliased)[]; set: UpdateSet; table: GoogleSqlTable; returning?: SelectedFieldsOrdered; - withList?: Subquery[]; + // withList?: Subquery[]; } export type GoogleSqlUpdateSetSource = @@ -54,7 +50,7 @@ export class GoogleSqlUpdateBuilder< private table: TTable, private session: GoogleSqlSession, private dialect: GoogleSqlDialect, - private withList?: Subquery[], + // private withList?: Subquery[], ) {} set(values: GoogleSqlUpdateSetSource): GoogleSqlUpdateBase { @@ -63,7 +59,7 @@ export class GoogleSqlUpdateBuilder< mapUpdateSet(this.table, values), this.session, this.dialect, - this.withList, + // this.withList, ); } } @@ -141,10 +137,10 @@ export class GoogleSqlUpdateBase< set: UpdateSet, private session: GoogleSqlSession, private dialect: GoogleSqlDialect, - withList?: Subquery[], + // withList?: Subquery[], ) { super(); - this.config = { set, table, withList }; + this.config = { set, table }; } /** @@ -185,37 +181,6 @@ export class GoogleSqlUpdateBase< return this as any; } - orderBy( - builder: (updateTable: TTable) => ValueOrArray, - ): GoogleSqlUpdateWithout; - orderBy(...columns: (GoogleSqlColumn | SQL | SQL.Aliased)[]): GoogleSqlUpdateWithout; - orderBy( - ...columns: - | [(updateTable: TTable) => ValueOrArray] - | (GoogleSqlColumn | SQL | SQL.Aliased)[] - ): GoogleSqlUpdateWithout { - if (typeof columns[0] === 'function') { - const orderBy = columns[0]( - new Proxy( - this.config.table[Table.Symbol.Columns], - new SelectionProxyHandler({ sqlAliasedBehavior: 'alias', sqlBehavior: 'sql' }), - ) as any, - ); - - const orderByArray = Array.isArray(orderBy) ? orderBy : [orderBy]; - this.config.orderBy = orderByArray; - } else { - const orderByArray = columns as (GoogleSqlColumn | SQL | SQL.Aliased)[]; - this.config.orderBy = orderByArray; - } - return this as any; - } - - limit(limit: number | Placeholder): GoogleSqlUpdateWithout { - this.config.limit = limit; - return this as any; - } - /** @internal */ getSQL(): SQL { return this.dialect.buildUpdateQuery(this.config); From da5f4305370334f6842295fd14569f23f6ba109d Mon Sep 17 00:00:00 2001 From: Gabriel Cipriano Date: Fri, 7 Mar 2025 15:42:11 +0100 Subject: [PATCH 3/3] feat: dialect with and view intrinsics --- drizzle-orm/src/googlesql/db.ts | 22 +++++++++++----------- drizzle-orm/src/googlesql/view.ts | 17 ++--------------- 2 files changed, 13 insertions(+), 26 deletions(-) diff --git a/drizzle-orm/src/googlesql/db.ts b/drizzle-orm/src/googlesql/db.ts index 65a26a2cdc..e57dccc7eb 100644 --- a/drizzle-orm/src/googlesql/db.ts +++ b/drizzle-orm/src/googlesql/db.ts @@ -283,11 +283,11 @@ export class GoogleSqlDatabase< * await db.update(cars).set({ color: 'red' }).where(eq(cars.brand, 'BMW')); * ``` */ - function update( - table: TTable, - ): GoogleSqlUpdateBuilder { - return new GoogleSqlUpdateBuilder(table, self.session, self.dialect, queries); - } + // function update( + // table: TTable, + // ): GoogleSqlUpdateBuilder { + // return new GoogleSqlUpdateBuilder(table, self.session, self.dialect, queries); + // } /** * Creates a delete query. @@ -308,13 +308,13 @@ export class GoogleSqlDatabase< * await db.delete(cars).where(eq(cars.color, 'green')); * ``` */ - function delete_( - table: TTable, - ): GoogleSqlDeleteBase { - return new GoogleSqlDeleteBase(table, self.session, self.dialect, queries); - } + // function delete_( + // table: TTable, + // ): GoogleSqlDeleteBase { + // return new GoogleSqlDeleteBase(table, self.session, self.dialect, queries); + // } - return { select, selectDistinct, update, delete: delete_ }; + return { select, selectDistinct }; } /** diff --git a/drizzle-orm/src/googlesql/view.ts b/drizzle-orm/src/googlesql/view.ts index 4b69572f1c..c89897ea24 100644 --- a/drizzle-orm/src/googlesql/view.ts +++ b/drizzle-orm/src/googlesql/view.ts @@ -12,9 +12,9 @@ import { GoogleSqlViewBase } from './view-base.ts'; import { GoogleSqlViewConfig } from './view-common.ts'; export interface ViewBuilderConfig { - algorithm?: 'undefined' | 'merge' | 'temptable'; + // algorithm?: 'undefined' | 'merge' | 'temptable'; sqlSecurity?: 'definer' | 'invoker'; - withCheckOption?: 'cascaded' | 'local'; + // withCheckOption?: 'cascaded' | 'local'; } export class ViewBuilderCore { @@ -32,13 +32,6 @@ export class ViewBuilderCore, - ): this { - this.config.algorithm = algorithm; - return this; - } - sqlSecurity( sqlSecurity: Exclude, ): this { @@ -46,12 +39,6 @@ export class ViewBuilderCore, - ): this { - this.config.withCheckOption = withCheckOption ?? 'cascaded'; - return this; - } } export class ViewBuilder extends ViewBuilderCore<{ name: TName }> {