Skip to content

Commit 70d033e

Browse files
committed
Split PgRaw
1 parent 95ed77f commit 70d033e

File tree

6 files changed

+87
-87
lines changed

6 files changed

+87
-87
lines changed

drizzle-orm/src/aws-data-api/pg/driver.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import { entityKind, is } from '~/entity.ts';
44
import type { Logger } from '~/logger.ts';
55
import { DefaultLogger } from '~/logger.ts';
66
import { PgAsyncDatabase } from '~/pg-core/async/db.ts';
7+
import type { PgAsyncRaw } from '~/pg-core/async/raw.ts';
78
import { PgDialect } from '~/pg-core/dialect.ts';
89
import type { PgColumn, PgInsertConfig, PgTable, TableConfig } from '~/pg-core/index.ts';
910
import { PgArray } from '~/pg-core/index.ts';
10-
import type { PgRaw } from '~/pg-core/query-builders/raw.ts';
1111
import type { AnyRelations, EmptyRelations } from '~/relations.ts';
1212
import { Param, type SQL, sql, type SQLWrapper } from '~/sql/sql.ts';
1313
import { Table } from '~/table.ts';
@@ -40,7 +40,7 @@ export class AwsDataApiPgDatabase<
4040

4141
override execute<
4242
TRow extends Record<string, unknown> = Record<string, unknown>,
43-
>(query: SQLWrapper | string): PgRaw<AwsDataApiPgQueryResult<TRow>> {
43+
>(query: SQLWrapper | string): PgAsyncRaw<AwsDataApiPgQueryResult<TRow>> {
4444
return super.execute(query);
4545
}
4646
}

drizzle-orm/src/pg-core/async/db.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import type { DrizzleTypeError, NeonAuthToken } from '~/utils.ts';
1515
import type { PgColumn } from '../columns/index.ts';
1616
import { _RelationalQueryBuilder } from '../query-builders/_query.ts';
1717
import { RelationalQueryBuilder } from '../query-builders/query.ts';
18-
import { PgRaw } from '../query-builders/raw.ts';
1918
import type { SelectedFields } from '../query-builders/select.types.ts';
2019
import type { PreparedQueryConfig } from '../session.ts';
2120
import type { WithBuilder } from '../subquery.ts';
@@ -25,6 +24,7 @@ import { PgAsyncCountBuilder } from './count.ts';
2524
import { PgAsyncDeleteBase } from './delete.ts';
2625
import { PgAsyncInsertBase, type PgAsyncInsertHKT } from './insert.ts';
2726
import { PgAsyncRelationalQuery, type PgAsyncRelationalQueryHKT } from './query.ts';
27+
import { PgAsyncRaw } from './raw.ts';
2828
import { PgAsyncRefreshMaterializedView } from './refresh-materialized-view.ts';
2929
import { PgAsyncSelectBase, type PgAsyncSelectBuilder } from './select.ts';
3030
import { PgAsyncUpdateBase, type PgAsyncUpdateHKT } from './update.ts';
@@ -653,17 +653,17 @@ export class PgAsyncDatabase<
653653

