|
1 | 1 | <!DOCTYPE html> |
2 | 2 | <html> |
| 3 | + |
3 | 4 | <head> |
4 | 5 | <meta charset='utf-8'> |
5 | 6 | <meta http-equiv='X-UA-Compatible' content='IE=edge'> |
6 | 7 | <title>Page Title</title> |
7 | 8 | <meta name='viewport' content='width=device-width, initial-scale=1'> |
8 | 9 | </head> |
| 10 | + |
9 | 11 | <body> |
10 | | - Indexes: |
11 | | - <div id="indexes"></div> |
12 | 12 | </body> |
| 13 | + |
13 | 14 | </html> |
14 | | -<script src="../../../dist/bundles/meilisearch.umd.js"></script> |
| 15 | +<script src="meilisearch.umd.js"></script> |
15 | 16 | <script> |
16 | | - const client = new window.MeiliSearch({ |
17 | | - host: 'http://127.0.0.1:7700', |
18 | | - apiKey: 'masterKey', |
19 | | - }) |
20 | | - client.getIndexes().then(res => { |
21 | | - console.log({ res }); |
22 | | - res.map(index => document.querySelector('#indexes').innerHTML = `<div>${index.name}</div>`) |
23 | | - }) |
| 17 | + ; (async () => { |
| 18 | + |
| 19 | + try { |
| 20 | + const UID = "testIndex" |
| 21 | + |
| 22 | + // also works with window.MeiliSearch |
| 23 | + |
| 24 | + const client = new window.MeiliSearch({ |
| 25 | + host: 'http://127.0.0.1:7700', |
| 26 | + apiKey: 'masterKey', |
| 27 | + }) |
| 28 | + const task = await client.createIndex(UID) |
| 29 | + await client.waitForTask(task.uid) |
| 30 | + |
| 31 | + const documentTask = await client.index(UID).addDocuments([{ id: 1, title: "wonder woman" }]) |
| 32 | + await client.waitForTask(documentTask.uid) |
| 33 | + |
| 34 | + const index = await client.index(UID).getRawInfo() |
| 35 | + |
| 36 | + // Create dynamic element to wait on with puppeteer |
| 37 | + const indexDiv = document.createElement("div"); |
| 38 | + indexDiv.setAttribute("id", "indexes"); |
| 39 | + indexDiv.innerHTML = index.uid |
| 40 | + document.body.insertBefore(indexDiv, document.querySelector("#content")); |
| 41 | + |
| 42 | + const searchDiv = document.createElement("div"); |
| 43 | + searchDiv.setAttribute("id", "search"); |
| 44 | + const search = await client.index(UID).search() |
| 45 | + searchDiv.innerHTML = JSON.stringify(search) |
| 46 | + document.body.insertBefore(searchDiv, document.querySelector("#content")); |
| 47 | + |
| 48 | + const deleteTask = await client.index(UID).delete() |
| 49 | + await client.waitForTask(deleteTask.uid) |
| 50 | + } catch (e) { |
| 51 | + console.error(e); |
| 52 | + } |
| 53 | + })() |
| 54 | + |
| 55 | + |
24 | 56 | </script> |
0 commit comments