@batadata/cli 0.1.14 → 0.1.16

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.
@@ -0,0 +1,17 @@
1
+ /**
2
+ * bata claim <token> — redeem a legacy claim token into your team.
3
+ *
4
+ * LEGACY: the anonymous, no-signup flow that issued claim tokens is gone —
5
+ * `bata new` now creates a database directly in your team. This command remains
6
+ * only to redeem a token from that retired flow. Authed. Resolves the team
7
+ * (like `bata create`), calls POST /v1/claim/redeem { claim_token, team_id },
8
+ * and on success the project becomes a permanent member of your team (its TTL
9
+ * is cleared). Typed errors for unknown / expired / already-claimed tokens.
10
+ */
11
+ export interface ClaimArgs {
12
+ token?: string;
13
+ help: boolean;
14
+ }
15
+ /** Parse `bata claim <token>` args. Exported for tests. */
16
+ export declare function parseClaimArgs(args: string[]): ClaimArgs;
17
+ export declare function claim(args: string[]): Promise<void>;
@@ -0,0 +1,84 @@
1
+ /**
2
+ * bata claim <token> — redeem a legacy claim token into your team.
3
+ *
4
+ * LEGACY: the anonymous, no-signup flow that issued claim tokens is gone —
5
+ * `bata new` now creates a database directly in your team. This command remains
6
+ * only to redeem a token from that retired flow. Authed. Resolves the team
7
+ * (like `bata create`), calls POST /v1/claim/redeem { claim_token, team_id },
8
+ * and on success the project becomes a permanent member of your team (its TTL
9
+ * is cleared). Typed errors for unknown / expired / already-claimed tokens.
10
+ */
11
+ import { api, apiError, resolveTeamId } from "../api.js";
12
+ import { requireToken, saveConfig, isJsonMode } from "../config.js";
13
+ import { colors, log, json, success, kvList } from "../utils/logger.js";
14
+ import { emitError } from "../utils/errors.js";
15
+ /** Parse `bata claim <token>` args. Exported for tests. */
16
+ export function parseClaimArgs(args) {
17
+ let token;
18
+ for (const arg of args) {
19
+ if (arg === "--help" || arg === "-h")
20
+ return { help: true };
21
+ if (!arg.startsWith("-") && !token)
22
+ token = arg;
23
+ }
24
+ return { token, help: false };
25
+ }
26
+ function printHelp() {
27
+ log();
28
+ log(` ${colors.bold("bata claim")} ${colors.dim("<token>")}`);
29
+ log();
30
+ log(` ${colors.yellow("Legacy:")} redeem a claim token from the retired anonymous flow.`);
31
+ log(` ${colors.cyan("bata new")} now creates a database directly in your team — no token to claim.`);
32
+ log(` Requires login — pass ${colors.cyan("--api-key")}, set ${colors.cyan("BATA_API_KEY")}, or run ${colors.cyan("bata login")}.`);
33
+ log();
34
+ log(` ${colors.bold("Examples")}`);
35
+ log(` ${colors.cyan("bata claim claim_EXAMPLE_TOKEN")}`);
36
+ log(` ${colors.cyan("bata claim claim_EXAMPLE_TOKEN --json")}`);
37
+ log();
38
+ }
39
+ export async function claim(args) {
40
+ const parsed = parseClaimArgs(args);
41
+ if (parsed.help) {
42
+ printHelp();
43
+ return;
44
+ }
45
+ if (!parsed.token) {
46
+ emitError("MISSING_ARG", "A claim token is required. Usage: bata claim <token>", "Get one from `bata new`.");
47
+ }
48
+ const token = requireToken();
49
+ const teamId = await resolveTeamId(token);
50
+ if (!teamId) {
51
+ emitError("NO_TEAM", "No team found for this credential.", "Run `bata login` or check your API key.");
52
+ }
53
+ const res = await api.post("/v1/claim/redeem", { claim_token: parsed.token, team_id: teamId }, token);
54
+ if (!res.ok) {
55
+ const data = res.data;
56
+ const code = data?.code;
57
+ if (code === "CLAIM_TOKEN_UNKNOWN") {
58
+ emitError("NOT_FOUND", "Unknown or invalid claim token.", "Double-check the token from `bata new`.");
59
+ }
60
+ if (code === "CLAIM_EXPIRED") {
61
+ emitError("NOT_FOUND", "This database has expired and been deleted.", "Create a fresh one with `bata new`.");
62
+ }
63
+ if (code === "CLAIM_ALREADY_CLAIMED") {
64
+ emitError("NOT_FOUND", "This database has already been claimed.", "");
65
+ }
66
+ emitError("CLI_ERROR", apiError(res, "Failed to claim database"));
67
+ }
68
+ const claimed = res.data;
69
+ saveConfig({ defaultProject: claimed.project_id });
70
+ if (isJsonMode()) {
71
+ json(claimed);
72
+ return;
73
+ }
74
+ log();
75
+ success(`Claimed ${colors.cyan(claimed.name)} into your team.`);
76
+ log();
77
+ kvList([
78
+ ["Project ID", colors.dim(claimed.project_id)],
79
+ ["Team", colors.dim(claimed.team_id)],
80
+ ]);
81
+ log();
82
+ log(` ${colors.dim("Connect:")} ${colors.cyan("bata db url")}`);
83
+ log();
84
+ }
@@ -0,0 +1,21 @@
1
+ /**
2
+ * bata new — instant Postgres in your team (one call, zero config).
3
+ *
4
+ * Authed (like every other command): requires a credential. Calls
5
+ * POST /v1/claim/new, which creates a REAL project directly in your team —
6
+ * owned, billed, and quota'd like any other — and prints the connection string.
7
+ * No name or region to pick; the compute is the smallest box and scales to zero.
8
+ *
9
+ * `--team <id>` targets a specific team (else your resolved/default team).
10
+ * `--json` emits the full payload for agents.
11
+ *
12
+ * (The old anonymous, no-signup flow with claim tokens is gone.)
13
+ */
14
+ export interface NewArgs {
15
+ team?: string;
16
+ help: boolean;
17
+ }
18
+ /** Parse `bata new` args: optional `--team <id>` and `--help` (global flags
19
+ * like --json are stripped upstream in index.ts). Exported for tests. */
20
+ export declare function parseNewArgs(args: string[]): NewArgs;
21
+ export declare function newDb(args: string[]): Promise<void>;
@@ -0,0 +1,95 @@
1
+ /**
2
+ * bata new — instant Postgres in your team (one call, zero config).
3
+ *
4
+ * Authed (like every other command): requires a credential. Calls
5
+ * POST /v1/claim/new, which creates a REAL project directly in your team —
6
+ * owned, billed, and quota'd like any other — and prints the connection string.
7
+ * No name or region to pick; the compute is the smallest box and scales to zero.
8
+ *
9
+ * `--team <id>` targets a specific team (else your resolved/default team).
10
+ * `--json` emits the full payload for agents.
11
+ *
12
+ * (The old anonymous, no-signup flow with claim tokens is gone.)
13
+ */
14
+ import { api, apiError, resolveTeamId } from "../api.js";
15
+ import { requireToken, isJsonMode, saveConfig } from "../config.js";
16
+ import { colors, log, json, success, kvList } from "../utils/logger.js";
17
+ import { emitError } from "../utils/errors.js";
18
+ /** Parse `bata new` args: optional `--team <id>` and `--help` (global flags
19
+ * like --json are stripped upstream in index.ts). Exported for tests. */
20
+ export function parseNewArgs(args) {
21
+ let team;
22
+ for (let i = 0; i < args.length; i++) {
23
+ const arg = args[i];
24
+ if (arg === "--help" || arg === "-h")
25
+ return { team, help: true };
26
+ if (arg === "--team" && args[i + 1])
27
+ team = args[++i];
28
+ }
29
+ return { team, help: false };
30
+ }
31
+ function printHelp() {
32
+ log();
33
+ log(` ${colors.bold("bata new")}`);
34
+ log();
35
+ log(` Create an instant Postgres database in your team and print how to connect.`);
36
+ log(` One call, zero config — you get a connection string immediately.`);
37
+ log(` Requires login — pass ${colors.cyan("--api-key")}, set ${colors.cyan("BATA_API_KEY")}, or run ${colors.cyan("bata login")}.`);
38
+ log();
39
+ log(` ${colors.bold("Options")}`);
40
+ log(` ${colors.dim("--team <id>")} Team to create the database in (default: your team)`);
41
+ log(` ${colors.dim("--json")} Machine-readable output`);
42
+ log(` ${colors.dim("--help, -h")} Show this help`);
43
+ log();
44
+ }
45
+ export async function newDb(args) {
46
+ const parsed = parseNewArgs(args);
47
+ if (parsed.help) {
48
+ printHelp();
49
+ return;
50
+ }
51
+ // Authed like every other command. Exits 4 NO_CREDENTIALS when absent.
52
+ const token = requireToken();
53
+ // Resolve the team unless one was passed explicitly. The server verifies
54
+ // membership either way.
55
+ const teamId = parsed.team ?? (await resolveTeamId(token));
56
+ const body = {};
57
+ if (teamId)
58
+ body.team_id = teamId;
59
+ const res = await api.post("/v1/claim/new", body, token);
60
+ if (!res.ok) {
61
+ const data = res.data;
62
+ const code = data?.code;
63
+ if (res.status === 404 && code === "CLAIMABLE_DISABLED") {
64
+ emitError("NOT_FOUND", "Instant databases (bata new) are not enabled on this server.", "Create a project with `bata create <name>` instead.");
65
+ }
66
+ if (res.status === 429) {
67
+ emitError("API_UNAVAILABLE", "Rate limit reached for instant databases from this network.", "Wait a bit before creating another.");
68
+ }
69
+ emitError("CLI_ERROR", apiError(res, "Failed to create instant database"));
70
+ }
71
+ const db = res.data;
72
+ saveConfig({ defaultProject: db.project_id });
73
+ if (isJsonMode()) {
74
+ json(db);
75
+ return;
76
+ }
77
+ log();
78
+ success("Your instant database is ready!");
79
+ log();
80
+ kvList([
81
+ ["Project ID", colors.dim(db.project_id)],
82
+ ["Team", colors.dim(db.team_id)],
83
+ ]);
84
+ log();
85
+ log(` ${colors.bold("Connection")}`);
86
+ log();
87
+ kvList([
88
+ ["Direct", colors.dim(db.connection.direct)],
89
+ ["Pooled", colors.dim(db.connection.pooled)],
90
+ ]);
91
+ log();
92
+ log(` ${colors.dim("Connect:")} ${colors.cyan("bata db url")}`);
93
+ log(` ${colors.dim("First connection may take a few seconds while the compute wakes.")}`);
94
+ log();
95
+ }
package/dist/index.js CHANGED
@@ -8,6 +8,8 @@ import { handleMigrate } from "./commands/migrate.js";
8
8
  import { handleApiKeys } from "./commands/api-keys.js";
