Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add reference id values to one array on the original doc #312

Merged
merged 2 commits into from
Jul 5, 2017
Merged
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
13 changes: 11 additions & 2 deletions dadi/lib/composer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,19 @@ Composer.prototype.createFromComposed = function (doc, req, callback) {
Promise.all(queue).then((results) => {
var allProperties = {}

_.each(_.compact(results), (result) => {
_.each(_.compact(results), result => {
var key = Object.keys(result)[0]

if (!allProperties[key]) {
// add the value to the field as a simple string
allProperties[key] = result[key]
} else {
allProperties[key] = [allProperties[key]]
// create an array for this key, another value needs to be added
if (!Array.isArray(allProperties[key])) {
allProperties[key] = [allProperties[key]]
}

// push the new value
allProperties[key].push(result[key])
}
})
Expand Down Expand Up @@ -244,8 +251,10 @@ Composer.prototype.createOrUpdate = function (model, field, obj, req) {
}

var newDoc = results && results.results && results.results[0]

var result = {}
result[field] = newDoc._id.toString()

return resolve(result)
})
}
Expand Down
53 changes: 53 additions & 0 deletions test/acceptance/fields/reference.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,59 @@ describe('Reference Field', function () {
})
})

it('should create multiple reference documents that don\'t have _id fields', function (done) {
var data = {
"word": "animals",
"children": [
{
"word": "dogs",
"children": [
{
"word": "guide_dogs",
"children": []
},
{
"word": "puppies",
"children": []
}
]
},
{
"word": "foxes",
"children": []
},
{
"word": "pandas",
"children": []
}
]
}

config.set('query.useVersionFilter', true)

var client = request(connectionString)
client
.post('/v1/library/taxonomy')
.set('Authorization', 'Bearer ' + bearerToken)
.send(data)
.expect(200)
.end(function (err, res) {
if (err) return done(err)

should.exist(res.body.results)
var newDoc = res.body.results[0]

should.exist(newDoc.word)
newDoc.word.should.eql('animals')

newDoc.children.length.should.eql(3)
newDoc.children[0].word.should.eql('dogs')
newDoc.children[1].word.should.eql('foxes')
newDoc.children[2].word.should.eql('pandas')
done()
})
})

it('should update reference documents that already have _id fields', function (done) {
var person = {
name: 'Ernest Hemingway'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"fields": {
"word": {
"type": "String",
"label": "Word",
"comments": "Taxanomic word",
"validation": {},
"required": true,
"message": "can't be empty"
},
"children": {
"type": "Reference",
"label": "Children",
"required": false,
"settings": {
"collection": "taxonomy",
"multiple": true
}
}
},
"settings": {
"compose": true,
"cache": false,
"authenticate": true,
"callback": null,
"defaultFilters": null,
"fieldLimiters": null,
"count": 40,
"sort": "createdAt",
"sortOrder": 1,
"limit": 1,
"storeRevisions": true,
"description": "Taxonomy",
"displayName": "Taxonomy"
}
}