-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Allow
Field.Enum
values that are used by a Field.Schema
field t…
…o refer to a document in a model, as if they were an association
- Loading branch information
Showing
2 changed files
with
46 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -212,6 +212,17 @@ SchemaField.setMethod(function resolveSchemaPath(context_schema, record, field_p | |
} else if (enum_value.schema) { | ||
schema = enum_value.schema; | ||
} else if (enum_value.value) { | ||
|
||
if (enum_value.value.via_model) { | ||
// The value is actually referring to a specific record | ||
return this.getSubschemaFromModel( | ||
enum_value.value.via_model, | ||
enum_value.value.foreign_key, | ||
enum_value.value.pk || record_value, | ||
property_name | ||
); | ||
} | ||
|
||
schema = enum_value.value[property_name] || enum_value.value.schema; | ||
} else { | ||
console.warn('Failed to find schema path', JSON.stringify(schema_path), 'in record', record); | ||
|
@@ -245,20 +256,44 @@ SchemaField.setMethod(function getSubschemaFromAssociation(record, path, associa | |
return false; | ||
} | ||
|
||
const associated_model = alchemy.getModel(association.model_name); | ||
return this.getSubschemaFromModel( | ||
association.model_name, | ||
association.options.foreign_key, | ||
local_value, | ||
path | ||
); | ||
}); | ||
|
||
/** | ||
* Get the subschema via an association | ||
* | ||
* @author Jelle De Loecker <[email protected]> | ||
* @since 0.2.0 | ||
* @version 1.4.0 | ||
* | ||
* @param {string} model_name | ||
* @param {string} foreign_key | ||
* @param {*} local_value | ||
* @param {string} path The path inside the associated schema | ||
* | ||
* @return {Schema} Response might be a promise | ||
*/ | ||
SchemaField.setMethod(function getSubschemaFromModel(model_name, foreign_key, local_value, path) { | ||
|
||
const model = alchemy.getModel(model_name); | ||
|
||
if (!associated_model) { | ||
if (!model) { | ||
return false; | ||
} | ||
|
||
return Pledge.Swift.execute(async () => { | ||
let remote_request = model.resolveRemoteSchemaRequest( | ||
this, | ||
foreign_key || '_id', | ||
local_value, | ||
path | ||
); | ||
|
||
let result = await associated_model.resolveRemoteSchemaRequest( | ||
this, | ||
association.options.foreign_key, | ||
local_value, | ||
path | ||
); | ||
return Pledge.Swift.waterfall(remote_request, result => { | ||
|
||
if (!result) { | ||
return null; | ||
|
@@ -269,7 +304,7 @@ SchemaField.setMethod(function getSubschemaFromAssociation(record, path, associa | |
return result; | ||
} | ||
|
||
let found_schema = this.resolveSchemaPath(associated_model.schema, result, path, path); | ||
let found_schema = this.resolveSchemaPath(model.schema, result, path, path); | ||
|
||
return found_schema; | ||
}); | ||
|