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

Minor Docs update for middlewares #45

Merged
merged 1 commit into from
Jun 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions Docs/Controller-Middlewares.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@
#### How to add custom business logic to Resource Controller


#### Override ResourceUpdateModel method
#### Override ResourceUpdateModel methods

Default implementation of that method is:
It can be as simple as:

```swift
func update(_ model: Model, req: Request, database: Database) -> EventLoopFuture<Model> {
return database.eventLoop.tryFuture { try update(model) }
database.eventLoop.tryFuture { try update(model) }
}
```
or its swift-concurrency compatible version:

```swift
func update(_ model: Model, req: Request, database: Database) async throws -> Model {
try await database.eventLoop.tryFuture { try update(model) }.get()
}
```

It can be made complex, but with the following restrictions:
It can be made more complex, but with the following restrictions:
- **All database requests should be performed with provided database instance parameter**
- **Database instance parameter should be used for obtaining event loop**

Expand All @@ -26,11 +32,19 @@ Default implementation of that method is:

```swift
func patch(_ model: Model, req: Request, database: Database) -> EventLoopFuture<Model> {
return database.eventLoop.tryFuture { try patch(model) }
database.eventLoop.tryFuture { try patch(model) }
}

```

or its swift-concurrency compatible version:

```swift
func patch(_ model: Model, req: Request, database: Database) async throws -> Model {
try await database.eventLoop.tryFuture { try patch(model) }.get()
}
```

It can be made as complicated as you wish, but with the following restrictions:
- **All database requests should be performed with provided database instance**
- **Database instance parameter should be used for obtaining event loop**
Expand Down