Skip to content

Commit 253a7a3

Browse files
committed
Merge branch 'main' of https://github.com/drizzle-team/drizzle-orm into beta
2 parents 4e7eaa6 + 378b043 commit 253a7a3

File tree

28 files changed

+1362
-88
lines changed

28 files changed

+1362
-88
lines changed

changelogs/drizzle-kit/0.31.8.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
### Bug fixes
2+
3+
- Fixed `algorythm` => `algorithm` typo

changelogs/drizzle-orm/0.45.0.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
- Fixed pg-native Pool detection in node-postgres transactions
2+
- Allowed subqueries in select fields
3+
- Updated typo algorythm => algorithm
4+
- Fixed `$onUpdate` not handling `SQL` values (fixes [#2388](https://github.com/drizzle-team/drizzle-orm/issues/2388), tests implemented by [L-Mario564](https://github.com/L-Mario564) in [#2911](https://github.com/drizzle-team/drizzle-orm/pull/2911))
5+
- Fixed `pg` mappers not handling `Date` instances in `bun-sql:postgresql` driver responses for `date`, `timestamp` types (fixes [#4493](https://github.com/drizzle-team/drizzle-orm/issues/4493))

drizzle-kit/src/serializer/mysqlSerializer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ export const generateMySqlSnapshot = (
365365
columns: indexColumns,
366366
isUnique: value.config.unique ?? false,
367367
using: value.config.using,
368-
algorithm: value.config.algorythm,
368+
algorithm: value.config.algorithm,
369369
lock: value.config.lock,
370370
};
371371
});

drizzle-kit/src/serializer/singlestoreSerializer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ export const generateSingleStoreSnapshot = (
274274
columns: indexColumns,
275275
isUnique: value.config.unique ?? false,
276276
using: value.config.using,
277-
algorithm: value.config.algorythm,
277+
algorithm: value.config.algorithm,
278278
lock: value.config.lock,
279279
};
280280
});

