neon 8.17.0 → 8.18.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.
@@ -20,6 +20,11 @@ const CREATED_ROLE_FIELDS = [
20
20
  "password",
21
21
  "created_at"
22
22
  ];
23
+ const ROLE_ACCESS = [
24
+ "admin",
25
+ "read_only",
26
+ "read_write"
27
+ ];
23
28
  const command = "roles";
24
29
  const describe = "Manage roles";
25
30
  const aliases = ["role"];
@@ -32,7 +37,7 @@ const builder = (argv) => argv.usage("$0 roles <sub-command> [options]").options
32
37
  describe: "Branch ID or name",
33
38
  type: "string"
34
39
  }
35
- }).middleware(fillSingleProject).command("list", "List roles", (yargs) => yargs, (args) => list(args)).command("create", "Create a role", (yargs) => yargs.options({
40
+ }).middleware(fillSingleProject).command("list", "List roles", (yargs) => yargs, (args) => list(args)).command("create", "Create a role. By default it joins neon_superuser (admin on every database); pass --access read_only or read_write for a scoped role", (yargs) => yargs.options({
36
41
  name: {
37
42
  describe: "Role name",
38
43
  type: "string",
@@ -41,7 +46,25 @@ const builder = (argv) => argv.usage("$0 roles <sub-command> [options]").options
41
46
  "no-login": {
42
47
  describe: "Create a passwordless role that cannot login",
43
48
  boolean: true
49
+ },
50
+ access: {
51
+ describe: "admin joins neon_superuser. read_only grants SELECT; read_write adds INSERT, UPDATE, DELETE. Scoped roles are created with SQL as the database owner and cover tables that owner owns, now and later",
52
+ type: "string",
53
+ choices: ROLE_ACCESS,
54
+ default: "admin"
55
+ },
56
+ database: {
57
+ describe: "Database a read_only or read_write role is scoped to. Defaults to the branch's only database",
58
+ type: "string"
59
+ },
60
+ schema: {
61
+ describe: "Schema a read_only or read_write role can use. Repeat for more. Defaults to public",
62
+ type: "string",
63
+ array: true
44
64
  }
65
+ }).check((args) => {
66
+ if (args.access === "admin" && (args.database !== void 0 || args.schema !== void 0)) throw new Error("--database and --schema need --access read_only or --access read_write");
67
+ return true;
45
68
  }), (args) => create(args)).command("delete <role>", "Delete a role", (yargs) => yargs, (args) => deleteRole(args));
46
69
  const handler = (args) => {
47
70
  return args;
@@ -59,6 +82,28 @@ const list = async (props) => {
59
82
  const create = async (props) => {
60
83
  const noLogin = props["no-login"] === true || props.login === false;
61
84
  const { branchId, branch } = await resolveBranchFromProps(props);
85
+ if (props.access === "read_only" || props.access === "read_write") {
86
+ const { fromSdkError, getNeonClient } = await import("../neon_client.js");
87
+ let role;
88
+ try {
89
+ role = await getNeonClient(props).postgres.roles.create({
90
+ projectId: props.projectId,
91
+ branchId,
92
+ name: props.name,
93
+ ...noLogin ? { no_login: true } : {},
94
+ access: props.access,
95
+ ...props.database === void 0 ? {} : { databaseName: props.database },
96
+ ...props.schema === void 0 ? {} : { schemas: props.schema }
97
+ }, { waitForReadiness: true });
98
+ } catch (error) {
99
+ throw fromSdkError(error, props.apiHost);
100
+ }
101
+ writer(props).end(role, {
102
+ fields: CREATED_ROLE_FIELDS,
103
+ humanTitle: `Role created on ${branch?.name ?? branchId}`
104
+ });
105
+ return;
106
+ }
62
107
  const { data } = await retryOnLock(() => props.apiClient.createProjectBranchRole(props.projectId, branchId, { role: {
63
108
  name: props.name,
64
109
  ...noLogin ? { no_login: true } : {}
package/dist/api.js CHANGED
@@ -258,7 +258,7 @@ const RETRY_DELAY = 3e3;
258
258
  const retryOnLock = async (fn) => {
259
259
  let attempt = 0;
260
260
  let errOut;
261
- while (attempt < RETRY_COUNT) try {
261
+ while (attempt < 5) try {
262
262
  return await fn();
263
263
  } catch (err) {
264
264
  errOut = err;
@@ -287,6 +287,15 @@ async function readJsonBody(response) {
287
287
  return text;
288
288
  }
289
289
  }
290
+ /** The fetch {@link getApiClient} sends requests with, for clients built outside it. */
291
+ const cliFetch = (requestTimeoutMs = REQUEST_TIMEOUT_MS) => {
292
+ const fetchWithTimeout = makeTimedFetch(validateRequestTimeout(requestTimeoutMs));
293
+ return (input, init) => {
294
+ const request = new Request(input, init);
295
+ request.headers.set("User-Agent", USER_AGENT);
296
+ return fetchWithTimeout(request);
297
+ };
298
+ };
290
299
  const getApiClient = ({ apiKey, apiHost, requestTimeoutMs = REQUEST_TIMEOUT_MS }) => {
291
300
  const baseUrl = apiHost ?? "https://console.neon.tech/api/v2";
292
301
  const fetchWithTimeout = makeTimedFetch(validateRequestTimeout(requestTimeoutMs));
@@ -1079,4 +1088,4 @@ const getApiClient = ({ apiKey, apiHost, requestTimeoutMs = REQUEST_TIMEOUT_MS }
1079
1088
  }))
1080
1089
  };
1081
1090
  };
1082
- export { ContentType, DEFAULT_API_HOST, NeonApiError, UnreadableResponseError, codeFromBody, getApiClient, isNeonApiError, messageFromBody, retryOnLock };
1091
+ export { ContentType, DEFAULT_API_HOST, NeonApiError, RETRY_COUNT, UnreadableResponseError, cliFetch, codeFromBody, getApiClient, httpError, isNeonApiError, messageFromBody, retryOnLock };
@@ -36,7 +36,7 @@ import { o as orgs_exports } from "../_chunks/orgs-Dpbr-K0A.js";
36
36
  import { o as profile_exports } from "../_chunks/profile-BAlM625e.js";
37
37
  import { a as psql_exports } from "../_chunks/psql-Bce4X0NG.js";
38
38
  import { s as realtime_exports } from "../_chunks/realtime-BwtzYf5a.js";
39
- import { l as roles_exports } from "../_chunks/roles-BVc69rjG.js";
39
+ import { l as roles_exports } from "../_chunks/roles-CNoDyRf4.js";
40
40
  import { a as set_context_exports } from "../_chunks/set_context-ompB6d2D.js";
41
41
  import { s as snapshots_exports } from "../_chunks/snapshots-DxNhjcco.js";
42
42
  import { a as status_exports } from "../_chunks/status-BlrzeKyA.js";
@@ -1 +1 @@
1
- export { t as aliases, n as builder, r as command, i as create, a as deleteRole, o as describe, s as handler, c as list } from "../_chunks/roles-BVc69rjG.js";
1
+ export { t as aliases, n as builder, r as command, i as create, a as deleteRole, o as describe, s as handler, c as list } from "../_chunks/roles-CNoDyRf4.js";
@@ -0,0 +1,27 @@
1
+ import { cliFetch, httpError } from "./api.js";
2
+ import { NeonApiError, createNeonClient } from "@neon/sdk";
3
+ // src/neon_client.ts
4
+ /**
5
+ * The `@neon/sdk` ergonomic client, for commands that run one of its multi-step workflows,
6
+ * with the timeout, debug logging, user agent, and 423 retries of `getApiClient`. Throws;
7
+ * pass failures through {@link fromSdkError}.
8
+ */
9
+ const getNeonClient = ({ apiKey, apiHost, requestTimeoutMs }) => createNeonClient({
10
+ apiKey,
11
+ baseUrl: apiHost ?? "https://console.neon.tech/api/v2",
12
+ fetch: cliFetch(requestTimeoutMs),
13
+ retries: 5,
14
+ throwOnError: true
15
+ });
16
+ /**
17
+ * Map an error from {@link getNeonClient} for the top-level handler. A management API
18
+ * response becomes the CLI's `NeonApiError`, so 401 and SSO recovery apply. Anything else
19
+ * keeps the SDK's message and drops its `cause`, so a socket code deep in the chain cannot
20
+ * turn a "role was created, but…" message into the generic connectivity hint.
21
+ */
22
+ function fromSdkError(error, apiHost) {
23
+ const base = apiHost ?? "https://console.neon.tech/api/v2";
24
+ if (error instanceof NeonApiError && error.response?.url.startsWith(base)) return httpError(error.response, error.body);
25
+ return new Error(error instanceof Error ? error.message : String(error));
26
+ }
27
+ export { fromSdkError, getNeonClient };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "neon",
3
- "version": "8.17.0",
3
+ "version": "8.18.0",
4
4
  "description": "CLI tool for Neon, the cloud backend primitives built around Lakebase Postgres",
5
5
  "keywords": [
6
6
  "neon",
@@ -73,10 +73,10 @@
73
73
  "yaml": "^2.9.0",
74
74
  "yargs": "17.7.2",
75
75
  "yoctocolors": "^2.1.2",
76
- "@neon/config": "1.10.0",
76
+ "@neon/config-runtime": "1.11.1",
77
77
  "@neon/realtime": "0.1.2",
78
- "@neon/sdk": "7.7.0",
79
- "@neon/config-runtime": "1.11.0"
78
+ "@neon/sdk": "7.8.0",
79
+ "@neon/config": "1.10.1"
80
80
  },
81
81
  "optionalDependencies": {
82
82
  "@napi-rs/keyring": "1.3.0",
@@ -99,7 +99,7 @@
99
99
  "@types/which": "3.0.4",
100
100
  "@types/yargs": "^17.0.35",
101
101
  "@vitest/coverage-v8": "^3.0.9",
102
- "@yao-pkg/pkg": "6.23.0",
102
+ "@yao-pkg/pkg": "6.24.0",
103
103
  "emocks": "3.0.4",
104
104
  "express": "4.19.2",
105
105
  "hono": "^4.12.25",
@@ -113,9 +113,9 @@
113
113
  "typescript": "^5.9.0",
114
114
  "vitest": "^3.0.9",
115
115
  "@neon-internals/cli-core": "0.0.0",
116
- "@neon-internals/env-core": "0.0.38",
116
+ "@neon-internals/env-core": "0.0.39",
117
117
  "@neon/e2e-harness": "0.0.0",
118
- "@neon/env": "1.6.8",
118
+ "@neon/env": "1.6.9",
119
119
  "@neon/functions": "0.11.0"
120
120
  },
121
121
  "publishConfig": {