Skip to content

Commit

Permalink
Merge pull request #504 from dadi/feature/media-field
Browse files Browse the repository at this point in the history
Add media field
  • Loading branch information
eduardoboucas committed Oct 29, 2018
2 parents 64ef706 + ea4970e commit 226de02
Show file tree
Hide file tree
Showing 10 changed files with 924 additions and 169 deletions.
381 changes: 242 additions & 139 deletions dadi/lib/controller/media.js

Large diffs are not rendered by default.

84 changes: 84 additions & 0 deletions dadi/lib/fields/media.js
@@ -0,0 +1,84 @@
module.exports.type = 'media'

module.exports.beforeOutput = function ({
client,
config,
field,
input,
schema
}) {
let bucket = (schema.settings && schema.settings.mediaBucket) || config.get('media.defaultBucket')
let model = this.getForeignModel(bucket)

if (!model) {
return input
}

let isArraySyntax = Array.isArray(input[field])
let normalisedValue = isArraySyntax ? input[field] : [input[field]]
let mediaObjectIDs = normalisedValue.map(value => {
if (typeof value !== 'string') {
return value._id
}

return value
}).filter(Boolean)

if (mediaObjectIDs.length === 0) {
return input
}

return model.get({
client,
query: {
_id: {
$in: mediaObjectIDs
}
}
}).then(({results}) => {
return results.reduce((mediaObjects, result) => {
mediaObjects[result._id] = result

return mediaObjects
}, {})
}).then(mediaObjects => {
return normalisedValue.map(value => {
if (mediaObjects[value._id]) {
let mergedValue = Object.assign({}, mediaObjects[value._id], value)
let sortedValue = Object.keys(mergedValue).sort().reduce((sortedValue, field) => {
sortedValue[field] = mergedValue[field]

return sortedValue
}, {})

return sortedValue
}

return value
})
}).then(composedValue => {
return Object.assign(input, {
[field]: isArraySyntax ? composedValue : composedValue[0]
})
})
}

module.exports.beforeSave = function ({
field,
input
}) {
let isArraySyntax = Array.isArray(input[field])
let normalisedValue = (isArraySyntax ? input[field] : [input[field]]).map(value => {
if (typeof value === 'string') {
return {
_id: value
}
}

return value
})

return {
[field]: isArraySyntax ? normalisedValue : normalisedValue[0]
}
}
5 changes: 1 addition & 4 deletions dadi/lib/model/collections/create.js
Expand Up @@ -68,10 +68,7 @@ function create ({
documents,
schema
}).catch(errors => {
let error = this._createValidationError('Validation Failed')

error.success = false
error.errors = errors
let error = this._createValidationError('Validation Failed', errors)

return Promise.reject(error)
})
Expand Down
26 changes: 12 additions & 14 deletions dadi/lib/model/collections/update.js
Expand Up @@ -115,17 +115,23 @@ function update ({
isUpdate: true,
schema
}).catch(errors => {
let error = this._createValidationError('Validation Failed')

error.success = false
error.errors = errors
let error = this._createValidationError('Validation Failed', errors)

return Promise.reject(error)
})
}).then(() => {
// Format the query.
query = this.formatQuery(query)

return this.find({
query
})
}).then(({results}) => {
// Create a copy of the documents that matched the find
// query, as these will be updated and we need to send back
// to the client a full result set of modified documents.
updatedDocuments = results

// Add any internal fields to the update.
Object.assign(update, internals)

Expand All @@ -138,7 +144,8 @@ function update ({
return result.then(transformedUpdate => {
return this.runFieldHooks({
data: {
internals
internals,
updatedDocuments
},
field,
input: {
Expand All @@ -152,15 +159,6 @@ function update ({
}, Promise.resolve({})).then(transformedUpdate => {
update = transformedUpdate

return this.find({
query
})
}).then(result => {
// Create a copy of the documents that matched the find
// query, as these will be updated and we need to send back
// to the client a full result set of modified documents.
updatedDocuments = result.results

// Run any `beforeUpdate` hooks.
if (this.settings.hooks && this.settings.hooks.beforeUpdate) {
return new Promise((resolve, reject) => {
Expand Down
27 changes: 23 additions & 4 deletions dadi/lib/model/index.js
Expand Up @@ -204,10 +204,16 @@ Model.prototype._compileFieldHooks = function () {
* @api private
*/
Model.prototype._createValidationError = function (message, data) {
const err = new Error(message || 'Model Validation Failed')
err.statusCode = 400
const error = new Error(message || 'Model Validation Failed')

return err
error.statusCode = 400
error.success = false

if (data) {
error.errors = data
}

return error
}

/**
Expand Down Expand Up @@ -637,7 +643,20 @@ Model.prototype.runFieldHooks = function ({
}
})

return queue
return queue.catch(error => {
let errorData = [
{
field,
message: error.message
}
]

logger.error({module: 'field hooks'}, error)

return Promise.reject(
this._createValidationError('Validation failed', errorData)
)
})
}

/**
Expand Down
3 changes: 2 additions & 1 deletion features.json
Expand Up @@ -2,5 +2,6 @@
"aclv1",
"i18nv1",
"i18nv2",
"collectionsv1"
"collectionsv1",
"validationv1"
]
4 changes: 1 addition & 3 deletions package.json
Expand Up @@ -21,7 +21,7 @@
]
},
"dependencies": {
"@dadi/api-validator": "^1.0.0",
"@dadi/api-validator": "^1.1.0",
"@dadi/boot": "^1.1.3",
"@dadi/cache": "^3.0.0",
"@dadi/et": "^2.0.0",
Expand All @@ -48,7 +48,6 @@
"jsonwebtoken": "^8.0.0",
"langs": "^2.0.0",
"length-stream": "^0.1.1",
"mime": "^2.3.1",
"mkdirp": "^0.5.1",
"moment": "2.19.3",
"natural": "^0.6.1",
Expand All @@ -64,7 +63,6 @@
"streamifier": "^0.1.1",
"underscore": "1.8.3",
"underscore-contrib": "^0.3.0",
"validator": "9.4.1",
"vary": "^1.1.2"
},
"devDependencies": {
Expand Down

0 comments on commit 226de02

Please sign in to comment.