@bezoar/nuxt 0.0.4 → 0.0.6

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.
@@ -13,9 +13,14 @@ export declare const VIRTUAL: {
13
13
  */
14
14
  export declare const DECLARATION = "types/bezoar.d.ts";
15
15
  /**
16
- * The modules of a kit build the module adopts.
16
+ * The modules of a kit build the module adopts. Every build has these.
17
17
  */
18
18
  export declare const ENTRIES: readonly ["index", "queries", "migrations", "seeds"];
19
+ /**
20
+ * The module of a kit build that carries its grants. A build made before
21
+ * roles existed has none, and the virtual build then exports no grant.
22
+ */
23
+ export declare const ROLES = "roles";
19
24
  /**
20
25
  * The export of each module of a kit build, re-exported by the virtual
21
26
  * build. Named, not `export *`: the modules are external to the server
@@ -23,6 +28,10 @@ export declare const ENTRIES: readonly ["index", "queries", "migrations", "seeds
23
28
  * a star re-export provides a name from.
24
29
  */
25
30
  export declare const EXPORTS: Record<(typeof ENTRIES)[number], string>;
31
+ /**
32
+ * The export of the roles module, re-exported by the virtual build.
33
+ */
34
+ export declare const ROLES_EXPORT = "roles";
26
35
  /**
27
36
  * The drivers the module can connect with. `pg` connects to a server with
28
37
  * the connection string of the runtime config. `pglite` runs Postgres in the
package/.dist/config.d.ts CHANGED
@@ -36,6 +36,20 @@ export interface NuxtBezoarConfig {
36
36
  * the migrations. Defaults to `false`.
37
37
  */
38
38
  seed?: boolean;
39
+ /**
40
+ * Whether the server applies the grants of the build when it starts: it
41
+ * declares their roles before the migrations and applies the grants after
42
+ * them, before the seeds. Defaults to `false`.
43
+ */
44
+ roles?: boolean;
45
+ /**
46
+ * The role the service is scoped to: a role of the build. `useBezoar()`
47
+ * offers the queries of the role and runs them as the role, and its type
48
+ * narrows to them. The migrations, the grants, and the seeds run on a
49
+ * connection of their own, as the login user. Without a role, the
50
+ * service offers every query as the login user.
51
+ */
52
+ role?: string;
39
53
  }
40
54
  /**
41
55
  * The runtime config of the module, under `runtimeConfig.bezoar`. An
@@ -52,6 +66,10 @@ export interface BezoarRuntimeConfig {
52
66
  migrate: boolean;
53
67
  /** Whether the server runs the seeds when it starts. */
54
68
  seed: boolean;
69
+ /** Whether the server applies the grants when it starts. */
70
+ roles: boolean;
71
+ /** The role the service is scoped to. Empty for none. */
72
+ role: string;
55
73
  }
