@sharadox/database-web-library 0.1.0-database-backup.12 → 0.1.0-database-backup.13

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/README.md CHANGED
@@ -124,7 +124,7 @@ A daily, encrypted copy of the production database in an S3 bucket nobody can em
124
124
  restores. The pieces, usable on their own:
125
125
 
126
126
  - `dumpDatabase(url, { program?, run? })` (`backup-dump`): `pg_dump` of the public schema as plain INSERTs, without
127
- owners or grants (needs `pg_dump` of the server's major version or later). The address goes in `PGDATABASE`, never
127
+ owners or grants (needs `pg_dump` of the server's major version or later). The address goes in libpq's environment (`PGHOST`, `PGPASSWORD`…), never
128
128
  on the command line. `restorableSql(dump)` drops psql's own commands (`\restrict`) so any Postgres can replay it.
129
129
  - `encryptBackup(bytes, key)`, `decryptBackup(bytes, key)` (`backup-encryption`): AES-256-GCM under
130
130
  `BACKUP_ENCRYPTION_KEY` (at least 32 characters, kept apart from the backups); any change is detected.
@@ -1,15 +1,17 @@
1
1
  import type { DumpOptions } from "@sharadox/database-web-library/backup-dump/type";
2
2
  /**
3
3
  * A plain SQL dump of the database's public schema, made by `pg_dump`: one INSERT per row (so any Postgres, PGlite
4
- * included, can replay it), without owners or grants. The address goes in the environment (`PGDATABASE`), never on
5
- * the command line, so the password isn't visible in the process list.
4
+ * included, can replay it), without owners or grants. The address goes in libpq's environment (`PGHOST`, `PGUSER`,
5
+ * `PGPASSWORD`…), never on the command line, so the password isn't visible in the process list.
6
6
  *
7
7
  * @example
8
8
  * const sql = await dumpDatabase(process.env.DATABASE_BACKUP_URL!);
9
9
  */
10
10
  export declare function dumpDatabase(databaseUrl: string, { program, run }?: DumpOptions): Promise<Uint8Array>;
11
11
  /**
12
- * A dump without psql's own commands (`\restrict`, `\unrestrict`, which pg_dump 18 adds), so it runs as plain SQL.
12
+ * A dump without psql's own commands (`\restrict`, `\unrestrict`, which pg_dump 18 adds) and without creating the
13
+ * public schema (which every Postgres has) or setting Neon's internal column options, so it runs as plain SQL in an
14
+ * empty database.
13
15
  *
14
16
  * @example
15
17
  * await sql.raw(restorableSql(text)).execute(database);
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/backup-dump/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAiB,MAAM,iDAAiD,CAAC;AAalG;;;;;;;GAOG;AACH,wBAAsB,YAAY,CAChC,WAAW,EAAE,MAAM,EACnB,EAAE,OAAmB,EAAE,GAAgB,EAAE,GAAE,WAAgB,GAC1D,OAAO,CAAC,UAAU,CAAC,CAIrB;AAED;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAKlD"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/backup-dump/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAiB,MAAM,iDAAiD,CAAC;AAalG;;;;;;;GAOG;AACH,wBAAsB,YAAY,CAChC,WAAW,EAAE,MAAM,EACnB,EAAE,OAAmB,EAAE,GAAgB,EAAE,GAAE,WAAgB,GAC1D,OAAO,CAAC,UAAU,CAAC,CAWrB;AASD;;;;;;;GAOG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAKlD"}
@@ -4,19 +4,33 @@ const runProgram = (program, parameters, environment) => new Promise((resolve, r
4
4
  });
5
5
  /**
6
6
  * A plain SQL dump of the database's public schema, made by `pg_dump`: one INSERT per row (so any Postgres, PGlite
7
- * included, can replay it), without owners or grants. The address goes in the environment (`PGDATABASE`), never on
8
- * the command line, so the password isn't visible in the process list.
7
+ * included, can replay it), without owners or grants. The address goes in libpq's environment (`PGHOST`, `PGUSER`,
8
+ * `PGPASSWORD`…), never on the command line, so the password isn't visible in the process list.
9
9
  *
10
10
  * @example
11
11
  * const sql = await dumpDatabase(process.env.DATABASE_BACKUP_URL!);
12
12
  */
13
13
  export async function dumpDatabase(databaseUrl, { program = "pg_dump", run = runProgram } = {}) {
14
+ const url = new URL(databaseUrl);
14
15
  return run(program, ["--schema=public", "--inserts", "--no-owner", "--no-privileges", "--quote-all-identifiers"], {
15
- PGDATABASE: databaseUrl,
16
+ PGHOST: url.hostname,
17
+ PGPORT: url.port || "5432",
18
+ PGUSER: decodeURIComponent(url.username),
19
+ PGPASSWORD: decodeURIComponent(url.password),
20
+ PGDATABASE: url.pathname.slice(1),
21
+ // Neon always speaks TLS; without a mode in the address, the certificate is checked too.
22
+ PGSSLMODE: url.searchParams.get("sslmode") ?? "verify-full",
16
23
  });
17
24
  }
18
25
  /**
19
- * A dump without psql's own commands (`\restrict`, `\unrestrict`, which pg_dump 18 adds), so it runs as plain SQL.
26
+ * Statements only the source could run: creating the public schema (every Postgres has it) and Neon's internal column
27
+ * option `pg_attver`, which other Postgres builds don't know.
28
+ */
29
+ const SOURCE_ONLY = /^(CREATE SCHEMA "public";|COMMENT ON SCHEMA "public" IS .*;|ALTER TABLE ONLY .* SET \(pg_attver=\d+\);)$/;
30
+ /**
31
+ * A dump without psql's own commands (`\restrict`, `\unrestrict`, which pg_dump 18 adds) and without creating the
32
+ * public schema (which every Postgres has) or setting Neon's internal column options, so it runs as plain SQL in an
33
+ * empty database.
20
34
  *
21
35
  * @example
22
36
  * await sql.raw(restorableSql(text)).execute(database);
@@ -24,7 +38,7 @@ export async function dumpDatabase(databaseUrl, { program = "pg_dump", run = run
24
38
  export function restorableSql(dump) {
25
39
  return dump
26
40
  .split("\n")
27
- .filter((line) => !line.startsWith("\\"))
41
+ .filter((line) => !line.startsWith("\\") && !SOURCE_ONLY.test(line))
28
42
  .join("\n");
29
43
  }
30
44
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/backup-dump/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAG9C,MAAM,UAAU,GAAkB,CAAC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,SAAS,CAAC,IAAI,aAAa;KAC5D,CAAC,CAAC;AACL,CAAC;AAED;;;GAGG;AACH,MAAM,WAAW,GACf,0GAA0G,CAAC;AAE7G;;;;;;;GAOG;AACH,MAAM,UAAU,aAAa,CAAC,IAAY;IACxC,OAAO,IAAI;SACR,KAAK,CAAC,IAAI,CAAC;SACX,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;SACnE,IAAI,CAAC,IAAI,CAAC,CAAC;AAChB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sharadox/database-web-library",
3
- "version": "0.1.0-database-backup.12",
3
+ "version": "0.1.0-database-backup.13",
4
4
  "description": "Typed, injection-safe Postgres for web applications: queries, migrations, backups and restore drills.",
5
5
  "private": false,
6
6
  "license": "SEE LICENSE IN LICENCA.md",