neon 2.47.0 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (198) hide show
  1. package/README.md +54 -0
  2. package/dist/_shared/auth_selection.js +76 -79
  3. package/dist/_shared/credentials.js +166 -187
  4. package/dist/_shared/env-core/env.js +395 -0
  5. package/dist/_shared/env-core/reuse-secrets.js +179 -0
  6. package/dist/_shared/paths.js +129 -126
  7. package/dist/_shared/profiles.js +192 -242
  8. package/dist/_shared/secure_file.js +36 -38
  9. package/dist/_virtual/_rolldown/runtime.js +13 -0
  10. package/dist/analytics.js +163 -207
  11. package/dist/api.js +815 -758
  12. package/dist/auth.js +121 -141
  13. package/dist/auth_context.js +39 -53
  14. package/dist/cli.js +4 -7
  15. package/dist/commands/api.js +220 -250
  16. package/dist/commands/api_keys.js +251 -314
  17. package/dist/commands/auth.js +283 -328
  18. package/dist/commands/bootstrap.js +372 -437
  19. package/dist/commands/branches.js +304 -455
  20. package/dist/commands/bucket.js +374 -514
  21. package/dist/commands/checkout.js +213 -298
  22. package/dist/commands/config.js +575 -658
  23. package/dist/commands/connection_string.js +137 -165
  24. package/dist/commands/data_api.js +238 -260
  25. package/dist/commands/databases.js +67 -76
  26. package/dist/commands/deploy.js +31 -25
  27. package/dist/commands/dev.js +642 -681
  28. package/dist/commands/diff.js +156 -200
  29. package/dist/commands/env.js +243 -303
  30. package/dist/commands/functions.js +275 -341
  31. package/dist/commands/index.js +70 -65
  32. package/dist/commands/init.js +84 -119
  33. package/dist/commands/inspect.js +55 -55
  34. package/dist/commands/ip_allow.js +88 -120
  35. package/dist/commands/link.js +874 -1019
  36. package/dist/commands/logs.js +291 -0
  37. package/dist/commands/neon_auth.js +725 -933
  38. package/dist/commands/operations.js +34 -25
  39. package/dist/commands/orgs.js +28 -18
  40. package/dist/commands/profile.js +614 -845
  41. package/dist/commands/projects.js +313 -373
  42. package/dist/commands/psql.js +60 -58
  43. package/dist/commands/roles.js +55 -58
  44. package/dist/commands/schema_diff.js +87 -131
  45. package/dist/commands/set_context.js +34 -26
  46. package/dist/commands/snapshots.js +288 -413
  47. package/dist/commands/status.js +41 -37
  48. package/dist/commands/user.js +21 -10
  49. package/dist/commands/vpc_endpoints.js +85 -113
  50. package/dist/config.js +7 -6
  51. package/dist/config_format.js +50 -66
  52. package/dist/config_template.js +128 -157
  53. package/dist/context.js +183 -235
  54. package/dist/current_branch_fast_path.js +40 -49
  55. package/dist/dev/env.js +313 -394
  56. package/dist/dev/functions.js +54 -64
  57. package/dist/dev/inputs.js +46 -58
  58. package/dist/dev/runtime.js +135 -164
  59. package/dist/dev/websocket.js +766 -959
  60. package/dist/env.js +27 -33
  61. package/dist/env_file.js +118 -132
  62. package/dist/env_services.js +36 -38
  63. package/dist/errors.js +57 -68
  64. package/dist/functions_api.js +45 -43
  65. package/dist/help.js +189 -140
  66. package/dist/index.js +182 -257
  67. package/dist/init/agents.js +137 -118
  68. package/dist/init/auth.js +58 -68
  69. package/dist/init/bootstrap.js +325 -396
  70. package/dist/init/build_config.js +4 -2
  71. package/dist/init/detect_agent.js +56 -101
  72. package/dist/init/editors.js +35 -52
  73. package/dist/init/enrich_output.js +51 -66
  74. package/dist/init/extension.js +134 -171
  75. package/dist/init/inspect.js +179 -266
  76. package/dist/init/interactive.js +510 -622
  77. package/dist/init/neonctl.js +117 -168
  78. package/dist/init/orchestrate.js +157 -173
  79. package/dist/init/phases/auth.js +188 -202
  80. package/dist/init/phases/cleanup.js +23 -23
  81. package/dist/init/phases/db.js +251 -277
  82. package/dist/init/phases/getting_started.js +213 -223
  83. package/dist/init/phases/mcp.js +174 -224
  84. package/dist/init/phases/migrations.js +247 -248
  85. package/dist/init/phases/neon_auth.js +114 -133
  86. package/dist/init/phases/setup.js +546 -703
  87. package/dist/init/phases/skills.js +75 -86
  88. package/dist/init/phases/status.js +72 -67
  89. package/dist/init/resolve_context.js +102 -99
  90. package/dist/init/route_command.js +91 -98
  91. package/dist/init/skills.js +174 -218
  92. package/dist/init/vsix.js +77 -99
  93. package/dist/log.js +17 -16
  94. package/dist/neon_services.js +104 -129
  95. package/dist/parameters.gen.js +481 -471
  96. package/dist/pkg.js +17 -19
  97. package/dist/profile_keys.js +44 -47
  98. package/dist/psql/cli.js +44 -47
  99. package/dist/psql/command/cmd_cond.js +231 -406
  100. package/dist/psql/command/cmd_connect.js +557 -764
  101. package/dist/psql/command/cmd_copy.js +727 -983
  102. package/dist/psql/command/cmd_describe.js +1499 -1688
  103. package/dist/psql/command/cmd_format.js +733 -905
  104. package/dist/psql/command/cmd_io.js +1293 -2082
  105. package/dist/psql/command/cmd_lo.js +297 -359
  106. package/dist/psql/command/cmd_meta.js +727 -878
  107. package/dist/psql/command/cmd_misc.js +138 -172
  108. package/dist/psql/command/cmd_pipeline.js +547 -1099
  109. package/dist/psql/command/cmd_restrict.js +119 -155
  110. package/dist/psql/command/cmd_show.js +529 -688
  111. package/dist/psql/command/dispatch.js +261 -325
  112. package/dist/psql/command/inputQueue.js +35 -33
  113. package/dist/psql/command/shared.js +49 -63
  114. package/dist/psql/complete/filenames.js +90 -133
  115. package/dist/psql/complete/index.js +59 -97
  116. package/dist/psql/complete/matcher.js +236 -300
  117. package/dist/psql/complete/psqlVars.js +218 -223
  118. package/dist/psql/complete/queries.js +159 -177
  119. package/dist/psql/complete/rules.js +1493 -2299
  120. package/dist/psql/core/common.js +762 -1180
  121. package/dist/psql/core/help.js +456 -546
  122. package/dist/psql/core/mainloop.js +692 -1302
  123. package/dist/psql/core/prompt.js +391 -408
  124. package/dist/psql/core/settings.js +429 -644
  125. package/dist/psql/core/sqlHelp.js +480 -554
  126. package/dist/psql/core/startup.js +626 -815
  127. package/dist/psql/core/syncVars.js +67 -110
  128. package/dist/psql/core/variables.js +156 -278
  129. package/dist/psql/describe/formatters.js +884 -1285
  130. package/dist/psql/describe/processNamePattern.js +173 -260
  131. package/dist/psql/describe/queries.js +1368 -2403
  132. package/dist/psql/describe/versionGate.js +32 -41
  133. package/dist/psql/index.js +1414 -1927
  134. package/dist/psql/io/history.js +232 -271
  135. package/dist/psql/io/input.js +103 -108
  136. package/dist/psql/io/lineEditor/buffer.js +238 -319
  137. package/dist/psql/io/lineEditor/complete.js +135 -213
  138. package/dist/psql/io/lineEditor/filename.js +139 -148
  139. package/dist/psql/io/lineEditor/index.js +653 -870
  140. package/dist/psql/io/lineEditor/keymap.js +544 -702
  141. package/dist/psql/io/lineEditor/vt100.js +294 -341
  142. package/dist/psql/io/pgpass.js +158 -187
  143. package/dist/psql/io/pgservice.js +146 -183
  144. package/dist/psql/io/psqlrc.js +328 -403
  145. package/dist/psql/print/aligned.js +1020 -1683
  146. package/dist/psql/print/asciidoc.js +180 -214
  147. package/dist/psql/print/crosstab.js +281 -442
  148. package/dist/psql/print/csv.js +48 -70
  149. package/dist/psql/print/html.js +195 -226
  150. package/dist/psql/print/json.js +75 -88
  151. package/dist/psql/print/latex.js +291 -364
  152. package/dist/psql/print/pager.js +171 -242
  153. package/dist/psql/print/troff.js +194 -226
  154. package/dist/psql/print/unaligned.js +69 -95
  155. package/dist/psql/print/units.js +167 -169
  156. package/dist/psql/scanner/slash.js +428 -483
  157. package/dist/psql/scanner/sql.js +445 -889
  158. package/dist/psql/scanner/stringutils.js +309 -379
  159. package/dist/psql/types/index.js +2 -7
  160. package/dist/psql/types/scanner.js +25 -22
  161. package/dist/psql/wire/connection.js +2042 -2803
  162. package/dist/psql/wire/copy.js +84 -100
  163. package/dist/psql/wire/notify.js +39 -59
  164. package/dist/psql/wire/pipeline.js +305 -518
  165. package/dist/psql/wire/protocol.js +349 -417
  166. package/dist/psql/wire/sasl.js +180 -265
  167. package/dist/psql/wire/tls.js +400 -561
  168. package/dist/storage_api.js +115 -129
  169. package/dist/test_utils/fixtures.js +94 -113
  170. package/dist/test_utils/oauth_server.js +10 -7
  171. package/dist/test_utils/project_dir.js +33 -0
  172. package/dist/utils/ai_gateway_notice.js +131 -162
  173. package/dist/utils/api_enums.js +21 -28
  174. package/dist/utils/auth.js +10 -4
  175. package/dist/utils/branch_notice.js +20 -19
  176. package/dist/utils/branch_picker.js +83 -89
  177. package/dist/utils/cli_name.js +15 -12
  178. package/dist/utils/compute_units.js +20 -27
  179. package/dist/utils/config_diff.js +127 -158
  180. package/dist/utils/enrichers.js +95 -148
  181. package/dist/utils/esbuild.js +133 -147
  182. package/dist/utils/flags.js +35 -47
  183. package/dist/utils/formats.js +8 -15
  184. package/dist/utils/git_diff.js +69 -80
  185. package/dist/utils/inspect_db.js +101 -143
  186. package/dist/utils/inspect_queries.js +179 -142
  187. package/dist/utils/middlewares.js +37 -44
  188. package/dist/utils/openapi.js +87 -99
  189. package/dist/utils/package_manager.js +312 -110
  190. package/dist/utils/point_in_time.js +49 -53
  191. package/dist/utils/psql.js +89 -106
  192. package/dist/utils/service_picker.js +55 -58
  193. package/dist/utils/string.js +5 -5
  194. package/dist/utils/ui.js +38 -55
  195. package/dist/utils/write_sync.js +26 -35
  196. package/dist/utils/zip.js +4 -3
  197. package/dist/writer.js +67 -87
  198. package/package.json +9 -7
