Skip to content

Commit 93ba2b7

Browse files
author
Joel Alejandro Villarreal Bertoldi
authored
updated readme with 0.1.8 changes
1 parent 3d307a1 commit 93ba2b7

File tree

1 file changed

+111
-3
lines changed

1 file changed

+111
-3
lines changed

README.md

Lines changed: 111 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ Simply run `npm install --save feathers-hooks-jsonapify` and you're good to go!
1010

1111
## Usage
1212

13-
> **Important:** As of version 0.1.0, this hook is intended to use with Sequelize. Further versions will move the coupling code to hook configurations.
14-
1513
This hook is intended to use with `feathers-rest`, since it'll convert that provider's response to a JSON-API compliant document.
1614

1715
Require the hook:
@@ -46,9 +44,11 @@ app.hooks({
4644

4745
### Relationships
4846

47+
*Available since: `v0.1.4`*
48+
4949
`feathers-hooks-jsonapify` will automatically detect metadata for relationships in the model. It'll create an `included` top-level array in the document when the hook is called via `find`.
5050

51-
> Currently working and tested with `belongsTo` and `hasMany` associations.
51+
> Currently working and tested with `belongsTo` and `hasMany` associations. This feature works only with a Sequelize adapter.
5252
5353
#### Example document for a self-referencing model
5454

@@ -95,6 +95,8 @@ app.hooks({
9595

9696
### Pagination
9797

98+
*Available since: `v0.1.4`*
99+
98100
The hook will also detect if `hook.result.skip`, `hook.result.limit` and `hook.result.total` are available as part of the `feathers-rest` provider. If available, it'll create `first`, `prev`, `next` and `last` links accordingly.
99101

100102
The raw pagination data is moved to a `meta` object.
@@ -143,6 +145,112 @@ The raw pagination data is moved to a `meta` object.
143145
}
144146
```
145147

148+
## Plain Object Serialization (POS) :new:
149+
150+
*Available since: `v0.1.8`*
151+
152+
Common `Object` arrays can also be `jsonapified` for any custom service's `result`:
153+
154+
### Multiple objects
155+
156+
```js
157+
// Sample hook result, with multiple objects, from a `person` custom service.
158+
hook.result = [{
159+
firstName: 'Joel',
160+
lastName: 'Villarreal',
161+
isEnabled: true
162+
}, {
163+
firstName: 'Alejandro',
164+
lastName: 'Bertoldi',
165+
isEnabled: false
166+
}];
167+
```
168+
169+
_JSONAPIfied_ result:
170+
171+
```json
172+
{
173+
"data": [
174+
{
175+
"id": "2f1faeefc0edc081b012113e08cd9960773a70eb4d16626fade328adb9be4477",
176+
"type": "person",
177+
"attributes": {
178+
"first-name": "Joel",
179+
"last-name": "Villarreal",
180+
"isEnabled": true
181+
},
182+
"links": {
183+
"self": "/person/2f1faeefc0edc081b012113e08cd9960773a70eb4d16626fade328adb9be4477"
184+
}
185+
},
186+
{
187+
"id": "5ad0e862ce3db03640bb696d1ca77a0905ef4400070549622e577c4001f3e96d",
188+
"type": "person",
189+
"attributes": {
190+
"first-name": "Alejandro",
191+
"last-name": "Bertoldi",
192+
"isEnabled": false
193+
},
194+
"links": {
195+
"self": "/person/5ad0e862ce3db03640bb696d1ca77a0905ef4400070549622e577c4001f3e96d"
196+
}
197+
}
198+
]
199+
}
200+
```
201+
202+
### Single object
203+
204+
```js
205+
// Sample hook result, with a single object in an array, from a `person` custom service.
206+
hook.result = [{
207+
firstName: 'Joel',
208+
lastName: 'Villarreal',
209+
isEnabled: true
210+
}];
211+
212+
// same as:
213+
hook.result = {
214+
firstName: 'Joel',
215+
lastName: 'Villarreal',
216+
isEnabled: true
217+
};
218+
```
219+
220+
_JSONAPIfied_ result:
221+
222+
```json
223+
{
224+
"data": {
225+
"id": "2f1faeefc0edc081b012113e08cd9960773a70eb4d16626fade328adb9be4477",
226+
"type": "person",
227+
"attributes": {
228+
"first-name": "Joel",
229+
"last-name": "Villarreal",
230+
"isEnabled": true
231+
},
232+
"links": {
233+
"self": "/person/2f1faeefc0edc081b012113e08cd9960773a70eb4d16626fade328adb9be4477"
234+
}
235+
}
236+
}
237+
```
238+
239+
### Identifier and type mapping
240+
241+
The `jsonapify` hook receives an `options` object that accepts two settings for POS:
242+
243+
- `identifierKey`: the name of the property to convert into `id`
244+
- `typeKey`: the name of the property to convert into `type`
245+
246+
#### What happens if I don't use `identifierKey`?
247+
248+
The hook's got your back. Using `crypto.createHash`, it creates a unique SHA-256 digest using the contents of the object.
249+
250+
#### What happens if I don't use `typeKey`?
251+
252+
The hook will use the service's name (`hook.service.options.name`) as each model's type.
253+
146254
## TODOs
147255

148256
Check out the [issues](https://github.com/joelalejandro/feathers-hooks-jsonapify/issues).

0 commit comments

Comments
 (0)