From 0efd05a9684f92bdcde4abf3e68cc1be51a5068c Mon Sep 17 00:00:00 2001 From: R2m1liA <15258427350@163.com> Date: Tue, 21 Oct 2025 08:52:46 +0000 Subject: [PATCH] =?UTF-8?q?feat(hook):=20=E6=B7=BB=E5=8A=A0init=20hook?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加init hook用于检查与创建meilisearch相关集合 --- src/meilisearch_hook/index.ts | 98 +++++++++++++++++++++++------------ 1 file changed, 66 insertions(+), 32 deletions(-) diff --git a/src/meilisearch_hook/index.ts b/src/meilisearch_hook/index.ts index d292ddd..7b6cc75 100644 --- a/src/meilisearch_hook/index.ts +++ b/src/meilisearch_hook/index.ts @@ -1,39 +1,73 @@ import { defineHook } from '@directus/extensions-sdk'; -export default defineHook(async ({ filter, action }, { services, getSchema }) => { - const { CollectionsService } = services; - const collectionsService = new CollectionsService({ schema: await getSchema() }); +export default defineHook(async ({ init, filter, action }, { services, getSchema }) => { + init('app.after', async () => { + 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); - if (!exist) { - console.log('Creating meili_index_configs collection...'); + // 检查meili_search_config集合是否存在 + const meili_search_config_exists = await collectionsSvc.readOne('meili_search_configs').then(() => true).catch(() => false); + 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({ - 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.'); - } + // 检查meili_index_configs集合是否存在 + const meili_index_configs_exists = await collectionsSvc.readOne('meili_index_configs').then(() => true).catch(() => false); + if (!meili_index_configs_exists) { + console.warn('collection meili_index_configs does not exist, creating...'); + await collectionsSvc.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!');