diff --git a/src/crud.js b/src/crud.js index 64e44b7..3991b1c 100644 --- a/src/crud.js +++ b/src/crud.js @@ -143,7 +143,7 @@ export const list = ({ server, model, prefix = '/', config }) => { where, include, }); - reply(list); + reply(list.map((item) => item.toJSON())); }, config, @@ -168,7 +168,7 @@ export const get = ({ server, model, prefix = '/', config }) => { if (!instance) return void reply(notFound(`${id} not found.`)); - reply(instance); + reply(instance.toJSON()); }, config: _.defaultsDeep(config, { validate: { @@ -196,7 +196,7 @@ export const scope = ({ server, model, prefix = '/', config }) => { const list = await model.scope(request.params.scope).findAll({ include, where }); - reply(list); + reply(list.map((item) => item.toJSON())); }, config: _.defaultsDeep(config, { validate: { @@ -217,7 +217,7 @@ export const create = ({ server, model, prefix = '/', config }) => { async handler(request, reply) { const instance = await model.create(request.payload); - reply(instance); + reply(instance.toJSON()); }, config, @@ -238,7 +238,8 @@ export const destroy = ({ server, model, prefix = '/', config }) => { await Promise.all(list.map(instance => instance.destroy())); - reply(list.length === 1 ? list[0] : list); + const listAsJSON = list.map((item) => item.toJSON()); + reply(listAsJSON.length === 1 ? listAsJSON[0] : listAsJSON); }, config, @@ -258,7 +259,8 @@ export const destroyAll = ({ server, model, prefix = '/', config }) => { await Promise.all(list.map(instance => instance.destroy())); - reply(list.length === 1 ? list[0] : list); + const listAsJSON = list.map((item) => item.toJSON()); + reply(listAsJSON.length === 1 ? listAsJSON[0] : listAsJSON); }, config, @@ -283,7 +285,8 @@ export const destroyScope = ({ server, model, prefix = '/', config }) => { await Promise.all(list.map(instance => instance.destroy())); - reply(list); + const listAsJSON = list.map((item) => item.toJSON()); + reply(listAsJSON.length === 1 ? listAsJSON[0] : listAsJSON); }, config: _.defaultsDeep(config, { validate: { @@ -309,7 +312,7 @@ export const update = ({ server, model, prefix = '/', config }) => { await instance.update(request.payload); - reply(instance); + reply(instance.toJSON()); }, config: _.defaultsDeep(config, { diff --git a/src/crud.test.js b/src/crud.test.js index 26d002d..53c16c5 100644 --- a/src/crud.test.js +++ b/src/crud.test.js @@ -1,6 +1,7 @@ import test from 'ava'; import { list } from './crud.js'; import { stub } from 'sinon'; +import uniqueId from 'lodash/uniqueId.js'; import 'sinon-bluebird'; const METHODS = { @@ -13,12 +14,23 @@ test.beforeEach('setup server', (t) => { }; }); -test.beforeEach('setup model', (t) => { - t.context.model = { +const makeModel = () => { + const id = uniqueId(); + return { findAll: stub(), _plural: 'models', _singular: 'model', + toJSON: () => ({ id }), + id, }; +}; + +test.beforeEach('setup model', (t) => { + t.context.model = makeModel(); +}); + +test.beforeEach('setup models', (t) => { + t.context.models = [t.context.model, makeModel()]; }); test.beforeEach('setup request stub', (t) => { @@ -93,12 +105,11 @@ test('crud#list config', (t) => { }); test('crud#list handler', async (t) => { - const { server, model, request, reply } = t.context; - const allModels = [{ id: 1 }, { id: 2 }]; + const { server, model, request, reply, models } = t.context; list({ server, model }); const { handler } = server.route.args[0][0]; - model.findAll.resolves(allModels); + model.findAll.resolves(models); try { await handler(request, reply); @@ -115,9 +126,9 @@ test('crud#list handler', async (t) => { const response = reply.args[0][0]; - t.is( + t.deepEqual( response, - allModels, + models.map(({ id }) => ({ id })), 'responds with the list of models' ); });