forked from nasa-gcn/architect-plugin-search
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.ts
54 lines (49 loc) · 1.61 KB
/
data.ts
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
/*!
* Copyright © 2023 United States Government as represented by the
* Administrator of the National Aeronautics and Space Administration.
* All Rights Reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
import { readFile } from 'node:fs/promises'
import { join } from 'node:path'
import { pathToFileURL } from 'node:url'
import { Client } from '@opensearch-project/opensearch'
import type { ClientOptions } from '@opensearch-project/opensearch'
import { exists } from './paths.js'
import chunk from 'lodash/chunk.js'
const jsonFilename = 'sandbox-search.json'
const jsFilename = 'sandbox-search.js'
async function getData(path: string): Promise<object[]> {
let result
const jsonPath = join(path, jsonFilename)
const jsPath = join(path, jsFilename)
if (await exists(jsonPath)) {
console.log(`Loading search records from ${jsonPath}`)
result = JSON.parse(await readFile(jsonPath, { encoding: 'utf-8' }))
} else if (await exists(jsPath)) {
console.log(`Loading search records from ${jsPath}`)
result = (await import(pathToFileURL(jsPath).toString())).default
if (typeof result === 'function') {
result = result()
}
if (result instanceof Promise) {
result = await result
}
}
if (result) {
console.log(`Loaded ${result.length} search records`)
}
return result
}
export async function populate(path: string, opts: ClientOptions) {
const data = await getData(path)
if (data) {
const client = new Client(opts)
const batch_size = 10
const batches = chunk(data, batch_size)
for (const batch of batches) {
await client.bulk({ body: batch })
}
}
}