diff --git a/CHANGELOG.md b/CHANGELOG.md index d000027b..51ff8e0d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ * Add `Plugin` class * Refactor loading of requirements * The `Alchemy.ClientSession` class should now be used as a map to store session data +* Fix `Field.Schema` contents not being converted from database representation when the schema also has a relation ## 1.3.22 (2023-12-21) diff --git a/lib/app/helper_field/schema_field.js b/lib/app/helper_field/schema_field.js index b47f436a..5159b4ac 100644 --- a/lib/app/helper_field/schema_field.js +++ b/lib/app/helper_field/schema_field.js @@ -410,12 +410,12 @@ SchemaField.setMethod(function _toApp(query, options, value, callback) { * * @author Jelle De Loecker * @since 0.2.0 - * @version 1.3.16 + * @version 1.4.0 * * @param {Mixed} value * @param {Function} callback */ -SchemaField.setMethod(function _toAppFromValue(query, options, value, callback) { +SchemaField.setMethod(async function _toAppFromValue(query, options, value, callback) { var that = this, recursive, @@ -427,7 +427,11 @@ SchemaField.setMethod(function _toAppFromValue(query, options, value, callback) recursive = options.recursive; if (recursive == null) { - recursive = 1; + if (this.options?.recursive != null) { + recursive = this.options.recursive; + } else { + recursive = 1; + } } if (recursive && Blast.isBrowser) { @@ -436,59 +440,6 @@ SchemaField.setMethod(function _toAppFromValue(query, options, value, callback) recursive = 0; } - // Get associated records if the subschema has associations defined - if (recursive && this.field_schema && !Object.isEmpty(this.field_schema.associations)) { - - name = this.name + 'FieldModel'; - Dummy = alchemy.getModel('Model', false); - - Dummy = new Dummy({ - root_model : this.root_model, - name : name - }); - - item = {}; - - item[name] = value; - - let sub_criteria = Dummy.find(); - - // Disable generating Document instances - // if the original find did the same - sub_criteria.setOption('document', options.document); - sub_criteria.setOption('original_query', query); - sub_criteria.setOption('_root_data', options._root_data); - sub_criteria.setOption('_parent_field', that); - sub_criteria.setOption('_parent_model', that.schema.model_name); - sub_criteria.setOption('recursive', recursive); - - sub_criteria.setOption('associations', this.field_schema.associations); - - // @todo: inherit other original find options? - - Dummy.addAssociatedDataToRecord(sub_criteria, item, function gotAssociatedData(err, result) { - - var key; - - if (err) { - return callback(err); - } - - for (key in result) { - if (key == name) { - continue; - } - - value[key] = result[key]; - } - - callback(null, value); - } - ); - - return; - } - let record; if (options.parent_value) { @@ -538,14 +489,60 @@ SchemaField.setMethod(function _toAppFromValue(query, options, value, callback) } }); - return Function.parallel(4, tasks, function convertedFields(err, result) { + value = await Function.parallel(4, tasks); + } - if (err) { - return callback(err); - } + // Get associated records if the subschema has associations defined + if (recursive && this.field_schema && !Object.isEmpty(this.field_schema.associations)) { - callback(null, that.castEntry(result)); + name = this.name + 'FieldModel'; + Dummy = alchemy.getModel('Model', false); + + Dummy = new Dummy({ + root_model : this.root_model, + name : name }); + + item = {}; + + item[name] = value; + + let sub_criteria = Dummy.find(); + + // Disable generating Document instances + // if the original find did the same + sub_criteria.setOption('document', options.document); + sub_criteria.setOption('original_query', query); + sub_criteria.setOption('_root_data', options._root_data); + sub_criteria.setOption('_parent_field', that); + sub_criteria.setOption('_parent_model', that.schema.model_name); + sub_criteria.setOption('recursive', recursive); + + sub_criteria.setOption('associations', this.field_schema.associations); + + // @todo: inherit other original find options? + + Dummy.addAssociatedDataToRecord(sub_criteria, item, function gotAssociatedData(err, result) { + + var key; + + if (err) { + return callback(err); + } + + for (key in result) { + if (key == name) { + continue; + } + + value[key] = result[key]; + } + + callback(null, value); + } + ); + + return; } callback(null, this.castEntry(value));