Is it possible to assign the field used for a collection id, such that /foo/:id
will use a different field than id to match the record?
Yes, you can achieve this by creating a custom controller.
Find one example, this will search by email:
Route: ./api/*/config/routes.json
[
//...
{
"method": "GET",
"path": "/candidates/:email",
"handler": "candidates.findOne",
"config": {
"policies": []
}
}
//...
]
Controller: ./api/*/controllers/*.js
const { sanitizeEntity } = require('strapi-utils');
module.exports = {
//...
async findOne(ctx) {
let email = ctx.params.email;
let candidate = await strapi.query('candidates').findOne({ email });
let entity = sanitizeEntity(candidate, {
model: strapi.models['candidates'],
});
return entity;
},
//...
};