-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(client): middleware args consistency (#15897)
Co-authored-by: Daniel Starns <danielstarns@hotmail.com>
- Loading branch information
Showing
10 changed files
with
310 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 37 additions & 16 deletions
53
packages/client/src/runtime/core/model/aggregates/count.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,48 @@ | ||
import type { Client } from '../../../getPrismaClient' | ||
import type { ModelAction } from '../applyModel' | ||
import type { UserArgs } from '../UserArgs' | ||
import { aggregate } from './aggregate' | ||
import { createUnpacker as createUnpackerAggregate, desugarUserArgs as desugarUserArgsAggregate } from './aggregate' | ||
|
||
/** | ||
* Executes the `.count` action on a model via {@link aggregate}. | ||
* @param client to provide dmmf information | ||
* @param userArgs the user input to desugar | ||
* @param modelAction a callback action that triggers request execution | ||
* Transforms the `userArgs` for the `.count` shorthand. It is an API sugar. It | ||
* reuses the logic from the `.aggregate` shorthand to add additional handling. | ||
* The goal here is to desugar it into something that is understood by the QE. | ||
* @param args to transform | ||
* @returns | ||
*/ | ||
export function count(client: Client, userArgs: UserArgs | undefined, modelAction: ModelAction) { | ||
const { select, ..._userArgs } = userArgs ?? {} // exclude select | ||
function desugarUserArgs(args: UserArgs = {}) { | ||
const { select, ..._args } = args // exclude select | ||
|
||
// count is an aggregate, we reuse that but hijack its unpacker | ||
if (typeof select === 'object') { | ||
// we transpose the original select field into the _count field | ||
return aggregate(client, { ..._userArgs, _count: select }, (p) => | ||
modelAction({ ...p, action: 'count', unpacker: (data) => p.unpacker?.(data)['_count'] }), | ||
) // for count selects, return the relevant part of the result | ||
return desugarUserArgsAggregate({ ..._args, _count: select }) | ||
} else { | ||
return desugarUserArgsAggregate({ ..._args, _count: { _all: true } }) | ||
} | ||
} | ||
|
||
/** | ||
* Creates an unpacker that adds sugar to the basic result of the QE. An | ||
* unpacker helps to transform a result before returning it to the user. | ||
* @param args the user input | ||
* @returns | ||
*/ | ||
export function createUnpacker(args: UserArgs = {}) { | ||
if (typeof args['select'] === 'object') { | ||
return (data: object) => createUnpackerAggregate(args)(data)['_count'] | ||
} else { | ||
return aggregate(client, { ..._userArgs, _count: { _all: true } }, (p) => | ||
modelAction({ ...p, action: 'count', unpacker: (data) => p.unpacker?.(data)['_count']['_all'] }), | ||
) // for simple counts, just return the result that is a number | ||
return (data: object) => createUnpackerAggregate(args)(data)['_count']['_all'] | ||
} | ||
} | ||
|
||
/** | ||
* Executes the `.count` action on a model via {@link aggregate}. | ||
* @param args the user input to desugar | ||
* @param modelAction a callback action that triggers request execution | ||
* @returns | ||
*/ | ||
export function count(args: UserArgs | undefined, modelAction: ModelAction) { | ||
return modelAction({ | ||
action: 'count', | ||
unpacker: createUnpacker(args), | ||
argsMapper: desugarUserArgs, | ||
})(args) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
packages/client/tests/functional/issues/15644-middleware-arg-types/_matrix.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { defineMatrix } from '../../_utils/defineMatrix' | ||
|
||
export default defineMatrix(() => [ | ||
[ | ||
{ | ||
provider: 'sqlite', | ||
}, | ||
], | ||
]) |
19 changes: 19 additions & 0 deletions
19
packages/client/tests/functional/issues/15644-middleware-arg-types/prisma/_schema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { idForProvider } from '../../../_utils/idForProvider' | ||
import testMatrix from '../_matrix' | ||
|
||
export default testMatrix.setupSchema(({ provider }) => { | ||
return /* Prisma */ ` | ||
generator client { | ||
provider = "prisma-client-js" | ||
} | ||
datasource db { | ||
provider = "${provider}" | ||
url = env("DATABASE_URI_${provider}") | ||
} | ||
model Resource { | ||
id ${idForProvider(provider)} | ||
} | ||
` | ||
}) |
Oops, something went wrong.