Created
October 13, 2015 10:22
-
-
Save mupkoo/5203dd5e06662e43867b to your computer and use it in GitHub Desktop.
Ember CLI Mirage JSONAPI helpers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// factories/country.js | |
import Mirage, { faker } from 'ember-cli-mirage'; | |
export default Mirage.Factory.extend({ | |
type: 'countries', | |
// Attributes | |
name: faker.address.country, | |
iso: () => faker.address.countryCode().toLowerCase(), | |
'updated-at': faker.date.recent, | |
// Relations | |
overview: (i) => i + 1, | |
institutions: (i) => [i + 1], | |
_relationshipsMeta: { | |
overview: { type: 'overviews', oneOrMany: 'one' }, | |
institutions: { type: 'institutions', oneOrMany: 'many' }, | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// scenarios/default.js | |
export default function (server) { | |
server.createList('country', 10).forEach((country) => { | |
let overview = server.create('overview', { country: country }); // It's working when you pass a model or just an id | |
let institutions = server.createList('institution', 10, { country: country }); | |
server.db.countries.update(country.id, { | |
overview: overview, | |
institutions: institutions | |
}); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
export default { | |
serializeRecord(record) { | |
return { | |
data: this._serialize(record) | |
}; | |
}, | |
serializeCollection(records) { | |
return { | |
data: records.map((record) => this._serialize(record)) | |
}; | |
}, | |
errorFor(attribute, message) { | |
return { | |
title: message, | |
detail: `${attribute} ${message}`, | |
source: { | |
pointer: `/data/attributes/${attribute}` | |
} | |
}; | |
}, | |
extractAttributes(request) { | |
return JSON.parse(request.requestBody).data.attributes; | |
}, | |
_serialize(record) { | |
let attributes = Ember.copy(record, true); | |
let relationships = {}; | |
delete attributes.id; | |
delete attributes.type; | |
delete attributes._relationshipsMeta; | |
if (record._relationshipsMeta) { | |
Object.keys(record._relationshipsMeta).forEach((relationName) => { | |
if (record[relationName]) { | |
delete attributes[relationName]; | |
relationships[relationName] = this._serializeRelation(record, relationName); | |
} | |
}); | |
} | |
let response = { | |
id: record.id, | |
type: record.type, | |
attributes: attributes | |
}; | |
if (Object.keys(relationships).length > 0) { | |
response.relationships = relationships; | |
} | |
return response; | |
}, | |
_serializeRelation(record, relationName) { | |
let oneOrMany = record._relationshipsMeta[relationName].oneOrMany; | |
let type = record._relationshipsMeta[relationName].type; | |
let idOrIds = record[relationName]; | |
if (oneOrMany === 'one') { | |
return this._serializeHasOne(relationName, type, idOrIds); | |
} else { | |
return this._serializeHasMany(relationName, type, idOrIds); | |
} | |
}, | |
_serializeHasOne(key, type, idOrModel) { | |
return { | |
data: { type: type, id: this._extractId(idOrModel) } | |
}; | |
}, | |
_serializeHasMany(key, type, idsOrModels) { | |
let data = idsOrModels.map((idOrModel) => { | |
return { type: type, id: this._extractId(idOrModel) }; | |
}); | |
return { data: data }; | |
}, | |
_extractId(idOrModel) { | |
if (idOrModel.id) { | |
return idOrModel.id; | |
} else { | |
return idOrModel; | |
} | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// factories/institution.js | |
import Mirage, { faker } from 'ember-cli-mirage'; | |
export default Mirage.Factory.extend({ | |
type: 'institutions', | |
// Attributes | |
uuid: 'super-uuid', | |
name: () => faker.company.companyName(), | |
// Relations | |
country: (i) => i + 1, | |
_relationshipsMeta: { | |
country: { type: 'countries', oneOrMany: 'one' } | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// factories/overview.js | |
import Mirage, { faker } from 'ember-cli-mirage'; | |
export default Mirage.Factory.extend({ | |
type: 'overviews', | |
// Attributes | |
'visits': faker.lorem.sentence, | |
'political': faker.lorem.sentence, | |
// Relations | |
country: (i) => i + 1, | |
_relationshipsMeta: { | |
country: { type: 'countries', oneOrMany: 'one' } | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment