@persql/sdk 1.2.0 → 1.2.1

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.
package/dist/index.d.ts CHANGED
@@ -1,9 +1,11 @@
1
+ /** Wire-shape query result — columns plus positional rows, before the SDK reshapes rows into objects. */
1
2
  interface RawQueryResult {
2
3
  columns: string[];
3
4
  rows: unknown[][];
4
5
  rowsRead: number;
5
6
  rowsWritten: number;
6
7
  }
8
+ /** In-process driver backing `new PerSQL({ local })` — runs SQL through `better-sqlite3` instead of the HTTP API. */
7
9
  declare class LocalDriver {
8
10
  private readonly path;
9
11
  private db;
@@ -46,7 +48,14 @@ declare class LocalDriver {
46
48
  * );
47
49
  */
48
50
 
51
+ /** Machine-readable category of a SQL failure, parsed from the engine's error message. */
49
52
  type SqlErrorKind = "unique_violation" | "not_null_violation" | "fk_violation" | "check_violation" | "unknown_table" | "unknown_column" | "syntax_error" | "type_mismatch" | "database_locked" | "too_many_params" | "readonly" | "unknown";
53
+ /**
54
+ * Structured SQL error envelope — the failure kind plus the table,
55
+ * column, constraint, and hint parsed from the engine message.
56
+ * Available as `PerSQLError.detail` so callers can branch on
57
+ * `detail.kind` instead of string-matching messages.
58
+ */
50
59
  interface SqlErrorDetail {
51
60
  kind: SqlErrorKind;
52
61
  message: string;
@@ -60,6 +69,7 @@ interface SqlErrorDetail {
60
69
  near?: string;
61
70
  hint?: string;
62
71
  }
72
+ /** Constructor options for `PerSQL`. */
63
73
  interface PerSQLOptions {
64
74
  /** Bearer token. Get one at https://console.persql.com → Tokens. */
65
75
  token?: string;
@@ -82,6 +92,10 @@ interface PerSQLOptions {
82
92
  */
83
93
  local?: string;
84
94
  }
95
+ /**
96
+ * Result of a SQL statement — column names, positional rows, and the
97
+ * same rows reshaped as objects (`data`).
98
+ */
85
99
  interface QueryResult<T = Record<string, unknown>> {
86
100
  /** Column names in result-set order. */
87
101
  columns: string[];
@@ -116,6 +130,7 @@ interface QueryMeta {
116
130
  } | null;
117
131
  queryLogId: string;
118
132
  }
133
+ /** Options for `db.batch()` — transaction wrapping and idempotency / plan-replay keys. */
119
134
  interface BatchOptions {
120
135
  /** Wrap all statements in BEGIN/COMMIT. Rolls back on first error. */
121
136
  transaction?: boolean;
@@ -212,6 +227,7 @@ interface SchemaDoctorReport {
212
227
  tablesScanned: number;
213
228
  generatedAt: string;
214
229
  }
230
+ /** Per-call options for `db.query()` — idempotency and plan-replay keys. */
215
231
  interface QueryOptions {
216
232
  idempotencyKey?: string;
217
233
  /** See `BatchOptions.planKey`. */
@@ -219,6 +235,7 @@ interface QueryOptions {
219
235
  /** See `BatchOptions.planStep`. */
220
236
  planStep?: string;
221
237
  }
238
+ /** One user table and its row count, as returned by `db.tables()`. */
222
239
  interface TableInfo {
223
240
  name: string;
224
241
  rowCount: number;
@@ -246,6 +263,7 @@ interface BatchExpect {
246
263
  rowsWrittenAtLeast?: number;
247
264
  rowsWrittenAtMost?: number;
248
265
  }
266
+ /** One entry of a `db.batch()` call — SQL, positional params, and an optional server-side assertion. */
249
267
  interface Statement {
250
268
  sql: string;
251
269
  params?: unknown[];
@@ -290,15 +308,22 @@ type ValidateResult = {
290
308
  error: string;
291
309
  errorDetail?: SqlErrorDetail;
292
310
  };
311
+ /**
312
+ * Thrown for any failed API call. `status` carries the HTTP status;
313
+ * `detail` (set on /query and /batch failures) is the parsed SQL
314
+ * error envelope.
315
+ */
293
316
  declare class PerSQLError extends Error {
294
317
  status: number;
295
318
  detail?: SqlErrorDetail;
296
319
  constructor(status: number, message: string, detail?: SqlErrorDetail);
297
320
  }
321
+ /** Thrown on HTTP 429. `retryAfterSeconds` echoes the server's Retry-After header. */
298
322
  declare class RateLimitError extends PerSQLError {
299
323
  retryAfterSeconds: number;
300
324
  constructor(retryAfterSeconds: number, message?: string);
301
325
  }
326
+ /** One approval rule a blocked write matched — which glob hit which table, and whether it requires approval or denies outright. */
302
327
  interface ApprovalRuleHit {
303
328
  ruleId: string;
304
329
  tableGlob: string;
@@ -325,6 +350,16 @@ declare class ApprovalRequiredError extends PerSQLError {
325
350
  expiresAt: string;
326
351
  });
327
352
  }
353
+ /**
354
+ * Entry point of the SDK. Construct with a bearer token — or with
355
+ * `{ local: ":memory:" }` for an in-process SQLite test database —
356
+ * then call `database()` for a handle to query.
357
+ *
358
+ * ```ts
359
+ * const persql = new PerSQL({ token: process.env.PERSQL_TOKEN! });
360
+ * const db = persql.database("acme/orders");
361
+ * ```
362
+ */
328
363
  declare class PerSQL {
329
364
  /** @internal — exposed read-only so PerSQLDatabase can build URLs. */
330
365
  readonly token: string;
@@ -406,6 +441,7 @@ declare class PerSQL {
406
441
  */
407
442
  get support(): SupportClient;
408
443
  }
444
+ /** Input for `persql.support.createTicket()`. */
409
445
  interface CreateSupportTicketOptions {
410
446
  subject: string;
411
447
  body: string;
@@ -414,6 +450,7 @@ interface CreateSupportTicketOptions {
414
450
  /** Caller-supplied extras merged into the ticket's diagnostic context. */
415
451
  errorContext?: Record<string, unknown>;
416
452
  }
453
+ /** Files support tickets from SDK code. Reached via `persql.support`. */
417
454
  declare class SupportClient {
418
455
  private readonly client;
419
456
  constructor(client: PerSQL);
@@ -421,6 +458,7 @@ declare class SupportClient {
421
458
  id: string;
422
459
  }>;
423
460
  }
461
+ /** Database metadata as returned by `persql.databases.create()` and `list()`. */
424
462
  interface DatabaseInfo {
425
463
  id: string;
426
464
  namespaceId: string;
@@ -435,6 +473,7 @@ interface DatabaseInfo {
435
473
  createdAt: string;
436
474
  updatedAt: string;
437
475
  }
476
+ /** Input for `persql.databases.create()`. */
438
477
  interface CreateDatabaseOptions {
439
478
  /** Display name, 1-50 chars. */
440
479
  name: string;
@@ -466,6 +505,11 @@ declare class PerSQLDatabases {
466
505
  nextCursor: string | null;
467
506
  }>;
468
507
  }
508
+ /**
509
+ * Handle to one database, addressed as `<namespace>/<slug>`. Obtained
510
+ * via `persql.database(...)`; all query, schema, branch, approval, and
511
+ * tool helpers live here.
512
+ */
469
513
  declare class PerSQLDatabase {
470
514
  private readonly client;
471
515
  readonly namespace: string;
@@ -730,7 +774,7 @@ declare class PerSQLDatabase {
730
774
  runTool(input: {
731
775
  sql: string;
732
776
  params?: unknown[];
733
- }): Promise<QueryResult<Record<string, unknown>>>;
777
+ }): Promise<QueryResult>;
734
778
  /**
735
779
  * Generate a *bundle* of typed tools — one per table — instead of one
736
780
  * generic `query` tool. Agents perform dramatically better with narrow
@@ -762,11 +806,13 @@ declare class PerSQLDatabase {
762
806
  */
763
807
  asTools(): Promise<DatabaseToolBundle>;
764
808
  }
809
+ /** Tool definition in the Anthropic Messages API shape — one `tools` entry for `messages.create`. */
765
810
  interface AnthropicTool {
766
811
  name: string;
767
812
  description: string;
768
813
  input_schema: Record<string, unknown>;
769
814
  }
815
+ /** Tool definition in the OpenAI Chat Completions shape — one `tools` entry for `chat.completions.create`. */
770
816
  interface OpenAiTool {
771
817
  type: "function";
772
818
  function: {
@@ -775,6 +821,7 @@ interface OpenAiTool {
775
821
  parameters: Record<string, unknown>;
776
822
  };
777
823
  }
824
+ /** Output of `db.asTool()` — one SQL-query tool rendered in every supported framework shape. */
778
825
  interface SingleToolBundle {
779
826
  /** Anthropic shape — pass as one entry of `tools` to messages.create. */
780
827
  anthropic: AnthropicTool;
@@ -789,6 +836,7 @@ interface SingleToolBundle {
789
836
  /** OpenAI Agents SDK shape — hand to `Agent({ tools: [...] })`. */
790
837
  openaiAgents: () => OpenAiAgentsTool[];
791
838
  }
839
+ /** Output of `db.asTools()` — typed per-table tools plus the `run` dispatcher, in every supported framework shape. */
792
840
  interface DatabaseToolBundle {
793
841
  /** Tool definitions in Anthropic shape — pass as `tools` to messages.create. */
794
842
  anthropic: AnthropicTool[];
@@ -822,11 +870,13 @@ interface DatabaseToolBundle {
822
870
  */
823
871
  openaiAgents: () => OpenAiAgentsTool[];
824
872
  }
873
+ /** Tool shape for the Vercel AI SDK — spread into `streamText({ tools })`. */
825
874
  interface AiSdkTool {
826
875
  description: string;
827
876
  inputSchema: Record<string, unknown>;
828
877
  execute: (input: Record<string, unknown>) => Promise<unknown>;
829
878
  }
879
+ /** Tool shape for Mastra — wrap with `createTool()` if you need the Mastra envelope. */
830
880
  interface MastraTool {
831
881
  id: string;
832
882
  description: string;
@@ -858,11 +908,14 @@ interface OpenAiAgentsTool {
858
908
  parameters: Record<string, unknown>;
859
909
  invoke: (input: Record<string, unknown>) => Promise<unknown>;
860
910
  }
911
+ /** Kind of row change reported by `db.subscribe()` and `db.changes()`. */
861
912
  type SubscribeChangeKind = "update" | "insert" | "delete" | "schema" | "unknown";
913
+ /** One row-change event delivered to `SubscribeOptions.onChange`. */
862
914
  interface SubscribeChange {
863
915
  table: string;
864
916
  kind: SubscribeChangeKind;
865
917
  }
918
+ /** Options for `db.subscribe()` — table filter and event callbacks. */
866
919
  interface SubscribeOptions {
867
920
  /** Tables to listen to. Omit / empty / `["*"]` = all tables. */
868
921
  tables?: string[];
@@ -895,7 +948,12 @@ interface BranchInfo {
895
948
  name: string;
896
949
  };
897
950
  }
951
+ /**
952
+ * How a branch is applied back to its parent: `schema` applies the DDL
953
+ * delta; `promote` replaces the parent's contents wholesale.
954
+ */
898
955
  type BranchMergeMode = "schema" | "promote";
956
+ /** One DDL operation in a branch-merge plan: create, drop, or replace. */
899
957
  type BranchMergePlanStep = {
900
958
  op: "create";
901
959
  type: string;
@@ -912,6 +970,7 @@ type BranchMergePlanStep = {
912
970
  beforeSql: string;
913
971
  afterSql: string;
914
972
  };
973
+ /** Outcome of `db.branches.merge()` — what was applied, the pre-merge snapshot, and the plan when previewing. */
915
974
  interface BranchMergeResult {
916
975
  mode: BranchMergeMode;
917
976
  preview: boolean;
@@ -925,6 +984,7 @@ interface BranchMergeResult {
925
984
  };
926
985
  note?: string;
927
986
  }
987
+ /** Options for `db.branches.upsert()`. */
928
988
  interface BranchUpsertOptions {
929
989
  /** Human-readable name. Defaults to `<parent name> (<ref>)`. */
930
990
  name?: string;
@@ -933,6 +993,7 @@ interface BranchUpsertOptions {
933
993
  /** Auto-delete after N days (1..30). Null/undefined = keep until deleted. */
934
994
  ttlDays?: number | null;
935
995
  }
996
+ /** Options for `db.branches.merge()`. */
936
997
  interface BranchMergeOptions {
937
998
  /** Default `schema`. `promote` replaces parent contents wholesale. */
938
999
  mode?: BranchMergeMode;
@@ -943,6 +1004,7 @@ interface BranchMergeOptions {
943
1004
  /** Default true — captures `pre-merge:<ref>` snapshot of the parent. */
944
1005
  snapshot?: boolean;
945
1006
  }
1007
+ /** Output of `db.proposals.propose()` — the EXPLAIN plan, estimated affected rows, and the single-use execution token. */
946
1008
  interface ProposalPlan {
947
1009
  sql: string;
948
1010
  plan: unknown[][];
@@ -951,10 +1013,12 @@ interface ProposalPlan {
951
1013
  expiresAt: string;
952
1014
  action: "read" | "write" | "admin";
953
1015
  }
1016
+ /** Options for `db.proposals.propose()`. */
954
1017
  interface ProposeOptions {
955
1018
  params?: unknown[];
956
1019
  ttlSec?: number;
957
1020
  }
1021
+ /** Lifecycle state of an approval token, as returned by `db.approvals.get()` and `poll()`. */
958
1022
  interface ApprovalStatus {
959
1023
  status: "pending" | "approved" | "denied";
960
1024
  hits: ApprovalRuleHit[];
@@ -962,6 +1026,7 @@ interface ApprovalStatus {
962
1026
  expiresAt: string;
963
1027
  decidedAt: string | null;
964
1028
  }
1029
+ /** Pacing and cancellation options for `db.approvals.poll()`. */
965
1030
  interface PollApprovalOptions {
966
1031
  /** Default 2000ms. */
967
1032
  intervalMs?: number;
@@ -970,6 +1035,7 @@ interface PollApprovalOptions {
970
1035
  /** Cancellation signal. */
971
1036
  signal?: AbortSignal;
972
1037
  }
1038
+ /** Callbacks and pacing for `db.approvals.subscribe()`. */
973
1039
  interface SubscribeApprovalOptions {
974
1040
  onApprovalRequired?: (event: ApprovalEvent) => void;
975
1041
  onApprovalResolved?: (event: ApprovalEvent) => void;
@@ -980,11 +1046,17 @@ interface SubscribeApprovalOptions {
980
1046
  /** Logged-by-default error sink. */
981
1047
  onError?: (err: Error) => void;
982
1048
  }
1049
+ /** One approval state change delivered by `db.approvals.subscribe()`. */
983
1050
  interface ApprovalEvent {
984
1051
  approvalToken: string;
985
1052
  status: "pending" | "approved" | "denied";
986
1053
  hits: ApprovalRuleHit[];
987
1054
  }
1055
+ /**
1056
+ * Approval-token lifecycle for writes blocked by a require_approval
1057
+ * rule — look up, poll, subscribe, and redeem. Reached via
1058
+ * `db.approvals`.
1059
+ */
988
1060
  declare class PerSQLApprovals {
989
1061
  private readonly client;
990
1062
  private readonly namespace;
@@ -1023,6 +1095,7 @@ declare class PerSQLApprovals {
1023
1095
  */
1024
1096
  redeem<T = Record<string, unknown>>(approvalToken: string): Promise<QueryResult<T> | QueryResult<T>[]>;
1025
1097
  }
1098
+ /** A require_approval / deny rule on a database, as returned by `db.approvalRules.list()`. */
1026
1099
  interface ApprovalRule {
1027
1100
  id: string;
1028
1101
  databaseId: string;
@@ -1032,11 +1105,13 @@ interface ApprovalRule {
1032
1105
  createdById: string;
1033
1106
  createdAt: string;
1034
1107
  }
1108
+ /** Input for `db.approvalRules.create()`. */
1035
1109
  interface CreateApprovalRuleInput {
1036
1110
  tableGlob: string;
1037
1111
  action: "require_approval" | "deny";
1038
1112
  note?: string;
1039
1113
  }
1114
+ /** Manage the require_approval / deny rules of a database. Reached via `db.approvalRules`. */
1040
1115
  declare class PerSQLApprovalRules {
1041
1116
  private readonly client;
1042
1117
  private readonly namespace;
@@ -1050,6 +1125,11 @@ declare class PerSQLApprovalRules {
1050
1125
  deleted: true;
1051
1126
  }>;
1052
1127
  }
1128
+ /**
1129
+ * Two-phase writes: `propose()` pre-flights the SQL and mints a
1130
+ * single-use execution token; `apply()` redeems it. Reached via
1131
+ * `db.proposals`.
1132
+ */
1053
1133
  declare class PerSQLProposals {
1054
1134
  private readonly client;
1055
1135
  private readonly namespace;
@@ -1075,6 +1155,7 @@ declare class PerSQLProposals {
1075
1155
  */
1076
1156
  apply<T = Record<string, unknown>>(executionToken: string): Promise<QueryResult<T>>;
1077
1157
  }
1158
+ /** Output of `db.branches.claim()` — the fresh branch plus the scoped token minted for it. */
1078
1159
  interface ClaimedBranch {
1079
1160
  branchRef: string;
1080
1161
  databaseId: string;
@@ -1087,6 +1168,7 @@ interface ClaimedBranch {
1087
1168
  expiresAt: string;
1088
1169
  outcome: "created" | "reset";
1089
1170
  }
1171
+ /** Options for `db.branches.claim()`. */
1090
1172
  interface ClaimBranchOptions {
1091
1173
  /** Explicit ref. Omit to auto-generate from `purpose`. */
1092
1174
  ref?: string;
@@ -1098,6 +1180,11 @@ interface ClaimBranchOptions {
1098
1180
  role?: "read" | "write" | "admin";
1099
1181
  region?: string;
1100
1182
  }
1183
+ /**
1184
+ * Branch management for one database — list, create-or-reset, delete,
1185
+ * merge, pin (handoff), claim (lease), and migration rendering.
1186
+ * Reached via `db.branches`.
1187
+ */
1101
1188
  declare class PerSQLBranches {
1102
1189
  private readonly client;
1103
1190
  private readonly namespace;
package/dist/index.js CHANGED
@@ -10,7 +10,7 @@ import {
10
10
  PerSQLProposals,
11
11
  RateLimitError,
12
12
  SupportClient
13
- } from "./chunk-GH75ERQ6.js";
13
+ } from "./chunk-VJ776W3K.js";
14
14
  export {
15
15
  ApprovalRequiredError,
16
16
  PerSQL,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@persql/sdk",
3
- "version": "1.2.0",
3
+ "version": "1.2.1",
4
4
  "description": "TypeScript SDK for PerSQL — SQLite databases on the edge for AI agents",
5
5
  "license": "MIT",
6
6
  "type": "module",