@agentic-kit/db-tools 0.11.5 → 0.12.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 (82) hide show
  1. package/README.md +40 -1
  2. package/app-workspace.d.ts +29 -2
  3. package/app-workspace.js +99 -8
  4. package/blueprint-construction.d.ts +60 -0
  5. package/blueprint-construction.js +161 -0
  6. package/context.d.ts +5 -1
  7. package/context.js +30 -5
  8. package/db-probe.d.ts +25 -17
  9. package/db-probe.js +97 -37
  10. package/esm/app-workspace.d.ts +29 -2
  11. package/esm/app-workspace.js +96 -8
  12. package/esm/blueprint-construction.d.ts +60 -0
  13. package/esm/blueprint-construction.js +154 -0
  14. package/esm/context.d.ts +5 -1
  15. package/esm/context.js +31 -6
  16. package/esm/db-probe.d.ts +25 -17
  17. package/esm/db-probe.js +96 -37
  18. package/esm/graphql-http.d.ts +35 -0
  19. package/esm/graphql-http.js +134 -0
  20. package/esm/host.d.ts +46 -0
  21. package/esm/host.js +9 -0
  22. package/esm/index.d.ts +11 -2
  23. package/esm/index.js +6 -0
  24. package/esm/provision-database/env-file.d.ts +23 -2
  25. package/esm/provision-database/env-file.js +86 -27
  26. package/esm/provision-database/pending-ticket.d.ts +33 -0
  27. package/esm/provision-database/pending-ticket.js +63 -0
  28. package/esm/provision-database/provision.d.ts +71 -0
  29. package/esm/provision-database/provision.js +557 -0
  30. package/esm/provision-database/request-database.d.ts +55 -3
  31. package/esm/provision-database/request-database.js +118 -17
  32. package/esm/schema-model/normalize.d.ts +21 -0
  33. package/esm/schema-model/normalize.js +244 -0
  34. package/esm/schema-model.d.ts +110 -0
  35. package/esm/schema-model.js +303 -0
  36. package/esm/tools/add-policies.d.ts +1 -1
  37. package/esm/tools/add-records.d.ts +1 -0
  38. package/esm/tools/add-records.js +18 -6
  39. package/esm/tools/add-relation.js +2 -3
  40. package/esm/tools/create-api-key.d.ts +2 -0
  41. package/esm/tools/create-api-key.js +27 -12
  42. package/esm/tools/manage-entity-types.d.ts +1 -1
  43. package/esm/tools/provision-blueprint.d.ts +6 -0
  44. package/esm/tools/provision-blueprint.js +11 -4
  45. package/esm/tools/provision-database.d.ts +8 -11
  46. package/esm/tools/provision-database.js +23 -243
  47. package/esm/tools/run-codegen.d.ts +19 -2
  48. package/esm/tools/run-codegen.js +184 -97
  49. package/esm/tools/templates.js +3 -15
  50. package/graphql-http.d.ts +35 -0
  51. package/graphql-http.js +140 -0
  52. package/host.d.ts +46 -0
  53. package/host.js +10 -0
  54. package/index.d.ts +11 -2
  55. package/index.js +26 -1
  56. package/package.json +4 -4
  57. package/provision-database/env-file.d.ts +23 -2
  58. package/provision-database/env-file.js +89 -28
  59. package/provision-database/pending-ticket.d.ts +33 -0
  60. package/provision-database/pending-ticket.js +72 -0
  61. package/provision-database/provision.d.ts +71 -0
  62. package/provision-database/provision.js +565 -0
  63. package/provision-database/request-database.d.ts +55 -3
  64. package/provision-database/request-database.js +124 -18
  65. package/schema-model/normalize.d.ts +21 -0
  66. package/schema-model/normalize.js +250 -0
  67. package/schema-model.d.ts +110 -0
  68. package/schema-model.js +309 -0
  69. package/tools/add-policies.d.ts +1 -1
  70. package/tools/add-records.d.ts +1 -0
  71. package/tools/add-records.js +18 -6
  72. package/tools/add-relation.js +2 -3
  73. package/tools/create-api-key.d.ts +2 -0
  74. package/tools/create-api-key.js +27 -12
  75. package/tools/manage-entity-types.d.ts +1 -1
  76. package/tools/provision-blueprint.d.ts +6 -0
  77. package/tools/provision-blueprint.js +11 -4
  78. package/tools/provision-database.d.ts +8 -11
  79. package/tools/provision-database.js +23 -245
  80. package/tools/run-codegen.d.ts +19 -2
  81. package/tools/run-codegen.js +184 -96
  82. package/tools/templates.js +3 -15
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ // The provision request provision_database stopped waiting on, as recorded in
3
+ // a project's .env. requestDatabase never resumes by name, so the ticket is
4
+ // the only way back to a provision that is still running.
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.ABANDONED_TICKETS_KEY = exports.MAX_PENDING_ATTEMPTS = void 0;
7
+ exports.recordedTicket = recordedTicket;
8
+ exports.recordPendingTicket = recordPendingTicket;
9
+ exports.clearPendingTicket = clearPendingTicket;
10
+ exports.archivePendingTicket = archivePendingTicket;
11
+ exports.abandonedTickets = abandonedTickets;
12
+ exports.recordAbandoned = recordAbandoned;
13
+ const env_file_1 = require("./env-file");
14
+ /**
15
+ * Pending results recorded for one ticket before provision_database stops
16
+ * following it. At the default 240 s deadline that is about 20 minutes.
17
+ */
18
+ exports.MAX_PENDING_ATTEMPTS = 5;
19
+ const KEYS = Object.values(env_file_1.PENDING_PROVISION_KEYS);
20
+ /**
21
+ * Tickets the host told provisioning to stop following (`abandonPending`),
22
+ * comma-separated. A lookup by name skips them, so an abandoned provision is
23
+ * never resumed.
24
+ */
25
+ exports.ABANDONED_TICKETS_KEY = 'PROVISION_TICKET_ABANDONED';
26
+ /** The newest abandoned ids kept; older ones are long finished or failed. */
27
+ const MAX_ABANDONED = 10;
28
+ /**
29
+ * The recorded ticket, with the name, backend and account it was requested
30
+ * for. Only a ticket from this backend and account is ours to poll, and one
31
+ * for another name is a different database.
32
+ */
33
+ function recordedTicket(env) {
34
+ const ticketId = env[env_file_1.PENDING_PROVISION_KEYS.ticketId];
35
+ if (!ticketId)
36
+ return undefined;
37
+ const attempts = Number.parseInt(env[env_file_1.PENDING_PROVISION_KEYS.attempts] ?? '', 10);
38
+ return {
39
+ ticketId,
40
+ databaseName: env[env_file_1.PENDING_PROVISION_KEYS.databaseName] ?? '',
41
+ apiEndpoint: env[env_file_1.PENDING_PROVISION_KEYS.apiEndpoint] ?? '',
42
+ userId: env[env_file_1.PENDING_PROVISION_KEYS.userId] ?? '',
43
+ attempts: Number.isFinite(attempts) && attempts > 0 ? attempts : 0,
44
+ };
45
+ }
46
+ function recordPendingTicket(content, ticket) {
47
+ return (0, env_file_1.mergeEnv)(content, {
48
+ [env_file_1.PENDING_PROVISION_KEYS.ticketId]: ticket.ticketId,
49
+ [env_file_1.PENDING_PROVISION_KEYS.databaseName]: ticket.databaseName,
50
+ [env_file_1.PENDING_PROVISION_KEYS.apiEndpoint]: ticket.apiEndpoint,
51
+ [env_file_1.PENDING_PROVISION_KEYS.userId]: ticket.userId,
52
+ [env_file_1.PENDING_PROVISION_KEYS.attempts]: String(ticket.attempts),
53
+ });
54
+ }
55
+ function clearPendingTicket(content) {
56
+ return (0, env_file_1.removeEnvKeys)(content, KEYS);
57
+ }
58
+ /** Comment the record out (`# archived <date>: …`), keeping it readable. */
59
+ function archivePendingTicket(content, archivedOn) {
60
+ return (0, env_file_1.archiveBindingKeys)(content, KEYS, archivedOn);
61
+ }
62
+ function abandonedTickets(env) {
63
+ return new Set((env[exports.ABANDONED_TICKETS_KEY] ?? '')
64
+ .split(',')
65
+ .map((id) => id.trim())
66
+ .filter(Boolean));
67
+ }
68
+ /** Add ticket ids to the abandoned list. */
69
+ function recordAbandoned(content, ticketIds) {
70
+ const ids = [...new Set([...abandonedTickets((0, env_file_1.readEnv)(content)), ...ticketIds])].slice(-MAX_ABANDONED);
71
+ return (0, env_file_1.mergeEnv)(content, { [exports.ABANDONED_TICKETS_KEY]: ids.join(',') });
72
+ }
@@ -0,0 +1,71 @@
1
+ import { type RunCommand } from '../app-workspace';
2
+ export type ProvisionInput = {
3
+ databaseName: string;
4
+ /** `ensure` keeps a live binding; `replace` swaps the binding for a new database. */
5
+ mode?: 'ensure' | 'replace';
6
+ /**
7
+ * Replace mode: the DATABASE_ID the caller expects bound (null: none). Any
8
+ * other binding fails `binding-changed`. Undefined replaces whatever is bound.
9
+ */
10
+ replaces?: string | null;
11
+ /** Host only: stop following an in-flight provision and request a new one. */
12
+ abandonPending?: boolean;
13
+ /**
14
+ * Host only: bind this database, which the signed-in account owns and is
15
+ * active, without requesting one (recovery after `name-taken`).
16
+ */
17
+ adopt?: string;
18
+ };
19
+ export type ProvisionFailureCode = 'signed-out' | 'backend-unreachable' | 'db-missing' | 'db-suspended' | 'account-mismatch' | 'binding-live' | 'binding-changed' | 'pending-other' | 'ticket-lost' | 'provision-stuck' | 'name-taken' | 'binding-not-saved' | 'env-write-failed' | 'reprovision-not-allowed' | 'failed';
20
+ export type ProvisionDatabaseDetails = {
21
+ success: boolean;
22
+ message: string;
23
+ /** Why it failed; absent on success and on a pending result. */
24
+ code?: ProvisionFailureCode;
25
+ databaseId?: string;
26
+ databaseName?: string;
27
+ ownerId?: string;
28
+ /** True when an existing live binding was kept: nothing changed. */
29
+ skipped?: boolean;
30
+ /**
31
+ * True when the call stopped waiting while the provision is still running.
32
+ * Calling again with the same database_name resumes it.
33
+ */
34
+ pending?: boolean;
35
+ /** The provision ticket, on a pending or stuck result. */
36
+ ticketId?: string;
37
+ /** True when this call finished a provision an earlier call had started. */
38
+ resumed?: boolean;
39
+ /** True when that provision was found on the backend by name, not in `.env`. */
40
+ recovered?: boolean;
41
+ /** The database the new binding replaced (replace mode). */
42
+ replaced?: string;
43
+ /** With `name-taken`: a database of this account holds the name. Never bound automatically. */
44
+ heldBy?: {
45
+ databaseId: string;
46
+ yours: boolean;
47
+ createdAt?: string;
48
+ };
49
+ };
50
+ type Details = ProvisionDatabaseDetails;
51
+ /** The DATABASE_ID in a project's `.env`, if any. */
52
+ export declare function boundDatabaseId(cwd: string): Promise<string | undefined>;
53
+ /**
54
+ * Provision the project's database, or keep the one it is bound to. The host
55
+ * entry point: replace mode refuses a live binding here, and `abandonPending`
56
+ * and `adopt` are honoured.
57
+ */
58
+ export declare function provisionDatabase(input: ProvisionInput, ctx: {
59
+ cwd: string;
60
+ signal?: AbortSignal;
61
+ }, exec?: RunCommand): Promise<Details>;
62
+ /**
63
+ * The provision_database tool's entry point: its `reprovision: true` may
64
+ * replace a live binding (a clean rebuild), unless the host turned tool
65
+ * reprovisioning off.
66
+ */
67
+ export declare function provisionFromTool(input: Pick<ProvisionInput, 'databaseName' | 'mode' | 'replaces'>, ctx: {
68
+ cwd: string;
69
+ signal?: AbortSignal;
70
+ }, exec?: RunCommand): Promise<Details>;
71
+ export {};