Skip to content

Commit 265d066

Browse files
committed
fix: working with numbers & strings as id
1 parent 983e804 commit 265d066

File tree

3 files changed

+237
-238
lines changed

3 files changed

+237
-238
lines changed

lib/shallow-populate.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ module.exports = function (options) {
7777
if (keyHere) {
7878
if (Array.isArray(keyHere)) {
7979
if (!requiredKeyMappings[key].asArray) {
80-
mapData(byKeyHere, key, keyHere[0], current)
80+
mapDataWithId(byKeyHere, key, keyHere[0], current)
8181
} else {
82-
keyHere.forEach(hereKey => mapData(byKeyHere, key, hereKey, current))
82+
keyHere.forEach(hereKey => mapDataWithId(byKeyHere, key, hereKey, current))
8383
}
8484
} else {
85-
mapData(byKeyHere, key, keyHere, current)
85+
mapDataWithId(byKeyHere, key, keyHere, current)
8686
}
8787
}
8888
})
@@ -104,7 +104,9 @@ module.exports = function (options) {
104104
// }
105105

106106
const paramSets = includes.map(i => {
107-
const keysHere = Object.keys(dataMap[i.keyHere]) || []
107+
const keyVals = dataMap[i.keyHere]
108+
let keysHere = Object.keys(keyVals) || []
109+
keysHere = keysHere.map(k => keyVals[k].key)
108110

109111
return { query: { [i.keyThere]: { $in: keysHere } } }
110112
})
@@ -143,7 +145,7 @@ function getRelatedItems (ids, relatedItems, include) {
143145
ids = [].concat(ids || [])
144146
return relatedItems.reduce((items, currentItem) => {
145147
ids.forEach(id => {
146-
id = typeof id === 'number' ? id : id.toString()
148+
// id = typeof id === 'number' ? id.toString() : id
147149
let currentId
148150
// Allow populating on nested array of objects like key[0].name, key[1].name
149151
// If keyThere includes a dot, we're looking for a nested prop. This checks if that nested prop is an array.
@@ -157,17 +159,18 @@ function getRelatedItems (ids, relatedItems, include) {
157159
// Map over the array to grab each nestedProp's value.
158160
currentId = currentItem[arrayName].map(nestedItem => {
159161
const keyThereVal = getByDot(nestedItem, nestedProp)
160-
return typeof keyThereVal === 'number' ? keyThereVal : keyThereVal.toString()
162+
return keyThereVal
161163
})
162164
} else {
163165
const keyThereVal = getByDot(currentItem, keyThere)
164-
currentId = typeof keyThereVal === 'number' ? [keyThereVal] : [keyThereVal.toString()]
165-
} if (asArray) {
166-
if (currentId.includes(id)) {
166+
currentId = keyThereVal
167+
}
168+
if (asArray) {
169+
if ((Array.isArray(currentId) && currentId.includes(id)) || currentId === id) {
167170
items.push(currentItem)
168171
}
169172
} else {
170-
if (currentId.includes(id)) {
173+
if (currentId === id) {
171174
items = currentItem
172175
}
173176
}
@@ -176,8 +179,11 @@ function getRelatedItems (ids, relatedItems, include) {
176179
}, asArray ? [] : {})
177180
}
178181

179-
function mapData (byKeyHere, key, keyHere, current) {
180-
byKeyHere[key][keyHere] = byKeyHere[key][keyHere] || []
181-
byKeyHere[key][keyHere].push(current)
182+
function mapDataWithId (byKeyHere, key, keyHere, current) {
183+
byKeyHere[key][keyHere] = byKeyHere[key][keyHere] || {
184+
key: keyHere,
185+
vals: []
186+
}
187+
byKeyHere[key][keyHere].vals.push(current)
182188
return byKeyHere
183189
}

0 commit comments

Comments
 (0)