The Node.js® Website
1import { siteContent } from './get-documents.mjs';
2import { ORAMA_SYNC_BATCH_SIZE } from '../../next.constants.mjs';
3
4// The following follows the instructions at https://docs.oramasearch.com/cloud/data-sources/custom-integrations/webhooks
5
6const INDEX_ID = process.env.ORAMA_INDEX_ID;
7const API_KEY = process.env.ORAMA_SECRET_KEY;
8const ORAMA_API_BASE_URL = `https://api.oramasearch.com/api/v1/webhooks/${INDEX_ID}`;
9
10const oramaHeaders = {
11 'Content-Type': 'application/json',
12 Authorization: `Bearer ${API_KEY}`,
13};
14
15// Orama allows to send several documents at once, so we batch them in groups of 50.
16// This is not strictly necessary, but it makes the process faster.
17const runUpdate = async () => {
18 const batchSize = ORAMA_SYNC_BATCH_SIZE;
19 const batches = [];
20
21 for (let i = 0; i < siteContent.length; i += batchSize) {
22 batches.push(siteContent.slice(i, i + batchSize));
23 }
24
25 await Promise.all(batches.map(insertBatch));
26};
27
28// We call the "notify" API to upsert the documents in the index.
29// Orama will keep a queue of all the documents we send, and will process them once we call the "deploy" API.
30// Full docs on the "notify" API: https://docs.oramasearch.com/cloud/data-sources/custom-integrations/webhooks#updating-removing-inserting-elements-in-a-live-index
31const insertBatch = async batch =>
32 await fetch(`${ORAMA_API_BASE_URL}/notify`, {
33 method: 'POST',
34 headers: oramaHeaders,
35 body: JSON.stringify({ upsert: batch }),
36 });
37
38// We call the "deploy" API to trigger a deployment of the index, which will process all the documents in the queue.
39// Full docs on the "deploy" API: https://docs.oramasearch.com/cloud/data-sources/custom-integrations/webhooks#deploying-the-index
40const triggerDeployment = async () =>
41 await fetch(`${ORAMA_API_BASE_URL}/deploy`, {
42 method: 'POST',
43 headers: oramaHeaders,
44 });
45
46// We call the "snapshot" API to empty the index before inserting the new documents.
47// The "snapshot" API is typically used to replace the entire index with a fresh set of documents, but we use it here to empty the index.
48// This operation gets queued, so the live index will still be available until we call the "deploy" API and redeploy the index.
49// Full docs on the "snapshot" API: https://docs.oramasearch.com/cloud/data-sources/custom-integrations/webhooks#inserting-a-snapshot
50const emptyOramaIndex = async () =>
51 await fetch(`${ORAMA_API_BASE_URL}/snapshot`, {
52 method: 'POST',
53 headers: oramaHeaders,
54 body: JSON.stringify([]),
55 });
56
57// Now we proceed to call the APIs in order:
58// 1. Empty the index
59// 2. Insert the documents
60// 3. Trigger a deployment
61// Once all these steps are done, the new documents will be available in the live index.
62// Allow Orama up to 1 minute to distribute the documents to all the 300+ nodes worldwide.
63await emptyOramaIndex();
64await runUpdate();
65await triggerDeployment();