You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+92Lines changed: 92 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -26,6 +26,98 @@ You can also override the default configuration by passing the [`serializerOpts`
26
26
This module is already used as default by Fastify.
27
27
If you need to provide to your server instance a different version, refer to [the official doc](https://www.fastify.io/docs/latest/Reference/Server/#schemacontroller).
28
28
29
+
### fast-json-stringify Standalone
30
+
31
+
`[email protected]` introduces the [standalone feature](https://github.com/fastify/fast-json-stringify#standalone) that let you to pre-compile your schemas and use them in your application for a faster startup.
32
+
33
+
To use this feature, you must be aware of the following:
34
+
35
+
1. You must generate and save the application's compiled schemas.
36
+
2. Read the compiled schemas from the file and provide them back to your Fastify application.
37
+
38
+
39
+
#### Generate and save the compiled schemas
40
+
41
+
Fastify helps you to generate the serialization schemas functions and it is your choice to save them where you want.
42
+
To accomplish this, you must use a new compiler: `@fastify/fast-json-stringify-compiler/standalone`.
43
+
44
+
You must provide 2 parameters to this compiler:
45
+
46
+
-`readMode: false`: a boolean to indicate that you want generate the schemas functions string.
47
+
-`storeFunction`" a sync function that must store the source code of the schemas functions. You may provide an async function too, but you must manage errors.
48
+
49
+
When `readMode: false`, **the compiler is meant to be used in development ONLY**.
// at this stage all your schemas are compiled and stored in the file system
78
+
// now it is important to turn off the readMode
79
+
})
80
+
```
81
+
82
+
#### Read the compiled schemas functions
83
+
84
+
At this stage, you should have a file for every route's schema.
85
+
To use them, you must use the `@fastify/fast-json-stringify-compiler/standalone` with the parameters:
86
+
87
+
-`readMode: true`: a boolean to indicate that you want read and use the schemas functions string.
88
+
-`restoreFunction`" a sync function that must return a function to serialize the route's payload.
89
+
90
+
Important keep away before you continue reading the documentation:
91
+
92
+
- when you use the `readMode: true`, the application schemas are not compiled (they are ignored). So, if you change your schemas, you must recompile them!
93
+
- as you can see, you must relate the route's schema to the file name using the `routeOpts` object. You may use the `routeOpts.schema.$id` field to do so, it is up to you to define a unique schema identifier.
// routeOpts is like: { schema, method, url, httpStatus }
102
+
constfileName=generateFileName(routeOpts)
103
+
returnrequire(path.join(__dirname, fileName))
104
+
}
105
+
})
106
+
107
+
constapp=fastify({
108
+
jsonShorthand:false,
109
+
schemaController: {
110
+
compilersFactory: {
111
+
buildSerializer: factory
112
+
}
113
+
}
114
+
})
115
+
116
+
// ... add all your routes with schemas as before...
117
+
118
+
app.listen({ port:3000 })
119
+
```
120
+
29
121
### How it works
30
122
31
123
This module provide a factory function to produce [Serializer Compilers](https://www.fastify.io/docs/latest/Reference/Server/#serializercompiler) functions.
0 commit comments