Skip to content
6 changes: 4 additions & 2 deletions drizzle-orm/src/mysql-core/columns/datetime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnCon
import type { ColumnBaseConfig } from '~/column.ts';
import { entityKind } from '~/entity.ts';
import type { AnyMySqlTable } from '~/mysql-core/table.ts';
import type { Placeholder, SQL } from '~/sql/sql.ts';
import { type Equal, getColumnNameAndConfig } from '~/utils.ts';
import { MySqlColumn, MySqlColumnBuilder } from './common.ts';

Expand Down Expand Up @@ -53,8 +54,9 @@ export class MySqlDateTime<T extends ColumnBaseConfig<'date', 'MySqlDateTime'>>
return `datetime${precision}`;
}

override mapToDriverValue(value: Date): unknown {
return value.toISOString().replace('T', ' ').replace('Z', '');
override mapToDriverValue(value: Date | SQL | Placeholder): unknown {
// eslint-disable-next-line no-instanceof/no-instanceof
return value instanceof Date ? value.toISOString().replace('T', ' ').replace('Z', '') : value;
}

override mapFromDriverValue(value: string): Date {
Expand Down
7 changes: 4 additions & 3 deletions drizzle-orm/src/mysql-core/columns/json.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnConfig } from '~/column-builder.ts';
import type { ColumnBaseConfig } from '~/column.ts';
import { entityKind } from '~/entity.ts';
import { entityKind, is } from '~/entity.ts';
import type { AnyMySqlTable } from '~/mysql-core/table.ts';
import { Placeholder, SQL } from '~/sql/sql.ts';
import { MySqlColumn, MySqlColumnBuilder } from './common.ts';

export type MySqlJsonBuilderInitial<TName extends string> = MySqlJsonBuilder<{
Expand Down Expand Up @@ -35,8 +36,8 @@ export class MySqlJson<T extends ColumnBaseConfig<'json', 'MySqlJson'>> extends
return 'json';
}

override mapToDriverValue(value: T['data']): string {
return JSON.stringify(value);
override mapToDriverValue(value: T['data'] | SQL | Placeholder): string | SQL | Placeholder {
return is(value, SQL) || is(value, Placeholder) ? value : JSON.stringify(value);
}
}

Expand Down
6 changes: 4 additions & 2 deletions drizzle-orm/src/mysql-core/columns/timestamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnCon
import type { ColumnBaseConfig } from '~/column.ts';
import { entityKind } from '~/entity.ts';
import type { AnyMySqlTable } from '~/mysql-core/table.ts';
import type { Placeholder, SQL } from '~/sql/sql.ts';
import { type Equal, getColumnNameAndConfig } from '~/utils.ts';
import { MySqlDateBaseColumn, MySqlDateColumnBaseBuilder } from './date.common.ts';

Expand Down Expand Up @@ -51,8 +52,9 @@ export class MySqlTimestamp<T extends ColumnBaseConfig<'date', 'MySqlTimestamp'>
return new Date(value + '+0000');
}

override mapToDriverValue(value: Date): string {
return value.toISOString().slice(0, -1).replace('T', ' ');
override mapToDriverValue(value: Date | SQL | Placeholder): string | SQL | Placeholder {
// eslint-disable-next-line no-instanceof/no-instanceof
return value instanceof Date ? value.toISOString().slice(0, -1).replace('T', ' ') : value;
}
}

Expand Down
6 changes: 4 additions & 2 deletions drizzle-orm/src/pg-core/columns/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnCon
import type { ColumnBaseConfig } from '~/column.ts';
import { entityKind } from '~/entity.ts';
import type { AnyPgTable } from '~/pg-core/table.ts';
import type { Placeholder, SQL } from '~/sql/sql.ts';
import { type Equal, getColumnNameAndConfig } from '~/utils.ts';
import { PgColumn } from './common.ts';
import { PgDateColumnBaseBuilder } from './date.common.ts';
Expand Down Expand Up @@ -41,8 +42,9 @@ export class PgDate<T extends ColumnBaseConfig<'date', 'PgDate'>> extends PgColu
return new Date(value);
}

