83 lines
3.0 KiB
TypeScript
83 lines
3.0 KiB
TypeScript
import { defineHook } from '@directus/extensions-sdk';
|
|
import { createLogger } from '../logger';
|
|
|
|
const logger = createLogger('meilisearch_hook');
|
|
|
|
export default defineHook(async ({ init, filter, action }, { services, getSchema }) => {
|
|
init('app.after', async () => {
|
|
logger.info('Directus App Started - MeiliSearch Hook Initialized');
|
|
const { CollectionsService } = services;
|
|
const schema = await getSchema();
|
|
const collectionsSvc = new CollectionsService({
|
|
schema,
|
|
});
|
|
|
|
// 检查meili_search_config集合是否存在
|
|
const meili_search_config_exists = await collectionsSvc.readOne('meili_search_config').then(() => true).catch(() => false);
|
|
if (!meili_search_config_exists) {
|
|
logger.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(() => {
|
|
logger.info('meili_search_config collection created successfully.');
|
|
}).catch((e) => {
|
|
logger.error('Error creating meili_search_config collection:', e);
|
|
});
|
|
} else {
|
|
logger.info('meili_index_config 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) {
|
|
logger.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(() => {
|
|
logger.info('meili_index_configs collection created successfully.');
|
|
}).catch((e) => {
|
|
logger.error('Error creating meili_index_configs collection:', e);
|
|
});
|
|
} else {
|
|
logger.info('meili_index_configs collection already exists.');
|
|
}
|
|
});
|
|
|
|
filter('items.create', () => {
|
|
logger.info('Creating Item!');
|
|
});
|
|
|
|
action('items.create', () => {
|
|
logger.info('Item created!');
|
|
});
|
|
});
|