Skip to content

Commit

Permalink
fix: processAfterFind bug
Browse files Browse the repository at this point in the history
  • Loading branch information
boycce committed Mar 1, 2022
1 parent 5d1d54e commit 3183b79
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 30 deletions.
2 changes: 1 addition & 1 deletion lib/model-crud.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ module.exports = {
return opts
},

_processAfterFind: function(data, projection, afterFindContext) {
_processAfterFind: function(data, projection={}, afterFindContext={}) {
/**
* Todo: Maybe make this method public?
* Recurses through fields that are models and populates missing default-fields and calls model.afterFind([fn,..])
Expand Down
29 changes: 18 additions & 11 deletions plugins/images/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ let plugin = module.exports = {
plugin.addImages(this, data).then(() => n(null, data)).catch(e => n(e))
})
model.afterFind.push(function(data, n) {
plugin.getSignedUrls(this, data).then(() => n(null, data)).catch(e => n(e))
plugin.getSignedUrls.call(model, this, data).then(() => n(null, data)).catch(e => n(e))
})
}
},
Expand Down Expand Up @@ -128,23 +128,23 @@ let plugin = module.exports = {
let uid = nanoid.nanoid()
let pathFilename = filesArr.imageField.filename ? '/' + filesArr.imageField.filename : ''
let image = {
bucket: this.awsBucket,
date: this.manager.useMilliseconds? Date.now() : Math.floor(Date.now() / 1000),
bucket: plugin.awsBucket,
date: plugin.manager.useMilliseconds? Date.now() : Math.floor(Date.now() / 1000),
filename: file.name,
filesize: file.size,
path: `${plugin.bucketDir}/${uid}${pathFilename}.${file.ext}`,
// sizes: ['large', 'medium', 'small'],
uid: uid,
}
this.manager.info(
plugin.manager.info(
`Uploading '${image.filename}' to '${image.bucket}/${image.path}'`
)
if (test) {
plugin._addImageObjectsToData(filesArr.inputPath, data, image)
resolve()
} else {
plugin.s3.upload({
Bucket: this.awsBucket,
Bucket: plugin.awsBucket,
Key: image.path,
Body: file.data,
// The IAM permission "s3:PutObjectACL" must be included in the appropriate policy
Expand Down Expand Up @@ -183,16 +183,23 @@ let plugin = module.exports = {
},

getSignedUrls: async function(options, data) {
/**
* Get signed urls for all image objects in data
* @param {object} options - monastery operation options {model, query, files, ..}
* @param {object} data
* @return promise(data)
* @this model
*/
// Not wanting signed urls for this operation?
if (util.isDefined(options.getSignedUrls) && !options.getSignedUrls) return

// Find all image objects in data
for (let doc of util.toArray(data)) {
for (let imageField of options.model.imageFields) {
for (let imageField of this.imageFields) {
if (options.getSignedUrls || imageField.getSignedUrl) {
let images = plugin._findImagesInData(doc, imageField, 0, '').filter(o => o.image)
for (let image of images) {
image.image.signedUrl = this._getSignedUrl(image.image.path)
image.image.signedUrl = plugin._getSignedUrl(image.image.path)
}
}
}
Expand Down Expand Up @@ -330,7 +337,7 @@ let plugin = module.exports = {
{ Key: `medium/${key}.jpg` },
{ Key: `large/${key}.jpg` }
)
this.manager.info(
plugin.manager.info(
`Removing '${pre.image.filename}' from '${pre.image.bucket}/${pre.image.path}'`
)
}
Expand Down Expand Up @@ -448,12 +455,12 @@ let plugin = module.exports = {
// Subdocument field
if (util.isSubdocument(field)) {//schema.isObject
// log(`Recurse 1: ${path2}`)
list = list.concat(this._findAndTransformImageFields(field, path2))
list = list.concat(plugin._findAndTransformImageFields(field, path2))

// Array field
} else if (util.isArray(field)) {//schema.isArray
// log(`Recurse 2: ${path2}`)
list = list.concat(this._findAndTransformImageFields(field, path2))
list = list.concat(plugin._findAndTransformImageFields(field, path2))

// Image field. Test for field.image as field.type may be 'any'
} else if (field.type == 'image' || field.image) {
Expand Down Expand Up @@ -507,7 +514,7 @@ let plugin = module.exports = {
if (`${dataPath}.${m}`.match(imageField.fullPathRegex)) {
list.push({ imageField: imageField, dataPath: `${dataPath}.${m}`, image: target[m] })
} else {
list.push(...this._findImagesInData(
list.push(...plugin._findImagesInData(
target[m],
imageField,
imageFieldChunkIndex+i+1,
Expand Down
34 changes: 16 additions & 18 deletions test/plugin-images.js
Original file line number Diff line number Diff line change
Expand Up @@ -734,29 +734,27 @@ module.exports = function(monastery, opendb) {
photos2: [image, image],
})

// Find signed URL
// Find signed URL via query option
let imageWithSignedUrl = { ...image, signedUrl: expect.stringMatching(/^https/) }
await expect(db.user.findOne({ query: userInserted._id, getSignedUrls: true })).resolves.toEqual({
_id: expect.any(Object),
photos: [
{ ...image, signedUrl: expect.stringMatching(/^https/) },
{ ...image, signedUrl: expect.stringMatching(/^https/) },
],
photos2: [
{ ...image, signedUrl: expect.stringMatching(/^https/) },
{ ...image, signedUrl: expect.stringMatching(/^https/) },
]
photos: [imageWithSignedUrl, imageWithSignedUrl],
photos2: [imageWithSignedUrl, imageWithSignedUrl],
})
// Find signed URL

// Find signed URL via schema option
await expect(db.user.findOne({ query: userInserted._id })).resolves.toEqual({
_id: expect.any(Object),
photos: [
{ ...image },
{ ...image },
],
photos2: [
{ ...image, signedUrl: expect.stringMatching(/^https/) },
{ ...image, signedUrl: expect.stringMatching(/^https/) },
]
photos: [image, image],
photos2: [imageWithSignedUrl, imageWithSignedUrl],
})

// Works with _processAfterFind
let rawUser = await db.user._findOne({ _id: userInserted._id })
await expect(db.user._processAfterFind(rawUser)).resolves.toEqual({
_id: expect.any(Object),
photos: [image, image],
photos2: [imageWithSignedUrl, imageWithSignedUrl],
})

db.close()
Expand Down

0 comments on commit 3183b79

Please sign in to comment.