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

Feat: optional where clause in relations #1451

Open
wants to merge 11 commits into
base: beta
Choose a base branch
from
Prev Previous commit
Next Next commit
[Mysql] initial implementation
  • Loading branch information
Angelelz committed Dec 27, 2023
commit 3c61f2a65b0ea3416d4f2756c190d237acb1e195
54 changes: 44 additions & 10 deletions drizzle-orm/src/mysql-core/dialect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -603,17 +603,33 @@ export class MySqlDialect {
)
),
);
let queryConfig = is(relation, One)
? (selectedRelationConfigValue === true
? { limit: 1 }
: { ...selectedRelationConfigValue, limit: 1 })
: selectedRelationConfigValue;
if (relation.getConfig()?.where) {
queryConfig = queryConfig === true
? { where: relation.getConfig()?.where }
: {
...queryConfig,
where: and(
sql`${
typeof queryConfig.where === 'function'
? queryConfig.where(aliasedColumns, getOperators())
: queryConfig.where
}`,
relation.getConfig()?.where,
),
};
}
const builtRelation = this.buildRelationalQuery({
fullSchema,
schema,
tableNamesMap,
table: fullSchema[relationTableTsName] as MySqlTable,
tableConfig: schema[relationTableTsName]!,
queryConfig: is(relation, One)
? (selectedRelationConfigValue === true
? { limit: 1 }
: { ...selectedRelationConfigValue, limit: 1 })
: selectedRelationConfigValue,
queryConfig,
tableAlias: relationTableAlias,
joinOn,
nestedQueryRelation: relation,
Expand Down Expand Up @@ -900,17 +916,35 @@ export class MySqlDialect {
)
),
);
let queryConfig = is(relation, One)
? (selectedRelationConfigValue === true
? { limit: 1 }
: { ...selectedRelationConfigValue, limit: 1 })
: selectedRelationConfigValue;
if (relation.getConfig()?.where) {
queryConfig = queryConfig === true
? { where: relation.getConfig()?.where }
: {
...queryConfig,
where: queryConfig.where
? and(
sql`${
typeof queryConfig.where === 'function'
? queryConfig.where(aliasedColumns, getOperators())
: queryConfig.where
}`,
relation.getConfig()?.where,
)
: relation.getConfig()?.where,
};
}
const builtRelation = this.buildRelationalQueryWithoutLateralSubqueries({
fullSchema,
schema,
tableNamesMap,
table: fullSchema[relationTableTsName] as MySqlTable,
tableConfig: schema[relationTableTsName]!,
queryConfig: is(relation, One)
? (selectedRelationConfigValue === true
? { limit: 1 }
: { ...selectedRelationConfigValue, limit: 1 })
: selectedRelationConfigValue,
queryConfig,
tableAlias: relationTableAlias,
joinOn,
nestedQueryRelation: relation,
Expand Down
10 changes: 10 additions & 0 deletions drizzle-orm/src/relations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ export abstract class Relation<TTableName extends string = string> {
}

abstract withFieldName(fieldName: string): Relation<TTableName>;

abstract getConfig(): RelationConfigBase | undefined;
}

export class Relations<
Expand Down Expand Up @@ -95,6 +97,10 @@ export class One<
relation.fieldName = fieldName;
return relation;
}

getConfig() {
return this.config;
}
}

export class Many<TTableName extends string> extends Relation<TTableName> {
Expand All @@ -119,6 +125,10 @@ export class Many<TTableName extends string> extends Relation<TTableName> {
relation.fieldName = fieldName;
return relation;
}

getConfig() {
return this.config;
}
}

export type TableRelationsKeysOnly<
Expand Down