9
9
  import { dev } from "./commands/dev.js";
10
10
  import { create } from "./commands/create.js";
11
+ import { newDb } from "./commands/new.js";
12
+ import { claim } from "./commands/claim.js";
11
13
  import { status } from "./commands/status.js";
12
14
  import { connect } from "./commands/connect.js";
13
15
  import { usage } from "./commands/usage.js";
@@ -25,6 +27,8 @@ function help() {
25
27
  log(` ${colors.cyan("bata")} ${colors.dim("<command>")} ${colors.dim("[options]")}`);
26
28
  log();
27
29
  log(` ${colors.bold("Quick Start")}`);
30
+ log(` ${colors.cyan("new")} Instant database in your team ${colors.dim("(one call, zero config)")}`);
31
+ log(` ${colors.cyan("claim <token>")} ${colors.dim("Legacy: redeem a claim token from the retired anonymous flow")}`);
28
32
  log(` ${colors.cyan("create <name>")} Create a project and wait for it to be ready`);
29
33
  log(` ${colors.cyan("connect <name>")} Open psql to a project (auto-wakes if suspended)`);
30
34
  log(` ${colors.cyan("status")} Show all projects and their status`);
@@ -126,6 +130,12 @@ async function main() {
126
130
  case "create":
127
131
  await create(rest);
128
132
  break;
133
+ case "new":
134
+ await newDb(rest);
135
+ break;
136
+ case "claim":
137
+ await claim(rest);
138
+ break;
129
139
  case "status":
130
140
  await status();
131
141
  break;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@batadata/cli",
3
- "version": "0.1.14",
3
+ "version": "0.1.16",
4
4
  "description": "CLI for BataDB — serverless Postgres platform",
5
5
  "bin": {
6
6
  "bata": "./dist/index.js"