override mapToDriverValue(value: Date): string {
return value.toISOString();
override mapToDriverValue(value: Date | SQL | Placeholder): string | SQL | Placeholder {
// eslint-disable-next-line no-instanceof/no-instanceof
return value instanceof Date ? value.toISOString() : value;
}
}

Expand Down
7 changes: 4 additions & 3 deletions drizzle-orm/src/pg-core/columns/json.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnConfig } from '~/column-builder.ts';
import type { ColumnBaseConfig } from '~/column.ts';
import { entityKind } from '~/entity.ts';
import { entityKind, is } from '~/entity.ts';
import type { AnyPgTable } from '~/pg-core/table.ts';
import { Placeholder, SQL } from '~/sql/sql.ts';
import { PgColumn, PgColumnBuilder } from './common.ts';

export type PgJsonBuilderInitial<TName extends string> = PgJsonBuilder<{
Expand Down Expand Up @@ -41,8 +42,8 @@ export class PgJson<T extends ColumnBaseConfig<'json', 'PgJson'>> extends PgColu
return 'json';
}

override mapToDriverValue(value: T['data']): string {
return JSON.stringify(value);
override mapToDriverValue(value: T['data'] | SQL | Placeholder): string | SQL | Placeholder {
return is(value, SQL) || is(value, Placeholder) ? value : JSON.stringify(value);
}

override mapFromDriverValue(value: T['data'] | string): T['data'] {
Expand Down
7 changes: 4 additions & 3 deletions drizzle-orm/src/pg-core/columns/jsonb.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnConfig } from '~/column-builder.ts';
import type { ColumnBaseConfig } from '~/column.ts';
import { entityKind } from '~/entity.ts';
import { entityKind, is } from '~/entity.ts';
import type { AnyPgTable } from '~/pg-core/table.ts';
import { Placeholder, SQL } from '~/sql/sql.ts';
import { PgColumn, PgColumnBuilder } from './common.ts';

export type PgJsonbBuilderInitial<TName extends string> = PgJsonbBuilder<{
Expand Down Expand Up @@ -39,8 +40,8 @@ export class PgJsonb<T extends ColumnBaseConfig<'json', 'PgJsonb'>> extends PgCo
return 'jsonb';
}

override mapToDriverValue(value: T['data']): string {
return JSON.stringify(value);
override mapToDriverValue(value: T['data'] | SQL | Placeholder): string | SQL | Placeholder {
return is(value, SQL) || is(value, Placeholder) ? value : JSON.stringify(value);
}

override mapFromDriverValue(value: T['data'] | string): T['data'] {
Expand Down
16 changes: 10 additions & 6 deletions drizzle-orm/src/pg-core/columns/line.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnConfig } from '~/column-builder.ts';
import type { ColumnBaseConfig } from '~/column.ts';
import { entityKind } from '~/entity.ts';
import { entityKind, is } from '~/entity.ts';
import type { AnyPgTable } from '~/pg-core/table.ts';

import { Placeholder, SQL } from '~/sql/sql.ts';
import { type Equal, getColumnNameAndConfig } from '~/utils.ts';
import { PgColumn, PgColumnBuilder } from './common.ts';

Expand Down Expand Up @@ -45,8 +45,8 @@ export class PgLineTuple<T extends ColumnBaseConfig<'array', 'PgLine'>> extends
return [Number.parseFloat(a!), Number.parseFloat(b!), Number.parseFloat(c!)];
}

override mapToDriverValue(value: [number, number, number]): string {
return `{${value[0]},${value[1]},${value[2]}}`;
override mapToDriverValue(value: [number, number, number] | SQL | Placeholder): string | SQL | Placeholder {
return Array.isArray(value) ? `{${value[0]},${value[1]},${value[2]}}` : value;
}
}

Expand Down Expand Up @@ -89,8 +89,12 @@ export class PgLineABC<T extends ColumnBaseConfig<'json', 'PgLineABC'>> extends
return { a: Number.parseFloat(a!), b: Number.parseFloat(b!), c: Number.parseFloat(c!) };
}

