Skip to content

Commit

Permalink
fix(polymorphism): support orderBy with base fields (#1086)
Browse files Browse the repository at this point in the history
  • Loading branch information
ymc9 authored Mar 6, 2024
1 parent 6e7993a commit 2e81a08
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 12 deletions.
34 changes: 22 additions & 12 deletions packages/runtime/src/enhancements/delegate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
this.injectWhereHierarchy(model, args?.where);
this.injectSelectIncludeHierarchy(model, args);

if (args.orderBy) {
// `orderBy` may contain fields from base types
args.orderBy = this.buildWhereHierarchy(this.model, args.orderBy);
}

if (this.options.logPrismaQuery) {
this.logger.info(`[delegate] \`${method}\` ${this.getModelName(model)}: ${formatObject(args)}`);
}
Expand Down Expand Up @@ -126,19 +131,19 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
});
}

private buildWhereHierarchy(where: any) {
private buildWhereHierarchy(model: string, where: any) {
if (!where) {
return undefined;
}

where = deepcopy(where);
Object.entries(where).forEach(([field, value]) => {
const fieldInfo = resolveField(this.options.modelMeta, this.model, field);
const fieldInfo = resolveField(this.options.modelMeta, model, field);
if (!fieldInfo?.inheritedFrom) {
return;
}

let base = this.getBaseModel(this.model);
let base = this.getBaseModel(model);
let target = where;

while (base) {
Expand Down Expand Up @@ -173,12 +178,17 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {

for (const kind of ['select', 'include'] as const) {
if (args[kind] && typeof args[kind] === 'object') {
for (const [field, value] of Object.entries(args[kind])) {
if (value !== undefined) {
for (const [field, value] of Object.entries<any>(args[kind])) {
const fieldInfo = resolveField(this.options.modelMeta, model, field);
if (fieldInfo && value !== undefined) {
if (value?.orderBy) {
// `orderBy` may contain fields from base types
value.orderBy = this.buildWhereHierarchy(fieldInfo.type, value.orderBy);
}

if (this.injectBaseFieldSelect(model, field, value, args, kind)) {
delete args[kind][field];
} else {
const fieldInfo = resolveField(this.options.modelMeta, model, field);
if (fieldInfo && this.isDelegateOrDescendantOfDelegate(fieldInfo.type)) {
let nextValue = value;
if (nextValue === true) {
Expand Down Expand Up @@ -847,15 +857,15 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
args = deepcopy(args);

if (args.cursor) {
args.cursor = this.buildWhereHierarchy(args.cursor);
args.cursor = this.buildWhereHierarchy(this.model, args.cursor);
}

if (args.orderBy) {
args.orderBy = this.buildWhereHierarchy(args.orderBy);
args.orderBy = this.buildWhereHierarchy(this.model, args.orderBy);
}

if (args.where) {
args.where = this.buildWhereHierarchy(args.where);
args.where = this.buildWhereHierarchy(this.model, args.where);
}

if (this.options.logPrismaQuery) {
Expand All @@ -875,11 +885,11 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
args = deepcopy(args);

if (args?.cursor) {
args.cursor = this.buildWhereHierarchy(args.cursor);
args.cursor = this.buildWhereHierarchy(this.model, args.cursor);
}

if (args?.where) {
args.where = this.buildWhereHierarchy(args.where);
args.where = this.buildWhereHierarchy(this.model, args.where);
}

if (this.options.logPrismaQuery) {
Expand Down Expand Up @@ -915,7 +925,7 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
args = deepcopy(args);

if (args.where) {
args.where = this.buildWhereHierarchy(args.where);
args.where = this.buildWhereHierarchy(this.model, args.where);
}

if (this.options.logPrismaQuery) {
Expand Down
1 change: 1 addition & 0 deletions tests/integration/tests/cli/plugins.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ describe('CLI Plugins Tests', () => {
strict: true,
lib: ['esnext', 'dom'],
esModuleInterop: true,
skipLibCheck: true,
},
})
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,41 @@ describe('Polymorphism Test', () => {
expect(imgAsset.owner).toMatchObject(user);
});

it('order by base fields', async () => {
const { db, user } = await setup();

await expect(
db.video.findMany({
orderBy: { viewCount: 'desc' },
})
).resolves.toHaveLength(1);

await expect(
db.ratedVideo.findMany({
orderBy: { duration: 'asc' },
})
).resolves.toHaveLength(1);

await expect(
db.user.findMany({
orderBy: { assets: { _count: 'desc' } },
})
).resolves.toHaveLength(1);

await expect(
db.user.findUnique({
where: { id: user.id },
include: {
ratedVideos: {
orderBy: {
viewCount: 'desc',
},
},
},
})
).toResolveTruthy();
});

it('update simple', async () => {
const { db, videoWithOwner: video } = await setup();

Expand Down

0 comments on commit 2e81a08

Please sign in to comment.