Skip to content

Commit

Permalink
Merge pull request #161 from drizzle-team/fix-circular
Browse files Browse the repository at this point in the history
Fix circular dependency issue for typings
  • Loading branch information
AndriiSherman authored Feb 3, 2023
2 parents a371248 + 126777c commit d2fded0
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 29 deletions.
3 changes: 3 additions & 0 deletions changelogs/drizzle-orm/0.17.6.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix circular dependency for query building on all pg and mysql drivers

Moved all aws data api typings specific logic to dialect from sql to prevent circular dependency issues
2 changes: 1 addition & 1 deletion drizzle-orm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "drizzle-orm",
"version": "0.17.5",
"version": "0.17.6",
"description": "Drizzle ORM package for SQL databases",
"scripts": {
"build": "tsc && resolve-tspaths && cp ../README.md package.json dist/",
Expand Down
26 changes: 23 additions & 3 deletions drizzle-orm/src/pg-core/dialect.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { AnyColumn, Column } from '~/column';
import { MigrationMeta } from '~/migrator';
import { SelectFieldsOrdered } from '~/operations';
import { AnyPgColumn, PgColumn } from '~/pg-core/columns';
import { AnyPgColumn, PgColumn, PgDate, PgJson, PgJsonb, PgNumeric, PgTime, PgTimestamp, PgUUID } from '~/pg-core/columns';
import { PgDatabase } from '~/pg-core/db';
import { PgDeleteConfig, PgInsertConfig, PgUpdateConfig, PgUpdateSet } from '~/pg-core/query-builders';
import { PgSelectConfig } from '~/pg-core/query-builders/select.types';
import { AnyPgTable, PgTable } from '~/pg-core/table';
import { Name, Query, SQL, sql, SQLResponse, SQLSourceParam } from '~/sql';
import { DriverValueEncoder, Name, Query, QueryTypingsValue, SQL, sql, SQLResponse, SQLSourceParam } from '~/sql';
import { Table } from '~/table';
import { PgSession } from './session';

Expand Down Expand Up @@ -248,10 +248,30 @@ export class PgDialect {
return sql`insert into ${table} ${insertOrder} values ${valuesSql}${onConflictSql}${returningSql}`;
}

prepareTyping(encoder: DriverValueEncoder<unknown, unknown>): QueryTypingsValue {
if (
encoder instanceof PgJsonb || encoder instanceof PgJson
) {
return 'json';
} else if (encoder instanceof PgNumeric) {
return 'decimal';
} else if (encoder instanceof PgTime) {
return 'time';
} else if (encoder instanceof PgTimestamp) {
return 'timestamp';
} else if (encoder instanceof PgDate) {
return 'date';
} else if (encoder instanceof PgUUID) {
return 'uuid';
} else {
return 'none';
}
}

sqlToQuery(sql: SQL): Query {
return sql.toQuery({
escapeName: this.escapeName,
escapeParam: this.escapeParam,
});
}, this.prepareTyping);
}
}
30 changes: 5 additions & 25 deletions drizzle-orm/src/sql/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { MySqlDate, MySqlDecimal, MySqlJson, MySqlTime, MySqlTimestamp } from '~/mysql-core';
import { PgDate, PgJson, PgJsonb, PgNumeric, PgTime, PgTimestamp, PgUUID } from '~/pg-core';
import { AnyColumn, Column } from '../column';
import { Table } from '../table';

Expand Down Expand Up @@ -40,33 +38,15 @@ export function isSQLWrapper(value: unknown): value is SQLWrapper {
&& typeof (value as any).getSQL === 'function';
}

function prepareTyping(encoder: DriverValueEncoder<unknown, unknown>): QueryTypingsValue {
if (
encoder instanceof PgJsonb || encoder instanceof PgJson
|| encoder instanceof MySqlJson
) {
return 'json';
} else if (encoder instanceof MySqlDecimal || encoder instanceof PgNumeric) {
return 'decimal';
} else if (encoder instanceof PgTime || encoder instanceof MySqlTime) {
return 'time';
} else if (encoder instanceof PgTimestamp || encoder instanceof MySqlTimestamp) {
return 'timestamp';
} else if (encoder instanceof PgDate || encoder instanceof MySqlDate) {
return 'date';
} else if (encoder instanceof PgUUID) {
return 'uuid';
} else {
return 'none';
}
}

export class SQL implements SQLWrapper {
declare protected $brand: 'SQL';

constructor(readonly queryChunks: Chunk[]) {}

toQuery({ escapeName, escapeParam }: BuildQueryConfig): Query {
toQuery(
{ escapeName, escapeParam }: BuildQueryConfig,
prepareTyping?: (encoder: DriverValueEncoder<unknown, unknown>) => QueryTypingsValue,
): Query {
const params: unknown[] = [];
const typings: QueryTypingsValue[] = [];

Expand All @@ -84,7 +64,7 @@ export class SQL implements SQLWrapper {
return escapeName(chunk.table[Table.Symbol.Name]) + '.' + escapeName(chunk.name);
} else if (chunk instanceof Param) {
params.push(chunk.encoder.mapToDriverValue(chunk.value));
typings.push(prepareTyping(chunk.encoder));
if (typeof prepareTyping !== 'undefined') typings.push(prepareTyping(chunk.encoder));
return escapeParam(params.length - 1, chunk.value);
} else {
const err = new Error('Unexpected chunk type!');
Expand Down

0 comments on commit d2fded0

Please sign in to comment.