Skip to content

Commit

Permalink
Fix the 'appends' issues: refactor the 'appends' process
Browse files Browse the repository at this point in the history
  • Loading branch information
jack-y committed Mar 19, 2018
1 parent eb4c445 commit 32eee26
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 98 deletions.
10 changes: 5 additions & 5 deletions entity-crud.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ module.exports = function (options) {
processAppend.append(act, entity_2, args.appends)
.then(function (entity_3) {
/* Returns the read entity or success = false */
done(null, {success: (entity !== null), entity: entity_3})
done(null, {success: (entity_3 !== null), entity: entity_3})
})
.catch(function (err) { throw err })
})
Expand Down Expand Up @@ -337,8 +337,8 @@ module.exports = function (options) {
/* Adds the appends data */
processAppend.readAppendsForList(act, formattedList, args.appends)
.then(function (appendResult) {
/* Returns the query result with joins */
return done(null, {success: true, list: appendResult.list, count: appendResult.list.length})
/* Returns the query result with appends */
return done(null, {success: true, list: appendResult, count: appendResult.length})
})
.catch(function (err) { throw err })
})
Expand All @@ -353,8 +353,8 @@ module.exports = function (options) {
/* Adds the appends data */
processAppend.readAppendsForList(act, result.list, args.appends)
.then(function (appendResult) {
/* Returns the query result with joins */
return done(null, {success: true, list: appendResult.list, count: appendResult.list.length})
/* Returns the query result with appends */
return done(null, {success: true, list: appendResult, count: appendResult.length})
})
.catch(function (err) { throw err })
})
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "seneca-entity-crud",
"version": "1.5.3",
"version": "1.5.4",
"description": "CRUD standard actions on entities",
"keywords": [
"node.js",
Expand Down
108 changes: 59 additions & 49 deletions process/append.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
See: https://github.com/jack-y/seneca-entity-crud/blob/master/README-APPENDS.md
*/

const promise = require('bluebird')

/* Prerequisites */
var processAppend = {}

/* Proceeds all the appends */
Expand All @@ -15,11 +18,8 @@ processAppend.append = function (act, entity, appends) {
if (entity && appends && appends.length) {
/* Performs the appends reading */
processAppend.readAppends(act, entity, appends)
.then(function (result) {
/* Returns the read entity with appends */
return resolve(result.entity)
})
.catch(function (err) { return reject(err) })
.then(function (result) { return resolve(result) })
.catch(function (err) {return reject(err) })
} else {
return resolve(entity)
}
Expand All @@ -29,79 +29,89 @@ processAppend.append = function (act, entity, appends) {
/* Reads the appends for one entity */
processAppend.readAppends = function (act, entity, appends) {
return new Promise(function (resolve, reject) {
var count = 0
var updatedEntity = entity
/* Loops on each join */
appends.forEach(function (append, index) {
processAppend.readOneAppend(act, updatedEntity, append)
.then(function (result) {
updatedEntity = result.entity
if (++count === appends.length) {
/* When all appends are done, returns the full entity */
return resolve({entity: updatedEntity})
}
})
.catch(function (err) { return reject(err) })
})
/* Checks if the appends are set */
if (appends && appends.length) {
/* Initializes */
var promises = []
/* Sets the promises */
for (var i = 0; i < appends.length; i++) {
promises.push(processAppend.readOneAppend(act, entity, appends[i]))
}
/* Runs the promises */
if (promises.length > 0) {
promise.all(promises)
.then(function (results) {
/* Loops on each append result */
for (var i = 0; i < results.length; i++) {
if (results[i]) {
entity = Object.assign(results[i], entity)
}
}
return resolve(entity)
})
} else {
return resolve(entity)
}
} else {
return resolve(entity)
}
})
}

/* Process appends on a list of entities */
/* Proceeds appends on a list of entities */
processAppend.readAppendsForList = function (act, list, appends) {
return new Promise(function (resolve, reject) {
if (list.length > 0 && appends && appends.length > 0) {
var newList = []
var itemread = 0
/* Loops on each entity */
list.forEach(function (entity, index) {
/* Proceeds the appends on the entity */
processAppend.readAppends(act, entity, appends)
.then(function (result) {
newList.push(result.entity)
if (++itemread === list.length) {
/* When all appends are done, returns the full list */
return resolve({list: newList})
}
})
})
/* Initializes */
var promises = []
/* Loops on the entites */
for (var i = 0; i < list.length; i++) {
promises.push(processAppend.readAppends(act, list[i], appends))
}
/* Runs the promises */
if (promises.length > 0) {
promise.all(promises)
.then(function (results) { return resolve(results) })
} else {
return resolve({list: list})
return resolve(list)
}
})
}

/* Reads the data specified by the append */
processAppend.readOneAppend = function (act, originEntity, append) {
processAppend.readOneAppend = function (act, entity, append) {
return new Promise(function (resolve, reject) {
/* Initializes */
var action = append.action
var action = Object.assign({}, append.action)
var fieldname = append.resultname ? append.resultname : append.action.role
/* Adds the optional select to the action */
if (append.select) {
/* Checks if the selection value is set */
if (originEntity[append.select.valuename]) {
/* The selection value must be set in the entity */
if (entity[append.select.valuename]) {
/* Initializes */
if (!action.select) { action.select = {}}
/* Adds the select */
action.select[append.select.idname] = originEntity[append.select.valuename]
action.select[append.select.idname] = entity[append.select.valuename]
/* Performs the action */
act(action)
.then(function (result) {
/* Adds the result to the origin entity */
originEntity[fieldname] = result
return resolve({entity: originEntity})
/* Returns the result */
var response = {}
response[fieldname] = result
return resolve(response)
})
.catch(function (err) { return reject(err) })
} else {
return resolve({entity: originEntity})
/* The selection value is not set in the entity: the selection cannot be performed */
return resolve(null)
}
} else {
/* Performs the action */
/* No select required: performs the action */
act(action)
.then(function (result) {
/* Adds the result to the origin entity */
originEntity[fieldname] = result
return resolve({entity: originEntity})
/* Returns the result */
var response = {}
response[fieldname] = result
return resolve(response)
})
.catch(function (err) { return reject(err) })
}
Expand Down
64 changes: 32 additions & 32 deletions test/append-readAppendsForList-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ describe('append readAppendsForList', function () {
var appends = getOneAppend()
/* Fires the test */
processAppend.readAppendsForList(act, entities, appends)
.then(function (result) {
.then(function(result) {
/* Checks the result */
expect(result.list.length).to.equal(entities.length)
expect(result.list[0][getOneAppend()[0].resultname]).to.exist()
expect(result.list[0][getOneAppend()[0].resultname].list.length).to.equal(1)
expect(result.list[0][getOneAppend()[0].resultname].list[0].id).to.equal(entities[0].id_city)
expect(result.length).to.equal(entities.length)
expect(result[0][getOneAppend()[0].resultname]).to.exist()
expect(result[0][getOneAppend()[0].resultname].list.length).to.equal(1)
expect(result[0][getOneAppend()[0].resultname].list[0].id).to.equal(entities[0].id_city)
fin()
})
})
Expand All @@ -37,17 +37,17 @@ describe('append readAppendsForList', function () {
var appends = getAppends()
/* Fires the test */
processAppend.readAppendsForList(act, entities, appends)
.then(function (result) {
.then(function(result) {
/* Checks the result */
expect(result.list.length).to.equal(entities.length)
expect(result.list[0][getAppends()[0].resultname]).to.exist()
expect(result.list[0][getAppends()[0].resultname].list.length).to.equal(1)
expect(result.list[0][getAppends()[0].resultname].list[0].id).to.equal(entities[0].id_brand)
expect(result.list[0][getAppends()[0].resultname].list[0].name).to.equal('Dior')
expect(result.list[0][getAppends()[1].resultname]).to.exist()
expect(result.list[0][getAppends()[1].resultname].list.length).to.equal(1)
expect(result.list[0][getAppends()[1].resultname].list[0].id).to.equal(entities[0].id_city)
expect(result.list[0][getAppends()[1].resultname].list[0].name).to.equal('Paris')
expect(result.length).to.equal(entities.length)
expect(result[0][getAppends()[0].resultname]).to.exist()
expect(result[0][getAppends()[0].resultname].list.length).to.equal(1)
expect(result[0][getAppends()[0].resultname].list[0].id).to.equal(entities[0].id_brand)
expect(result[0][getAppends()[0].resultname].list[0].name).to.equal('Dior')
expect(result[0][getAppends()[1].resultname]).to.exist()
expect(result[0][getAppends()[1].resultname].list.length).to.equal(1)
expect(result[0][getAppends()[1].resultname].list[0].id).to.equal(entities[0].id_city)
expect(result[0][getAppends()[1].resultname].list[0].name).to.equal('Paris')
fin()
})
})
Expand All @@ -63,23 +63,23 @@ describe('append readAppendsForList', function () {
processAppend.readAppendsForList(act, entities, appends)
.then(function (result) {
/* Checks the result */
expect(result.list.length).to.equal(entities.length)
expect(result.list[0][getAppends()[0].resultname]).to.exist()
expect(result.list[0][getAppends()[0].resultname].list.length).to.equal(1)
expect(result.list[0][getAppends()[0].resultname].list[0].id).to.equal(entities[0].id_brand)
expect(result.list[0][getAppends()[0].resultname].list[0].name).to.equal('Dior')
expect(result.list[0][getAppends()[1].resultname]).to.exist()
expect(result.list[0][getAppends()[1].resultname].list.length).to.equal(1)
expect(result.list[0][getAppends()[1].resultname].list[0].id).to.equal(entities[0].id_city)
expect(result.list[0][getAppends()[1].resultname].list[0].name).to.equal('Paris')
expect(result.list[1][getAppends()[0].resultname]).to.exist()
expect(result.list[1][getAppends()[0].resultname].list.length).to.equal(1)
expect(result.list[1][getAppends()[0].resultname].list[0].id).to.equal(entities[1].id_brand)
expect(result.list[1][getAppends()[0].resultname].list[0].name).to.equal('Dior')
expect(result.list[1][getAppends()[1].resultname]).to.exist()
expect(result.list[1][getAppends()[1].resultname].list.length).to.equal(1)
expect(result.list[1][getAppends()[1].resultname].list[0].id).to.equal(entities[1].id_city)
expect(result.list[1][getAppends()[1].resultname].list[0].name).to.equal('Paris')
expect(result.length).to.equal(entities.length)
expect(result[0][getAppends()[0].resultname]).to.exist()
expect(result[0][getAppends()[0].resultname].list.length).to.equal(1)
expect(result[0][getAppends()[0].resultname].list[0].id).to.equal(entities[0].id_brand)
expect(result[0][getAppends()[0].resultname].list[0].name).to.equal('Dior')
expect(result[0][getAppends()[1].resultname]).to.exist()
expect(result[0][getAppends()[1].resultname].list.length).to.equal(1)
expect(result[0][getAppends()[1].resultname].list[0].id).to.equal(entities[0].id_city)
expect(result[0][getAppends()[1].resultname].list[0].name).to.equal('Paris')
expect(result[1][getAppends()[0].resultname]).to.exist()
expect(result[1][getAppends()[0].resultname].list.length).to.equal(1)
expect(result[1][getAppends()[0].resultname].list[0].id).to.equal(entities[1].id_brand)
expect(result[1][getAppends()[0].resultname].list[0].name).to.equal('Dior')
expect(result[1][getAppends()[1].resultname]).to.exist()
expect(result[1][getAppends()[1].resultname].list.length).to.equal(1)
expect(result[1][getAppends()[1].resultname].list[0].id).to.equal(entities[1].id_city)
expect(result[1][getAppends()[1].resultname].list[0].name).to.equal('Paris')
fin()
})
})
Expand Down
7 changes: 3 additions & 4 deletions test/append-readOneAppend-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ describe('append readOneAppend', function () {
processAppend.readOneAppend(act, entity, getAppend())
.then(function (result) {
/* Checks the result */
expect(result.entity.id).to.equal(entity.id)
expect(result.entity[getAppend().resultname]).to.exist()
expect(result.entity[getAppend().resultname].list.length).to.equal(1)
expect(result.entity[getAppend().resultname].list[0].id).to.equal(entity.id_city)
expect(result[getAppend().resultname]).to.exist()
expect(result[getAppend().resultname].list.length).to.equal(1)
expect(result[getAppend().resultname].list[0].id).to.equal(entity.id_city)
fin()
})
})
Expand Down
14 changes: 7 additions & 7 deletions test/append-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,13 @@ describe('append', function () {
processAppend.readAppendsForList(act, list, appends)
.then(function (result) {
/* Checks the result */
expect(result.list.length).to.equal(list.length)
expect(result.list[0].id).to.equal('id01')
expect(result.list[0].query.list.length).to.equal(1)
expect(result.list[0].query.list[0].id).to.equal('i69')
expect(result.list[1].id).to.equal('id02')
expect(result.list[1].query.list.length).to.equal(1)
expect(result.list[1].query.list[0].id).to.equal('i69')
expect(result.length).to.equal(list.length)
expect(result[0].id).to.equal('id01')
expect(result[0].query.list.length).to.equal(1)
expect(result[0].query.list[0].id).to.equal('i69')
expect(result[1].id).to.equal('id02')
expect(result[1].query.list.length).to.equal(1)
expect(result[1].query.list[0].id).to.equal('i69')
fin()
})
})
Expand Down

0 comments on commit 32eee26

Please sign in to comment.