@coffre/db 0.1.3 → 0.1.5

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/migrate.d.ts CHANGED
@@ -1,5 +1,8 @@
1
1
  import { Engine } from "./dialect.js";
2
+ import { PoolClient } from "pg";
2
3
  //#region src/migrate.d.ts
4
+ /** Reassert database privileges that pg_dump and CREATE DATABASE TEMPLATE do not carry. */
5
+ export declare function restrictDatabase(client: Pick<PoolClient, 'query'>): Promise<void>;
3
6
  /**
4
7
  * Beside this module: src/migrations in the workspace, and dist/migrations
5
8
  * in the built package, where the build copies them.
package/dist/migrate.js CHANGED
@@ -15,6 +15,15 @@ import { fileURLToPath } from "node:url";
15
15
  * whole run. Restricted runtime roles are Postgres-only (baseline/postgres.sql).
16
16
  */
17
17
  const MIGRATION_LOCK_KEY = "7165058122361679213";
18
+ /** Reassert database privileges that pg_dump and CREATE DATABASE TEMPLATE do not carry. */
19
+ async function restrictDatabase(client) {
20
+ await client.query(`DO $$ BEGIN
21
+ EXECUTE format(
22
+ 'REVOKE CREATE, TEMPORARY ON DATABASE %I FROM PUBLIC, coffre_app, coffre_runtime, coffre_vault, coffre_vault_runtime',
23
+ current_database()
24
+ );
25
+ END $$`);
26
+ }
18
27
  /**
19
28
  * Beside this module: src/migrations in the workspace, and dist/migrations
20
29
  * in the built package, where the build copies them.
@@ -72,14 +81,7 @@ async function postgresMigrator(url) {
72
81
  await client.query("SELECT pg_advisory_unlock($1::bigint)", [MIGRATION_LOCK_KEY]);
73
82
  },
74
83
  migrate: (migrationsFolder) => migrate(drizzle(client), { migrationsFolder }),
75
- async restrict() {
76
- await client.query(`DO $$ BEGIN
77
- EXECUTE format(
78
- 'REVOKE CREATE, TEMPORARY ON DATABASE %I FROM PUBLIC, coffre_app, coffre_runtime, coffre_vault, coffre_vault_runtime',
79
- current_database()
80
- );
81
- END $$`);
82
- },
84
+ restrict: () => restrictDatabase(client),
83
85
  async close() {
84
86
  client.release();
85
87
  await pool.end();
@@ -136,4 +138,4 @@ async function migrateDatabase(url, onPlan) {
136
138
  }
137
139
  }
138
140
  //#endregion
139
- export { migrateDatabase, migrationsFolder };
141
+ export { migrateDatabase, migrationsFolder, restrictDatabase };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coffre/db",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "description": "coffre's database: the schema, its migrations and the connections, on Postgres or SQLite.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -74,7 +74,7 @@
74
74
  "drizzle-orm": "0.45.2",
75
75
  "pg": "8.16.3",
76
76
  "pg-connection-string": "2.14.0",
77
- "@coffre/core": "0.1.3"
77
+ "@coffre/core": "0.1.5"
78
78
  },
79
79
  "devDependencies": {
80
80
  "@types/node": "26.1.1",
package/src/migrate.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { createHash } from 'node:crypto';
2
2
  import { readFile } from 'node:fs/promises';
3
3
  import { fileURLToPath } from 'node:url';
4
+ import type { PoolClient } from 'pg';
4
5
 
5
6
  import { engineOfUrl } from './connect.ts';
6
7
  import type { Engine } from './dialect.ts';
@@ -19,6 +20,16 @@ import { postgresConnection } from './postgres.ts';
19
20
 
20
21
  const MIGRATION_LOCK_KEY = '7165058122361679213';
21
22
 
23
+ /** Reassert database privileges that pg_dump and CREATE DATABASE TEMPLATE do not carry. */
24
+ export async function restrictDatabase(client: Pick<PoolClient, 'query'>): Promise<void> {
25
+ await client.query(`DO $$ BEGIN
26
+ EXECUTE format(
27
+ 'REVOKE CREATE, TEMPORARY ON DATABASE %I FROM PUBLIC, coffre_app, coffre_runtime, coffre_vault, coffre_vault_runtime',
28
+ current_database()
29
+ );
30
+ END $$`);
31
+ }
32
+
22
33
  interface JournalEntry {
23
34
  idx: number;
24
35
  when: number;
@@ -140,15 +151,7 @@ async function postgresMigrator(url: string): Promise<Migrator> {
140
151
  await client.query('SELECT pg_advisory_unlock($1::bigint)', [MIGRATION_LOCK_KEY]);
141
152
  },
142
153
  migrate: (migrationsFolder) => migrate(drizzle(client), { migrationsFolder }),
143
- async restrict() {
144
- // pg_dump carries table privileges, but not privileges on the database itself.
145
- await client.query(`DO $$ BEGIN
146
- EXECUTE format(
147
- 'REVOKE CREATE, TEMPORARY ON DATABASE %I FROM PUBLIC, coffre_app, coffre_runtime, coffre_vault, coffre_vault_runtime',
148
- current_database()
149
- );
150
- END $$`);
151
- },
154
+ restrict: () => restrictDatabase(client),
152
155
  async close() {
153
156
  client.release();
154
157
  await pool.end();