override mapToDriverValue(value: { a: number; b: number; c: number }): string {
return `{${value.a},${value.b},${value.c}}`;
override mapToDriverValue(
value: { a: number; b: number; c: number } | SQL | Placeholder,
): string | SQL | Placeholder {
return is(value, SQL) || is(value, Placeholder)
? value
: `{${(value as any).a},${(value as any).b},${(value as any).c}}`;
}
}

Expand Down
12 changes: 6 additions & 6 deletions drizzle-orm/src/pg-core/columns/point.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnConfig } from '~/column-builder.ts';
import type { ColumnBaseConfig } from '~/column.ts';
import { entityKind } from '~/entity.ts';
import { entityKind, is } from '~/entity.ts';
import type { AnyPgTable } from '~/pg-core/table.ts';

import { Placeholder, SQL } from '~/sql/sql.ts';
import { type Equal, getColumnNameAndConfig } from '~/utils.ts';
import { PgColumn, PgColumnBuilder } from './common.ts';

Expand Down Expand Up @@ -50,8 +50,8 @@ export class PgPointTuple<T extends ColumnBaseConfig<'array', 'PgPointTuple'>> e
return [value.x, value.y];
}

override mapToDriverValue(value: [number, number]): string {
return `(${value[0]},${value[1]})`;
override mapToDriverValue(value: [number, number] | SQL | Placeholder): string | SQL | Placeholder {
return Array.isArray(value) ? `(${value[0]},${value[1]})` : value;
}
}

Expand Down Expand Up @@ -99,8 +99,8 @@ export class PgPointObject<T extends ColumnBaseConfig<'json', 'PgPointObject'>>
return value;
}

override mapToDriverValue(value: { x: number; y: number }): string {
return `(${value.x},${value.y})`;
override mapToDriverValue(value: { x: number; y: number } | SQL | Placeholder): string | SQL | Placeholder {
return is(value, SQL) || is(value, Placeholder) ? value : `(${(value as any).x},${(value as any).y})`;
}
}

Expand Down
12 changes: 6 additions & 6 deletions drizzle-orm/src/pg-core/columns/postgis_extension/geometry.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnConfig } from '~/column-builder.ts';
import type { ColumnBaseConfig } from '~/column.ts';
import { entityKind } from '~/entity.ts';
import { entityKind, is } from '~/entity.ts';
import type { AnyPgTable } from '~/pg-core/table.ts';

import { Placeholder, SQL } from '~/sql/sql.ts';
import { type Equal, getColumnNameAndConfig } from '~/utils.ts';
import { PgColumn, PgColumnBuilder } from '../common.ts';
import { parseEWKB } from './utils.ts';
Expand Down Expand Up @@ -45,8 +45,8 @@ export class PgGeometry<T extends ColumnBaseConfig<'array', 'PgGeometry'>> exten
return parseEWKB(value);
}

override mapToDriverValue(value: [number, number]): string {
return `point(${value[0]} ${value[1]})`;
override mapToDriverValue(value: [number, number] | SQL | Placeholder): string | SQL | Placeholder {
return Array.isArray(value) ? `point(${value[0]} ${value[1]})` : value;
}
}

Expand Down Expand Up @@ -91,8 +91,8 @@ export class PgGeometryObject<T extends ColumnBaseConfig<'json', 'PgGeometryObje
return { x: parsed[0], y: parsed[1] };
}

override mapToDriverValue(value: { x: number; y: number }): string {
return `point(${value.x} ${value.y})`;
override mapToDriverValue(value: { x: number; y: number } | SQL | Placeholder): string | SQL | Placeholder {
return is(value, SQL) || is(value, Placeholder) ? value : `point(${(value as any).x} ${(value as any).y})`;
}
}

