@crombolo/data 0.59.0 → 0.60.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crombolo/data",
3
- "version": "0.59.0",
3
+ "version": "0.60.0",
4
4
  "description": "Internal to crombolo, with no compatibility promise: use @crombolo/crombolo. SQLite and Postgres storage for crombolo: schema, migrations and repositories.",
5
5
  "license": "MIT",
6
6
  "author": "Crombolo",
@@ -29,7 +29,7 @@
29
29
  "access": "public"
30
30
  },
31
31
  "dependencies": {
32
- "@crombolo/core": "0.59.0",
32
+ "@crombolo/core": "0.60.0",
33
33
  "kysely": "0.29.6"
34
34
  }
35
35
  }
@@ -18,6 +18,9 @@ export const SITE_OWNER = 'site'
18
18
  /** Table and column names, as an owner writes them. */
19
19
  export const LOGICAL_NAME = /^[a-z][a-z0-9_]*$/
20
20
 
21
+ /** An extension's id, as `crombolo.id` is read (`BUNDLE_ID` in `@crombolo/backoffice-host`). */
22
+ export const EXTENSION_ID = /^[a-z][a-z0-9-]{1,38}$/
23
+
21
24
  const underscored = (id: string): string => id.replaceAll('-', '_')
22
25
 
23
26
  /** The owner's Postgres schema: `site`, or `bundle_<id>` with the id's hyphens as underscores. */
@@ -14,7 +14,7 @@ import { connect, type Db } from '../database.ts'
14
14
  import { Locks } from '../locks.ts'
15
15
  import { deleteKeyValue, readKeyValue, writeKeyValue } from '../migrations.ts'
16
16
  import { queryFor } from '../query.ts'
17
- import { ownerFile, roleComment, roleName, schemaName } from './names.ts'
17
+ import { EXTENSION_ID, ownerFile, roleComment, roleName, SITE_OWNER, schemaName } from './names.ts'
18
18
 
19
19
  /** Where one owner's tables are. */
20
20
  export interface OwnerPlace {
@@ -82,20 +82,58 @@ export class OwnerSchemaTakenError extends Error {
82
82
 
83
83
  const literal = (value: string): string => `'${value.replaceAll("'", "''")}'`
84
84
 
85
- /** The statements that make an owner's role and schema, as an operator would run them. */
86
- export function ownerSetupStatements(
87
- db: Db,
88
- place: { role: string; schema: string },
89
- database: string,
90
- grantee: string,
91
- ): string[] {
92
- const q = (id: string) => db.dialect.quote(id)
93
- return [
94
- `CREATE ROLE ${q(place.role)} NOLOGIN;`,
95
- `COMMENT ON ROLE ${q(place.role)} IS ${literal(roleComment(database))};`,
96
- `GRANT ${q(place.role)} TO ${q(grantee)};`,
97
- `CREATE SCHEMA IF NOT EXISTS ${q(place.schema)} AUTHORIZATION ${q(place.role)};`,
98
- ]
85
+ const quote = (id: string): string => `"${id.replaceAll('"', '""')}"`
86
+
87
+ /** What a host provisioning an owner itself needs to know (story 321). */
88
+ export interface OwnerSetupInput {
89
+ /** The site name the CMS will boot with. */
90
+ siteName: string
91
+ /** `site`, or an extension's id. */
92
+ owner: string
93
+ /** The Postgres database's name. */
94
+ database: string
95
+ /** The site's database user, which is granted the role. */
96
+ grantee: string
97
+ }
98
+
99
+ /** An owner's role and schema, and the statements that make them. */
100
+ export interface OwnerSetup {
101
+ role: string
102
+ schema: string
103
+ comment: string
104
+ statements: string[]
105
+ }
106
+
107
+ /**
108
+ * The role, schema and statements the CMS would use for an owner, without a
109
+ * connection, so a host may make them as its own administrator before a site
110
+ * boots. A role name the database already recorded wins over this one.
111
+ */
112
+ export function ownerSetup(input: OwnerSetupInput): OwnerSetup {
113
+ if (input.owner !== SITE_OWNER && !EXTENSION_ID.test(input.owner))
114
+ throw new Error(`"${input.owner}" is neither "${SITE_OWNER}" nor an extension's id.`)
115
+ return setupFor(
116
+ roleName(input.siteName, input.owner, input.database),
117
+ input.owner,
118
+ input.database,
119
+ input.grantee,
120
+ )
121
+ }
122
+
123
+ function setupFor(role: string, owner: string, database: string, grantee: string): OwnerSetup {
124
+ const schema = schemaName(owner)
125
+ const comment = roleComment(database)
126
+ return {
127
+ role,
128
+ schema,
129
+ comment,
130
+ statements: [
131
+ `CREATE ROLE ${quote(role)} NOLOGIN;`,
132
+ `COMMENT ON ROLE ${quote(role)} IS ${literal(comment)};`,
133
+ `GRANT ${quote(role)} TO ${quote(grantee)};`,
134
+ `CREATE SCHEMA IF NOT EXISTS ${quote(schema)} AUTHORIZATION ${quote(role)};`,
135
+ ],
136
+ }
99
137
  }
100
138
 
101
139
  interface Catalogue {
@@ -169,8 +207,7 @@ export async function provisionOwner(
169
207
  const found = await catalogue(db)
170
208
  const role =
171
209
  (await readKeyValue(db, roleKey(owner))) ?? roleName(settings.siteName, owner, found.database)
172
- const place = { role, schema }
173
- const statements = ownerSetupStatements(db, place, found.database, found.user)
210
+ const { statements } = setupFor(role, owner, found.database, found.user)
174
211
  const q = (id: string) => db.dialect.quote(id)
175
212
  const k = queryFor(db)
176
213