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

Commit

Permalink
Added recursive dependencies support for denormalize method
Browse files Browse the repository at this point in the history
  • Loading branch information
maxsbelt committed Jan 13, 2017
1 parent e365e51 commit c1fa0ec
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 10 deletions.
15 changes: 15 additions & 0 deletions src/__tests__/__snapshots__/index.test.js.snap
Expand Up @@ -33,6 +33,21 @@ Object {
}
`;

exports[`denormalize denormalizes recursive dependencies 1`] = `
Object {
"title": "Weekly report",
"user": Object {
"reports": Array [
Object {
"title": "Weekly report",
"user": Object {},
},
],
"role": "manager",
},
}
`;

exports[`normalize can use fully custom entity classes 1`] = `
Object {
"entities": Object {
Expand Down
36 changes: 36 additions & 0 deletions src/__tests__/index.test.js
Expand Up @@ -206,4 +206,40 @@ describe('denormalize', () => {
});
expect(() => denormalize('123', article, entities)).not.toThrow();
});

it('denormalizes recursive dependencies', () => {
const user = new schema.Entity('users');
const report = new schema.Entity('reports');

user.define({
reports: [ report ]
});
report.define({
user: user
});

const entities = {
reports: {
1: {
title: 'Weekly report',
user: 1
},
2: {
title: 'Monthly report',
user: 2
}
},
users: {
1: {
role: 'manager',
reports: [ 1 ]
},
2: {
role: 'user',
reports: []
}
}
};
expect(denormalize('1', report, entities)).toMatchSnapshot();
});
});
1 change: 1 addition & 0 deletions src/index.js
Expand Up @@ -72,5 +72,6 @@ export const denormalize = (input, schema, entities) => {
return input;
}

entities = { ...entities, __cache: {} };
return unvisit(input, schema, entities);
};
12 changes: 3 additions & 9 deletions src/schemas/Entity.js
@@ -1,3 +1,5 @@
import { denormalize } from './Object';

export default class EntitySchema {
constructor(key, definition = {}, options = {}) {
if (!key || typeof key !== 'string') {
Expand Down Expand Up @@ -53,14 +55,6 @@ export default class EntitySchema {

denormalize(entityOrId, unvisit, entities) {
const entity = typeof entityOrId === 'object' ? entityOrId : entities[this.key][entityOrId];
const entityCopy = { ...entity };
Object.keys(this.schema).forEach((key) => {
if (entityCopy.hasOwnProperty(key)) {
const schema = this.schema[key];
entityCopy[key] = unvisit(entityCopy[key], schema, entities);
}
});

return entityCopy;
return denormalize(this.schema, entity, unvisit, entities);
}
}
16 changes: 15 additions & 1 deletion src/schemas/Object.js
Expand Up @@ -17,7 +17,21 @@ export const denormalize = (schema, input, unvisit, entities) => {
Object.keys(schema).forEach((key) => {
const localSchema = schema[key];
if (object[key]) {
object[key] = unvisit(object[key], localSchema, entities);
if (Array.isArray(object[key])) {
object[key] = unvisit(object[key], localSchema, entities);
} else {
const skey = localSchema.key;
if (!entities.__cache[skey]) {
entities.__cache[skey] = {};
}

if (!entities.__cache[skey][object[key]]) {
entities.__cache[skey][object[key]] = {};
entities.__cache[skey][object[key]] = unvisit(object[key], localSchema, entities);
}

object[key] = entities.__cache[skey][object[key]];
}
}
});
return object;
Expand Down

0 comments on commit c1fa0ec

Please sign in to comment.