-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to work around id
and type
field names?
#206
Comments
Looking through the source, it seems like I should be able to pass a custom |
Tried a hack like the following, but this approach doesn't seem feasible. class ModMongooseAdapter extends JSON_API.dbAdapters.Mongoose {
constructor(models, toTypeName, idGenerator) {
super(models, toTypeName, idGenerator);
log('ModMongooseAdapter constructor', ...arguments);
}
mapReservedFieldNames(doc) {
const reservedNames = ['id', 'type'];
const docProps = {};
for (const fieldName of Object.keys(doc.schema.paths).filter(key => !key.startsWith('__'))) {
if (reservedNames.includes(fieldName)) {
const newFieldName = `${fieldName}Field`;
docProps[newFieldName] = doc[fieldName];
} else {
docProps[fieldName] = doc[fieldName];
}
}
// This part doesn't quite work since we don't actually want to change the underlying data
// plus the Model/Schema still lists `id` and `type` and we still end up with
// "Error: Cannot construct a ResourceSet with missing data"`
const modDoc = doc.overwrite({
...docProps
});
log(`mapReservedFieldNames returning`, modDoc);
return modDoc;
}
docsToResourceData(docs, isPlural, fields) {
log('docsToResourceData', ...arguments);
const modDocs = docs.map(this.mapReservedFieldNames);
super.docsToResourceData(modDocs, isPlural, fields);
}
docToResource(doc, fields) {
log('docToResource', doc, fields);
const modDoc = this.mapReservedFieldNames(doc);
return super.docToResource(modDoc, fields);
}
} I'm going to try creating alternate schema/models with something like a virtual to get |
I honestly haven't looked at the code for this library in a long time, so I'd have to refresh my memory to be able to help. I'll try to take a look soonish |
OK. For now I just made a branch with models having different names, e.g. |
I have some Mongoose models/Schema that include
id
and/ortype
keys. If I pass these models toJSON_API.dbAdapters.Mongoose(...)
and then use that to set upJSON_API.ResourceTypeRegistry
it runs fine until a request comes in. Then, if I have debug logging enabled, I can seejson-api:warn Errors converted to json-api Result Error:
typeand
idcannot be used as field names.
How can I handle this situation? Perhaps there's a way I can telljson-api
that these fields should be remapped/renamed somehow (e.g.type
->typeField
)?The text was updated successfully, but these errors were encountered: