Skip to content

Commit

Permalink
Merge branch 'beta' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
AndriiSherman authored Sep 5, 2023
2 parents c2532ee + a613c36 commit cfcdb82
Show file tree
Hide file tree
Showing 30 changed files with 261 additions and 88 deletions.
4 changes: 2 additions & 2 deletions drizzle-orm/src/aws-data-api/pg/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
} from '~/pg-core/index.ts';
import type { SelectedFieldsOrdered } from '~/pg-core/query-builders/select.types.ts';
import { type RelationalSchemaConfig, type TablesRelationalConfig } from '~/relations.ts';
import { fillPlaceholders, type Query, type QueryTypingsValue, type SQL, sql } from '~/sql/index.ts';
import { fillPlaceholders, type QueryTypingsValue, type QueryWithTypings, type SQL, sql } from '~/sql/index.ts';
import { mapResultRow } from '~/utils.ts';
import { getValueFromDataApi, toValueParam } from '../common/index.ts';

Expand Down Expand Up @@ -147,7 +147,7 @@ export class AwsDataApiSession<
}

prepareQuery<T extends PreparedQueryConfig = PreparedQueryConfig>(
query: Query,
query: QueryWithTypings,
fields: SelectedFieldsOrdered | undefined,
transactionId?: string,
customResultMapper?: (rows: unknown[][]) => T['execute'],
Expand Down
4 changes: 2 additions & 2 deletions drizzle-orm/src/mysql-core/dialect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
type TableRelationalConfig,
type TablesRelationalConfig,
} from '~/relations.ts';
import { and, eq, Param, type Query, SQL, sql, type SQLChunk } from '~/sql/index.ts';
import { and, eq, Param, type QueryWithTypings, SQL, sql, type SQLChunk } from '~/sql/index.ts';
import { Subquery, SubqueryConfig } from '~/subquery.ts';
import { getTableName, Table } from '~/table.ts';
import { orderSelectedFields, type UpdateSet } from '~/utils.ts';
Expand Down Expand Up @@ -374,7 +374,7 @@ export class MySqlDialect {
return sql`insert${ignoreSql} into ${table} ${insertOrder} values ${valuesSql}${onConflictSql}`;
}

