如何在apostrophe-headless中配置自定义字段过滤bundles模块?
Hey there! Let's break down how to get custom field filtering working smoothly for your bundles pieces module in Apostrophe Headless, building on the configuration you already have.
First, Confirm Your Base Setup
You're off to a great start by extending apostrophe-pieces-pages and defining initial filters for name and slug. Here's your existing config for reference:
'bundles': { extend: 'apostrophe-pieces', name: 'bundles', restApi: { maxPerPage: 10 } }, 'bundles-pages': { extend: 'apostrophe-pieces-pages', piecesFilters: [ { name: 'name' }, { name: 'slug' } ] },
Step 1: Add Custom Fields to Your Bundles Module
Before you can filter by a custom field, you need to define it in your bundles module. For example, let's add a category select field and a price integer field:
'bundles': { extend: 'apostrophe-pieces', name: 'bundles', restApi: { maxPerPage: 10 }, addFields: [ { name: 'category', label: 'Bundle Category', type: 'select', choices: [ { label: 'Tech', value: 'tech' }, { label: 'Lifestyle', value: 'lifestyle' }, { label: 'Home', value: 'home' } ] }, { name: 'price', label: 'Bundle Price', type: 'integer', help: 'Price in USD' } ] },
Step 2: Add Custom Filters to bundles-pages
Now update your bundles-pages config to include these new fields in piecesFilters. You can specify matching types (like exact matches for select fields, or range filters for numbers):
'bundles-pages': { extend: 'apostrophe-pieces-pages', piecesFilters: [ { name: 'name' }, { name: 'slug' }, // Filter for our custom category field (exact match) { name: 'category', label: 'Filter by Category', match: 'exact' // Optional for select fields, but explicit is better }, // Range filter for price { name: 'price', label: 'Filter by Price Range', type: 'range' // Enables min/max parameters } ] },
Step 3: Enable Filters for Headless REST API
Since you're using Apostrophe Headless, you need to explicitly allow these filters in the restApi config of your bundles module. This ensures the API accepts these query parameters:
'bundles': { extend: 'apostrophe-pieces', name: 'bundles', restApi: { maxPerPage: 10, // List all filters you want to expose via the API filters: ['name', 'slug', 'category', 'price'] }, addFields: [/* Your custom fields here */] },
You can now test the API with queries like:
- Exact category match:
GET /api/v1/bundles?category=tech - Price range:
GET /api/v1/bundles?price[min]=50&price[max]=200
Advanced: Custom Filter Logic
If you need more complex filtering (like dynamic filters based on user role, or custom search logic), you can override the buildFilters method in your bundles-pages module:
'bundles-pages': { extend: 'apostrophe-pieces-pages', piecesFilters: [/* Your filters */], construct: function(self, options) { // Save the original buildFilters method const originalBuildFilters = self.buildFilters; self.buildFilters = function(req, filters) { // Add custom logic here—example: filter bundles created by the current user if (req.user) { filters._author = req.user._id; } // Call the original method to keep default filtering behavior return originalBuildFilters(req, filters); }; } }
内容的提问来源于stack exchange,提问作者Marjan

