Skip to content
This repository was archived by the owner on Mar 20, 2022. It is now read-only.

Commit

Permalink
changed signature of fallbackStrategy to be (id, schema), no point in…
Browse files Browse the repository at this point in the history
… passing entity when the fallback is only executed when entity is undefined, + updated tests and documentation
  • Loading branch information
bjartebore committed Feb 21, 2020
1 parent c35b5bb commit 03d0525
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 6 deletions.
53 changes: 53 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ const normalizedData = normalize(data, myArray);
- `value`: The input value of the entity.
- `parent`: The parent object of the input array.
- `key`: The key at which the input array appears on the parent object.
- `fallbackStrategy(key, schema)`: Strategy to use when denormalizing data structures with id references to missing entities.
- `key`: The key at which the input array appears on the parent object.
- `schema`: The schema of the missing entity

#### Instance Methods

Expand Down Expand Up @@ -253,6 +256,56 @@ normalize(data, [patronsSchema]);
}
```

#### `fallbackStrategy` Usage
```js
const users = [
{ id: '1', name: "Emily", requestState: 'SUCCEEDED' },
{ id: '2', name: "Douglas", requestState: 'SUCCEEDED' }
];
const books = [
{id: '1', name: "Book 1", author: 1 },
{id: '2', name: "Book 2", author: 2 },
{id: '3', name: "Book 3", author: 3 }
]

const authorSchema = new schema.Entity('authors');
const bookSchema = new schema.Entity('books', {
author: authorSchema
}, {
fallbackStrategy: (key, schema) => {
return {
[schema.idAttribute]: key,
name: 'Unknown',
requestState: 'NONE'
};
}
});

```


#### Output
```js
[
{
id: '1',
name: "Book 1",
author: { id: '1', name: "Emily", requestState: 'SUCCEEDED' }
},
{
id: '2',
name: "Book 2",
author: { id: '2', name: "Douglas", requestState: 'SUCCEEDED' },
},
{
id: '3',
name: "Book 3",
author: { id: '3', name: "Unknown", requestState: 'NONE' },
}
]

```

### `Object(definition)`

Define a plain object mapping that has values needing to be normalized into Entities. _Note: The same behavior can be defined with shorthand syntax: `{ ... }`_
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const unvisitEntity = (id, schema, unvisit, getEntity, cache) => {
let entity = getEntity(id, schema);

if (entity === undefined && schema instanceof EntitySchema) {
entity = schema.fallback(entity, id);
entity = schema.fallback(id, schema);
}

if (typeof entity !== 'object' || entity === null) {
Expand Down
6 changes: 3 additions & 3 deletions src/schemas/Entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default class EntitySchema {
return { ...entityA, ...entityB };
},
processStrategy = (input) => ({ ...input }),
fallbackStrategy = (input, key) => input
fallbackStrategy = (key, schema) => undefined
} = options;

this._key = key;
Expand Down Expand Up @@ -50,8 +50,8 @@ export default class EntitySchema {
return this._mergeStrategy(entityA, entityB);
}

fallback(input, id) {
return this._fallbackStrategy(input, id);
fallback(id, schema) {
return this._fallbackStrategy(id, schema);
}

normalize(input, parent, key, visit, addEntity, visitedEntities) {
Expand Down
6 changes: 4 additions & 2 deletions src/schemas/__tests__/Entity.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,9 @@ describe(`${schema.Entity.name} denormalization`, () => {
'users',
{},
{
fallbackStrategy: (entity, id) => ({
id: id,
idAttribute: 'userId',
fallbackStrategy: (id, schema) => ({
[schema.idAttribute]: id,
name: 'John Doe'
})
}
Expand All @@ -325,6 +326,7 @@ describe(`${schema.Entity.name} denormalization`, () => {

expect(denormalizedReport.publishedBy).toBe(denormalizedReport.draftedBy);
expect(denormalizedReport.publishedBy.name).toBe('John Doe');
expect(denormalizedReport.publishedBy.userId).toBe('456');
//
});
});

0 comments on commit 03d0525

Please sign in to comment.