feat(hook): 添加init hook
- 添加init hook用于检查与创建meilisearch相关集合
This commit is contained in:
@ -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!');
|
||||
|
||||
Reference in New Issue
Block a user