654654
execute<TRow extends Record<string, unknown> = Record<string, unknown>>(
655655
query: SQLWrapper | string,
656-
): PgRaw<PgQueryResultKind<TQueryResult, TRow>>;
656+
): PgAsyncRaw<PgQueryResultKind<TQueryResult, TRow>>;
657657
/** @internal */
658658
execute<TRow extends Record<string, unknown> = Record<string, unknown>>(
659659
query: SQLWrapper | string,
660660
authToken: NeonAuthToken,
661-
): PgRaw<PgQueryResultKind<TQueryResult, TRow>>;
661+
): PgAsyncRaw<PgQueryResultKind<TQueryResult, TRow>>;
662662
/** @internal */
663663
execute<TRow extends Record<string, unknown> = Record<string, unknown>>(
664664
query: SQLWrapper | string,
665665
authToken?: NeonAuthToken,
666-
): PgRaw<PgQueryResultKind<TQueryResult, TRow>> {
666+
): PgAsyncRaw<PgQueryResultKind<TQueryResult, TRow>> {
667667
const sequel = typeof query === 'string' ? sql.raw(query) : query.getSQL();
668668
const builtQuery = this.dialect.sqlToQuery(sequel);
669669
const prepared = this.session.prepareQuery<
@@ -674,7 +674,7 @@ export class PgAsyncDatabase<
674674
undefined,
675675
false,
676676
).setToken(authToken);
677-
return new PgRaw(
677+
return new PgAsyncRaw(
678678
() => prepared.execute(),
679679
sequel,
680680
builtQuery,
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { entityKind } from '~/entity.ts';
2+
import { QueryPromise } from '~/query-promise.ts';
3+
import type { RunnableQuery } from '~/runnable-query.ts';
4+
import type { PreparedQuery } from '~/session.ts';
5+
import type { Query, SQL, SQLWrapper } from '~/sql/sql.ts';
6+
import { applyMixins } from '~/utils';
7+
import { PgRaw } from '../query-builders/raw';
8+
9+
export interface PgAsyncRaw<TResult> extends QueryPromise<TResult>, RunnableQuery<TResult, 'pg'>, SQLWrapper {}
10+
export class PgAsyncRaw<TResult> extends PgRaw<TResult> implements RunnableQuery<TResult, 'pg'> {
11+
static override readonly [entityKind]: string = 'PgAsyncRaw';
12+
13+
declare readonly _: {
14+
readonly dialect: 'pg';
15+
readonly result: TResult;
16+
};
17+
18+
constructor(
19+
public execute: () => Promise<TResult>,
20+
sql: SQL,
21+
query: Query,
22+
mapBatchResult: (result: unknown) => unknown,
23+
) {
24+
super(sql, query, mapBatchResult);
25+
}
26+
27+
_prepare(): PreparedQuery {
28+
return this;
29+
}
30+
}
31+
32+
applyMixins(PgAsyncRaw, [QueryPromise]);

drizzle-orm/src/pg-core/effect/db.ts

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@ import type { PgViewBase } from '~/pg-core/view-base.ts';
1515
import type { TypedQueryBuilder } from '~/query-builders/query-builder.ts';
1616
import type { AnyRelations, EmptyRelations } from '~/relations.ts';
1717
import { SelectionProxyHandler } from '~/selection-proxy.ts';
18-
import type { ColumnsSelection, SQL, SQLWrapper } from '~/sql/sql.ts';
18+
import { type ColumnsSelection, type SQL, sql, type SQLWrapper } from '~/sql/sql.ts';
1919
import { WithSubquery } from '~/subquery.ts';
20-
import type { DrizzleTypeError } from '~/utils.ts';
2120
import type { PgColumn } from '../columns/common.ts';
2221
import { QueryBuilder } from '../query-builders/query-builder.ts';
2322
import type { SelectedFields } from '../query-builders/select.types.ts';
2423
import { PgUpdateBuilder } from '../query-builders/update.ts';
25-
import type { PgQueryResultHKT } from '../session.ts';
24+
import type { PgQueryResultHKT, PgQueryResultKind, PreparedQueryConfig } from '../session.ts';
2625
import type { WithBuilder } from '../subquery.ts';
2726
import type { PgMaterializedView } from '../view.ts';
2827
import { PgEffectDeleteBase } from './delete.ts';
2928
import { PgEffectRelationalQuery, type PgEffectRelationalQueryHKT } from './query.ts';
29+
import { PgEffectRaw } from './raw.ts';
3030
import { PgEffectRefreshMaterializedView } from './refresh-materialized-view.ts';
3131
import type { PgEffectSession } from './session.ts';
3232
import { PgEffectUpdateBase, type PgEffectUpdateHKT } from './update.ts';
@@ -47,13 +47,6 @@ export class PgEffectDatabase<
4747
readonly session: PgEffectSession<TFullSchema, TRelations, TSchema>;
4848
};
4949

50-
/** @deprecated */
51-
_query: TFullSchema extends Record<string, never>
52-
? DrizzleTypeError<'Seems like the schema generic is missing - did you forget to add it to your DB type?'>
53-
: {
54-
[K in keyof TSchema]: _RelationalQueryBuilder<TSchema, TSchema[K]>;
55-
};
56-
5750
// TO-DO: Figure out how to pass DrizzleTypeError without breaking withReplicas
5851
query: {
5952
[K in keyof TRelations]: RelationalQueryBuilder<
@@ -87,21 +80,7 @@ export class PgEffectDatabase<
8780
relations: relations,
8881
session,
8982
};
90-
this._query = {} as typeof this['_query'];
91-
// if (this._.schema) {
92-
// for (const [tableName, columns] of Object.entries(this._.schema)) {
93-
// (this._query as PgDatabase<TQueryResult, Record<string, any>>['_query'])[tableName] =
94-
// new _RelationalQueryBuilder(
95-
// schema!.fullSchema,
96-
// this._.schema,
97-
// this._.tableNamesMap,
98-
// schema!.fullSchema[tableName] as PgTable,
99-
// columns,
100-
// dialect,
101-
// session,
102-
// );
103-
// }
104-
// }
83+
10584
this.query = {} as typeof this['query'];
10685
for (const [tableName, relation] of Object.entries(relations)) {
10786
(this.query as PgEffectDatabase<
@@ -656,6 +635,27 @@ export class PgEffectDatabase<
656635
): PgEffectRefreshMaterializedView<TQueryResult> {
657636
return new PgEffectRefreshMaterializedView(view, this.session, this.dialect);
658637
}
638+
639+
execute<TRow extends Record<string, unknown> = Record<string, unknown>>(
640+
query: SQLWrapper | string,
641+
): PgEffectRaw<PgQueryResultKind<TQueryResult, TRow>> {
642+
const sequel = typeof query === 'string' ? sql.raw(query) : query.getSQL();
643+
const builtQuery = this.dialect.sqlToQuery(sequel);
644+
const prepared = this.session.prepareQuery<
645+
PreparedQueryConfig & { execute: PgQueryResultKind<TQueryResult, TRow> }
646+
>(
647+
builtQuery,
648+
undefined,
649+
undefined,
650+
false,
651+
);
652+
return new PgEffectRaw(
653+
() => prepared.execute(),
654+
sequel,
655+
builtQuery,
656+
(result) => prepared.mapResult(result, true),
657+
);
658+
}
659659
}
660660

661661
export type PgEffectWithReplicas<Q> = Q & { $primary: Q; $replicas: Q[] };
@@ -686,7 +686,7 @@ export const withReplicas = <
686686
const update: Q['update'] = (...args: [any]) => primary.update(...args);
687687
const insert: Q['insert'] = (...args: [any]) => primary.insert(...args);
688688
const $delete: Q['delete'] = (...args: [any]) => primary.delete(...args);
689-
// const execute: Q['execute'] = (...args: [any]) => primary.execute(...args);
689+
const execute: Q['execute'] = (...args: [any]) => primary.execute(...args);
690690
// const transaction: Q['transaction'] = (...args: [any]) => primary.transaction(...args);
691691
const refreshMaterializedView: Q['refreshMaterializedView'] = (...args: [any]) =>
692692
primary.refreshMaterializedView(...args);
@@ -696,7 +696,7 @@ export const withReplicas = <
696696
update,
697697
insert,
698698
delete: $delete,
699-
// execute,
699+
execute,
700700
// transaction,
701701
refreshMaterializedView,
702702
$primary: primary,
@@ -707,9 +707,6 @@ export const withReplicas = <
707707
$count,
708708
$with,
709709
with: _with,
710-
get _query() {
711-
return getReplica(replicas)._query;
712-
},
713710
get query() {
714711
return getReplica(replicas).query;
715712
},
Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,33 @@
1-
import { QueryEffect } from '~/effect-core/query-effect.ts';
1+
import type { Effect } from 'effect/Effect';
2+
import { applyEffectWrapper, type QueryEffect } from '~/effect-core/query-effect';
23
import { entityKind } from '~/entity.ts';
3-
import type { DrizzleQueryError } from '~/errors.ts';
4+
import type { DrizzleQueryError } from '~/errors';
45
import type { RunnableQuery } from '~/runnable-query.ts';
56
import type { PreparedQuery } from '~/session.ts';
67
import type { Query, SQL, SQLWrapper } from '~/sql/sql.ts';
8+
import { PgRaw } from '../query-builders/raw';
79

8-
export interface EffectPgRaw<TResult>
9-
extends QueryEffect<TResult, DrizzleQueryError>, RunnableQuery<TResult, 'pg'>, SQLWrapper
10-
{}
11-
12-
export class EffectPgRaw<TResult> extends QueryEffect<TResult, DrizzleQueryError>
13-
implements RunnableQuery<TResult, 'pg'>, SQLWrapper, PreparedQuery
14-
{
15-
static override readonly [entityKind]: string = 'EffectPgRaw';
10+
export interface PgEffectRaw<TResult> extends QueryEffect<TResult>, RunnableQuery<TResult, 'pg'>, SQLWrapper {}
11+
export class PgEffectRaw<TResult> extends PgRaw<TResult> implements RunnableQuery<TResult, 'pg'> {
12+
static override readonly [entityKind]: string = 'PgEffectRaw';
1613

1714
declare readonly _: {
1815
readonly dialect: 'pg';
1916
readonly result: TResult;
2017
};
2118

2219
constructor(
23-
public execute: QueryEffect<TResult, DrizzleQueryError>['execute'],
24-
private sql: SQL,
25-
private query: Query,
26-
private mapBatchResult: (result: unknown) => unknown,
20+
public execute: () => Effect<TResult, DrizzleQueryError>,
21+
sql: SQL,
22+
query: Query,
23+
mapBatchResult: (result: unknown) => unknown,
2724
) {
28-
super();
29-
}
30-
31-
/** @internal */
32-
getSQL() {
33-
return this.sql;
34-
}
35-
36-
getQuery() {
37-
return this.query;
38-
}
39-
40-
mapResult(result: unknown, isFromBatch?: boolean) {
41-
return isFromBatch ? this.mapBatchResult(result) : result;
25+
super(sql, query, mapBatchResult);
4226
}
4327

4428
_prepare(): PreparedQuery {
4529
return this;
4630
}
47-
48-
/** @internal */
49-
isResponseInArrayMode() {
50-
return false;
51-
}
5231
}
32+
33+
applyEffectWrapper(PgEffectRaw);

drizzle-orm/src/pg-core/query-builders/raw.ts

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,22 @@
11
import { entityKind } from '~/entity.ts';
2-
import { QueryPromise } from '~/query-promise.ts';
3-
import type { RunnableQuery } from '~/runnable-query.ts';
42
import type { PreparedQuery } from '~/session.ts';
53
import type { Query, SQL, SQLWrapper } from '~/sql/sql.ts';
64

7-
export interface PgRaw<TResult> extends QueryPromise<TResult>, RunnableQuery<TResult, 'pg'>, SQLWrapper {}
8-
9-
export class PgRaw<TResult> extends QueryPromise<TResult>
10-
implements RunnableQuery<TResult, 'pg'>, SQLWrapper, PreparedQuery
11-
{
12-
static override readonly [entityKind]: string = 'PgRaw';
5+
// oxlint-disable-next-line no-unused-vars
6+
export interface PgRaw<TResult> extends SQLWrapper {}
7+
export class PgRaw<TResult> implements SQLWrapper, PreparedQuery {
8+
static readonly [entityKind]: string = 'PgRaw';
139

1410
declare readonly _: {
1511
readonly dialect: 'pg';
1612
readonly result: TResult;
1713
};
1814

1915
constructor(
20-
public execute: () => Promise<TResult>,
21-
private sql: SQL,
22-
private query: Query,
23-
private mapBatchResult: (result: unknown) => unknown,
16+
protected sql: SQL,
17+
protected query: Query,
18+
protected mapBatchResult: (result: unknown) => unknown,
2419
) {
25-
super();
2620
}
2721

2822
/** @internal */
@@ -38,10 +32,6 @@ export class PgRaw<TResult> extends QueryPromise<TResult>
3832
return isFromBatch ? this.mapBatchResult(result) : result;
3933
}
4034

41-
_prepare(): PreparedQuery {
42-
return this;
43-
}
44-
4535
/** @internal */
4636
isResponseInArrayMode() {
4737
return false;

0 commit comments

Comments
 (0)