Skip to content

Commit

Permalink
feat: support changing nested params action
Browse files Browse the repository at this point in the history
Currently modifications to the params of nested actions has been limited
to the args field in order to prevent breaking params. The reason
changing the action in nested params can cause unexpected behaviour is
that the new action may already exist, causing the old args to be
overwritten by the new args. Changing two different actions to be the
same action would also cause args to be lost.

When combining the updated params from nested middleware with the
original params there are a few scenarios we need to handle:
- if the action does not already exist set the args
- if the action exists but and is an operation array push the args
- if the action exists but is not an operation array turn it into one
  and make the previous args the first operation and the new args the
  second
- if the action is createMany operation arrays are not supported and so
  the args.data arrays must be combined instead.

For nested reads the above problems go away since a select cannot be
found at the same level as an include, instead of merging we can set and
delete the original action.

Supporting changing actions also introduces a new problem: Since the
passed middleware function for each action at a particular level is
processed in parallel the order the actions are defined on the params
object matters. If the middleware processes action A before action B
then if action B is changed to be action A the new operations added
will not be processed by the middleware.

Since it is only possible to know if an action has been changed when the
params have been passed to next the execution strategy of nested
middleware needs to change. Update the middleware execution to wait for
params to be passed to next and then execute middleware with the updated
params again if the action has changed. This recursive execution ensures
every operation is processed regardless of the order of nested actions.
  • Loading branch information
olivierwilkinson committed Mar 27, 2023
1 parent 8a3dcd6 commit aabf8c7
Show file tree
Hide file tree
Showing 16 changed files with 2,522 additions and 423 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"author": "Olivier Wilkinson",
"license": "Apache-2.0",
"dependencies": {
"@open-draft/deferred-promise": "^2.1.0",
"lodash": "^4.17.21"
},
"peerDependencies": {
Expand Down
13 changes: 7 additions & 6 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ datasource db {
}

model User {
id Int @id @default(autoincrement())
email String @unique
name String?
posts Post[]
profile Profile?
id Int @id @default(autoincrement())
email String @unique
name String?
posts Post[]
profile Profile?
comments Comment[]
}

Expand All @@ -23,6 +23,7 @@ model Post {
published Boolean @default(false)
title String
content String?
deleted Boolean @default(false)
author User @relation(fields: [authorId], references: [id])
authorId Int
comments Comment[]
Expand All @@ -35,7 +36,7 @@ model Comment {
content String
author User @relation(fields: [authorId], references: [id])
authorId Int
post Post? @relation(fields: [postId], references: [id])
post Post? @relation(fields: [postId], references: [id])
postId Int?
repliedTo Comment? @relation("replies", fields: [repliedToId], references: [id])
repliedToId Int?
Expand Down
Loading

0 comments on commit aabf8c7

Please sign in to comment.