-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathimport.mjs
More file actions
48 lines (37 loc) · 1.2 KB
/
import.mjs
File metadata and controls
48 lines (37 loc) · 1.2 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
import { setManyDocs } from "@junobuild/core";
import { readFile } from "node:fs/promises";
import { jsonReviver } from "@dfinity/utils";
import * as dotenv from "dotenv";
import { defineRun } from "@junobuild/config";
dotenv.config({ quiet: true });
const collection = process.env.JUNO_DATASTORE_COLLECTION;
const inputFile = process.env.DATA_SRC;
const readData = async () => {
const json = await readFile(inputFile, "utf-8");
return JSON.parse(json, jsonReviver);
};
export const onRun = defineRun(() => ({
run: async ({ identity, ...rest }) => {
const { items } = await readData();
console.log(
`⚠️ The documents will be imported using ${identity.getPrincipal().toText()} as owner.`,
);
console.log(`${items.length} documents to import`);
const limit = 10;
for (let i = 0; i < items.length; i = i + limit) {
const batch = items.slice(i, i + limit);
console.log(`Importing documents ${i + 1}/${i + batch.length}...`);
await setManyDocs({
docs: batch.map((doc) => ({
collection,
doc,
})),
satellite: {
identity,
...rest,
},
});
}
console.log(`Data imported`);
},
}));