-
Notifications
You must be signed in to change notification settings - Fork 982
/
Copy pathdatabase-update.ts
81 lines (75 loc) · 2.95 KB
/
database-update.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
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
import { URL } from "url";
import * as clc from "colorette";
import * as fs from "fs";
import { Client } from "../apiv2";
import { Command } from "../command";
import { Emulators } from "../emulator/types";
import { FirebaseError } from "../error";
import { populateInstanceDetails } from "../management/database";
import { printNoticeIfEmulated } from "../emulator/commandUtils";
import { promptOnce } from "../prompt";
import { realtimeOriginOrEmulatorOrCustomUrl } from "../database/api";
import { requirePermissions } from "../requirePermissions";
import { logger } from "../logger";
import { requireDatabaseInstance } from "../requireDatabaseInstance";
import * as utils from "../utils";
export const command = new Command("database:update <path> [infile]")
.description("update some of the keys for the defined path in your Realtime Database")
.option("-d, --data <data>", "specify escaped JSON directly")
.option("-f, --force", "pass this option to bypass confirmation prompt")
.option(
"--instance <instance>",
"use the database <instance>.firebaseio.com (if omitted, use default database instance)",
)
.option("--disable-triggers", "suppress any Cloud functions triggered by this operation")
.before(requirePermissions, ["firebasedatabase.instances.update"])
.before(requireDatabaseInstance)
.before(populateInstanceDetails)
.before(printNoticeIfEmulated, Emulators.DATABASE)
.action(async (path: string, infile: string | undefined, options) => {
if (!path.startsWith("/")) {
throw new FirebaseError("Path must begin with /");
}
const origin = realtimeOriginOrEmulatorOrCustomUrl(options.instanceDetails.databaseUrl);
const url = utils.getDatabaseUrl(origin, options.instance, path);
const confirmed = await promptOnce(
{
type: "confirm",
name: "force",
default: false,
message: `You are about to modify data at ${clc.cyan(url)}. Are you sure?`,
},
options,
);
if (!confirmed) {
throw new FirebaseError("Command aborted.");
}
const inStream =
utils.stringToStream(options.data) ||
(infile && fs.createReadStream(infile)) ||
process.stdin;
const jsonUrl = new URL(utils.getDatabaseUrl(origin, options.instance, path + ".json"));
if (options.disableTriggers) {
jsonUrl.searchParams.set("disableTriggers", "true");
}
if (!infile && !options.data) {
utils.explainStdin();
}
const c = new Client({ urlPrefix: jsonUrl.origin, auth: true });
try {
await c.request({
method: "PATCH",
path: jsonUrl.pathname,
body: inStream,
queryParams: jsonUrl.searchParams,
});
} catch (err: unknown) {
throw new FirebaseError("Unexpected error while setting data");
}
utils.logSuccess("Data updated successfully");
logger.info();
logger.info(
clc.bold("View data at:"),
utils.getDatabaseViewDataUrl(origin, options.project, options.instance, path),
);
});