Usage example
import MeiliSearch from 'meilisearch';
import { waitForTask } from './wait-for-task';
export type InitIndexOptions = {
client: MeiliSearch;
indexName: string;
};
export async function initIndex({ client, indexName }: InitIndexOptions) {
try {
return await client.getIndex(indexName);
} catch (error) {
const task = await client.createIndex(indexName, {
primaryKey: 'id',
});
await waitForTask({
client,
taskUid: task.taskUid,
customErrorMessage: 'Failed to create index',
});
return await client.getIndex(indexName);
}
}