feat: make req
partial and optional in DB / Local API operations
#9935
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What?
Previously, the
req
argument:In database operations (e.g
payload.db
) was required and you needed to pass the wholereq
with all the properties. This is confusing because in database operations we never use its properties outside ofreq.transactionID
andreq.t
, both of which should be optional as well.Now, you don't have to do that cast:
Becomes:
If you need to use transactions, you're not required to do the
as
cast as well now, as thereq
not only optional but also partial -Partial<PayloadRequest>
.initTransaction
,commitTransaction
,killTransaction
utilities are typed better now as well. They do not require to you pass all the properties ofreq
, but onlypayload
-MarkRequired<Partial<PayloadRequest>, 'payload'>
The same for the Local API. Internal operations (for example
packages/payload/src/collections/operations/find.ts
) still accept the wholereq
, but local ones (packages/payload/src/collections/operations/local/find.ts
) which are used throughpayload.
now acceptPartial<PayloadRequest>
, as they pass it through to internal operations withcreateLocalReq
.So now, this is also valid, while previously you had to do
as
cast forreq
.Marked as deprecated
PayloadRequest['transactionIDPromise']
to remove in the next major version. It was never used anywhere.Refactored
withSession
that returns an object togetSession
that returns justClientSession
. Better type safety for argumentsDeduplicated in all drizzle operations to
getTransaction(this, req)
utility:Added fallback for throwing unique validation errors in database operations when
req.t
is not available.In migration
up
anddown
functions ourreq
is not partial, while we used to passedreq
with only 2 properties -payload
andtransactionID
. This is misleading and you can't access for examplereq.t
.Now, to achieve "real" full
req
- we generate it withcreateLocalReq
in all migration functions.This all is backwards compatible. In all public API places where you expect the full
req
(like hooks) you still have it.Why?
Better DX, more expected types, less errors because of types casting.