Skip to content

Commit

Permalink
refactor: 💡 index delete use delete if exists
Browse files Browse the repository at this point in the history
  • Loading branch information
heldrida committed May 31, 2024
1 parent 75cc4c7 commit 9b0f4c0
Showing 1 changed file with 5 additions and 17 deletions.
22 changes: 5 additions & 17 deletions Services/Search/Indexer/indexDelete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,6 @@ if (!apiKey || !host) {
process.exit(1);
}

const sleep = (timeout: number) => {
return new Promise((resolve) => {
setTimeout(() => resolve(true), timeout);
});
}

async function waitForTask(client: MeiliSearch, id: number) {
while (true) {
const status = await client.getTask(id);
if (status.error) throw new Error(status.error.message);
if (status.status !== 'processing') break;
await sleep(1000);
}
}

const client = new MeiliSearch({
host,
apiKey,
Expand All @@ -41,8 +26,11 @@ const deleteIndex = async ({
indexName: string;
}) => {
try {
const task = await client.deleteIndex(indexName);
await waitForTask(client, task.taskUid);
const hasDelete = await client.deleteIndexIfExists(indexName);

if (!hasDelete) {
console.log(`🤡 Index ${indexName} doesn't exist!`);
}

console.log(`✅ Deleted index ${indexName}`);
} catch (e) {
Expand Down

0 comments on commit 9b0f4c0

Please sign in to comment.