Expand Down
8 changes: 5 additions & 3 deletions drizzle-orm/src/pg-core/columns/timestamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnCon
import type { ColumnBaseConfig } from '~/column.ts';
import { entityKind } from '~/entity.ts';
import type { AnyPgTable } from '~/pg-core/table.ts';
import type { Placeholder, SQL } from '~/sql/sql.ts';
import { type Equal, getColumnNameAndConfig } from '~/utils.ts';
import { PgColumn } from './common.ts';
import { PgDateColumnBaseBuilder } from './date.common.ts';
Expand Down Expand Up @@ -58,12 +59,13 @@ export class PgTimestamp<T extends ColumnBaseConfig<'date', 'PgTimestamp'>> exte
return `timestamp${precision}${this.withTimezone ? ' with time zone' : ''}`;
}

override mapFromDriverValue = (value: string): Date | null => {
override mapFromDriverValue = (value: string): Date => {
return new Date(this.withTimezone ? value : value + '+0000');
};

override mapToDriverValue = (value: Date): string => {
return value.toISOString();
override mapToDriverValue = (value: Date | SQL | Placeholder): string | SQL | Placeholder => {
// eslint-disable-next-line no-instanceof/no-instanceof
return value instanceof Date ? value.toISOString() : value;
};
}

Expand Down
5 changes: 3 additions & 2 deletions drizzle-orm/src/pg-core/columns/vector_extension/halfvec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnConfig } from '~/column-builder.ts';
import type { ColumnBaseConfig } from '~/column.ts';
import { entityKind } from '~/entity.ts';
import { entityKind, is } from '~/entity.ts';
import type { AnyPgTable } from '~/pg-core/table.ts';
import { Placeholder, SQL } from '~/sql/sql.ts';
import { getColumnNameAndConfig } from '~/utils.ts';
import { PgColumn, PgColumnBuilder } from '../common.ts';

Expand Down Expand Up @@ -52,7 +53,7 @@ export class PgHalfVector<T extends ColumnBaseConfig<'array', 'PgHalfVector'> &
}

override mapToDriverValue(value: unknown): unknown {
return JSON.stringify(value);
return is(value, SQL) || is(value, Placeholder) ? value : JSON.stringify(value);
}

override mapFromDriverValue(value: string): unknown {
Expand Down
5 changes: 3 additions & 2 deletions drizzle-orm/src/pg-core/columns/vector_extension/vector.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnConfig } from '~/column-builder.ts';
import type { ColumnBaseConfig } from '~/column.ts';
import { entityKind } from '~/entity.ts';
import { entityKind, is } from '~/entity.ts';
import type { AnyPgTable } from '~/pg-core/table.ts';
import { Placeholder, SQL } from '~/sql/sql.ts';
import { getColumnNameAndConfig } from '~/utils.ts';
import { PgColumn, PgColumnBuilder } from '../common.ts';

Expand Down Expand Up @@ -52,7 +53,7 @@ export class PgVector<T extends ColumnBaseConfig<'array', 'PgVector'> & { dimens
}

override mapToDriverValue(value: unknown): unknown {
return JSON.stringify(value);
return is(value, SQL) || is(value, Placeholder) ? value : JSON.stringify(value);
}

override mapFromDriverValue(value: string): unknown {
Expand Down
11 changes: 6 additions & 5 deletions drizzle-orm/src/sqlite-core/columns/blob.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnConfig } from '~/column-builder.ts';
import type { ColumnBaseConfig } from '~/column.ts';
import { entityKind } from '~/entity.ts';
import { entityKind, is } from '~/entity.ts';
import { Placeholder, SQL } from '~/sql/sql.ts';
import type { AnySQLiteTable } from '~/sqlite-core/table.ts';
import { type Equal, getColumnNameAndConfig } from '~/utils.ts';
import { SQLiteColumn, SQLiteColumnBuilder } from './common.ts';
Expand Down Expand Up @@ -55,8 +56,8 @@ export class SQLiteBigInt<T extends ColumnBaseConfig<'bigint', 'SQLiteBigInt'>>
return BigInt(String.fromCodePoint(...value));
}