drizzle-orm/src/gel-core/dialect.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,8 @@ export class GelDialect {
175175
return sql.join(columnNames.flatMap((colName, i) => {
176176
const col = tableColumns[colName]!;
177177

178-
const value = set[colName] ?? sql.param(col.onUpdateFn!(), col);
178+
const onUpdateFnResult = col.onUpdateFn?.();
179+
const value = set[colName] ?? (is(onUpdateFnResult, SQL) ? onUpdateFnResult : sql.param(onUpdateFnResult, col));
179180
const res = sql`${sql.identifier(this.casing.getColumnCasing(col))} = ${value}`;
180181

181182
if (i < setLength - 1) {
@@ -267,6 +268,23 @@ export class GelDialect {
267268
// } else {
268269
chunk.push(field);
269270
// }
271+
} else if (is(field, Subquery)) {
272+
const entries = Object.entries(field._.selectedFields) as [string, SQL.Aliased | Column | SQL][];
273+
274+
if (entries.length === 1) {
275+
const entry = entries[0]![1];
276+
277+
const fieldDecoder = is(entry, SQL)
278+
? entry.decoder
279+
: is(entry, Column)
280+
? { mapFromDriverValue: (v: any) => entry.mapFromDriverValue(v) }
281+
: entry.sql.decoder;
282+
283+
if (fieldDecoder) {
284+
field._.sql.decoder = fieldDecoder;
285+
}
286+
}
287+
chunk.push(field);
270288
}
271289

272290
if (i < columnsLen - 1) {

drizzle-orm/src/mysql-core/dialect.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,8 @@ export class MySqlDialect {
175175
return sql.join(columnNames.flatMap((colName, i) => {
176176
const col = tableColumns[colName]!;
177177

178-
const value = set[colName] ?? sql.param(col.onUpdateFn!(), col);
178+
const onUpdateFnResult = col.onUpdateFn?.();
179+
const value = set[colName] ?? (is(onUpdateFnResult, SQL) ? onUpdateFnResult : sql.param(onUpdateFnResult, col));
179180
const res = sql`${sql.identifier(this.casing.getColumnCasing(col))} = ${value}`;
180181

181182
if (i < setLength - 1) {
@@ -253,6 +254,23 @@ export class MySqlDialect {
253254
} else {
254255
chunk.push(field);
255256
}
257+
} else if (is(field, Subquery)) {
258+
const entries = Object.entries(field._.selectedFields) as [string, SQL.Aliased | Column | SQL][];
259+
260+
if (entries.length === 1) {
261+
const entry = entries[0]![1];
262+
263+
const fieldDecoder = is(entry, SQL)
264+
? entry.decoder
265+
: is(entry, Column)
266+
? { mapFromDriverValue: (v: any) => entry.mapFromDriverValue(v) }
267+
: entry.sql.decoder;
268+
269+
if (fieldDecoder) {
270+
field._.sql.decoder = fieldDecoder;
271+
}
272+
}
273+
chunk.push(field);
256274
}
257275

258276
if (i < columnsLen - 1) {

drizzle-orm/src/mysql-core/indexes.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ interface IndexConfig {
1919
using?: 'btree' | 'hash';
2020

2121
/**
22-
* If set, the index will be created as `create index ... algorythm { 'default' | 'inplace' | 'copy' }`.
22+
* If set, the index will be created as `create index ... algorithm { 'default' | 'inplace' | 'copy' }`.
2323
*/
24-
algorythm?: 'default' | 'inplace' | 'copy';
24+
algorithm?: 'default' | 'inplace' | 'copy';
2525

2626
/**
2727
* If set, adds locks to the index creation.
@@ -67,8 +67,8 @@ export class IndexBuilder implements AnyIndexBuilder {
6767
return this;
6868
}
6969

70-
algorythm(algorythm: IndexConfig['algorythm']): this {
71-
this.config.algorythm = algorythm;
70+
algorithm(algorithm: IndexConfig['algorithm']): this {
71+
this.config.algorithm = algorithm;
7272
return this;
7373
}
7474

drizzle-orm/src/node-postgres/session.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { tracer } from '~/tracing.ts';
1616
import { type Assume, mapResultRow } from '~/utils.ts';
1717

1818
const { Pool, types } = pg;
19+
const NativePool = (<any> pg).native ? (<{ Pool: typeof Pool }> (<any> pg).native).Pool : undefined;
1920

2021
export type NodePgClient = pg.Pool | PoolClient | Client;
2122

@@ -304,7 +305,7 @@ export class NodePgSession<
304305
transaction: (tx: NodePgTransaction<TFullSchema, TRelations, TSchema>) => Promise<T>,
305306
config?: PgTransactionConfig | undefined,
306307
): Promise<T> {
307-
const session = this.client instanceof Pool // eslint-disable-line no-instanceof/no-instanceof
308+
const session = (this.client instanceof Pool || (NativePool && this.client instanceof NativePool)) // eslint-disable-line no-instanceof/no-instanceof
308309
? new NodePgSession(await this.client.connect(), this.dialect, this.relations, this.schema, this.options)
309310
: this;
310311
const tx = new NodePgTransaction<TFullSchema, TRelations, TSchema>(
@@ -322,7 +323,7 @@ export class NodePgSession<
322323
await tx.execute(sql`rollback`);
323324
throw error;
324325
} finally {
325-
if (this.client instanceof Pool) { // eslint-disable-line no-instanceof/no-instanceof
326+
if (this.client instanceof Pool || (NativePool && this.client instanceof NativePool)) { // eslint-disable-line no-instanceof/no-instanceof
326327
(session.client as PoolClient).release();
327328
}
328329
}

drizzle-orm/src/operations.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Column } from './column.ts';
22
import type { SQL } from './sql/sql.ts';
3+
import type { Subquery } from './subquery.ts';
34
import type { Table } from './table.ts';
45

56
export type RequiredKeyOnly<TKey extends string, T extends Column> = T['_'] extends {
@@ -25,7 +26,7 @@ export type OptionalKeyOnly<TKey extends string, T extends Column, OverrideT ext
2526
// TODO: SQL -> SQLWrapper
2627
export type SelectedFieldsFlat<TColumn extends Column> = Record<
2728
string,
28-
TColumn | SQL | SQL.Aliased
29+
TColumn | SQL | SQL.Aliased | Subquery
2930
>;
3031

3132
export type SelectedFieldsFlatFull<TColumn extends Column> = Record<
@@ -40,5 +41,5 @@ export type SelectedFields<TColumn extends Column, TTable extends Table> = Recor
4041

4142
export type SelectedFieldsOrdered<TColumn extends Column> = {
4243
path: string[];
43-
field: TColumn | SQL | SQL.Aliased;
44+
field: TColumn | SQL | SQL.Aliased | Subquery;
4445
}[];

drizzle-orm/src/pg-core/columns/date.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ export class PgDate<T extends ColumnBaseConfig<'object date'>> extends PgColumn<
3030
return 'date';
3131
}
3232

33-
override mapFromDriverValue(value: string): Date {
34-
return new Date(value);
33+
override mapFromDriverValue(value: string | Date): Date {
34+
if (typeof value === 'string') return new Date(value);
35+
36+
return value;
3537
}
3638

3739
override mapToDriverValue(value: Date): string {
@@ -66,6 +68,12 @@ export class PgDateString<T extends ColumnBaseConfig<'string date'>> extends PgC
6668
getSQLType(): string {
6769
return 'date';
6870
}
71+
72+
override mapFromDriverValue(value: Date | string): string {
73+
if (typeof value === 'string') return value;
74+
75+
return value.toISOString().slice(0, -14);
76+
}
6977
}
7078

7179
export interface PgDateConfig<T extends 'date' | 'string' = 'date' | 'string'> {

0 commit comments

Comments
 (0)