Skip to content

Commit

Permalink
šŸ› Fix Field.Schema contents not being converted from database repreā€¦
Browse files Browse the repository at this point in the history
ā€¦sentation
  • Loading branch information
skerit committed Feb 4, 2024
1 parent 5bab4df commit 0d33b37
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 61 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
119 changes: 58 additions & 61 deletions lib/app/helper_field/schema_field.js
Original file line number Diff line number Diff line change
Expand Up @@ -410,12 +410,12 @@ SchemaField.setMethod(function _toApp(query, options, value, callback) {
*
* @author Jelle De Loecker <[email protected]>
* @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,
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit 0d33b37

Please sign in to comment.