feat: 添加meilisearch hook
This commit is contained in:
45
src/meilisearch_hook/index.ts
Normal file
45
src/meilisearch_hook/index.ts
Normal file
@ -0,0 +1,45 @@
|
||||
import { defineHook } from '@directus/extensions-sdk';
|
||||
|
||||
export default defineHook(async ({ filter, action }, { services, getSchema }) => {
|
||||
const { CollectionsService } = services;
|
||||
const collectionsService = new CollectionsService({ schema: await getSchema() });
|
||||
|
||||
const exist = await collectionsService.readOne('meili_index_configs').then(() => true).catch(() => false);
|
||||
if (!exist) {
|
||||
console.log('Creating meili_index_configs collection...');
|
||||
|
||||
await collectionsService.createOne({
|
||||
collection: 'meili_index_configs',
|
||||
meta: {
|
||||
note: '配置哪些集合需要被索引到 MeiliSearch',
|
||||
icon: 'search',
|
||||
hidden: true,
|
||||
system: true,
|
||||
},
|
||||
schema: {
|
||||
name: 'meili_index_configs',
|
||||
},
|
||||
fields: [
|
||||
{ field: 'collection_name', type: 'string', meta: { note: '要索引的集合名', interface: 'input' } },
|
||||
{ field: 'index_name', type: 'string', meta: { note: 'MeiliSearch 索引名称', interface: 'input' } },
|
||||
{ field: 'fields', type: 'json', meta: { note: '要索引的字段数组', interface: 'code-editor' } },
|
||||
{ field: 'enabled', type: 'boolean', meta: { note: '是否启用', interface: 'boolean' } },
|
||||
{ field: 'settings', type: 'json', meta: { note: 'MeiliSearch 索引设置', interface: 'code-editor' } },
|
||||
],
|
||||
}).then(() => {
|
||||
console.log('meili_index_configs collection created successfully.');
|
||||
}).catch((e) => {
|
||||
console.error('Error creating meili_index_configs collection:', e);
|
||||
})
|
||||
} else {
|
||||
console.log('meili_index_configs collection already exists.');
|
||||
}
|
||||
|
||||
filter('items.create', () => {
|
||||
console.log('Creating Item!');
|
||||
});
|
||||
|
||||
action('items.create', () => {
|
||||
console.log('Item created!');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user