56
74
  /**
57
75
  * Identity helper that types a Nuxt bezoar configuration.
@@ -1,2 +1,2 @@
1
- import { DECLARATION, DRIVERS, ENTRIES, EXPORTS, VIRTUAL } from "./_chunks/constant.js";
2
- export { DECLARATION, DRIVERS, ENTRIES, EXPORTS, VIRTUAL };
1
+ import { DECLARATION, DRIVERS, ENTRIES, EXPORTS, ROLES, ROLES_EXPORT, VIRTUAL } from "./_chunks/constant.js";
2
+ export { DECLARATION, DRIVERS, ENTRIES, EXPORTS, ROLES, ROLES_EXPORT, VIRTUAL };
package/.dist/constant.js CHANGED
@@ -9,11 +9,13 @@ const ENTRIES = [
9
9
  "migrations",
10
10
  "seeds"
11
11
  ];
12
+ const ROLES = "roles";
12
13
  const EXPORTS = {
13
14
  index: "contract",
14
15
  queries: "queries",
15
16
  migrations: "migrations",
16
17
  seeds: "seeds"
17
18
  };
19
+ const ROLES_EXPORT = "roles";
18
20
  const DRIVERS = ["pg", "pglite"];
19
- export { DECLARATION, DRIVERS, ENTRIES, EXPORTS, VIRTUAL };
21
+ export { DECLARATION, DRIVERS, ENTRIES, EXPORTS, ROLES, ROLES_EXPORT, VIRTUAL };
package/.dist/module.d.ts CHANGED
@@ -9,10 +9,11 @@ import { NuxtBezoarConfig } from "./config.js";
9
9
  * declaration in the build directory types the service by the contract. The
10
10
  * module provides `useBezoar()` to the server from the virtual service: a
11
11
  * service over the contract of the build, typed by it, connected through
12
- * the driver and the connection string of the runtime config. The plugin of
13
- * the runtime makes the service when the server starts, applies the
14
- * migrations and runs the seeds of the virtual build when the config asks,
15
- * and closes the connection when the server stops.
12
+ * the driver and the connection string of the runtime config, and scoped
13
+ * to the role of the options when there is one. The plugin of the runtime
14
+ * makes the service when the server starts, declares the roles, applies the
15
+ * migrations, applies the grants, and runs the seeds of the virtual build
16
+ * when the config asks, and closes the connection when the server stops.
16
17
  *
17
18
  * The modules are virtual, not written, so Nitro bundles them in dev as in
18
19
  * production. Nitro loads a written file through Node in dev, where neither
package/.dist/module.js CHANGED
@@ -1,4 +1,4 @@
1
- import { DECLARATION, ENTRIES, EXPORTS, VIRTUAL } from "./constant.js";
1
+ import { DECLARATION, ENTRIES, EXPORTS, ROLES, ROLES_EXPORT, VIRTUAL } from "./constant.js";
2
2
  import { dirname, isAbsolute, join, resolve } from "node:path";
3
3
  import { addServerImports, addServerPlugin, addServerTemplate, addTypeTemplate, createResolver, defineNuxtModule, directoryToURL, tryResolveModule } from "@nuxt/kit";
4
4
  import { record } from "objectively";
@@ -17,22 +17,29 @@ const locate = async (build, root) => {
17
17
  return directory;
18
18
  };
19
19
  const from = (directory, file) => JSON.stringify(join(directory, file));
20
- const build = ({ build: directory }) => [...ENTRIES.map((entry) => `export { ${EXPORTS[entry]} } from ${from(directory, `${entry}.mjs`)};`), ""].join("\n");
20
+ const build = ({ build: directory }) => [
21
+ ...ENTRIES.map((entry) => `export { ${EXPORTS[entry]} } from ${from(directory, `${entry}.mjs`)};`),
22
+ existsSync(join(directory, `roles.mjs`)) ? `export { ${ROLES_EXPORT} } from ${from(directory, `${ROLES}.mjs`)};` : `export const ${ROLES_EXPORT} = Object.freeze([]);`,
23
+ ""
24
+ ].join("\n");
21
25
  const service = ({ runtime }) => [
22
26
  `import { contract, queries } from ${JSON.stringify(VIRTUAL.build)};`,
23
27
  `import { defineService } from ${from(runtime, "service.js")};`,
24
28
  "",
25
- "export const { useBezoar, closeBezoar } = defineService(contract, queries);",
29
+ "export const { useBezoar, useBezoarOwner, closeBezoar } = defineService(contract, queries);",
26
30
  ""
27
31
  ].join("\n");
28
- const declaration = ({ build, runtime }) => {
32
+ const declaration = ({ build, runtime }, role) => {
29
33
  const contract = `typeof import(${from(build, "index.mjs")}).contract`;
30
34
  const service = `import(${from(runtime, "service.js")})`;
35
+ const defined = `${service}.Defined<AppBezoarContract, AppBezoarRole>`;
31
36
  return [
32
37
  `declare module ${JSON.stringify(VIRTUAL.service)} {`,
33
38
  ` export type AppBezoarContract = ${contract};`,
34
- ` export const useBezoar: ${service}.Defined<AppBezoarContract>["useBezoar"];`,
35
- ` export const closeBezoar: ${service}.Defined<AppBezoarContract>["closeBezoar"];`,
39
+ ` export type AppBezoarRole = ${role === void 0 ? "undefined" : JSON.stringify(role)};`,
40
+ ` export const useBezoar: ${defined}["useBezoar"];`,
41
+ ` export const useBezoarOwner: ${defined}["useBezoarOwner"];`,
42
+ ` export const closeBezoar: ${defined}["closeBezoar"];`,
36
43
  " export type AppBezoar = ReturnType<typeof useBezoar>;",
37
44
  ` export type AppBezoarQuery = ${service}.Query<AppBezoarContract>;`,
38
45
  ` export type AppBezoarArguments<Q extends AppBezoarQuery> = ${service}.Arguments<AppBezoarContract, Q>;`,
@@ -65,7 +72,7 @@ var module_default = defineNuxtModule({
65
72
  });
66
73
  addTypeTemplate({
67
74
  filename: DECLARATION,
68
- getContents: () => declaration(sources)
75
+ getContents: () => declaration(sources, options.role)
69
76
  }, {
70
77
  nitro: true,
71
78
  nuxt: true
@@ -75,7 +82,9 @@ var module_default = defineNuxtModule({
75
82
  dataDir: "",
76
83
  driver: options.driver ?? "pg",
77
84
  migrate: options.migrate ?? false,
78
- seed: options.seed ?? false
85
+ seed: options.seed ?? false,
86
+ roles: options.roles ?? false,
87
+ role: options.role ?? ""
79
88
  };
80
89
  const given = nuxt.options.runtimeConfig.bezoar;
81
90
  nuxt.options.runtimeConfig.bezoar = {
@@ -88,6 +97,10 @@ var module_default = defineNuxtModule({
88
97
  from: VIRTUAL.service,
89
98
  name: "useBezoar"
90
99
  },
100
+ {
101
+ from: VIRTUAL.service,
102
+ name: "useBezoarOwner"
103
+ },
91
104
  {
92
105
  from: VIRTUAL.service,
93
106
  name: "closeBezoar"
@@ -98,7 +111,8 @@ var module_default = defineNuxtModule({
98
111
  "AppBezoarQuery",
99
112
  "AppBezoarArguments",
100
113
  "AppBezoarRow",
101
- "AppBezoarDatabase"
114
+ "AppBezoarDatabase",
115
+ "AppBezoarRole"
102
116
  ].map((name) => ({
103
117
  from: VIRTUAL.service,
104
118
  name,
@@ -1,10 +1,12 @@
1
1
  import { definePlugin } from "./startup.js";
2
- import { migrations, seeds } from "#bezoar/build";
3
- import { closeBezoar, useBezoar } from "#bezoar/service";
2
+ import { migrations, roles, seeds } from "#bezoar/build";
3
+ import { closeBezoar, useBezoar, useBezoarOwner } from "#bezoar/service";
4
4
  var plugin_default = definePlugin({
5
5
  useBezoar,
6
+ useBezoarOwner,
6
7
  closeBezoar,
7
8
  migrations,
8
- seeds
9
+ seeds,
10
+ roles
9
11
  });
10
12
  export { plugin_default as default };
@@ -1,10 +1,13 @@
1
- import { Arguments, Bezoar, Bundle, Database, Definition, Query, Row } from "bezoar";
1
+ import { Arguments, Bezoar, Bundle, Database, Definition, Query, Row, Scoping } from "bezoar";
2
2
  /**
3
- * The service of an app: `useBezoar` for the service, `closeBezoar`
4
- * to close its connection.
3
+ * The service of an app: `useBezoar` for the service, scoped to the role
4
+ * of the config when there is one; `useBezoarOwner` for the service over
5
+ * every query as the login user, for the work of the plugin; `closeBezoar`
6
+ * to close the connections.
5
7
  */
