Unique field types and specific GET-parameters for api calls in Apostrophe CMS

324 views Asked by At

I have several questions about Apostrophe CMS:

  1. 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
    }
]
  1. 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!

1

There are 1 answers

5
Stuart Romanek On

Custom field types

You can add custom field types at project level by extending apostrophe-schemas and 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's index.js and public/js/user.js respectively.

Filtering

You can search your piece index for a string like Jon by adding ?search=jon to your query but more likely you want to filter pieces by the value of a join.

If you had piece types article and authors, you could have a joinByOne field in article's schema that lets you relate that article to an author piece. Then, by enabling pieceFilters you could filter directly on those joined properties.

A complete rundown of piecesFilters can be found here https://apostrophecms.org/docs/tutorials/intermediate/cursors.html#filtering-joins-browsing-profiles-by-market

I think you'd also need to mark that filter as safe for api use in your apostrophe-headless configuration https://github.com/apostrophecms/apostrophe-headless#filtering-products