From bb3e39e2f910b16a33cbc01c49d92a0fdb49ff59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvaro=20Cabrera=20Dur=C3=A1n?= Date: Sat, 28 Aug 2021 21:25:01 -0500 Subject: [PATCH] cleanup --- lib/res.js | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/lib/res.js b/lib/res.js index 94fed99..183cedc 100644 --- a/lib/res.js +++ b/lib/res.js @@ -140,7 +140,7 @@ function _fixRefs(refs, values) { }); } -function _fileInfo(baseDir, data) { +function _fileInfo(baseDir, data, ex) { /* istanbul ignore else */ if (!data) { throw new Error('File data is missing'); @@ -148,10 +148,13 @@ function _fileInfo(baseDir, data) { /* istanbul ignore else */ if (Array.isArray(data)) { - return data.map(x => _fileInfo(baseDir, x)); + // FIXME: additional fields on uploads + return data.map(x => _fileInfo(baseDir, x, ex)); } + delete ex.$upload; return { + ...ex, mtime: (data.lastModifiedDate || new Date()).toGMTString(), path: path.relative(baseDir, data.path), name: data.name, @@ -215,7 +218,7 @@ function _saveUploads(Model, field, payload, attachments, onUploadCallback) { /* istanbul ignore else */ try { if (payload.$upload) { - return _fileInfo(baseDir, attachments.files[payload.$upload]); + return _fileInfo(baseDir, attachments.files[payload.$upload], payload); } } catch (e) { throw new Error(`Cannot retrieve ${field} '${payload.$upload}' (${e.message})`); @@ -223,7 +226,7 @@ function _saveUploads(Model, field, payload, attachments, onUploadCallback) { if (payload[Model.name]) { if (payload[Model.name].$upload) { - const attachment = _fileInfo(baseDir, attachments.files[payload[Model.name].$upload]); + const attachment = _fileInfo(baseDir, attachments.files[payload[Model.name].$upload], payload[Model.name]); tasks.push(_pushUpload(Model.name, schema, attachment.path, payload, attachment, onUploadCallback)); } else { @@ -231,7 +234,7 @@ function _saveUploads(Model, field, payload, attachments, onUploadCallback) { const data = payload[Model.name][assoc]; if (data && data.$upload && attachments.files[data.$upload]) { - const attachment = _fileInfo(baseDir, attachments.files[data.$upload]); + const attachment = _fileInfo(baseDir, attachments.files[data.$upload], data); tasks.push(_pushUpload(assoc, schema[assoc], attachment.path, payload[Model.name], attachment, onUploadCallback)); } @@ -244,7 +247,7 @@ function _saveUploads(Model, field, payload, attachments, onUploadCallback) { && payload[prop].$upload && attachments.files[payload[prop].$upload] ) { - const attachment = _fileInfo(baseDir, attachments.files[payload[prop].$upload]); + const attachment = _fileInfo(baseDir, attachments.files[payload[prop].$upload], payload[prop]); tasks.push(_pushUpload(prop, schema[prop], attachment.path, payload, attachment, onUploadCallback)); } @@ -789,7 +792,7 @@ module.exports = (conn, options, modelName) => { function build(payload, isUpdate, _options) { _options = _options || {}; _options.logging = options.logging || _options.logging; - _options.where = Object.assign({}, options.where, _options.where, _where); + _options.where = Object.assign({}, _options.where, _where); let _payload; let _tasks; @@ -804,9 +807,7 @@ module.exports = (conn, options, modelName) => { .then(() => _tasks.before.reduce((prev, cur) => prev.then(() => cur(_opts, isUpdate)), Promise.resolve())) .then(() => model[isUpdate ? 'update' : 'create'](_payload, _opts)) .then(row => { - let pk = !isUpdate - ? row[0] - : null; + let pk; /* istanbul ignore else */ if (!Array.isArray(row)) { @@ -820,16 +821,20 @@ module.exports = (conn, options, modelName) => { _payload[model.primaryKeyAttribute] = _payload[model.primaryKeyAttribute] || pk; - return _tasks.after.reduce((prev, cur) => prev.then(() => cur(pk, _opts, isUpdate)), Promise.resolve()); + /* istanbul ignore else */ + if (!isUpdate) { + row = [row, _payload]; + } + + return _tasks.after.reduce((prev, cur) => prev.then(() => cur(pk, _opts, isUpdate)), Promise.resolve()).then(() => row); }) - .then(() => _payload) .catch(err); } function fetch(method, _options) { _options = _options || {}; _options.logging = options.logging || _options.logging; - _options.where = Object.assign({}, options.where, _options.where, _where); + _options.where = Object.assign({}, _options.where, _where); const _pk = model.primaryKeyAttribute; const _opts = _getOpts(model, _props, method, _options);