override mapToDriverValue(value: bigint): Buffer {
return Buffer.from(value.toString());
override mapToDriverValue(value: bigint | SQL | Placeholder): Buffer | SQL | Placeholder {
return typeof value === 'bigint' ? Buffer.from(value.toString()) : value;
}
}

Expand Down Expand Up @@ -111,8 +112,8 @@ export class SQLiteBlobJson<T extends ColumnBaseConfig<'json', 'SQLiteBlobJson'>
return JSON.parse(String.fromCodePoint(...value));
}

override mapToDriverValue(value: T['data']): Buffer {
return Buffer.from(JSON.stringify(value));
override mapToDriverValue(value: T['data'] | SQL | Placeholder): Buffer | SQL | Placeholder {
return is(value, SQL) || is(value, Placeholder) ? value : Buffer.from(JSON.stringify(value));
}
}

Expand Down
17 changes: 11 additions & 6 deletions drizzle-orm/src/sqlite-core/columns/integer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import type {
NotNull,
} from '~/column-builder.ts';
import type { ColumnBaseConfig } from '~/column.ts';
import { entityKind } from '~/entity.ts';
import { sql } from '~/sql/sql.ts';
import { entityKind, is } from '~/entity.ts';
import { Placeholder, SQL, sql } from '~/sql/sql.ts';
import type { OnConflict } from '~/sqlite-core/utils.ts';
import { type Equal, getColumnNameAndConfig, type Or } from '~/utils.ts';
import type { AnySQLiteTable } from '../table.ts';
Expand Down Expand Up @@ -147,8 +147,11 @@ export class SQLiteTimestamp<T extends ColumnBaseConfig<'date', 'SQLiteTimestamp
return new Date(value);
}

override mapToDriverValue(value: Date): number {
const unix = value.getTime();
override mapToDriverValue(value: Date | SQL | Placeholder): number | SQL | Placeholder {
if (is(value, SQL) || is(value, Placeholder)) {
return value;
}
const unix = (value as Date).getTime();
if (this.config.mode === 'timestamp') {
return Math.floor(unix / 1000);
}
Expand Down Expand Up @@ -196,8 +199,10 @@ export class SQLiteBoolean<T extends ColumnBaseConfig<'boolean', 'SQLiteBoolean'
return Number(value) === 1;
}

override mapToDriverValue(value: boolean): number {
return value ? 1 : 0;
override mapToDriverValue(value: boolean | SQL | Placeholder): number | SQL | Placeholder {
return typeof value === 'boolean'
? value ? 1 : 0
: value;
}
}

Expand Down
7 changes: 4 additions & 3 deletions drizzle-orm/src/sqlite-core/columns/text.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnConfig } from '~/column-builder.ts';
import type { ColumnBaseConfig } from '~/column.ts';
import { entityKind } from '~/entity.ts';
import { entityKind, is } from '~/entity.ts';
import { Placeholder, SQL } from '~/sql/sql.ts';
import type { AnySQLiteTable } from '~/sqlite-core/table.ts';
import { type Equal, getColumnNameAndConfig, type Writable } from '~/utils.ts';
import { SQLiteColumn, SQLiteColumnBuilder } from './common.ts';
Expand Down Expand Up @@ -109,8 +110,8 @@ export class SQLiteTextJson<T extends ColumnBaseConfig<'json', 'SQLiteTextJson'>
return JSON.parse(value);
}

override mapToDriverValue(value: T['data']): string {
return JSON.stringify(value);
override mapToDriverValue(value: T['data'] | SQL | Placeholder): string | SQL | Placeholder {
return is(value, SQL) || is(value, Placeholder) ? value : JSON.stringify(value);
}
}

Expand Down
Loading