feat(hook): 添加init hook

- 添加init hook用于检查与创建meilisearch相关集合
This commit is contained in:
2025-10-21 08:52:46 +00:00
parent ef8ad6436a
commit 0efd05a968

View File

@ -1,39 +1,73 @@
import { defineHook } from '@directus/extensions-sdk'; import { defineHook } from '@directus/extensions-sdk';
export default defineHook(async ({ filter, action }, { services, getSchema }) => { export default defineHook(async ({ init, filter, action }, { services, getSchema }) => {
const { CollectionsService } = services; init('app.after', async () => {
const collectionsService = new CollectionsService({ schema: await getSchema() }); console.log('Directus App Started - MeiliSearch Hook Initialized');
const { CollectionsService } = services;
const schema = await getSchema();
const collectionsSvc = new CollectionsService({
schema,
});
const exist = await collectionsService.readOne('meili_index_configs').then(() => true).catch(() => false); // 检查meili_search_config集合是否存在
if (!exist) { const meili_search_config_exists = await collectionsSvc.readOne('meili_search_configs').then(() => true).catch(() => false);
console.log('Creating meili_index_configs collection...'); if (!meili_search_config_exists) {
console.warn('collection meili_search_config does not exist, creating...');
await collectionsSvc.createOne({
collection: 'meili_search_config',
meta: {
note: 'MeiliSearch 配置',
singleton: true,
hidden: true,
icon: 'settings',
},
schema: {
name: 'meili_search_config',
},
fields: [
{ field: 'host', type: 'string', meta: { note: 'MeiliSearch 主机地址', interface: 'input' } },
{ field: 'api_key', type: 'string', meta: { note: 'MeiliSearch API Key', interface: 'input' } },
],
}).then(() => {
console.log('meili_search_config collection created successfully.');
}).catch((e) => {
console.error('Error creating meili_search_config collection:', e);
});
} else {
console.log('meili_index_config collection already exists.');
}
await collectionsService.createOne({ // 检查meili_index_configs集合是否存在
collection: 'meili_index_configs', const meili_index_configs_exists = await collectionsSvc.readOne('meili_index_configs').then(() => true).catch(() => false);
meta: { if (!meili_index_configs_exists) {
note: '配置哪些集合需要被索引到 MeiliSearch', console.warn('collection meili_index_configs does not exist, creating...');
icon: 'search', await collectionsSvc.createOne({
hidden: true, collection: 'meili_index_configs',
system: true, meta: {
}, note: '配置哪些集合需要被索引到 MeiliSearch',
schema: { icon: 'search',
name: 'meili_index_configs', hidden: true,
}, system: true,
fields: [ },
{ field: 'collection_name', type: 'string', meta: { note: '要索引的集合名', interface: 'input' } }, schema: {
{ field: 'index_name', type: 'string', meta: { note: 'MeiliSearch 索引名称', interface: 'input' } }, name: 'meili_index_configs',
{ field: 'fields', type: 'json', meta: { note: '要索引的字段数组', interface: 'code-editor' } }, },
{ field: 'enabled', type: 'boolean', meta: { note: '是否启用', interface: 'boolean' } }, fields: [
{ field: 'settings', type: 'json', meta: { note: 'MeiliSearch 索引设置', interface: 'code-editor' } }, { field: 'collection_name', type: 'string', meta: { note: '要索引的集合名', interface: 'input' } },
], { field: 'index_name', type: 'string', meta: { note: 'MeiliSearch 索引名称', interface: 'input' } },
}).then(() => { { field: 'fields', type: 'json', meta: { note: '要索引的字段数组', interface: 'code-editor' } },
console.log('meili_index_configs collection created successfully.'); { field: 'enabled', type: 'boolean', meta: { note: '是否启用', interface: 'boolean' } },
}).catch((e) => { { field: 'settings', type: 'json', meta: { note: 'MeiliSearch 索引设置', interface: 'code-editor' } },
console.error('Error creating meili_index_configs collection:', e); ],
}) }).then(() => {
} else { console.log('meili_index_configs collection created successfully.');
console.log('meili_index_configs collection already exists.'); }).catch((e) => {
} console.error('Error creating meili_index_configs collection:', e);
});
} else {
console.log('meili_index_configs collection already exists.');
}
});
filter('items.create', () => { filter('items.create', () => {
console.log('Creating Item!'); console.log('Creating Item!');