@bezoar/pglite 0.0.4 → 0.0.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/index.d.mts CHANGED
@@ -1,5 +1,5 @@
1
- import { PGliteInterface, PGliteOptions, ParserOptions, Transaction } from "@electric-sql/pglite";
2
1
  import { Executor } from "@bezoar/core";
2
+ import { PGliteInterface, PGliteOptions, ParserOptions, Transaction } from "@electric-sql/pglite";
3
3
  /**
4
4
  * The parsers that make a row match the type table of `@bezoar/schema`.
5
5
  * PGlite parses `date` and `timestamp` to a `Date`. The table declares them
@@ -12,11 +12,27 @@ export declare const PARSERS: ParserOptions;
12
12
  * depth of the transaction follows it.
13
13
  */
14
14
  export declare const SAVEPOINT = "bezoar_";
15
+ /**
16
+ * Takes a role for the rest of the transaction. The client has one session,
17
+ * so a scoped executor takes the role inside each transaction it opens
18
+ * rather than on the session, and an executor without a role over the same
19
+ * client is untouched.
20
+ */
21
+ export declare const ROLE: (role: string) => string;
15
22
  /**
16
23
  * What `pglite` takes: nothing for a database in memory, a path for a
17
24
  * database on a data directory, PGlite options, or a client the caller made.
18
25
  */
19
26
  export type Source = string | PGliteOptions | PGliteInterface;
27
+ /**
28
+ * The options of an executor. `role` scopes it: every statement runs as the
29
+ * role, through `SET LOCAL ROLE` inside a transaction, so the session of
30
+ * the client keeps its own role and another executor over the same client
31
+ * runs as it did.
32
+ */
33
+ export interface ExecutorOptions {
34
+ role?: string;
35
+ }
20
36
  /**
21
37
  * An {@link Executor} over PGlite. `client` is the PGlite instance, for a
22
38
  * caller that needs more of it than the executor exposes. `close` closes the
@@ -32,11 +48,13 @@ export interface PGliteExecutor extends Executor {
32
48
  export type Connection = PGliteInterface | Transaction;
33
49
  /**
34
50
  * The state the executors over one client share. `changed` is `true` after
35
- * an `exec` ran and before the client refreshed its array types.
51
+ * an `exec` ran and before the client refreshed its array types. `role` is
52
+ * the role the executors run as, if any.
36
53
  */
37
54
  export interface Session {
38
55
  readonly client: PGliteInterface;
39
56
  changed: boolean;
57
+ readonly role?: string;
40
58
  }
41
59
  /**
42
60
  * Builds a bezoar `Executor` over PGlite.
@@ -48,8 +66,15 @@ export interface Session {
48
66
  * function inside a transaction of the client. A transaction inside a
49
67
  * transaction is a savepoint.
50
68
  *
69
+ * With a role, every statement runs as the role: `SET LOCAL ROLE` inside a
70
+ * transaction the executor opens around it. The session of the client keeps
71
+ * its own role, so an executor without one over the same client, as
72
+ * `pglite(client)` makes, applies migrations and grants as the owner while
73
+ * the scoped one runs queries as the role.
74
+ *
51
75
  * @param source - Nothing for a database in memory, a path to a data
52
76
  * directory, PGlite options, or a client. A client the caller made stays
53
77
  * the caller's to close.
78
+ * @param options - The role to run as, if any.
54
79
  */
55
- export declare const pglite: (source?: Source) => PGliteExecutor;
80
+ export declare const pglite: (source?: Source, options?: ExecutorOptions) => PGliteExecutor;
package/.dist/index.mjs CHANGED
@@ -1,9 +1,11 @@
1
+ import { quote } from "@bezoar/core";
1
2
  import { PGlite, types } from "@electric-sql/pglite";
2
3
  const PARSERS = {
3
4
  [types.DATE]: (text) => text,
4
5
  [types.TIMESTAMP]: (text) => text
5
6
  };
6
7
  const SAVEPOINT = "bezoar_";
