-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathindex.mjs
More file actions
65 lines (49 loc) · 1.24 KB
/
Copy pathindex.mjs
File metadata and controls
65 lines (49 loc) · 1.24 KB
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
#!/usr/bin/env node
import { getDoc, setDoc } from "@junobuild/core";
import { getIdentity } from "./auth.mjs";
import { readFile } from "fs/promises";
const identity = getIdentity();
const readData = async () => {
const buffer = await readFile(process.env.DATA_SRC);
return JSON.parse(buffer.toString("utf-8"));
};
const satellite = {
identity,
satelliteId: process.env.JUNO_SATELLITE_ID,
};
const collection = process.env.JUNO_DATASTORE_COLLECTION;
const keyField = process.env.DATA_KEY_FIELD;
const get = async (key) =>
getDoc({
collection,
key,
satellite,
});
const set = async (entry) => {
const key = entry[keyField];
console.log(`Set key ${key}: start`);
const existingEntry = await get(key);
await setDoc({
collection,
satellite,
doc: {
...(existingEntry !== undefined && existingEntry),
key,
data: entry,
},
});
console.log(`Set key ${key}: end`);
};
const loadData = async () => {
const data = await readData();
const promises = data.map((entry) => set(entry));
await Promise.all(promises);
};
await loadData();
// Uncomment to list documents
// console.log("List:", await listDocs({
// collection,
// satellite,
// filter: {}
// }))
console.log("Done.");