@@ -1,455 +1,330 @@
1
- import { retryOnLock } from "../api.js";
1
+ import { __exportAll } from "../_virtual/_rolldown/runtime.js";
2
2
  import { log } from "../log.js";
3
- import { branchIdResolve, fillSingleProject, resolveBranchRef, } from "../utils/enrichers.js";
4
- import { looksLikeLSN, looksLikeTimestamp } from "../utils/formats.js";
3
+ import { retryOnLock } from "../api.js";
5
4
  import { writer } from "../writer.js";
5
+ import { looksLikeLSN, looksLikeTimestamp } from "../utils/formats.js";
6
+ import { branchIdResolve, fillSingleProject, resolveBranchRef } from "../utils/enrichers.js";
6
7
  import { BRANCH_FIELDS } from "./branches.js";
7
- export const SNAPSHOT_FIELDS = [
8
- "id",
9
- "name",
10
- "source_branch_id",
11
- "created_at",
12
- "expires_at",
8
+ //#region src/commands/snapshots.ts
9
+ var snapshots_exports = /* @__PURE__ */ __exportAll({
10
+ SNAPSHOT_FIELDS: () => SNAPSHOT_FIELDS,
11
+ aliases: () => aliases,
12
+ builder: () => builder,
13
+ command: () => command,
14
+ describe: () => describe,
15
+ handler: () => handler
16
+ });
17
+ const SNAPSHOT_FIELDS = [
18
+ "id",
19
+ "name",
20
+ "source_branch_id",
21
+ "created_at",
22
+ "expires_at"
13
23
  ];
14
24
  const SCHEDULE_FIELDS = [
15
- "frequency",
16
- "hour",
17
- "day",
18
- "month",
19
- "retention_seconds",
25
+ "frequency",
26
+ "hour",
27
+ "day",
28
+ "month",
29
+ "retention_seconds"
20
30
  ];
21
31
  const OPERATION_FIELDS = [
22
- "id",
23
- "action",
24
- "status",
32
+ "id",
33
+ "action",
34
+ "status"
25
35
  ];
26
- // The values the Neon API accepts for a backup-schedule entry's `frequency`
27
- // (per the OpenAPI `BackupScheduleItem` description). `satisfies` fails the
28
- // build if a value listed here leaves the SDK's `SnapshotFrequency` union, so
29
- // the CLI can never offer a frequency the API rejects.
30
36
  const SNAPSHOT_FREQUENCIES = [
31
- "daily",
32
- "weekly",
33
- "monthly",
37
+ "daily",
38
+ "weekly",
39
+ "monthly"
34
40
  ];
35
41
  /** Narrow an arbitrary string to a supported {@link SnapshotFrequency}. */
36
42
  const isSnapshotFrequency = (value) => SNAPSHOT_FREQUENCIES.some((frequency) => frequency === value);
37
- export const command = "snapshots";
38
- export const describe = "Manage snapshots";
39
- export const aliases = ["snapshot"];
40
- export const builder = (argv) => argv
41
- .usage("$0 snapshots <sub-command> [options]")
42
- .options({
43
- "project-id": {
44
- describe: "Project ID",
45
- type: "string",
46
- },
47
- })
48
- .middleware(fillSingleProject)
49
- .command("list", "List snapshots in the project", (yargs) => yargs, (args) => list(args))
50
- .command("get <id>", "Get a snapshot by id or name", (yargs) => yargs, (args) => get(args))
51
- .command("create", "Create a snapshot from a branch", (yargs) => yargs
52
- .options({
53
- branch: {
54
- alias: "b",
55
- describe: "Branch id or name to snapshot. Defaults to the branch in your context, or the project's default branch.",
56
- type: "string",
57
- },
58
- name: {
59
- describe: "A name for the snapshot",
60
- type: "string",
61
- },
62
- timestamp: {
63
- describe: "Take the snapshot at this point in time (RFC 3339, e.g. 2025-01-01T00:00:00Z). Must fall within the branch's restore window. Mutually exclusive with --lsn.",
64
- type: "string",
65
- },
66
- lsn: {
67
- describe: "Take the snapshot at this LSN (e.g. 0/1F3C8A0). Must fall within the branch's restore window. Mutually exclusive with --timestamp.",
68
- type: "string",
69
- },
70
- "expires-at": {
71
- describe: "When the snapshot is automatically deleted (RFC 3339, e.g. 2025-12-31T23:59:59Z). Omit to keep it indefinitely.",
72
- type: "string",
73
- },
74
- })
75
- .conflicts("timestamp", "lsn")
76
- .example([
77
- [
78
- "$0 snapshots create",
79
- "Snapshot the head of the context/default branch",
80
- ],
81
- [
82
- "$0 snapshots create --branch main --name pre-migration",
83
- "Snapshot the head of main with a name",
84
- ],
85
- [
86
- "$0 snapshots create --branch main --timestamp 2025-01-01T00:00:00Z",
87
- "Snapshot main at a point in time",
88
- ],
89
- [
90
- "$0 snapshots create --branch main --lsn 0/1F3C8A0 --expires-at 2025-12-31T23:59:59Z",
91
- "Snapshot main at an LSN, auto-deleting at the given time",
92
- ],
93
- ]), (args) => create(args))
94
- .command("update <id>", "Update a snapshot's name or expiration", (yargs) => yargs
95
- .options({
96
- name: {
97
- describe: "Rename the snapshot",
98
- type: "string",
99
- },
100
- "expires-at": {
101
- describe: "Set when the snapshot expires (RFC 3339). Mutually exclusive with --clear-expiration.",
102
- type: "string",
103
- },
104
- "clear-expiration": {
105
- describe: "Clear the expiration so the snapshot is kept indefinitely.",
106
- type: "boolean",
107
- },
108
- })
109
- .conflicts("expires-at", "clear-expiration"), (args) => update(args))
110
- .command("delete <id>", "Delete a snapshot by id or name", (yargs) => yargs, (args) => deleteSnapshot(args))
111
- .command("restore <id>", "Restore a snapshot into a branch", (yargs) => yargs
112
- .options({
113
- name: {
114
- describe: "Name for the newly restored branch. Auto-generated when omitted.",
115
- type: "string",
116
- },
117
- "target-branch": {
118
- describe: "Branch id or name to restore the snapshot onto. Defaults to the snapshot's source branch. Recommended when you intend to finalize (replace an existing branch).",
119
- type: "string",
120
- },
121
- finalize: {
122
- describe: "Finalize the restore immediately: move computes onto the restored branch and swap it in for the target. Without this, the restore is left un-finalized so you can inspect it first, then run `snapshots finalize <branch>`.",
123
- type: "boolean",
124
- default: false,
125
- },
126
- })
127
- .example([
128
- [
129
- "$0 snapshots restore snap-1234 --name recovered",
130
- "Restore a snapshot to a new branch named 'recovered'",
131
- ],
132
- [
133
- "$0 snapshots restore snap-1234 --target-branch main --finalize",
134
- "Restore onto main and swap it in immediately",
135
- ],
136
- [
137
- "$0 snapshots restore snap-1234 --target-branch main",
138
- "Restore onto main un-finalized to preview, then run `snapshots finalize`",
139
- ],
140
- ]), (args) => restore(args))
141
- .command("finalize <branch>", "Finalize a previewed snapshot restore (swap the restored branch in)", (yargs) => yargs.options({
142
- name: {
143
- describe: "Name to give the replaced (old) branch. Auto-generated when omitted.",
144
- type: "string",
145
- },
146
- }), (args) => finalize(args))
147
- .command("schedule", "Manage the automatic snapshot (backup) schedule of a branch", (yargs) => yargs
148
- .usage("$0 snapshots schedule <sub-command> [options]")
149
- .command("get", "Get a branch's automatic snapshot schedule", (yargs) => yargs.options({
150
- branch: {
151
- alias: "b",
152
- describe: "Branch id or name. Defaults to the branch in your context, or the project's default branch.",
153
- type: "string",
154
- },
155
- }), (args) => scheduleGet(args))
156
- .command("set", "Set a branch's automatic snapshot schedule", (yargs) => yargs
157
- .options({
158
- branch: {
159
- alias: "b",
160
- describe: "Branch id or name. Defaults to the branch in your context, or the project's default branch.",
161
- type: "string",
162
- },
163
- frequency: {
164
- describe: "How often to take snapshots. Builds a single-entry schedule together with --hour/--day/--month/--retention.",
165
- choices: SNAPSHOT_FREQUENCIES,
166
- type: "string",
167
- },
168
- hour: {
169
- describe: "Hour of the day (0-23) to take the snapshot (used with --frequency).",
170
- type: "number",
171
- },
172
- day: {
173
- describe: "Day of the week/month (1-31) to take the snapshot (used with --frequency).",
174
- type: "number",
175
- },
176
- month: {
177
- describe: "Month of the year (1-12) to take the snapshot (used with --frequency).",
178
- type: "number",
179
- },
180
- retention: {
181
- describe: "How long to keep each snapshot, in seconds (min 3600). Omit to keep indefinitely.",
182
- type: "number",
183
- },
184
- schedule: {
185
- describe: 'Full schedule as JSON, for multi-entry schedules, e.g. \'[{"frequency":"daily","hour":3,"retention_seconds":604800}]\'. Overrides the single-entry flags.',
186
- type: "string",
187
- },
188
- })
189
- .example([
190
- [
191
- "$0 snapshots schedule set --branch main --frequency daily --hour 3 --retention 604800",
192
- "A daily 03:00 snapshot kept for 7 days",
193
- ],
194
- [
195
- '$0 snapshots schedule set --branch main --schedule \'[{"frequency":"weekly","day":1,"hour":2},{"frequency":"daily","hour":3}]\'',
196
- "A multi-entry schedule via JSON",
197
- ],
198
- ]), (args) => scheduleSet(args))
199
- .demandCommand(1, "Specify `get` or `set`."), () => { })
200
- .demandCommand(1, "Specify a snapshots sub-command.");
201
- export const handler = (args) => {
202
- return args;
43
+ const command = "snapshots";
44
+ const describe = "Manage snapshots";
45
+ const aliases = ["snapshot"];
46
+ const builder = (argv) => argv.usage("$0 snapshots <sub-command> [options]").options({ "project-id": {
47
+ describe: "Project ID",
48
+ type: "string"
49
+ } }).middleware(fillSingleProject).command("list", "List snapshots in the project", (yargs) => yargs, (args) => list(args)).command("get <id>", "Get a snapshot by id or name", (yargs) => yargs, (args) => get(args)).command("create", "Create a snapshot from a branch", (yargs) => yargs.options({
50
+ branch: {
51
+ alias: "b",
52
+ describe: "Branch id or name to snapshot. Defaults to the branch in your context, or the project's default branch.",
53
+ type: "string"
54
+ },
55
+ name: {
56
+ describe: "A name for the snapshot",
57
+ type: "string"
58
+ },
59
+ timestamp: {
60
+ describe: "Take the snapshot at this point in time (RFC 3339, e.g. 2025-01-01T00:00:00Z). Must fall within the branch's restore window. Mutually exclusive with --lsn.",
61
+ type: "string"
62
+ },
63
+ lsn: {
64
+ describe: "Take the snapshot at this LSN (e.g. 0/1F3C8A0). Must fall within the branch's restore window. Mutually exclusive with --timestamp.",
65
+ type: "string"
66
+ },
67
+ "expires-at": {
68
+ describe: "When the snapshot is automatically deleted (RFC 3339, e.g. 2025-12-31T23:59:59Z). Omit to keep it indefinitely.",
69
+ type: "string"
70
+ }
71
+ }).conflicts("timestamp", "lsn").example([
72
+ ["$0 snapshots create", "Snapshot the head of the context/default branch"],
73
+ ["$0 snapshots create --branch main --name pre-migration", "Snapshot the head of main with a name"],
74
+ ["$0 snapshots create --branch main --timestamp 2025-01-01T00:00:00Z", "Snapshot main at a point in time"],
75
+ ["$0 snapshots create --branch main --lsn 0/1F3C8A0 --expires-at 2025-12-31T23:59:59Z", "Snapshot main at an LSN, auto-deleting at the given time"]
76
+ ]), (args) => create(args)).command("update <id>", "Update a snapshot's name or expiration", (yargs) => yargs.options({
77
+ name: {
78
+ describe: "Rename the snapshot",
79
+ type: "string"
80
+ },
81
+ "expires-at": {
82
+ describe: "Set when the snapshot expires (RFC 3339). Mutually exclusive with --clear-expiration.",
83
+ type: "string"
84
+ },
85
+ "clear-expiration": {
86
+ describe: "Clear the expiration so the snapshot is kept indefinitely.",
87
+ type: "boolean"
88
+ }
89
+ }).conflicts("expires-at", "clear-expiration"), (args) => update(args)).command("delete <id>", "Delete a snapshot by id or name", (yargs) => yargs, (args) => deleteSnapshot(args)).command("restore <id>", "Restore a snapshot into a branch", (yargs) => yargs.options({
90
+ name: {
91
+ describe: "Name for the newly restored branch. Auto-generated when omitted.",
92
+ type: "string"
93
+ },
94
+ "target-branch": {
95
+ describe: "Branch id or name to restore the snapshot onto. Defaults to the snapshot's source branch. Recommended when you intend to finalize (replace an existing branch).",
96
+ type: "string"
97
+ },
98
+ finalize: {
99
+ describe: "Finalize the restore immediately: move computes onto the restored branch and swap it in for the target. Without this, the restore is left un-finalized so you can inspect it first, then run `snapshots finalize <branch>`.",
100
+ type: "boolean",
101
+ default: false
102
+ }
103
+ }).example([
104
+ ["$0 snapshots restore snap-1234 --name recovered", "Restore a snapshot to a new branch named 'recovered'"],
105
+ ["$0 snapshots restore snap-1234 --target-branch main --finalize", "Restore onto main and swap it in immediately"],
106
+ ["$0 snapshots restore snap-1234 --target-branch main", "Restore onto main un-finalized to preview, then run `snapshots finalize`"]
107
+ ]), (args) => restore(args)).command("finalize <branch>", "Finalize a previewed snapshot restore (swap the restored branch in)", (yargs) => yargs.options({ name: {
108
+ describe: "Name to give the replaced (old) branch. Auto-generated when omitted.",
109
+ type: "string"
110
+ } }), (args) => finalize(args)).command("schedule", "Manage the automatic snapshot (backup) schedule of a branch", (yargs) => yargs.usage("$0 snapshots schedule <sub-command> [options]").command("get", "Get a branch's automatic snapshot schedule", (yargs) => yargs.options({ branch: {
111
+ alias: "b",
112
+ describe: "Branch id or name. Defaults to the branch in your context, or the project's default branch.",
113
+ type: "string"
114
+ } }), (args) => scheduleGet(args)).command("set", "Set a branch's automatic snapshot schedule", (yargs) => yargs.options({
115
+ branch: {
116
+ alias: "b",
117
+ describe: "Branch id or name. Defaults to the branch in your context, or the project's default branch.",
118
+ type: "string"
119
+ },
120
+ frequency: {
121
+ describe: "How often to take snapshots. Builds a single-entry schedule together with --hour/--day/--month/--retention.",
122
+ choices: SNAPSHOT_FREQUENCIES,
123
+ type: "string"
124
+ },
125
+ hour: {
126
+ describe: "Hour of the day (0-23) to take the snapshot (used with --frequency).",
127
+ type: "number"
128
+ },
129
+ day: {
130
+ describe: "Day of the week/month (1-31) to take the snapshot (used with --frequency).",
131
+ type: "number"
132
+ },
133
+ month: {
134
+ describe: "Month of the year (1-12) to take the snapshot (used with --frequency).",
135
+ type: "number"
136
+ },
137
+ retention: {
138
+ describe: "How long to keep each snapshot, in seconds (min 3600). Omit to keep indefinitely.",
139
+ type: "number"
140
+ },
141
+ schedule: {
142
+ describe: "Full schedule as JSON, for multi-entry schedules, e.g. '[{\"frequency\":\"daily\",\"hour\":3,\"retention_seconds\":604800}]'. Overrides the single-entry flags.",
143
+ type: "string"
144
+ }
145
+ }).example([["$0 snapshots schedule set --branch main --frequency daily --hour 3 --retention 604800", "A daily 03:00 snapshot kept for 7 days"], ["$0 snapshots schedule set --branch main --schedule '[{\"frequency\":\"weekly\",\"day\":1,\"hour\":2},{\"frequency\":\"daily\",\"hour\":3}]'", "A multi-entry schedule via JSON"]]), (args) => scheduleSet(args)).demandCommand(1, "Specify `get` or `set`."), () => {}).demandCommand(1, "Specify a snapshots sub-command.");
146
+ const handler = (args) => {
147
+ return args;
203
148
  };
204
149
  /** Narrow an unknown parsed JSON value to a plain object without type casting. */
205
150
  const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
206
151
  /** Normalize a user-supplied date to an ISO 8601 string, or throw a friendly error. */
207
152
  const toIso = (value, flag) => {
208
- const ms = Date.parse(value);
209
- if (Number.isNaN(ms)) {
210
- throw new Error(`Invalid ${flag} value: "${value}". Use an RFC 3339 timestamp, e.g. 2025-12-31T23:59:59Z.`);
211
- }
212
- return new Date(ms).toISOString();
153
+ const ms = Date.parse(value);
154
+ if (Number.isNaN(ms)) throw new Error(`Invalid ${flag} value: "${value}". Use an RFC 3339 timestamp, e.g. 2025-12-31T23:59:59Z.`);
155
+ return new Date(ms).toISOString();
213
156
  };
214
157
  /**
215
- * Resolve a snapshot from an id **or** a name. Snapshot names are not guaranteed
216
- * unique, so an id match wins; a name that resolves to more than one snapshot is a
217
- * hard error asking the user to disambiguate by id.
218
- */
158
+ * Resolve a snapshot from an id **or** a name. Snapshot names are not guaranteed
159
+ * unique, so an id match wins; a name that resolves to more than one snapshot is a
160
+ * hard error asking the user to disambiguate by id.
161
+ */
219
162
  const resolveSnapshot = async (props) => {
220
- const { data: { snapshots }, } = await props.apiClient.listSnapshots(props.projectId);
221
- const byId = snapshots.find((s) => s.id === props.id);
222
- if (byId) {
223
- return byId;
224
- }
225
- const byName = snapshots.filter((s) => s.name === props.id);
226
- if (byName.length === 1) {
227
- return byName[0];
228
- }
229
- if (byName.length > 1) {
230
- throw new Error(`Multiple snapshots are named "${props.id}". Re-run with the snapshot id:\n${byName
231
- .map((s) => ` ${s.id}`)
232
- .join("\n")}`);
233
- }
234
- throw new Error(`Snapshot "${props.id}" not found.\nAvailable snapshots: ${snapshots.map((s) => `${s.name} (${s.id})`).join(", ") ||
235
- "none"}`);
163
+ const { data: { snapshots } } = await props.apiClient.listSnapshots(props.projectId);
164
+ const byId = snapshots.find((s) => s.id === props.id);
165
+ if (byId) return byId;
166
+ const byName = snapshots.filter((s) => s.name === props.id);
167
+ if (byName.length === 1) return byName[0];
168
+ if (byName.length > 1) throw new Error(`Multiple snapshots are named "${props.id}". Re-run with the snapshot id:\n${byName.map((s) => ` ${s.id}`).join("\n")}`);
169
+ throw new Error(`Snapshot "${props.id}" not found.\nAvailable snapshots: ${snapshots.map((s) => `${s.name} (${s.id})`).join(", ") || "none"}`);
236
170
  };
237
171
  const list = async (props) => {
238
- const { data: { snapshots }, } = await props.apiClient.listSnapshots(props.projectId);
239
- writer(props).end(snapshots, {
240
- fields: SNAPSHOT_FIELDS,
241
- title: "snapshots",
242
- emptyMessage: "No snapshots found. Create one with:\n> neon snapshots create --help",
243
- renderColumns: {
244
- expires_at: (s) => s.expires_at || "never",
245
- },
246
- });
172
+ const { data: { snapshots } } = await props.apiClient.listSnapshots(props.projectId);
173
+ writer(props).end(snapshots, {
174
+ fields: SNAPSHOT_FIELDS,
175
+ title: "snapshots",
176
+ emptyMessage: "No snapshots found. Create one with:\n> neon snapshots create --help",
177
+ renderColumns: { expires_at: (s) => s.expires_at || "never" }
178
+ });
247
179
  };
248
180
  const get = async (props) => {
249
- const snapshot = await resolveSnapshot(props);
250
- writer(props).end(snapshot, {
251
- fields: SNAPSHOT_FIELDS,
252
- renderColumns: {
253
- expires_at: (s) => s.expires_at || "never",
254
- },
255
- });
181
+ const snapshot = await resolveSnapshot(props);
182
+ writer(props).end(snapshot, {
183
+ fields: SNAPSHOT_FIELDS,
184
+ renderColumns: { expires_at: (s) => s.expires_at || "never" }
185
+ });
256
186
  };
257
187
  const create = async (props) => {
258
- if (props.lsn !== undefined && !looksLikeLSN(props.lsn)) {
259
- throw new Error(`Invalid --lsn value: "${props.lsn}". Expected an LSN like 0/1F3C8A0.`);
260
- }
261
- if (props.timestamp !== undefined && !looksLikeTimestamp(props.timestamp)) {
262
- throw new Error(`Invalid --timestamp value: "${props.timestamp}". Use an RFC 3339 timestamp, e.g. 2025-01-01T00:00:00Z.`);
263
- }
264
- const { branchId } = await resolveBranchRef({
265
- ...props,
266
- branch: props.branch,
267
- });
268
- const { data } = await retryOnLock(() => props.apiClient.createSnapshot(props.projectId, branchId, {
269
- name: props.name,
270
- timestamp: props.timestamp,
271
- lsn: props.lsn,
272
- expires_at: props.expiresAt
273
- ? toIso(props.expiresAt, "--expires-at")
274
- : undefined,
275
- }));
276
- writer(props).end(data.snapshot, {
277
- fields: SNAPSHOT_FIELDS,
278
- title: "snapshot",
279
- renderColumns: {
280
- expires_at: (s) => s.expires_at || "never",
281
- },
282
- });
188
+ if (props.lsn !== void 0 && !looksLikeLSN(props.lsn)) throw new Error(`Invalid --lsn value: "${props.lsn}". Expected an LSN like 0/1F3C8A0.`);
189
+ if (props.timestamp !== void 0 && !looksLikeTimestamp(props.timestamp)) throw new Error(`Invalid --timestamp value: "${props.timestamp}". Use an RFC 3339 timestamp, e.g. 2025-01-01T00:00:00Z.`);
190
+ const { branchId } = await resolveBranchRef({
191
+ ...props,
192
+ branch: props.branch
193
+ });
194
+ const { data } = await retryOnLock(() => props.apiClient.createSnapshot(props.projectId, branchId, {
195
+ name: props.name,
196
+ timestamp: props.timestamp,
197
+ lsn: props.lsn,
198
+ expires_at: props.expiresAt ? toIso(props.expiresAt, "--expires-at") : void 0
199
+ }));
200
+ writer(props).end(data.snapshot, {
201
+ fields: SNAPSHOT_FIELDS,
202
+ title: "snapshot",
203
+ renderColumns: { expires_at: (s) => s.expires_at || "never" }
204
+ });
283
205
  };
284
206
  const update = async (props) => {
285
- if (props.name === undefined &&
286
- props.expiresAt === undefined &&
287
- !props.clearExpiration) {
288
- throw new Error("Nothing to update. Pass --name, --expires-at, or --clear-expiration.");
289
- }
290
- const snapshot = await resolveSnapshot(props);
291
- // `undefined` fields are dropped by JSON serialization, so an omitted
292
- // `expires_at` leaves the expiration unchanged while an explicit `null`
293
- // clears it.
294
- const expiresAt = props.clearExpiration
295
- ? null
296
- : props.expiresAt !== undefined
297
- ? toIso(props.expiresAt, "--expires-at")
298
- : undefined;
299
- const { data } = await retryOnLock(() => props.apiClient.updateSnapshot(props.projectId, snapshot.id, {
300
- snapshot: {
301
- name: props.name,
302
- expires_at: expiresAt,
303
- },
304
- }));
305
- writer(props).end(data.snapshot, {
306
- fields: SNAPSHOT_FIELDS,
307
- renderColumns: {
308
- expires_at: (s) => s.expires_at || "never",
309
- },
310
- });
207
+ if (props.name === void 0 && props.expiresAt === void 0 && !props.clearExpiration) throw new Error("Nothing to update. Pass --name, --expires-at, or --clear-expiration.");
208
+ const snapshot = await resolveSnapshot(props);
209
+ const expiresAt = props.clearExpiration ? null : props.expiresAt !== void 0 ? toIso(props.expiresAt, "--expires-at") : void 0;
210
+ const { data } = await retryOnLock(() => props.apiClient.updateSnapshot(props.projectId, snapshot.id, { snapshot: {
211
+ name: props.name,
212
+ expires_at: expiresAt
213
+ } }));
214
+ writer(props).end(data.snapshot, {
215
+ fields: SNAPSHOT_FIELDS,
216
+ renderColumns: { expires_at: (s) => s.expires_at || "never" }
217
+ });
311
218
  };
312
219
  const deleteSnapshot = async (props) => {
313
- const snapshot = await resolveSnapshot(props);
314
- await retryOnLock(() => props.apiClient.deleteSnapshot(props.projectId, snapshot.id));
315
- // The delete endpoint returns the tracking operations (202), not the snapshot
316
- // body, so echo the snapshot we just deleted for confirmation.
317
- writer(props).end(snapshot, {
318
- fields: SNAPSHOT_FIELDS,
319
- renderColumns: {
320
- expires_at: (s) => s.expires_at || "never",
321
- },
322
- });
220
+ const snapshot = await resolveSnapshot(props);
221
+ await retryOnLock(() => props.apiClient.deleteSnapshot(props.projectId, snapshot.id));
222
+ writer(props).end(snapshot, {
223
+ fields: SNAPSHOT_FIELDS,
224
+ renderColumns: { expires_at: (s) => s.expires_at || "never" }
225
+ });
323
226
  };
324
227
  const restore = async (props) => {
325
- const snapshot = await resolveSnapshot(props);
326
- const targetBranchId = props.targetBranch
327
- ? await branchIdResolve({
328
- branch: props.targetBranch,
329
- projectId: props.projectId,
330
- apiClient: props.apiClient,
331
- })
332
- : undefined;
333
- const { data } = await retryOnLock(() => props.apiClient.restoreSnapshot(props.projectId, snapshot.id, {
334
- name: props.name,
335
- target_branch_id: targetBranchId,
336
- finalize_restore: props.finalize,
337
- }));
338
- const out = writer(props).write(data.branch, {
339
- fields: BRANCH_FIELDS,
340
- title: "restored branch",
341
- });
342
- if (data.operations?.length) {
343
- out.write(data.operations, {
344
- fields: OPERATION_FIELDS,
345
- title: "operations",
346
- });
347
- }
348
- out.end();
349
- if (!props.finalize) {
350
- log.info(`Restore left un-finalized. Inspect branch ${data.branch.id}, then run:\n neon snapshots finalize ${data.branch.id} --project-id ${props.projectId}`);
351
- }
228
+ const snapshot = await resolveSnapshot(props);
229
+ const targetBranchId = props.targetBranch ? await branchIdResolve({
230
+ branch: props.targetBranch,
231
+ projectId: props.projectId,
232
+ apiClient: props.apiClient
233
+ }) : void 0;
234
+ const { data } = await retryOnLock(() => props.apiClient.restoreSnapshot(props.projectId, snapshot.id, {
235
+ name: props.name,
236
+ target_branch_id: targetBranchId,
237
+ finalize_restore: props.finalize
238
+ }));
239
+ const out = writer(props).write(data.branch, {
240
+ fields: BRANCH_FIELDS,
241
+ title: "restored branch"
242
+ });
243
+ if (data.operations?.length) out.write(data.operations, {
244
+ fields: OPERATION_FIELDS,
245
+ title: "operations"
246
+ });
247
+ out.end();
248
+ if (!props.finalize) log.info(`Restore left un-finalized. Inspect branch ${data.branch.id}, then run:\n neon snapshots finalize ${data.branch.id} --project-id ${props.projectId}`);
352
249
  };
353
250
  const finalize = async (props) => {
354
- const branchId = await branchIdResolve({
355
- branch: props.branch,
356
- projectId: props.projectId,
357
- apiClient: props.apiClient,
358
- });
359
- const { data } = await retryOnLock(() => props.apiClient.finalizeRestoreBranch(props.projectId, branchId, props.name ? { name: props.name } : undefined));
360
- writer(props).end(data.operations ?? [], {
361
- fields: OPERATION_FIELDS,
362
- title: "operations",
363
- emptyMessage: `Finalized restore for branch ${branchId}.`,
364
- });
251
+ const branchId = await branchIdResolve({
252
+ branch: props.branch,
253
+ projectId: props.projectId,
254
+ apiClient: props.apiClient
255
+ });
256
+ const { data } = await retryOnLock(() => props.apiClient.finalizeRestoreBranch(props.projectId, branchId, props.name ? { name: props.name } : void 0));
257
+ writer(props).end(data.operations ?? [], {
258
+ fields: OPERATION_FIELDS,
259
+ title: "operations",
260
+ emptyMessage: `Finalized restore for branch ${branchId}.`
261
+ });
365
262
  };
366
263
  const scheduleGet = async (props) => {
367
- const { branchId } = await resolveBranchRef({
368
- ...props,
369
- branch: props.branch,
370
- });
371
- const { data } = await props.apiClient.getSnapshotSchedule(props.projectId, branchId);
372
- writer(props).end(data.schedule, {
373
- fields: SCHEDULE_FIELDS,
374
- title: "schedule",
375
- emptyMessage: "No automatic snapshot schedule is configured.",
376
- });
264
+ const { branchId } = await resolveBranchRef({
265
+ ...props,
266
+ branch: props.branch
267
+ });
268
+ const { data } = await props.apiClient.getSnapshotSchedule(props.projectId, branchId);
269
+ writer(props).end(data.schedule, {
270
+ fields: SCHEDULE_FIELDS,
271
+ title: "schedule",
272
+ emptyMessage: "No automatic snapshot schedule is configured."
273
+ });
377
274
  };
378
275
  /**
379
- * Validate an untrusted parsed value as a {@link BackupScheduleItem}[] without any
380
- * type casting, throwing a clear error for the first invalid field it finds.
381
- */
276
+ * Validate an untrusted parsed value as a {@link BackupScheduleItem}[] without any
277
+ * type casting, throwing a clear error for the first invalid field it finds.
278
+ */
382
279
  const parseScheduleJson = (raw) => {
383
- let parsed;
384
- try {
385
- parsed = JSON.parse(raw);
386
- }
387
- catch {
388
- throw new Error("--schedule must be valid JSON.");
389
- }
390
- if (!Array.isArray(parsed)) {
391
- throw new Error('--schedule must be a JSON array of schedule entries, e.g. \'[{"frequency":"daily","hour":3}]\'.');
392
- }
393
- return parsed.map((entry, index) => {
394
- if (!isRecord(entry)) {
395
- throw new Error(`--schedule entry ${index} must be an object.`);
396
- }
397
- const record = entry;
398
- const frequency = record.frequency;
399
- if (typeof frequency !== "string") {
400
- throw new Error(`--schedule entry ${index} is missing a string "frequency".`);
401
- }
402
- if (!isSnapshotFrequency(frequency)) {
403
- throw new Error(`--schedule entry ${index} has an unsupported "frequency": "${frequency}". Use one of: ${SNAPSHOT_FREQUENCIES.join(", ")}.`);
404
- }
405
- const item = { frequency };
406
- for (const key of [
407
- "hour",
408
- "day",
409
- "month",
410
- "retention_seconds",
411
- ]) {
412
- const value = record[key];
413
- if (value === undefined) {
414
- continue;
415
- }
416
- if (typeof value !== "number") {
417
- throw new Error(`--schedule entry ${index} field "${key}" must be a number.`);
418
- }
419
- item[key] = value;
420
- }
421
- return item;
422
- });
280
+ let parsed;
281
+ try {
282
+ parsed = JSON.parse(raw);
283
+ } catch {
284
+ throw new Error("--schedule must be valid JSON.");
285
+ }
286
+ if (!Array.isArray(parsed)) throw new Error("--schedule must be a JSON array of schedule entries, e.g. '[{\"frequency\":\"daily\",\"hour\":3}]'.");
287
+ return parsed.map((entry, index) => {
288
+ if (!isRecord(entry)) throw new Error(`--schedule entry ${index} must be an object.`);
289
+ const record = entry;
290
+ const frequency = record.frequency;
291
+ if (typeof frequency !== "string") throw new Error(`--schedule entry ${index} is missing a string "frequency".`);
292
+ if (!isSnapshotFrequency(frequency)) throw new Error(`--schedule entry ${index} has an unsupported "frequency": "${frequency}". Use one of: ${SNAPSHOT_FREQUENCIES.join(", ")}.`);
293
+ const item = { frequency };
294
+ for (const key of [
295
+ "hour",
296
+ "day",
297
+ "month",
298
+ "retention_seconds"
299
+ ]) {
300
+ const value = record[key];
301
+ if (value === void 0) continue;
302
+ if (typeof value !== "number") throw new Error(`--schedule entry ${index} field "${key}" must be a number.`);
303
+ item[key] = value;
304
+ }
305
+ return item;
306
+ });
423
307
  };
424
308
  const scheduleSet = async (props) => {
425
- const { branchId } = await resolveBranchRef({
426
- ...props,
427
- branch: props.branch,
428
- });
429
- let schedule;
430
- if (props.schedule) {
431
- schedule = parseScheduleJson(props.schedule);
432
- }
433
- else if (props.frequency) {
434
- const item = { frequency: props.frequency };
435
- if (props.hour !== undefined)
436
- item.hour = props.hour;
437
- if (props.day !== undefined)
438
- item.day = props.day;
439
- if (props.month !== undefined)
440
- item.month = props.month;
441
- if (props.retention !== undefined)
442
- item.retention_seconds = props.retention;
443
- schedule = [item];
444
- }
445
- else {
446
- throw new Error("Provide --frequency (optionally with --hour/--day/--month/--retention) or --schedule <json>.");
447
- }
448
- await retryOnLock(() => props.apiClient.setSnapshotSchedule(props.projectId, branchId, {
449
- schedule,
450
- }));
451
- writer(props).end(schedule, {
452
- fields: SCHEDULE_FIELDS,
453
- title: "schedule",
454
- });
309
+ const { branchId } = await resolveBranchRef({
310
+ ...props,
311
+ branch: props.branch
312
+ });
313
+ let schedule;
314
+ if (props.schedule) schedule = parseScheduleJson(props.schedule);
315
+ else if (props.frequency) {
316
+ const item = { frequency: props.frequency };
317
+ if (props.hour !== void 0) item.hour = props.hour;
318
+ if (props.day !== void 0) item.day = props.day;
319
+ if (props.month !== void 0) item.month = props.month;
320
+ if (props.retention !== void 0) item.retention_seconds = props.retention;
321
+ schedule = [item];
322
+ } else throw new Error("Provide --frequency (optionally with --hour/--day/--month/--retention) or --schedule <json>.");
323
+ await retryOnLock(() => props.apiClient.setSnapshotSchedule(props.projectId, branchId, { schedule }));
324
+ writer(props).end(schedule, {
325
+ fields: SCHEDULE_FIELDS,
326
+ title: "schedule"
327
+ });
455
328
  };
329
+ //#endregion
330
+ export { SNAPSHOT_FIELDS, aliases, builder, command, describe, handler, snapshots_exports };