8
+ const ROLE = (role) => `SET LOCAL ROLE ${quote(role)}`;
7
9
  const outcome = (results) => ({
8
10
  rows: results.rows,
9
11
  count: results.affectedRows || results.rows.length
@@ -26,16 +28,25 @@ const savepoint = async (tx, depth, run) => {
26
28
  throw error;
27
29
  }
28
30
  };
31
+ const open = (session, client, run) => client.transaction(async (tx) => {
32
+ if (session.role !== void 0) await tx.exec(ROLE(session.role));
33
+ return run(tx);
34
+ });
29
35
  const defineExecutor = (session, connection, depth) => {
30
- const query = async (sql, params) => outcome(await connection.query(sql, [...params], { parsers: PARSERS }));
36
+ const scoped = session.role !== void 0 && "transaction" in connection;
37
+ const query = async (sql, params) => {
38
+ const statement = (on) => on.query(sql, [...params], { parsers: PARSERS });
39
+ return outcome(await (scoped ? open(session, connection, statement) : statement(connection)));
40
+ };
31
41
  const exec = async (sql) => {
32
- await connection.exec(sql);
42
+ if (scoped) await open(session, connection, (tx) => tx.exec(sql));
43
+ else await connection.exec(sql);
33
44
  session.changed = true;
34
45
  if (depth === 0) await refresh(session);
35
46
  };
36
47
  const transaction = async (run) => {
37
48
  if ("transaction" in connection) {
38
- const result = await connection.transaction((tx) => run(defineExecutor(session, tx, depth + 1)));
49
+ const result = await open(session, connection, (tx) => run(defineExecutor(session, tx, depth + 1)));
39
50
  await refresh(session);
40
51
  return result;
41
52
  }
@@ -47,14 +58,15 @@ const defineExecutor = (session, connection, depth) => {
47
58
  transaction
48
59
  };
49
60
  };
50
- const pglite = (source = {}) => {
61
+ const pglite = (source = {}, options = {}) => {
51
62
  let client;
52
63
  if (typeof source === "string") client = new PGlite(source);
53
64
  else if ("query" in source) client = source;
54
65
  else client = new PGlite(source);
55
66
  const executor = defineExecutor({
56
67
  client,
57
- changed: false
68
+ changed: false,
69
+ role: options.role
58
70
  }, client, 0);
59
71
  const close = async () => {
60
72
  await client.waitReady;
@@ -66,4 +78,4 @@ const pglite = (source = {}) => {
66
78
  close
67
79
  };
68
80
  };
69
- export { PARSERS, SAVEPOINT, pglite };
81
+ export { PARSERS, ROLE, SAVEPOINT, pglite };
package/README.md CHANGED
@@ -55,6 +55,28 @@ again. The executor refreshes them after each `exec`, and after each
55
55
  transaction that ran one, so a column such as `event_day[]` parses as an
56
56
  array as soon as `migrate` returns.
57
57
 
58
+ ## Roles
59
+
60
+ `pglite(source, { role })` makes an executor that runs every statement as
61
+ the role: each statement at the top level runs inside a transaction that
62
+ takes the role with `SET LOCAL ROLE`, and each transaction the executor
63
+ opens takes it first. The session of the client keeps its own role, so an
64
+ executor without one over the same client runs as the owner.
65
+
66
+ ```ts
67
+ const owner = pglite();
68
+ const web = pglite(owner.client, { role: "web" });
69
+
70
+ await makeBezoar(contract, { executor: owner, queries }).migrate(migrations);
71
+ const db = makeBezoar(contract, { executor: web, queries, role: "web" });
72
+ ```
73
+
74
+ The one in-process database serves both: the owner applies the migrations
75
+ and the grants, and the queries of the app run as the role, under its
76
+ grants and its policies, as they do in production. PGlite runs one
77
+ statement at a time, so a statement of the owner never lands inside a
78
+ transaction of the role.
79
+
58
80
  ## Transactions
59
81
 
60
82
  `transaction` runs a function inside a transaction of the client. A
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bezoar/pglite",
3
- "version": "0.0.4",
3
+ "version": "0.0.5",
4
4
  "description": "A bezoar executor over PGlite",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -22,8 +22,8 @@
22
22
  ],
23
23
  "dependencies": {
24
24
  "@electric-sql/pglite": "^0.5.10",
25
- "@bezoar/core": "0.0.4",
26
- "@bezoar/schema": "0.0.4"
25
+ "@bezoar/core": "0.0.5",
26
+ "@bezoar/schema": "0.0.5"
27
27
  },
28
28
  "scripts": {
29
29
  "build": "obuild",