-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathalgolia.js
73 lines (55 loc) · 1.54 KB
/
algolia.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
require('dotenv').config()
import AlgoliaSearch from 'algoliasearch'
const { ALGOLIA_APP_ID, ALGOLIA_ADMIN_KEY } = process.env
let client = null
const indices = {}
function init(indexName) {
if (!client) client = AlgoliaSearch(ALGOLIA_APP_ID, ALGOLIA_ADMIN_KEY)
// Create an algolia index
const index = client.initIndex(indexName)
// Store index in list of indices to use later during save operations
indices[indexName] = index
return index
}
function write(indexName, records, projectName) {
const index = indices[indexName]
return index
.addObjects(records)
.then(() =>
console.log(
`ALGOLIA:: Successfully added ${records.length} records to ${indexName}`
)
)
.catch(err =>
console.error(
`ALGOLIA:: Error adding ${records.length} records to ${indexName}`,
err
)
)
}
function clear(indexName) {
const index = indices[indexName]
return index.clearIndex()
}
async function getPreviouslyIndexedVersions(projectName) {
let hits
try {
;({ hits } = await indices['versions'].search(projectName))
} catch (err) {
console.error(err)
}
if (!hits) {
return []
}
let hit = hits.filter(hit => (hit.name === projectName) && hit.index_date_timestamp)
.sort(function (a, b) {
return a.index_date_timestamp - b.index_date_timestamp;
})
.pop()
if (!hit) {
return []
}
let { versions } = hit
return versions
}
export default { init, write, clear, getPreviouslyIndexedVersions }