Skip to content

Commit c35d331

Browse files
committed
make middleware docs better
1 parent 802f8c8 commit c35d331

File tree

1 file changed

+41
-5
lines changed

1 file changed

+41
-5
lines changed

packages/docs/src/pages/docs/middleware.mdx

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,51 @@ This is all quite simple with a call to [`use()`](/docs/reference/use):
1313

1414
```ts
1515
use(userTable, async (ctx) => {
16-
// Log the method called
17-
console.log(ctx.action);
18-
// Log parameters given to the method
19-
console.log(ctx.params);
16+
// Execute code before calling the implementation
17+
console.log(ctx.action); // Log the method called
18+
console.log(ctx.params); // Log parameters given to the method
2019

2120
// Call the native implementation
2221
const result = await ctx.next(...ctx.params);
22+
23+
// Execute code after the implementation
24+
console.log("Everything worked!");
25+
26+
// Don't forget to return the result ;)
2327
return result;
2428
})
2529
```
2630

27-
More info about how hooks work and what you can do with them can be found in [the reference](/docs/reference/use).
31+
More info about how hooks work can be found in [the reference](/docs/reference/use).
32+
33+
## Modifying parameters
34+
35+
Hooks allow you to modify parameters passed to BlinkDB functions.
36+
37+
As an example, you can make sure that [`count`](/docs/reference/count) will always [prefer performance to accuracy](/docs/reference/count#performance):
38+
39+
```ts
40+
use(userTable, async (ctx) => {
41+
if(isAction(ctx, "count")) {
42+
// Always pass `{ exact: false }` as an option to `count`
43+
return ctx.next(ctx.params[0], ctx.params[1], { exact: false });
44+
}
45+
return ctx.next(...ctx.params);
46+
})
47+
```
48+
49+
## Modifying return values
50+
51+
Just like you can change the objects you pass to `ctx.next()`, you can also modify the return value:
52+
53+
```ts
54+
use(userTable, async (ctx) => {
55+
if(isAction(ctx, "count")) {
56+
// `count(...)` will now always return 10 users more than we actually have
57+
const num = await ctx.next(...ctx.params);
58+
return num + 10;
59+
}
60+
return ctx.next(...ctx.params);
61+
})
62+
```
63+

0 commit comments

Comments
 (0)