Skip to content

Commit c873f49

Browse files
committed
Add documentation for serialization customization
1 parent 79f344d commit c873f49

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

README.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,124 @@ be output as attributes you can specify a whitelist of attributes for each type.
220220
#### note
221221
The attributes arrays are keyed by type not by model name. Type is the term used by json api to describe the resource type in question and while not required by json api it is usually plural. In `loopback-component-jsonapi` it is whatever the models `plural` is set to in `model.json`. So in our example above we defined: `"posts": ["title", "content"]` as the resource type for the `post` model is `posts`
222222

223+
## Custom Serialization
224+
For occasions where you need greater control over the serialization process, you can implement a custom serialization function for each model as needed. This function will be used instead of the regular serialization process.
225+
226+
#### example
227+
```js
228+
module.exports = function (MyModel) {
229+
MyModel.jsonApiSerialize = function (options, callback) {
230+
// either return an error
231+
var err = new Error('Unable to serialize record');
232+
err.status = 500;
233+
cb(err)
234+
235+
// or return serialized records
236+
if (Array.isArray(options.records)) {
237+
// serialize an array of records
238+
} else {
239+
// serialize a single record
240+
}
241+
cb(null, options);
242+
}
243+
}
244+
```
245+
246+
##### function parameters
247+
- `options` All config options set for the serialization process
248+
- `options.type`
249+
- `options.model`
250+
- `options.method`
251+
- `options.primaryKeyField`
252+
- `options.requestedIncludes`
253+
- `options.host`
254+
- `options.topLevelLinks`
255+
- `options.dataLinks`
256+
- `options.relationships`
257+
- `options.results`
258+
- `options.restApiRoot`
259+
- `options.enable`
260+
- `options.handleErrors`
261+
- `options.exclude`
262+
- `options.hideIrrelevantMethods`
263+
- `options.attributes`
264+
- `callback` Callback to call with error or serialized records
265+
266+
## Serialization Hooks
267+
For occasions when you don't want to fully implement serialization for a model manually but
268+
you need to manipulate the serialization process, you can use the serialization
269+
hooks `beforeJsonApiSerialization` and `afterJsonApiSerialization`.
270+
271+
### beforeJsonApiSerialization
272+
273+
#### example
274+
```js
275+
module.exports = function (MyModel) {
276+
MyModel.beforeJsonApiSerialize = function (options, callback) {
277+
// either return an error
278+
var err = new Error('Unable to serialize record');
279+
err.status = 500;
280+
cb(err)
281+
282+
// or return modified records
283+
if (Array.isArray(options.records)) {
284+
// modify an array of records
285+
} else {
286+
// modify a single record
287+
}
288+
// returned options.records will be serialized by either the default serialization process
289+
// or by a custom serialize function (described above) if one is present on the model.
290+
cb(null, options);
291+
}
292+
}
293+
```
294+
295+
##### function parameters
296+
- `options` All config options set for the serialization process
297+
- `callback` Callback to call with error or modified records
298+
299+
#### example use case
300+
Because the `beforeJsonApiSerialize` method is passed all the options that will
301+
be used during serialization, it is possible to tweak options to affect the
302+
serialization process. One example of this is modifiying the `type` option to
303+
change the resource type that will be output.
304+
305+
```js
306+
module.exports = function (MyModel) {
307+
MyModel.beforeJsonApiSerialize = function (options, callback) {
308+
options.type = 'mycustommodels';
309+
cb(null, options);
310+
}
311+
}
312+
```
313+
314+
### afterJsonApiSerialization
315+
316+
#### example
317+
```js
318+
module.exports = function (MyModel) {
319+
MyModel.afterJsonApiSerialize = function (options, callback) {
320+
// either return an error
321+
var err = new Error('Unable to modify serialized record');
322+
err.status = 500;
323+
cb(err)
324+
325+
// or return modified records
326+
if (Array.isArray(options.records)) {
327+
// modify an array of serialized records
328+
} else {
329+
// modify a single serialized record
330+
}
331+
// returned options.records will be output through the api.
332+
cb(null, options);
333+
}
334+
}
335+
```
336+
337+
##### function parameters
338+
- `options` All config options set for the serialization process
339+
- `callback` Callback to call with modified serialized records
340+
223341
## Debugging
224342
You can enable debug logging by setting an environment variable:
225343
`DEBUG=loopback-component-jsonapi`

lib/serialize.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ module.exports = function (app, defaults) {
8484
}
8585
options = {
8686
model: model,
87+
method: ctx.method.name,
8788
primaryKeyField: primaryKeyField,
8889
requestedIncludes: requestedIncludes,
8990
host: utils.hostFromContext(ctx),

0 commit comments

Comments
 (0)