-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate.mts
More file actions
101 lines (100 loc) · 3.28 KB
/
update.mts
File metadata and controls
101 lines (100 loc) · 3.28 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* TODO: this is buggy */
/*
//cspell:disable
import ora from "ora";
import { Options } from "commander";
import { existsSync } from "fs";
import { resolve } from "path";
import { validateFileInput } from "./utils/validate-file-input.mjs";
// import { formatData } from "./utils/format-data.mjs";
import { CLI_LOG } from "./utils/logging.mjs";
import { authenticateFirestore } from "./auth/authenticate-firestore.mjs";
export default async (
collection: string,
data: string,
documentIds: string[],
options: Options,
) => {
if (documentIds.length > 1 && !options.bulk)
throw new Error(
"Multiple IDs should only be provided in conjunction with the --bulk flag",
);
if (!options.file && !data)
throw new Error(
"Must provide data or a file containing the data to update document.",
);
const spinner = ora("Updating document(s) in " + collection + "\n").start();
let parsedData = null;
try {
const db = await authenticateFirestore(options);
let dataToUpdate: any = null;
if (options.file) {
const inputFile = options.file;
if (!existsSync(inputFile)) {
throw new Error(
"Invalid file path for the --file option: " + inputFile,
);
}
if (!options.fileType || options.fileType.toUpperCase() === "JSON") {
TODO: change to readFile from 'fs/promises'
({ default: dataToUpdate } = await import(resolve(inputFile), {
with: { type: "json" },
}));
} else {
}
if (!options.bulk && Object.keys(dataToUpdate).length > 1)
throw new Error(
"Invalid data format: For update operations without the --bulk flag, the data must be a single object, not an array.",
);
validateFileInput(dataToUpdate);
} else {
parsedData = JSON.parse(data);
if (options.bulk) {
if (!Array.isArray(parsedData))
throw new Error(
"Invalid data format: The data provided with the --bulk flag must be in array format for JSON/YAML or tabular format for CSV. Ensure your input is properly structured.",
);
dataToUpdate = formatData(parsedData, documentIds);
}
}
if (options.bulk) {
const batch = db.batch();
for (const [documentId, data] of Object.entries(dataToUpdate)) {
const ref = db.collection(collection).doc(documentId);
batch.set(
ref,
data,
options.overwrite ? { merge: false } : { merge: true },
);
}
try {
await batch.commit();
} catch (e) {
throw new Error("Failed to add new documents: " + e);
}
} else {
if (Array.isArray(parsedData)) {
throw new Error(
"Invalid data format: For update operations without the --bulk flag, the data must be a single object, not an array.",
);
}
if (documentIds.length > 1)
throw new Error(
"Number of IDs provided must be one or use the --bulk flag.",
);
await db
.collection(collection)
.doc(documentIds[0])
.set(
parsedData,
options.overwrite ? { merge: false } : { merge: true },
);
}
spinner.succeed("Done!");
} catch (e) {
spinner.fail("Failed to update documents!");
CLI_LOG(e.message, "error");
process.exitCode = 1;
}
};
*/