6
- export interface Defined<D extends Definition> {
7
- useBezoar: () => Bezoar<D>;
8
+ export interface Defined<D extends Definition, R extends Scoping<D> = undefined> {
9
+ useBezoar: () => Bezoar<D, R>;
10
+ useBezoarOwner: () => Bezoar<D>;
8
11
  closeBezoar: () => Promise<void>;
9
12
  }
10
13
  /**
@@ -12,12 +15,19 @@ export interface Defined<D extends Definition> {
12
15
  * service of the build directory calls this with the modules of the build.
13
16
  *
14
17
  * `useBezoar` connects on its first call, with the driver of the runtime
15
- * config, and makes the service. Every call after returns the same service.
16
- * `closeBezoar` closes the connection. The next `useBezoar` connects
17
- * again.
18
+ * config, and makes the service: over the role of the config when there is
19
+ * one, as `makeBezoar` scopes it, and over every query otherwise. Every
20
+ * call after returns the same service. `useBezoarOwner` is the service over
21
+ * every query as the login user, which is `useBezoar` itself without a
22
+ * role. `closeBezoar` closes the connections. The next `useBezoar`
23
+ * connects again.
24
+ *
25
+ * `R` is the role the declaration of the app names, from the module
26
+ * options; the runtime reads the role of the runtime config, which defaults
27
+ * to it.
18
28
  *
19
29
  * @param contract - The contract of the build.
20
30
  * @param queries - The SQL of the build.
21
31
  */
22
- export declare const defineService: <const D extends Definition>(contract: D, queries: Bundle<D>) => Defined<D>;
32
+ export declare const defineService: <const D extends Definition, R extends Scoping<D> = undefined>(contract: D, queries: Bundle<D>) => Defined<D, R>;
23
33
  export type { Arguments, Database, Query, Row };
@@ -3,31 +3,62 @@ import { pg } from "@bezoar/pg";
3
3
  import { pglite } from "@bezoar/pglite";
4
4
  import { makeBezoar } from "bezoar";
5
5
  const connect = (config) => {
6
- if (config.driver === "pglite") return pglite(config.dataDir === "" ? {} : config.dataDir);
6
+ const role = config.role === "" ? void 0 : config.role;
7
+ if (config.driver === "pglite") {
8
+ const owner = pglite(config.dataDir === "" ? {} : config.dataDir);
9
+ return {
10
+ owner,
11
+ scoped: role === void 0 ? void 0 : pglite(owner.client, { role }),
12
+ close: () => owner.close()
13
+ };
14
+ }
7
15
  if (config.url === "") throw new Error("bezoar: no connection string; set NUXT_BEZOAR_URL, or runtimeConfig.bezoar.url");
8
- return pg(config.url);
16
+ const owner = pg(config.url);
17
+ const scoped = role === void 0 ? void 0 : pg(config.url, { role });
18
+ return {
19
+ owner,
20
+ scoped,
21
+ close: async () => {
22
+ await Promise.all([owner.close(), scoped?.close()]);
23
+ }
24
+ };
9
25
  };
10
26
  const defineService = (contract, queries) => {
11
27
  let connection;
28
+ let owner;
12
29
  let service;
13
- const useBezoar = () => {
14
- if (service === void 0) {
30
+ const useBezoarOwner = () => {
31
+ if (owner === void 0) {
15
32
  connection = connect(useRuntimeConfig().bezoar);
16
- service = makeBezoar(contract, {
17
- executor: connection,
33
+ owner = makeBezoar(contract, {
34
+ executor: connection.owner,
18
35
  queries
19
36
  });
20
37
  }
38
+ return owner;
39
+ };
40
+ const useBezoar = () => {
41
+ if (service === void 0) {
42
+ const over = useBezoarOwner();
43
+ const { role } = useRuntimeConfig().bezoar;
44
+ service = connection?.scoped === void 0 ? over : makeBezoar(contract, {
45
+ executor: connection.scoped,
46
+ queries,
47
+ role
48
+ });
49
+ }
21
50
  return service;
22
51
  };
23
52
  const closeBezoar = async () => {
24
53
  const open = connection;
25
54
  connection = void 0;
55
+ owner = void 0;
26
56
  service = void 0;
27
57
  await open?.close();
28
58
  };
29
59
  return {
30
60
  useBezoar,
61
+ useBezoarOwner,
31
62
  closeBezoar
32
63
  };
33
64
  };
@@ -1,21 +1,23 @@
1
1
  import { Defined } from "./service.js";
2
- import { Definition, Migration, Seed } from "bezoar";
2
+ import { Definition, Grant, Migration, Scoping, Seed } from "bezoar";
3
3
  /**
4
- * What the plugin of an app runs with: its service, and the migrations and
5
- * the seeds of its build.
4
+ * What the plugin of an app runs with: its service, and the migrations,
5
+ * the seeds, and the grants of its build.
6
6
  */
7
- export interface Startup<D extends Definition> extends Defined<D> {
7
+ export interface Startup<D extends Definition, R extends Scoping<D> = undefined> extends Defined<D, R> {
8
8
  migrations: readonly Migration[];
9
9
  seeds: readonly Seed[];
10
+ roles: readonly Grant[];
10
11
  }
11
12
  /**
12
13
  * Defines the plugin of an app. The plugin of the runtime calls this with
13
14
  * the virtual service and the virtual build of the app.
14
15
  *
15
16
  * The plugin makes the service when the server starts. When the runtime
16
- * config asks, it applies the migrations of the build and runs its seeds.
17
- * It closes the connection when the server stops.
17
+ * config asks, it declares the roles of the build, applies its migrations,
18
+ * applies its grants, and runs its seeds, in that order, on the service of
19
+ * the owner. It closes the connection when the server stops.
18
20
  *
19
- * @param startup - The service, the migrations, and the seeds.
21
+ * @param startup - The service, the migrations, the seeds, and the grants.
20
22
  */
21
- export declare const definePlugin: <D extends Definition>(startup: Startup<D>) => (nitroApp: import("#imports").NitroApp) => Promise<void>;
23
+ export declare const definePlugin: <D extends Definition, R extends Scoping<D> = undefined>(startup: Startup<D, R>) => (nitroApp: import("#imports").NitroApp) => Promise<void>;
@@ -1,9 +1,12 @@
1
1
  import { defineNitroPlugin, useRuntimeConfig } from "#imports";
2
2
  const definePlugin = (startup) => defineNitroPlugin(async (nitroApp) => {
3
- const { migrate, seed } = useRuntimeConfig().bezoar;
4
- const db = startup.useBezoar();
5
- if (migrate) await db.migrate(startup.migrations);
6
- if (seed) await db.seed(startup.seeds);
3
+ const { migrate, seed, roles } = useRuntimeConfig().bezoar;
4
+ startup.useBezoar();
5
+ const owner = startup.useBezoarOwner();
6
+ if (roles) await owner.roles.declare(startup.roles);
7
+ if (migrate) await owner.migrate(startup.migrations);
8
+ if (roles) await owner.roles(startup.roles);
9
+ if (seed) await owner.seed(startup.seeds);
7
10
  nitroApp.hooks.hook("close", startup.closeBezoar);
8
11
  });
9
12
  export { definePlugin };
package/README.md CHANGED
@@ -41,9 +41,11 @@ each member.
41
41
  | `driver` | `NUXT_BEZOAR_DRIVER` | `pg` or `pglite`. Defaults to `pg` |
42
42
  | `migrate` | `NUXT_BEZOAR_MIGRATE` | Apply the migrations when the server starts |
43
43
  | `seed` | `NUXT_BEZOAR_SEED` | Run the seeds when the server starts |
44
+ | `roles` | `NUXT_BEZOAR_ROLES` | Apply the grants when the server starts |
45
+ | `role` | `NUXT_BEZOAR_ROLE` | The role the service is scoped to |
44
46
 
45
- `driver`, `migrate`, and `seed` can also be set in the module options. A
46
- value in the runtime config wins.
47
+ `driver`, `migrate`, `seed`, `roles`, and `role` can also be set in the
48
+ module options. A value in the runtime config wins.
47
49
 
48
50
  The `pg` driver connects to a Postgres server and needs `url`. The `pglite`
49
51
  driver runs Postgres in the process, on `dataDir`, or in memory when it is
@@ -72,14 +74,49 @@ completes as well.
72
74
  `AppBezoarArguments<Q>`, `AppBezoarRow<Q>`, and `AppBezoarDatabase` are the
73
75
  types, also auto-imported.
74
76
 
77
+ ## Roles
78
+
79
+ A build with roles carries, for each role, the queries it may run. `role`
80
+ scopes the service of the app to one of them.
81
+
82
+ ```ts
83
+ // nuxt.config.ts
84
+ export default defineNuxtConfig({
85
+ modules: ["@bezoar/nuxt"],
86
+ bezoar: { build: "@bbf/database", role: "web" },
87
+ });
88
+ ```
89
+
90
+ `useBezoar()` then offers the queries of the role and no other, and its
91
+ type narrows to them: in the app of `web`, `db.admin.events.update` is a
92
+ type error. The queries run as the role: the `pg` driver opens a pool of
93
+ its own that takes the role on each connection, and the `pglite` driver a
94
+ second executor over the one database. The login user of the connection
95
+ string must be a member of the role. In production that user can be a
96
+ member of the role alone, and the database holds the scope whatever the
97
+ service does. In development on `pglite`, the one superuser takes the role,
98
+ and a query that reaches past it fails on your machine as it would in
99
+ production.
100
+
101
+ `useBezoarOwner()` is the service over every query as the login user, which
102
+ the plugin runs the startup work on. Without a role, it is `useBezoar()`
103
+ itself. `AppBezoarRole` is the role of the module options, as a type, or
104
+ `undefined`.
105
+
106
+ A login user that is a member of its role alone cannot migrate or grant.
107
+ An app over one sets `migrate`, `roles`, and `seed` to false, and the owner
108
+ of the database, another app or a deploy step, applies them.
109
+
75
110
  ## Startup and shutdown
76
111
 
77
- A server plugin makes the service when the server starts. With `migrate`,
78
- it applies the migrations of the build first, through `migrate` of
79
- `@bezoar/core`, under an advisory lock, so instances that start together
80
- apply each migration once. With `seed`, it runs the seeds after. When the
81
- server stops, the plugin closes the connection. `closeBezoar()` closes it
82
- by hand.
112
+ A server plugin makes the service when the server starts. With `roles`, it
113
+ declares the roles of the build first, through `roles.declare` of
114
+ `@bezoar/core`, so a migration can name one. With `migrate`, it applies the
115
+ migrations of the build, through `migrate`, under an advisory lock, so
116
+ instances that start together apply each migration once. With `roles`, it
117
+ then applies the grants, through `roles`. With `seed`, it runs the seeds
118
+ last. The startup work runs on `useBezoarOwner()`. When the server stops,
119
+ the plugin closes the connections. `closeBezoar()` closes them by hand.
83
120
 
84
121
  ## Runtime
85
122
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bezoar/nuxt",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
4
4
  "description": "Nuxt module for bezoar: a typed database service for server routes, from a kit build",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -31,9 +31,9 @@
31
31
  "dependencies": {
32
32
  "@nuxt/kit": "^4.5.0",
33
33
  "objectively": "^0.3.0",
34
- "@bezoar/pg": "0.0.4",
35
- "@bezoar/pglite": "0.0.4",
36
- "bezoar": "0.0.4"
34
+ "@bezoar/pg": "0.0.6",
35
+ "@bezoar/pglite": "0.0.6",
36
+ "bezoar": "0.0.6"
37
37
  },
38
38
  "devDependencies": {
39
39
  "@nuxt/nitro-server": "^4.5.0",