Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions model/fetch.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const _ = require('lodash');
const MODELS = Symbol.for('Jsonmonger.models');

module.exports = fetch;

Expand Down Expand Up @@ -80,9 +81,27 @@ function get_related_fields({ object, related }) {
.map(item => get_related_fields({ object, related: item }))
.map(item => item[0])
.filter(item => item)
)
);
} else if (related) {
related_fields = _.flattenDeep(related_fields.concat(Object.keys(related)
.map(prop => {
if (related[prop] instanceof Object) {
return Object.keys(related[prop]).map(key => {
const Model = global[MODELS][key];
if (!Model) {
return null;
}

const dummy = new Model({});
return get_related_fields({ object: dummy, related: dummy.__config.related })
.map(item => `${prop}.${item}`);
});
} else {
return null;
}
}).filter(item => item)
));
}
// @TODO: Support non-iterable objects (for nested relationships?)
break;

case 'boolean':
Expand Down
7 changes: 6 additions & 1 deletion model/toObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@ function populate_values({ object, result = {} }) {
Object.keys(maps).forEach(key => {
const value = object[key];
result[key] = get_value(value);
return result;
});

// If object is a Model, include type and id.
if (object.__maps) {
result.id = object.id;
result.type = object.type;
}

return result;
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"devDependencies": {
"chai": "^4.1.2",
"eslint": "^4.19.1",
"jsonapilite": "^0.1.0",
"mocha": "^5.1.1",
"pluralize": "^7.0.0",
"pre-push": "^0.1.1",
Expand Down
6 changes: 2 additions & 4 deletions test/fixtures/models/Person.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const Model = require('../../../').Model;

module.exports = ({ axios } = {}) => new Model({
module.exports = config => new Model({
type: 'person',
endpoint: '/people',
fullName: 'attributes.name',
Expand Down Expand Up @@ -28,6 +28,4 @@ module.exports = ({ axios } = {}) => new Model({
bio: 'attributes.biography',
alias: 'attributes.path.alias',
roles: 'relationships.roles',
}, {
axios,
});
}, config);
2 changes: 1 addition & 1 deletion test/model/destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const sinon = require('sinon');
chai.use(require('sinon-chai'));
const expect = chai.expect;
const Model = require('../../model');
const api = require('../fixtures/api');
const api = require('jsonapilite')(`${__dirname}/../fixtures/data`);
require('../fixtures/config')();

describe('destroy() method', () => {
Expand Down
19 changes: 18 additions & 1 deletion test/model/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const chai = require('chai');
const expect = chai.expect;
const sinon = require('sinon');
chai.use(require('sinon-chai'));
const api = require('../fixtures/api');
const api = require('jsonapilite')(`${__dirname}/../fixtures/data`);
require('../fixtures/config')();

describe('fetch() method', () => {
Expand All @@ -23,6 +23,12 @@ describe('fetch() method', () => {
base_url = global[Symbol.for('Jsonmonger.config')].base_url;

Post = require('../fixtures/models/Post')({ axios });
// Register models for nested relationship tests.
require('../fixtures/models/Person')({
axios,
related: 'roles',
});
require('../fixtures/models/Role')({ axios });
});

afterEach(() => {
Expand Down Expand Up @@ -86,6 +92,17 @@ describe('fetch() method', () => {
});
});

it('should request to include nested relationships', () => {
return new Post({ id }).fetch({ related: { author: { person: 'roles' } } })
.then(() => {
expect(axios).to.be.calledOnce;
expect(axios).to.be.calledWith({
method: 'get',
url: 'https://some.contrived.url/posts/1?include=author.roles',
});
});
});

it('should request to include all relationships', () => {
return new Post({ id }).fetch({ related: true }).then(() => {
expect(axios).to.be.calledOnce;
Expand Down
28 changes: 11 additions & 17 deletions test/model/relationships.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ const chai = require('chai');
const expect = chai.expect;
const sinon = require('sinon');
chai.use(require('sinon-chai'));
const api = require('../fixtures/api');
const api = require('jsonapilite')(`${__dirname}/../fixtures/data`);
require('../fixtures/config')();

describe('relationships', () => {
let axios, Image, Paragraph, Person, Post, post, raw_json; //, Role;
let axios, Image, Paragraph, Person, Post, post, raw_json, Role;
before(() => {
axios = sinon.spy(request => {
return api(request).then(result => {
Expand All @@ -21,7 +21,7 @@ describe('relationships', () => {
Paragraph = require('../fixtures/models/Paragraph')({ axios });
Person = require('../fixtures/models/Person')({ axios });
Post = require('../fixtures/models/Post')({ axios });
// Role = require('../fixtures/models/Role')({ axios });
Role = require('../fixtures/models/Role')({ axios });

return api({ url: '/posts/1?include=author,body' }).then(data => {
raw_json = JSON.parse(data);
Expand All @@ -37,12 +37,10 @@ describe('relationships', () => {
it('should load relationships as models', () => {
expect(post.author).to.be.instanceOf(Person);

// Nested relationships can be checked when the new test api and .fetch()
// support them.
// expect(post.author.roles).to.be.instanceOf(Array);
// post.author.roles.forEach(role => {
// expect(role).to.be.instanceOf(Role);
// });
expect(post.author.roles).to.be.instanceOf(Array);
post.author.roles.forEach(role => {
expect(role).to.be.instanceOf(Role);
});

expect(post.body).to.be.instanceOf(Array);
post.body.forEach(block => {
Expand All @@ -51,9 +49,7 @@ describe('relationships', () => {
expectedModel = Paragraph;
} else if (block.type === 'image') {
expectedModel = Image;
// Nested relationships can be checked when the new test api and
// .fetch() support them.
// expect(block.credit).to.be.instanceOf(Person);
expect(block.credit).to.be.instanceOf(Person);
} else if (block.type === 'blockquote') {
// We’re not defining a dedicated Blockquote model, so we expect it
// to return the raw data.
Expand All @@ -66,11 +62,9 @@ describe('relationships', () => {

it('should store a reference to the related record’s immediate parent in the tree', () => {
expect(post.author.__parent).to.deep.equal(post);
// Nested relationships can be checked when the new test api and .fetch()
// support them.
// post.author.roles.forEach(role => {
// expect(role.__parent).to.deep.equal(post.author);
// });
post.author.roles.forEach(role => {
expect(role.__parent).to.deep.equal(post.author);
});
});

it('should load raw related data when a model is not available', () => {
Expand Down
18 changes: 16 additions & 2 deletions test/model/toObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const chai = require('chai');
const expect = chai.expect;
const sinon = require('sinon');
chai.use(require('sinon-chai'));
const api = require('../fixtures/api');
const api = require('jsonapilite')(`${__dirname}/../fixtures/data`);
require('../fixtures/config')();

/* eslint-disable no-unused-vars */
Expand Down Expand Up @@ -38,9 +38,13 @@ describe('to_object() method', () => {
it('should return an object', () => {
expect(post.toObject()).to.be.instanceOf(Object);
expect(post.toObject()).to.deep.equal({
id: '1',
type: 'post',
title: 'Your run of the mill post.',
subtitle: 'Or: That time I couldn’t decide between two titles.',
author: {
id: '201',
type: 'person',
fullName: 'Testy McTestface',
firstName: 'Testy',
lastName: 'McTestface',
Expand All @@ -50,17 +54,25 @@ describe('to_object() method', () => {
{
id: '401',
type: 'role',
name: null,
url: null,
},
],
},
body: [
{
id: '101',
type: 'paragraph',
text: 'Et id animi optio voluptatem sunt voluptas dolorem. Et neque quasi aliquid quia soluta enim quia deserunt. Eum fugit est non accusamus ut nisi recusandae veniam. Quia vero excepturi minima. Et reiciendis voluptas error vel rerum omnis ipsum quia.',
},
{
id: '102',
type: 'image',
url: '/path/to/image.jpg',
alt: 'You do provide ALT values, right?',
credit: {
id: '202',
type: 'person',
fullName: null,
firstName: null,
lastName: null,
Expand All @@ -80,11 +92,13 @@ describe('to_object() method', () => {
},
},
{
id: '103',
type: 'paragraph',
text: 'Quia sed repellat id cum. Aperiam reprehenderit amet minima ut dolorem non nostrum placeat. Culpa esse id dolorum ducimus. Est quae nemo et rerum sapiente nam inventore.',
},
{
type: 'blockquote',
id: '104',
type: 'blockquote',
attributes: {
text: 'It matters not how strait the gate, how charged with punishments the scroll, I am the master of my fate, I am the captain of my soul.',
source: {
Expand Down