@otwdesign/db-core 1.2.0 → 1.3.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 (49) hide show
  1. package/dist/backup/api.d.ts +55 -0
  2. package/dist/backup/api.js +441 -0
  3. package/dist/backup/api.js.map +1 -0
  4. package/dist/backup/catalog.d.ts +47 -0
  5. package/dist/backup/catalog.js +348 -0
  6. package/dist/backup/catalog.js.map +1 -0
  7. package/dist/backup/dump.d.ts +48 -0
  8. package/dist/backup/dump.js +860 -0
  9. package/dist/backup/dump.js.map +1 -0
  10. package/dist/backup/format.d.ts +38 -0
  11. package/dist/backup/format.js +70 -0
  12. package/dist/backup/format.js.map +1 -0
  13. package/dist/backup/pg-tools.d.ts +29 -0
  14. package/dist/backup/pg-tools.js +154 -0
  15. package/dist/backup/pg-tools.js.map +1 -0
  16. package/dist/backup/restore.d.ts +41 -0
  17. package/dist/backup/restore.js +196 -0
  18. package/dist/backup/restore.js.map +1 -0
  19. package/dist/backup/split.d.ts +18 -0
  20. package/dist/backup/split.js +316 -0
  21. package/dist/backup/split.js.map +1 -0
  22. package/dist/backup/staging.d.ts +29 -0
  23. package/dist/backup/staging.js +96 -0
  24. package/dist/backup/staging.js.map +1 -0
  25. package/dist/browser.d.ts +5 -0
  26. package/dist/drivers/pglite.js +57 -0
  27. package/dist/drivers/pglite.js.map +1 -1
  28. package/dist/drivers/postgres.js +45 -0
  29. package/dist/drivers/postgres.js.map +1 -1
  30. package/dist/handler.d.ts +5 -0
  31. package/dist/handler.js +48 -1
  32. package/dist/handler.js.map +1 -1
  33. package/dist/index.d.ts +5 -0
  34. package/dist/types.d.ts +30 -1
  35. package/package.json +2 -2
  36. package/src/backup/api.ts +543 -0
  37. package/src/backup/catalog.ts +381 -0
  38. package/src/backup/dump.ts +1102 -0
  39. package/src/backup/format.ts +114 -0
  40. package/src/backup/pg-tools.ts +185 -0
  41. package/src/backup/restore.ts +270 -0
  42. package/src/backup/split.ts +300 -0
  43. package/src/backup/staging.ts +143 -0
  44. package/src/browser.ts +5 -0
  45. package/src/drivers/pglite.ts +55 -0
  46. package/src/drivers/postgres.ts +46 -0
  47. package/src/handler.ts +56 -1
  48. package/src/index.ts +6 -0
  49. package/src/types.ts +29 -1
package/src/types.ts CHANGED
@@ -47,6 +47,11 @@ export interface Capabilities {
47
47
  * offers them with `databases()` and `withDatabase()`, and a connection may be pinned to its own.
48
48
  */
49
49
  databases?: boolean
50
+ /**
51
+ * Whole-database backups and restores: PostgreSQL and PGlite. The API sets it: a driver offers
52
+ * them with `session()`.
53
+ */
54
+ backup?: boolean
50
55
  }
51
56
 
52
57
  /** What a schema-tree entry is: SQLite and MySQL have tables and views, PostgreSQL the rest too. */
@@ -271,9 +276,32 @@ export interface Driver {
271
276
  * on first use; whoever asked for it closes it.
272
277
  */
273
278
  withDatabase?(name: string): Driver
279
+ /**
280
+ * One transaction on a connection of its own, for a backup (`snapshot`: read-only, all of it as
281
+ * of one moment) or a restore (`write`: all of it or none). PostgreSQL and PGlite.
282
+ */
283
+ session?(mode: 'snapshot' | 'write'): Promise<Session>
284
+ /**
285
+ * The URL pg_dump and pg_restore may connect with: a server's driver has one, PGlite's (a
286
+ * database in the host's own process) doesn't.
287
+ */
288
+ readonly pgUrl?: string
274
289
  close(): Promise<void>
275
290
  }
276
291
 
292
+ /** A backup's or a restore's transaction. Values come back as the server's text. */
293
+ export interface Session {
294
+ /** One statement, its values as $1, $2… (a Uint8Array as bytea). */
295
+ query(sql: string, values?: unknown[]): Promise<{ rows: (string | null)[][] }>
296
+ /** Commits (a restore) or rolls back (a backup), then lets the connection go. */
297
+ end(commit: boolean): Promise<void>
298
+ /**
299
+ * It holds the host while open: PGlite runs in the host's process, one transaction at a time.
300
+ * Finish quickly, without waiting on a slow download.
301
+ */
302
+ readonly exclusive: boolean
303
+ }
304
+
277
305
  /** Who is asking, as the host's `authorize()` says. */
278
306
  export interface Viewer {
279
307
  /** Recorded in the audit log, such as a user id or an email. */
@@ -292,7 +320,7 @@ export interface AuditEvent {
292
320
  connection: string
293
321
  /** The database it ran on, when it wasn't the connection's own. */
294
322
  database?: string
295
- action: 'query' | 'rows' | 'count' | 'apply' | 'export' | 'cancel'
323
+ action: 'query' | 'rows' | 'count' | 'apply' | 'export' | 'cancel' | 'backup' | 'restore'
296
324
  /** The SQL run, or for `rows`, the table read. */
297
325
  statement: string
298
326
  durationMs: number