I have several questions about Apostrophe CMS:
- Is it possible to add a unique field type in apostrophe-pieces? I can't find a way to do this.
Edit: I noticed that I wasn't specific enough. I want to make sure that there can't be two instances in the database with the same value in an added field. It should be something like an additional id. Is there an option for this? Maybe something like:
addFields: [
{
name: 'secondId',
label: 'Second ID',
type: 'string',
required: true,
unique: true
}
]
- I want to access the apostrophe-headless api and get a specific element by passing a certain value of one of the created field types of the correspondent piece in a GET-parameter. Is something like this possible?
For example:
Piece:
module.exports = {
extend: 'apostrophe-pieces',
name: 'article',
label: 'Article',
pluralLabel: 'Articles',
restApi: {
safeFor: 'manage'
},
addFields: [
{
name: 'title',
label: 'Name',
type: 'string',
required: true
},
{
name: 'author',
label: 'Author',
type: 'string',
required: true
}
]
};
Desired api call for getting all articles which have strored "Jon" as author:
http://example.com/api/v1/article?author=Jon
Thank you very much in advance!
Custom field types
You can add custom field types at project level by extending
apostrophe-schemasand adding the proper definition. You'll need to add a converter for server-side sanitization and a populator for the front-end of the form field.You can follow the examples in Apostrophe's schema module, linked are the functions defining a float
https://github.com/apostrophecms/apostrophe/blob/0bcd5faf84bc7b05c51de7331b17f5929794f524/lib/modules/apostrophe-schemas/index.js#L1367 https://github.com/apostrophecms/apostrophe/blob/0bcd5faf84bc7b05c51de7331b17f5929794f524/lib/modules/apostrophe-schemas/public/js/user.js#L991
You would add your definitions in your project level
lib/modules/apostrophe-schemas'sindex.jsandpublic/js/user.jsrespectively.Filtering
You can search your piece index for a string like
Jonby adding?search=jonto your query but more likely you want to filter pieces by the value of a join.If you had piece types
articleandauthors, you could have ajoinByOnefield inarticle's schema that lets you relate that article to anauthorpiece. Then, by enablingpieceFiltersyou could filter directly on those joined properties.A complete rundown of
piecesFilterscan be found here https://apostrophecms.org/docs/tutorials/intermediate/cursors.html#filtering-joins-browsing-profiles-by-marketI think you'd also need to mark that filter as safe for api use in your
apostrophe-headlessconfiguration https://github.com/apostrophecms/apostrophe-headless#filtering-products