Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: deep nested queries #2991

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions drizzle-orm/src/pg-core/dialect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import { ViewBaseConfig } from '~/view-common.ts';
import type { PgSession } from './session.ts';
import { PgViewBase } from './view-base.ts';
import type { PgMaterializedView } from './view.ts';
import { hash } from './utils/string.ts';

export class PgDialect {
static readonly [entityKind]: string = 'PgDialect';
Expand Down Expand Up @@ -1225,7 +1226,7 @@ export class PgDialect {
const normalizedRelation = normalizeRelation(schema, tableNamesMap, relation);
const relationTableName = getTableUniqueName(relation.referencedTable);
const relationTableTsName = tableNamesMap[relationTableName]!;
const relationTableAlias = `${tableAlias}_${selectedRelationTsKey}`;
const relationTableAlias = hash(`${tableAlias}_${selectedRelationTsKey}`);
const joinOn = and(
...normalizedRelation.fields.map((field, i) =>
eq(
Expand Down Expand Up @@ -1281,7 +1282,7 @@ export class PgDialect {
sql.join(
selection.map(({ field, tsKey, isJson }) =>
isJson
? sql`${sql.identifier(`${tableAlias}_${tsKey}`)}.${sql.identifier('data')}`
? sql`${sql.identifier(hash(`${tableAlias}_${tsKey}`))}.${sql.identifier('data')}`
: is(field, SQL.Aliased)
? field.sql
: field
Expand Down
1 change: 1 addition & 0 deletions drizzle-orm/src/pg-core/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './array.ts';
export * from './string.ts';
25 changes: 25 additions & 0 deletions drizzle-orm/src/pg-core/utils/string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as crypto from "crypto";

interface IHashOptions {
length?: number;
}

/**
* Returns a hashed input.
*
* @param input String to be hashed.
* @param options.length Optionally, shorten the output to desired length.
*/
export function hash(input: string, options: IHashOptions = {}): string {
const hashFunction = crypto.createHash("sha256");

hashFunction.update(input, "utf8");

const hashedInput = hashFunction.digest("hex");

if (options.length) {
return hashedInput.slice(0, options.length);
}

return hashedInput;
}