|
| 1 | +// @ts-check |
| 2 | + |
| 3 | +import { container } from "../../config/container.mjs"; |
| 4 | +import { EXPORT_TERMINAL_STATES } from "../../lib/account-api.mjs"; |
| 5 | +import { ValidationError } from "../../lib/errors.mjs"; |
| 6 | +import { colorize, Format } from "../../lib/formatting/colorize.mjs"; |
| 7 | +import { DATABASE_PATH_OPTIONS } from "../../lib/options.mjs"; |
| 8 | +import { WAIT_OPTIONS, waitUntilExportIsReady } from "./wait.mjs"; |
| 9 | + |
| 10 | +async function createS3Export(argv) { |
| 11 | + const { |
| 12 | + database, |
| 13 | + path, |
| 14 | + bucket, |
| 15 | + format, |
| 16 | + json, |
| 17 | + color, |
| 18 | + collection: collections, |
| 19 | + wait, |
| 20 | + maxWait, |
| 21 | + quiet, |
| 22 | + } = argv; |
| 23 | + const logger = container.resolve("logger"); |
| 24 | + const { createExport } = container.resolve("accountAPI"); |
| 25 | + |
| 26 | + let createdExport = await createExport({ |
| 27 | + database, |
| 28 | + collections, |
| 29 | + destination: { |
| 30 | + s3: { |
| 31 | + bucket, |
| 32 | + path, |
| 33 | + }, |
| 34 | + }, |
| 35 | + format, |
| 36 | + }); |
| 37 | + |
| 38 | + if (wait && !EXPORT_TERMINAL_STATES.includes(createdExport.state)) { |
| 39 | + createdExport = await waitUntilExportIsReady({ |
| 40 | + id: createdExport.id, |
| 41 | + opts: { |
| 42 | + maxWait, |
| 43 | + quiet, |
| 44 | + }, |
| 45 | + }); |
| 46 | + } |
| 47 | + |
| 48 | + if (json) { |
| 49 | + logger.stdout(colorize(createdExport, { color, format: Format.JSON })); |
| 50 | + } else { |
| 51 | + logger.stdout(colorize(createdExport, { color, format: Format.YAML })); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +const sharedExamples = [ |
| 56 | + [ |
| 57 | + "$0 export create s3 --database us/my_db --bucket my-bucket --path exports/my_db", |
| 58 | + "Export the 'us-std/my_db' database to the 'exports/my_db' path of the 'my-bucket' S3 bucket. Outputs the export ID.", |
| 59 | + ], |
| 60 | + [ |
| 61 | + "$0 export create s3 --database us/my_db --bucket my-bucket --path my-prefix --json", |
| 62 | + "Output the full JSON of the export request.", |
| 63 | + ], |
| 64 | + [ |
| 65 | + "$0 export create s3 --database us/my_db --bucket my-bucket --path my-prefix --collection my-collection", |
| 66 | + "Export the 'my-collection' collection only.", |
| 67 | + ], |
| 68 | + [ |
| 69 | + "$0 export create s3 --database us/my_db --bucket my-bucket --path my-prefix --format tagged", |
| 70 | + "Encode the export's document data using the 'tagged' format.", |
| 71 | + ], |
| 72 | + [ |
| 73 | + "$0 export create s3 --database us/my_db --bucket my-bucket --path my-prefix --wait --max-wait 180", |
| 74 | + "Wait for the export to complete or fail before exiting. Waits up to 180 minutes.", |
| 75 | + ], |
| 76 | +]; |
| 77 | + |
| 78 | +function buildCreateS3ExportCommand(yargs) { |
| 79 | + return yargs |
| 80 | + .options({ |
| 81 | + bucket: { |
| 82 | + type: "string", |
| 83 | + required: true, |
| 84 | + description: "Name of the S3 bucket where the export will be stored.", |
| 85 | + group: "API:", |
| 86 | + }, |
| 87 | + path: { |
| 88 | + type: "string", |
| 89 | + required: true, |
| 90 | + description: |
| 91 | + "Path prefix for the S3 bucket. Separate subfolders using a slash (`/`).", |
| 92 | + group: "API:", |
| 93 | + }, |
| 94 | + format: { |
| 95 | + type: "string", |
| 96 | + required: true, |
| 97 | + description: |
| 98 | + "Data format used to encode the exported FQL document data as JSON.", |
| 99 | + choices: ["simple", "tagged"], |
| 100 | + default: "simple", |
| 101 | + group: "API:", |
| 102 | + }, |
| 103 | + }) |
| 104 | + .options(WAIT_OPTIONS) |
| 105 | + .check((argv) => { |
| 106 | + if (!argv.database) { |
| 107 | + throw new ValidationError( |
| 108 | + "--database is required to create an export.", |
| 109 | + ); |
| 110 | + } |
| 111 | + |
| 112 | + return true; |
| 113 | + }) |
| 114 | + .example(sharedExamples); |
| 115 | +} |
| 116 | + |
| 117 | +function buildCreateCommand(yargs) { |
| 118 | + return yargs |
| 119 | + .options(DATABASE_PATH_OPTIONS) |
| 120 | + .options({ |
| 121 | + collection: { |
| 122 | + type: "array", |
| 123 | + required: false, |
| 124 | + description: |
| 125 | + "Used-defined collections to export. Pass values as a space-separated list. If omitted, all user-defined collections are exported.", |
| 126 | + default: [], |
| 127 | + group: "API:", |
| 128 | + }, |
| 129 | + }) |
| 130 | + .command({ |
| 131 | + command: "s3", |
| 132 | + description: "Export to an S3 bucket.", |
| 133 | + builder: buildCreateS3ExportCommand, |
| 134 | + handler: createS3Export, |
| 135 | + }) |
| 136 | + .example(sharedExamples) |
| 137 | + .demandCommand(); |
| 138 | +} |
| 139 | + |
| 140 | +export default { |
| 141 | + command: "create <destination-type>", |
| 142 | + description: |
| 143 | + "Start the export of a database or collections. Outputs the export ID.", |
| 144 | + builder: buildCreateCommand, |
| 145 | +}; |
0 commit comments