sqlToQuery(sql: SQL): Query {
sqlToQuery(sql: SQL): QueryWithTypings {
return sql.toQuery({
escapeName: this.escapeName,
escapeParam: this.escapeParam,
Expand Down
2 changes: 1 addition & 1 deletion drizzle-orm/src/mysql-core/query-builders/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class MySqlDelete<
return this.dialect.buildDeleteQuery(this.config);
}

toSQL(): { sql: Query['sql']; params: Query['params'] } {
toSQL(): Query {
const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
return rest;
}
Expand Down
2 changes: 1 addition & 1 deletion drizzle-orm/src/mysql-core/query-builders/insert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export class MySqlInsert<
return this.dialect.buildInsertQuery(this.config);
}

toSQL(): { sql: Query['sql']; params: Query['params'] } {
toSQL(): Query {
const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
return rest;
}
Expand Down
35 changes: 23 additions & 12 deletions drizzle-orm/src/mysql-core/query-builders/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import { entityKind } from '~/entity.ts';
import { QueryPromise } from '~/query-promise.ts';
import {
type BuildQueryResult,
type BuildRelationalQueryResult,
type DBQueryConfig,
mapRelationalRow,
type TableRelationalConfig,
type TablesRelationalConfig,
} from '~/relations.ts';
import { type SQL } from '~/sql/index.ts';
import { type Query, type QueryWithTypings, type SQL } from '~/sql/index.ts';
import { type KnownKeysOnly } from '~/utils.ts';
import { type MySqlDialect } from '../dialect.ts';
import {
Expand Down Expand Up @@ -96,6 +97,21 @@ export class MySqlRelationalQuery<
}

prepare() {
const { query, builtQuery } = this._toSQL();
return this.session.prepareQuery(
builtQuery,
undefined,
(rawRows) => {
const rows = rawRows.map((row) => mapRelationalRow(this.schema, this.tableConfig, row, query.selection));
if (this.queryMode === 'first') {
return rows[0] as TResult;
}
return rows as TResult;
},
) as PreparedQueryKind<TPreparedQueryHKT, PreparedQueryConfig & { execute: TResult }, true>;
}

private _toSQL(): { query: BuildRelationalQueryResult; builtQuery: QueryWithTypings } {
const query = this.mode === 'planetscale'
? this.dialect.buildRelationalQueryWithoutLateralSubqueries({
fullSchema: this.fullSchema,
Expand All @@ -117,17 +133,12 @@ export class MySqlRelationalQuery<
});

const builtQuery = this.dialect.sqlToQuery(query.sql as SQL);
return this.session.prepareQuery(
builtQuery,
undefined,
(rawRows) => {
const rows = rawRows.map((row) => mapRelationalRow(this.schema, this.tableConfig, row, query.selection));
if (this.queryMode === 'first') {
return rows[0] as TResult;
}
return rows as TResult;
},
) as PreparedQueryKind<TPreparedQueryHKT, PreparedQueryConfig & { execute: TResult }, true>;

return { builtQuery, query };
}

toSQL(): Query {
return this._toSQL().builtQuery;
}

override execute(): Promise<TResult> {
Expand Down
2 changes: 1 addition & 1 deletion drizzle-orm/src/mysql-core/query-builders/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ export abstract class MySqlSelectQueryBuilder<
return this.dialect.buildSelectQuery(this.config);
}

toSQL(): { sql: Query['sql']; params: Query['params'] } {
toSQL(): Query {
const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
return rest;
}
Expand Down
2 changes: 1 addition & 1 deletion drizzle-orm/src/mysql-core/query-builders/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class MySqlUpdate<
return this.dialect.buildUpdateQuery(this.config);
}

toSQL(): { sql: Query['sql']; params: Query['params'] } {
toSQL(): Query {
const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
return rest;
}
Expand Down
4 changes: 2 additions & 2 deletions drizzle-orm/src/pg-core/dialect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import {
type DriverValueEncoder,
eq,
Param,
type Query,
type QueryTypingsValue,
type QueryWithTypings,
SQL,
sql,
type SQLChunk,
Expand Down Expand Up @@ -415,7 +415,7 @@ export class PgDialect {
}
}

sqlToQuery(sql: SQL): Query {
sqlToQuery(sql: SQL): QueryWithTypings {
return sql.toQuery({
escapeName: this.escapeName,
escapeParam: this.escapeParam,
Expand Down
2 changes: 1 addition & 1 deletion drizzle-orm/src/pg-core/query-builders/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class PgDelete<
return this.dialect.buildDeleteQuery(this.config);
}

toSQL(): { sql: Query['sql']; params: Query['params'] } {
toSQL(): Query {
const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
return rest;
}
Expand Down
2 changes: 1 addition & 1 deletion drizzle-orm/src/pg-core/query-builders/insert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export class PgInsert<
return this.dialect.buildInsertQuery(this.config);
}

toSQL(): { sql: Query['sql']; params: Query['params'] } {
toSQL(): Query {
const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
return rest;
}
Expand Down
55 changes: 23 additions & 32 deletions drizzle-orm/src/pg-core/query-builders/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import { entityKind } from '~/entity.ts';
import { QueryPromise } from '~/query-promise.ts';
import {
type BuildQueryResult,
type BuildRelationalQueryResult,
type DBQueryConfig,
mapRelationalRow,
type TableRelationalConfig,
type TablesRelationalConfig,
} from '~/relations.ts';
import type { SQL } from '~/sql/index.ts';
import type { Query, QueryWithTypings, SQL } from '~/sql/index.ts';
import { tracer } from '~/tracing.ts';
import { type KnownKeysOnly } from '~/utils.ts';
import type { PgDialect } from '../dialect.ts';
Expand Down Expand Up @@ -81,38 +82,8 @@ export class PgRelationalQuery<TResult> extends QueryPromise<TResult> {

private _prepare(name?: string): PreparedQuery<PreparedQueryConfig & { execute: TResult }> {
return tracer.startActiveSpan('drizzle.prepareQuery', () => {
// const query = this.tableConfig.primaryKey.length > 0
// ? this.dialect.buildRelationalQueryWithPK({
// fullSchema: this.fullSchema,
// schema: this.schema,
// tableNamesMap: this.tableNamesMap,
// table: this.table,
// tableConfig: this.tableConfig,
// queryConfig: this.config,
// tableAlias: this.tableConfig.tsName,
// isRoot: true,
// })
// : this.dialect.buildRelationalQueryWithoutPK({
// fullSchema: this.fullSchema,
// schema: this.schema,
// tableNamesMap: this.tableNamesMap,
// table: this.table,
// tableConfig: this.tableConfig,
// queryConfig: this.config,
// tableAlias: this.tableConfig.tsName,
// });
const { query, builtQuery } = this._toSQL();

const query = this.dialect.buildRelationalQueryWithoutPK({
fullSchema: this.fullSchema,
schema: this.schema,
tableNamesMap: this.tableNamesMap,
table: this.table,
tableConfig: this.tableConfig,
queryConfig: this.config,
tableAlias: this.tableConfig.tsName,
});

const builtQuery = this.dialect.sqlToQuery(query.sql as SQL);
return this.session.prepareQuery<PreparedQueryConfig & { execute: TResult }>(
builtQuery,
undefined,
Expand All @@ -134,6 +105,26 @@ export class PgRelationalQuery<TResult> extends QueryPromise<TResult> {
return this._prepare(name);
}

private _toSQL(): { query: BuildRelationalQueryResult; builtQuery: QueryWithTypings } {
const query = this.dialect.buildRelationalQueryWithoutPK({
fullSchema: this.fullSchema,
schema: this.schema,
tableNamesMap: this.tableNamesMap,
table: this.table,
tableConfig: this.tableConfig,
queryConfig: this.config,
tableAlias: this.tableConfig.tsName,
});

const builtQuery = this.dialect.sqlToQuery(query.sql as SQL);

return { query, builtQuery };
}

toSQL(): Query {
return this._toSQL().builtQuery;
}

override execute(): Promise<TResult> {
return tracer.startActiveSpan('drizzle.operation', () => {
return this._prepare().execute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class PgRefreshMaterializedView<TQueryResult extends QueryResultHKT>
return this.dialect.buildRefreshMaterializedViewQuery(this.config);
}

toSQL(): { sql: Query['sql']; params: Query['params'] } {
toSQL(): Query {
const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
return rest;
}
Expand Down
2 changes: 1 addition & 1 deletion drizzle-orm/src/pg-core/query-builders/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ export abstract class PgSelectQueryBuilder<
return this.dialect.buildSelectQuery(this.config);
}

toSQL(): { sql: Query['sql']; params: Query['params'] } {
toSQL(): Query {
const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
return rest;
}
Expand Down
15 changes: 10 additions & 5 deletions drizzle-orm/src/sql/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ export type QueryTypingsValue = 'json' | 'decimal' | 'time' | 'timestamp' | 'uui
export interface Query {
sql: string;
params: unknown[];
}

export interface QueryWithTypings extends Query {
typings?: QueryTypingsValue[];
}

Expand All @@ -65,13 +68,15 @@ export function isSQLWrapper(value: unknown): value is SQLWrapper {
&& typeof (value as any).getSQL === 'function';
}

function mergeQueries(queries: Query[]): Query {
const result: Query = { sql: '', params: [] };
function mergeQueries(queries: QueryWithTypings[]): QueryWithTypings {
const result: QueryWithTypings = { sql: '', params: [] };
for (const query of queries) {
result.sql += query.sql;
result.params.push(...query.params);
if (query.typings?.length) {
result.typings = result.typings || [];
if (!result.typings) {
result.typings = [];
}
result.typings.push(...query.typings);
}
}
Expand Down Expand Up @@ -111,7 +116,7 @@ export class SQL<T = unknown> implements SQLWrapper {
return this;
}

toQuery(config: BuildQueryConfig): Query {
toQuery(config: BuildQueryConfig): QueryWithTypings {
return tracer.startActiveSpan('drizzle.buildSQL', (span) => {
const query = this.buildQueryFromSourceParams(this.queryChunks, config);
span?.setAttributes({
Expand All @@ -136,7 +141,7 @@ export class SQL<T = unknown> implements SQLWrapper {
paramStartIndex,
} = config;

return mergeQueries(chunks.map((chunk): Query => {
return mergeQueries(chunks.map((chunk): QueryWithTypings => {
if (is(chunk, StringChunk)) {
return { sql: chunk.value.join(''), params: [] };
}
Expand Down
6 changes: 6 additions & 0 deletions drizzle-orm/src/sqlite-core/columns/blob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ export interface BlobConfig<TMode extends BlobMode = BlobMode> {
mode: TMode;
}

/**
* It's recommended to use `text('...', { mode: 'json' })` instead of `blob` in JSON mode, because it supports JSON functions:
* >All JSON functions currently throw an error if any of their arguments are BLOBs because BLOBs are reserved for a future enhancement in which BLOBs will store the binary encoding for JSON.
*
* https://www.sqlite.org/json1.html
*/
export function blob<TName extends string, TMode extends BlobMode = BlobMode>(
name: TName,
config?: BlobConfig<TMode>,
Expand Down
Loading

0 comments on commit cfcdb82

Please sign in to comment.