feat(hook): 添加init hook
- 添加init hook用于检查与创建meilisearch相关集合
This commit is contained in:
@ -1,14 +1,47 @@
|
|||||||
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 }) => {
|
||||||
|
init('app.after', async () => {
|
||||||
|
console.log('Directus App Started - MeiliSearch Hook Initialized');
|
||||||
const { CollectionsService } = services;
|
const { CollectionsService } = services;
|
||||||
const collectionsService = new CollectionsService({ schema: await getSchema() });
|
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集合是否存在
|
||||||
|
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',
|
collection: 'meili_index_configs',
|
||||||
meta: {
|
meta: {
|
||||||
note: '配置哪些集合需要被索引到 MeiliSearch',
|
note: '配置哪些集合需要被索引到 MeiliSearch',
|
||||||
@ -30,10 +63,11 @@ export default defineHook(async ({ filter, action }, { services, getSchema }) =>
|
|||||||
console.log('meili_index_configs collection created successfully.');
|
console.log('meili_index_configs collection created successfully.');
|
||||||
}).catch((e) => {
|
}).catch((e) => {
|
||||||
console.error('Error creating meili_index_configs collection:', e);
|
console.error('Error creating meili_index_configs collection:', e);
|
||||||
})
|
});
|
||||||
} else {
|
} else {
|
||||||
console.log('meili_index_configs collection already exists.');
|
console.log('meili_index_configs collection already exists.');
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
filter('items.create', () => {
|
filter('items.create', () => {
|
||||||
console.log('Creating Item!');
|
console.log('Creating Item!');
|
||||||
|
|||||||
Reference in New Issue
Block a user