@astram/core 1.6.0 → 1.7.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/dist/database/framework-schemas.d.ts +149 -0
- package/dist/database/framework-schemas.d.ts.map +1 -0
- package/dist/database/framework-schemas.js +222 -0
- package/dist/database/framework-schemas.js.map +1 -0
- package/dist/database/index.d.ts +9 -0
- package/dist/database/index.d.ts.map +1 -0
- package/dist/database/index.js +14 -0
- package/dist/database/index.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist-cjs/database/framework-schemas.js +233 -0
- package/dist-cjs/database/index.js +30 -0
- package/dist-cjs/index.js +2 -0
- package/package.json +1 -1
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Framework database names (configurable-framework-db, spec §5.A, §10, §13; ADR-0037).
|
|
3
|
+
*
|
|
4
|
+
* The single source of the four framework database names. `SHIELD_DB_NAMESPACE` renames all
|
|
5
|
+
* four together (`beta` ⇒ `beta_app`, `beta_identity`, `beta_authz`, `beta_audit`); unset keeps
|
|
6
|
+
* today's `shield_*` names. Pure and zero-dep (ADR-0003): the caller passes an env-like record,
|
|
7
|
+
* nothing here reads `process.env`.
|
|
8
|
+
*
|
|
9
|
+
* SECURITY: fail closed (Shield #1) — a set-but-empty or non-matching namespace throws; a
|
|
10
|
+
* failure never returns a partial set. Names reach SQL only after this validation, and the
|
|
11
|
+
* pattern admits no quote, backtick, dot or whitespace.
|
|
12
|
+
*/
|
|
13
|
+
/** The env variable that selects the namespace. */
|
|
14
|
+
export declare const FRAMEWORK_DB_NAMESPACE_ENV = "SHIELD_DB_NAMESPACE";
|
|
15
|
+
/** The namespace used when `SHIELD_DB_NAMESPACE` is unset (today's names). */
|
|
16
|
+
export declare const DEFAULT_FRAMEWORK_DB_NAMESPACE = "shield";
|
|
17
|
+
/**
|
|
18
|
+
* Allowed namespace. `<ns>_identity`, the longest name, stays within MariaDB's 64-character
|
|
19
|
+
* identifier limit (55 + 9).
|
|
20
|
+
*/
|
|
21
|
+
export declare const FRAMEWORK_DB_NAMESPACE_PATTERN: RegExp;
|
|
22
|
+
/** The fixed database suffixes, one per framework module. */
|
|
23
|
+
export declare const FRAMEWORK_SCHEMA_SUFFIXES: Readonly<{
|
|
24
|
+
readonly app: "_app";
|
|
25
|
+
readonly identity: "_identity";
|
|
26
|
+
readonly authz: "_authz";
|
|
27
|
+
readonly audit: "_audit";
|
|
28
|
+
}>;
|
|
29
|
+
/** The resolved framework database names. Always frozen. */
|
|
30
|
+
export interface FrameworkSchemas {
|
|
31
|
+
readonly namespace: string;
|
|
32
|
+
readonly app: string;
|
|
33
|
+
readonly identity: string;
|
|
34
|
+
readonly authz: string;
|
|
35
|
+
readonly audit: string;
|
|
36
|
+
/**
|
|
37
|
+
* True on the `shield` default: `SHIELD_DB_NAMESPACE` unset, or set to `shield` explicitly
|
|
38
|
+
* (which behaves exactly like unset).
|
|
39
|
+
*/
|
|
40
|
+
readonly isDefault: boolean;
|
|
41
|
+
}
|
|
42
|
+
/** Thrown for an invalid framework database configuration. Never carries a secret. */
|
|
43
|
+
export declare class FrameworkSchemaConfigError extends Error {
|
|
44
|
+
constructor(message: string);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Resolve the four framework database names from an env-like record.
|
|
48
|
+
* @throws FrameworkSchemaConfigError when `SHIELD_DB_NAMESPACE` is set but empty or invalid.
|
|
49
|
+
*/
|
|
50
|
+
export declare function resolveFrameworkSchemas(env: Readonly<Record<string, string | undefined>>): FrameworkSchemas;
|
|
51
|
+
/**
|
|
52
|
+
* The DB account name for a matrix account role under a namespace: `<ns>_<role>`
|
|
53
|
+
* (`frameworkAccountName('beta', 'core')` ⇒ `beta_core`). Roles are the matrix account names
|
|
54
|
+
* without the `shield_` prefix (`core`, `migrate`, `audit_ops`, ...).
|
|
55
|
+
*/
|
|
56
|
+
export declare function frameworkAccountName(namespace: string, role: string): string;
|
|
57
|
+
/**
|
|
58
|
+
* A database name a caller may hand to framework SQL (`opts.schema`, `forRoot({ schemas })`):
|
|
59
|
+
* lower-case letters, digits and `_`, 1..64 characters (MariaDB's identifier limit). Names come
|
|
60
|
+
* from the resolver or from operator config, both lower-case (MariaDB database names are
|
|
61
|
+
* case-sensitive on Linux, so a mixed-case name is refused rather than guessed). It admits no
|
|
62
|
+
* backtick, quote, dot, whitespace or `;`, so a name that passes can be backtick-quoted safely.
|
|
63
|
+
*/
|
|
64
|
+
export declare const SCHEMA_NAME_PATTERN: RegExp;
|
|
65
|
+
/**
|
|
66
|
+
* Check a caller-supplied database name before it reaches SQL (configurable-framework-db,
|
|
67
|
+
* spec §11). Returns the name unchanged.
|
|
68
|
+
* @param label names the setting in the error (e.g. `opts.schema`); the value is never echoed.
|
|
69
|
+
* @throws FrameworkSchemaConfigError when the name is not a string matching
|
|
70
|
+
* {@link SCHEMA_NAME_PATTERN}.
|
|
71
|
+
*/
|
|
72
|
+
export declare function assertSchemaName(name: unknown, label: string): string;
|
|
73
|
+
/** The four framework database names, as `FrameworkSchemas` or any object carrying them. */
|
|
74
|
+
export type FrameworkSchemaNameSet = Pick<FrameworkSchemas, 'app' | 'identity' | 'authz' | 'audit'>;
|
|
75
|
+
/**
|
|
76
|
+
* Check all four names of a caller-supplied set (`forRoot({ schemas })`, a store's `schemas`
|
|
77
|
+
* argument, `ctx.schemas`) with {@link assertSchemaName}. Returns the same object.
|
|
78
|
+
* @throws FrameworkSchemaConfigError naming `<label>.<field>` for the first bad name.
|
|
79
|
+
*/
|
|
80
|
+
export declare function assertFrameworkSchemaNames<T extends FrameworkSchemaNameSet>(schemas: T, label: string): T;
|
|
81
|
+
/** The context shape {@link frameworkSchemasForMigration} reads (any `@astram/migrate` version). */
|
|
82
|
+
export interface MigrationSchemasContext {
|
|
83
|
+
readonly schemas?: FrameworkSchemas | undefined;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* The framework database names for a framework migration's `up`/`down` (spec v2 §5.H, decided
|
|
87
|
+
* Q2). `@astram/guard` and `@astram/identity` do not depend on `@astram/migrate`, so their
|
|
88
|
+
* migrations may run under an older CLI that passes no `ctx.schemas`:
|
|
89
|
+
* - `ctx.schemas` present (current `@astram/migrate`) ⇒ those names, validated;
|
|
90
|
+
* - absent and `SHIELD_DB_NAMESPACE` unset (or `shield`) ⇒ the default `shield_*` names (today's
|
|
91
|
+
* behaviour);
|
|
92
|
+
* - absent and `SHIELD_DB_NAMESPACE` set to any other value ⇒ throws before any DDL, telling the
|
|
93
|
+
* operator to upgrade `@astram/migrate`.
|
|
94
|
+
*
|
|
95
|
+
* Usage in a migration file: `const s = frameworkSchemasForMigration(ctx, process.env);`.
|
|
96
|
+
* Pure: the caller passes the env.
|
|
97
|
+
* @throws FrameworkSchemaConfigError as above, or for an invalid name in `ctx.schemas`.
|
|
98
|
+
*/
|
|
99
|
+
export declare function frameworkSchemasForMigration(ctx: MigrationSchemasContext | undefined, env: Readonly<Record<string, string | undefined>>): FrameworkSchemas;
|
|
100
|
+
/** The `@astram/migrate` ledger table when `APP_MIGRATION_TABLE` is unset. */
|
|
101
|
+
export declare const DEFAULT_MIGRATION_LEDGER_TABLE = "schema_migrations";
|
|
102
|
+
/** A ledger table name: a plain identifier, safe to backtick-quote. */
|
|
103
|
+
export declare const MIGRATION_LEDGER_TABLE_PATTERN: RegExp;
|
|
104
|
+
/** Where the `@astram/migrate` ledger (tracking table) lives. */
|
|
105
|
+
export interface MigrationLedgerLocation {
|
|
106
|
+
readonly database: string;
|
|
107
|
+
readonly table: string;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* An operator-supplied ledger database name (`DDL_DB_NAME`, or a migrate job's `APP_DB_NAME` in
|
|
111
|
+
* default mode): any plain MariaDB identifier, mixed case included, so a name that worked with
|
|
112
|
+
* earlier `@astram/migrate` releases keeps working. Letters, digits and `_` only, 1..64: no
|
|
113
|
+
* backtick, quote, dot, whitespace or `;`, so it is safe to backtick-quote or single-quote.
|
|
114
|
+
* Resolver-produced names stay on the stricter {@link SCHEMA_NAME_PATTERN}.
|
|
115
|
+
*/
|
|
116
|
+
export declare const LEDGER_DATABASE_NAME_PATTERN: RegExp;
|
|
117
|
+
/**
|
|
118
|
+
* Check an operator-supplied ledger database name before it reaches SQL. Returns it unchanged.
|
|
119
|
+
* @param label names the setting in the error (e.g. `DDL_DB_NAME`); the value is never echoed.
|
|
120
|
+
* @throws FrameworkSchemaConfigError when the name does not match
|
|
121
|
+
* {@link LEDGER_DATABASE_NAME_PATTERN}.
|
|
122
|
+
*/
|
|
123
|
+
export declare function assertLedgerDatabaseName(name: unknown, label: string): string;
|
|
124
|
+
/**
|
|
125
|
+
* The ledger-location rule of the migrate CLI (configurable-framework-db spec 5.G):
|
|
126
|
+
* - namespace set ⇒ always `<ns>_app`; a `DDL_DB_NAME` other than `<ns>_app` throws;
|
|
127
|
+
* - namespace unset (or `shield`) ⇒ `DDL_DB_NAME`, else the default app database. (The CLI adds
|
|
128
|
+
* its released fallback to the job's `APP_DB_NAME` before `shield_app`.)
|
|
129
|
+
* Table: `APP_MIGRATION_TABLE`, else `schema_migrations`. The runtime boot check does not use this
|
|
130
|
+
* single location: it tries {@link migrationLedgerCandidates}. Pure: the caller passes the env.
|
|
131
|
+
* @throws FrameworkSchemaConfigError naming the variable on a conflicting or invalid name.
|
|
132
|
+
*/
|
|
133
|
+
export declare function resolveMigrationLedgerLocation(schemas: FrameworkSchemas, env: Readonly<Record<string, string | undefined>>): MigrationLedgerLocation;
|
|
134
|
+
/**
|
|
135
|
+
* Where the runtime boot check looks for the ledger, in order (G-007 QA P1). A standard scaffold
|
|
136
|
+
* runs `astram-migrate` with the server's env, so in default mode migrate writes the ledger to
|
|
137
|
+
* `DDL_DB_NAME`, else the job's `APP_DB_NAME`, else `shield_app`; the boot check cannot know
|
|
138
|
+
* which env the migrate job had, so it tries each:
|
|
139
|
+
* - namespace set ⇒ only `<ns>_app` (as {@link resolveMigrationLedgerLocation}; `APP_DB_NAME`
|
|
140
|
+
* is ignored, a conflicting `DDL_DB_NAME` throws);
|
|
141
|
+
* - namespace unset (or `shield`) ⇒ `DDL_DB_NAME`, the runtime `APP_DB_NAME`, then the default
|
|
142
|
+
* app database; unset or blank values skipped, duplicates dropped.
|
|
143
|
+
* The caller uses the first location whose ledger table exists and must still find every shipped
|
|
144
|
+
* id there; when none exists it fails closed naming every location. Pure: the caller passes the
|
|
145
|
+
* env. Returns a frozen, non-empty list.
|
|
146
|
+
* @throws FrameworkSchemaConfigError naming the variable on a conflicting or invalid name.
|
|
147
|
+
*/
|
|
148
|
+
export declare function migrationLedgerCandidates(schemas: FrameworkSchemas, env: Readonly<Record<string, string | undefined>>): readonly MigrationLedgerLocation[];
|
|
149
|
+
//# sourceMappingURL=framework-schemas.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"framework-schemas.d.ts","sourceRoot":"","sources":["../../src/database/framework-schemas.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,mDAAmD;AACnD,eAAO,MAAM,0BAA0B,wBAAwB,CAAC;AAEhE,8EAA8E;AAC9E,eAAO,MAAM,8BAA8B,WAAW,CAAC;AAEvD;;;GAGG;AAIH,eAAO,MAAM,8BAA8B,QAA0B,CAAC;AAEtE,6DAA6D;AAC7D,eAAO,MAAM,yBAAyB;;;;;EAK3B,CAAC;AAEZ,4DAA4D;AAC5D,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB;;;OAGG;IACH,QAAQ,CAAC,SAAS,EAAE,OAAO,CAAC;CAC7B;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E,MAAM,CAAC;IAC1B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;CACxB;AAED;;;;;;GAMG;AACH,eAAO,MAAM,4BAA4B,QAAyB,CAAC;AAEnE;;;;;GAKG;AACH,wBAAgB,wBAAwB,CAAC,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAO7E;AAYD;;;;;;;;GAQG;AACH,wBAAgB,8BAA8B,CAC5C,OAAO,EAAE,gBAAgB,EACzB,GAAG,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC,GAChD,uBAAuB,CAmBzB;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,yBAAyB,CACvC,OAAO,EAAE,gBAAgB,EACzB,GAAG,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC,GAChD,SAAS,uBAAuB,EAAE,CAepC"}
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Framework database names (configurable-framework-db, spec §5.A, §10, §13; ADR-0037).
|
|
3
|
+
*
|
|
4
|
+
* The single source of the four framework database names. `SHIELD_DB_NAMESPACE` renames all
|
|
5
|
+
* four together (`beta` ⇒ `beta_app`, `beta_identity`, `beta_authz`, `beta_audit`); unset keeps
|
|
6
|
+
* today's `shield_*` names. Pure and zero-dep (ADR-0003): the caller passes an env-like record,
|
|
7
|
+
* nothing here reads `process.env`.
|
|
8
|
+
*
|
|
9
|
+
* SECURITY: fail closed (Shield #1) — a set-but-empty or non-matching namespace throws; a
|
|
10
|
+
* failure never returns a partial set. Names reach SQL only after this validation, and the
|
|
11
|
+
* pattern admits no quote, backtick, dot or whitespace.
|
|
12
|
+
*/
|
|
13
|
+
/** The env variable that selects the namespace. */
|
|
14
|
+
export const FRAMEWORK_DB_NAMESPACE_ENV = 'SHIELD_DB_NAMESPACE';
|
|
15
|
+
/** The namespace used when `SHIELD_DB_NAMESPACE` is unset (today's names). */
|
|
16
|
+
export const DEFAULT_FRAMEWORK_DB_NAMESPACE = 'shield';
|
|
17
|
+
/**
|
|
18
|
+
* Allowed namespace. `<ns>_identity`, the longest name, stays within MariaDB's 64-character
|
|
19
|
+
* identifier limit (55 + 9).
|
|
20
|
+
*/
|
|
21
|
+
// Letters and digits only (spec v3): `_` is a one-character wildcard in MariaDB database-level
|
|
22
|
+
// grants, so a namespace containing `_` could make one environment's grants match another's
|
|
23
|
+
// databases (`team_a_app` also matches `team1a_app`).
|
|
24
|
+
export const FRAMEWORK_DB_NAMESPACE_PATTERN = /^[a-z][a-z0-9]{0,54}$/;
|
|
25
|
+
/** The fixed database suffixes, one per framework module. */
|
|
26
|
+
export const FRAMEWORK_SCHEMA_SUFFIXES = Object.freeze({
|
|
27
|
+
app: '_app',
|
|
28
|
+
identity: '_identity',
|
|
29
|
+
authz: '_authz',
|
|
30
|
+
audit: '_audit',
|
|
31
|
+
});
|
|
32
|
+
/** Thrown for an invalid framework database configuration. Never carries a secret. */
|
|
33
|
+
export class FrameworkSchemaConfigError extends Error {
|
|
34
|
+
constructor(message) {
|
|
35
|
+
super(message);
|
|
36
|
+
this.name = 'FrameworkSchemaConfigError';
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function namesFor(namespace, isDefault) {
|
|
40
|
+
return Object.freeze({
|
|
41
|
+
namespace,
|
|
42
|
+
app: `${namespace}${FRAMEWORK_SCHEMA_SUFFIXES.app}`,
|
|
43
|
+
identity: `${namespace}${FRAMEWORK_SCHEMA_SUFFIXES.identity}`,
|
|
44
|
+
authz: `${namespace}${FRAMEWORK_SCHEMA_SUFFIXES.authz}`,
|
|
45
|
+
audit: `${namespace}${FRAMEWORK_SCHEMA_SUFFIXES.audit}`,
|
|
46
|
+
isDefault,
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Resolve the four framework database names from an env-like record.
|
|
51
|
+
* @throws FrameworkSchemaConfigError when `SHIELD_DB_NAMESPACE` is set but empty or invalid.
|
|
52
|
+
*/
|
|
53
|
+
export function resolveFrameworkSchemas(env) {
|
|
54
|
+
const raw = env[FRAMEWORK_DB_NAMESPACE_ENV];
|
|
55
|
+
if (raw === undefined)
|
|
56
|
+
return namesFor(DEFAULT_FRAMEWORK_DB_NAMESPACE, true);
|
|
57
|
+
if (!FRAMEWORK_DB_NAMESPACE_PATTERN.test(raw)) {
|
|
58
|
+
// The value is not echoed: it is config, not a secret, but the message stays a fixed shape.
|
|
59
|
+
throw new FrameworkSchemaConfigError(`${FRAMEWORK_DB_NAMESPACE_ENV} is set but ${raw === '' ? 'empty' : 'invalid'}; ` +
|
|
60
|
+
`expected ${FRAMEWORK_DB_NAMESPACE_PATTERN.source} (unset it for the default "${DEFAULT_FRAMEWORK_DB_NAMESPACE}")`);
|
|
61
|
+
}
|
|
62
|
+
// An explicit default namespace is the default: same names AND the same rules (ledger, old
|
|
63
|
+
// migrate fallback), so `SHIELD_DB_NAMESPACE=shield` never behaves differently from unset.
|
|
64
|
+
return namesFor(raw, raw === DEFAULT_FRAMEWORK_DB_NAMESPACE);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* The DB account name for a matrix account role under a namespace: `<ns>_<role>`
|
|
68
|
+
* (`frameworkAccountName('beta', 'core')` ⇒ `beta_core`). Roles are the matrix account names
|
|
69
|
+
* without the `shield_` prefix (`core`, `migrate`, `audit_ops`, ...).
|
|
70
|
+
*/
|
|
71
|
+
export function frameworkAccountName(namespace, role) {
|
|
72
|
+
return `${namespace}_${role}`;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* A database name a caller may hand to framework SQL (`opts.schema`, `forRoot({ schemas })`):
|
|
76
|
+
* lower-case letters, digits and `_`, 1..64 characters (MariaDB's identifier limit). Names come
|
|
77
|
+
* from the resolver or from operator config, both lower-case (MariaDB database names are
|
|
78
|
+
* case-sensitive on Linux, so a mixed-case name is refused rather than guessed). It admits no
|
|
79
|
+
* backtick, quote, dot, whitespace or `;`, so a name that passes can be backtick-quoted safely.
|
|
80
|
+
*/
|
|
81
|
+
export const SCHEMA_NAME_PATTERN = /^[a-z0-9_]{1,64}$/;
|
|
82
|
+
/**
|
|
83
|
+
* Check a caller-supplied database name before it reaches SQL (configurable-framework-db,
|
|
84
|
+
* spec §11). Returns the name unchanged.
|
|
85
|
+
* @param label names the setting in the error (e.g. `opts.schema`); the value is never echoed.
|
|
86
|
+
* @throws FrameworkSchemaConfigError when the name is not a string matching
|
|
87
|
+
* {@link SCHEMA_NAME_PATTERN}.
|
|
88
|
+
*/
|
|
89
|
+
export function assertSchemaName(name, label) {
|
|
90
|
+
if (typeof name !== 'string' || !SCHEMA_NAME_PATTERN.test(name)) {
|
|
91
|
+
throw new FrameworkSchemaConfigError(`${label} is not a valid database name; expected ${SCHEMA_NAME_PATTERN.source}`);
|
|
92
|
+
}
|
|
93
|
+
return name;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Check all four names of a caller-supplied set (`forRoot({ schemas })`, a store's `schemas`
|
|
97
|
+
* argument, `ctx.schemas`) with {@link assertSchemaName}. Returns the same object.
|
|
98
|
+
* @throws FrameworkSchemaConfigError naming `<label>.<field>` for the first bad name.
|
|
99
|
+
*/
|
|
100
|
+
export function assertFrameworkSchemaNames(schemas, label) {
|
|
101
|
+
if (typeof schemas !== 'object' || schemas === null) {
|
|
102
|
+
throw new FrameworkSchemaConfigError(`${label} must be an object with app, identity, authz and audit database names`);
|
|
103
|
+
}
|
|
104
|
+
for (const key of ['app', 'identity', 'authz', 'audit']) {
|
|
105
|
+
assertSchemaName(schemas[key], `${label}.${key}`);
|
|
106
|
+
}
|
|
107
|
+
return schemas;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* The framework database names for a framework migration's `up`/`down` (spec v2 §5.H, decided
|
|
111
|
+
* Q2). `@astram/guard` and `@astram/identity` do not depend on `@astram/migrate`, so their
|
|
112
|
+
* migrations may run under an older CLI that passes no `ctx.schemas`:
|
|
113
|
+
* - `ctx.schemas` present (current `@astram/migrate`) ⇒ those names, validated;
|
|
114
|
+
* - absent and `SHIELD_DB_NAMESPACE` unset (or `shield`) ⇒ the default `shield_*` names (today's
|
|
115
|
+
* behaviour);
|
|
116
|
+
* - absent and `SHIELD_DB_NAMESPACE` set to any other value ⇒ throws before any DDL, telling the
|
|
117
|
+
* operator to upgrade `@astram/migrate`.
|
|
118
|
+
*
|
|
119
|
+
* Usage in a migration file: `const s = frameworkSchemasForMigration(ctx, process.env);`.
|
|
120
|
+
* Pure: the caller passes the env.
|
|
121
|
+
* @throws FrameworkSchemaConfigError as above, or for an invalid name in `ctx.schemas`.
|
|
122
|
+
*/
|
|
123
|
+
export function frameworkSchemasForMigration(ctx, env) {
|
|
124
|
+
const given = ctx?.schemas;
|
|
125
|
+
if (given !== undefined)
|
|
126
|
+
return assertFrameworkSchemaNames(given, 'ctx.schemas');
|
|
127
|
+
const ns = env[FRAMEWORK_DB_NAMESPACE_ENV];
|
|
128
|
+
if (ns !== undefined && ns !== DEFAULT_FRAMEWORK_DB_NAMESPACE) {
|
|
129
|
+
throw new FrameworkSchemaConfigError(`${FRAMEWORK_DB_NAMESPACE_ENV} is set but this @astram/migrate does not pass ctx.schemas; ` +
|
|
130
|
+
'upgrade @astram/migrate');
|
|
131
|
+
}
|
|
132
|
+
return namesFor(DEFAULT_FRAMEWORK_DB_NAMESPACE, true);
|
|
133
|
+
}
|
|
134
|
+
/** The `@astram/migrate` ledger table when `APP_MIGRATION_TABLE` is unset. */
|
|
135
|
+
export const DEFAULT_MIGRATION_LEDGER_TABLE = 'schema_migrations';
|
|
136
|
+
/** A ledger table name: a plain identifier, safe to backtick-quote. */
|
|
137
|
+
export const MIGRATION_LEDGER_TABLE_PATTERN = /^[A-Za-z0-9_$]{1,64}$/;
|
|
138
|
+
/**
|
|
139
|
+
* An operator-supplied ledger database name (`DDL_DB_NAME`, or a migrate job's `APP_DB_NAME` in
|
|
140
|
+
* default mode): any plain MariaDB identifier, mixed case included, so a name that worked with
|
|
141
|
+
* earlier `@astram/migrate` releases keeps working. Letters, digits and `_` only, 1..64: no
|
|
142
|
+
* backtick, quote, dot, whitespace or `;`, so it is safe to backtick-quote or single-quote.
|
|
143
|
+
* Resolver-produced names stay on the stricter {@link SCHEMA_NAME_PATTERN}.
|
|
144
|
+
*/
|
|
145
|
+
export const LEDGER_DATABASE_NAME_PATTERN = /^[A-Za-z0-9_]{1,64}$/;
|
|
146
|
+
/**
|
|
147
|
+
* Check an operator-supplied ledger database name before it reaches SQL. Returns it unchanged.
|
|
148
|
+
* @param label names the setting in the error (e.g. `DDL_DB_NAME`); the value is never echoed.
|
|
149
|
+
* @throws FrameworkSchemaConfigError when the name does not match
|
|
150
|
+
* {@link LEDGER_DATABASE_NAME_PATTERN}.
|
|
151
|
+
*/
|
|
152
|
+
export function assertLedgerDatabaseName(name, label) {
|
|
153
|
+
if (typeof name !== 'string' || !LEDGER_DATABASE_NAME_PATTERN.test(name)) {
|
|
154
|
+
throw new FrameworkSchemaConfigError(`${label} is not a valid database name; expected ${LEDGER_DATABASE_NAME_PATTERN.source}`);
|
|
155
|
+
}
|
|
156
|
+
return name;
|
|
157
|
+
}
|
|
158
|
+
function ledgerTable(env) {
|
|
159
|
+
const table = env.APP_MIGRATION_TABLE ?? DEFAULT_MIGRATION_LEDGER_TABLE;
|
|
160
|
+
if (!MIGRATION_LEDGER_TABLE_PATTERN.test(table)) {
|
|
161
|
+
throw new FrameworkSchemaConfigError(`APP_MIGRATION_TABLE is not a valid table name; expected ${MIGRATION_LEDGER_TABLE_PATTERN.source}`);
|
|
162
|
+
}
|
|
163
|
+
return table;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* The ledger-location rule of the migrate CLI (configurable-framework-db spec 5.G):
|
|
167
|
+
* - namespace set ⇒ always `<ns>_app`; a `DDL_DB_NAME` other than `<ns>_app` throws;
|
|
168
|
+
* - namespace unset (or `shield`) ⇒ `DDL_DB_NAME`, else the default app database. (The CLI adds
|
|
169
|
+
* its released fallback to the job's `APP_DB_NAME` before `shield_app`.)
|
|
170
|
+
* Table: `APP_MIGRATION_TABLE`, else `schema_migrations`. The runtime boot check does not use this
|
|
171
|
+
* single location: it tries {@link migrationLedgerCandidates}. Pure: the caller passes the env.
|
|
172
|
+
* @throws FrameworkSchemaConfigError naming the variable on a conflicting or invalid name.
|
|
173
|
+
*/
|
|
174
|
+
export function resolveMigrationLedgerLocation(schemas, env) {
|
|
175
|
+
const ddl = env.DDL_DB_NAME;
|
|
176
|
+
let database;
|
|
177
|
+
if (schemas.isDefault) {
|
|
178
|
+
database =
|
|
179
|
+
ddl === undefined
|
|
180
|
+
? assertSchemaName(schemas.app, 'ledger')
|
|
181
|
+
: assertLedgerDatabaseName(ddl, 'DDL_DB_NAME');
|
|
182
|
+
}
|
|
183
|
+
else {
|
|
184
|
+
if (ddl !== undefined && ddl !== schemas.app) {
|
|
185
|
+
throw new FrameworkSchemaConfigError(`DDL_DB_NAME must be ${schemas.app} or unset when ` +
|
|
186
|
+
`${FRAMEWORK_DB_NAMESPACE_ENV}=${schemas.namespace}: the migration ledger lives in ` +
|
|
187
|
+
`${schemas.app} (spec 5.G).`);
|
|
188
|
+
}
|
|
189
|
+
database = schemas.app;
|
|
190
|
+
}
|
|
191
|
+
return { database, table: ledgerTable(env) };
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Where the runtime boot check looks for the ledger, in order (G-007 QA P1). A standard scaffold
|
|
195
|
+
* runs `astram-migrate` with the server's env, so in default mode migrate writes the ledger to
|
|
196
|
+
* `DDL_DB_NAME`, else the job's `APP_DB_NAME`, else `shield_app`; the boot check cannot know
|
|
197
|
+
* which env the migrate job had, so it tries each:
|
|
198
|
+
* - namespace set ⇒ only `<ns>_app` (as {@link resolveMigrationLedgerLocation}; `APP_DB_NAME`
|
|
199
|
+
* is ignored, a conflicting `DDL_DB_NAME` throws);
|
|
200
|
+
* - namespace unset (or `shield`) ⇒ `DDL_DB_NAME`, the runtime `APP_DB_NAME`, then the default
|
|
201
|
+
* app database; unset or blank values skipped, duplicates dropped.
|
|
202
|
+
* The caller uses the first location whose ledger table exists and must still find every shipped
|
|
203
|
+
* id there; when none exists it fails closed naming every location. Pure: the caller passes the
|
|
204
|
+
* env. Returns a frozen, non-empty list.
|
|
205
|
+
* @throws FrameworkSchemaConfigError naming the variable on a conflicting or invalid name.
|
|
206
|
+
*/
|
|
207
|
+
export function migrationLedgerCandidates(schemas, env) {
|
|
208
|
+
if (!schemas.isDefault) {
|
|
209
|
+
return Object.freeze([Object.freeze(resolveMigrationLedgerLocation(schemas, env))]);
|
|
210
|
+
}
|
|
211
|
+
const table = ledgerTable(env);
|
|
212
|
+
const databases = [];
|
|
213
|
+
for (const key of ['DDL_DB_NAME', 'APP_DB_NAME']) {
|
|
214
|
+
const raw = env[key];
|
|
215
|
+
if (raw === undefined || raw.trim() === '')
|
|
216
|
+
continue;
|
|
217
|
+
databases.push(assertLedgerDatabaseName(raw.trim(), key));
|
|
218
|
+
}
|
|
219
|
+
databases.push(assertSchemaName(schemas.app, 'ledger'));
|
|
220
|
+
return Object.freeze([...new Set(databases)].map((database) => Object.freeze({ database, table })));
|
|
221
|
+
}
|
|
222
|
+
//# sourceMappingURL=framework-schemas.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"framework-schemas.js","sourceRoot":"","sources":["../../src/database/framework-schemas.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,mDAAmD;AACnD,MAAM,CAAC,MAAM,0BAA0B,GAAG,qBAAqB,CAAC;AAEhE,8EAA8E;AAC9E,MAAM,CAAC,MAAM,8BAA8B,GAAG,QAAQ,CAAC;AAEvD;;;GAGG;AACH,+FAA+F;AAC/F,4FAA4F;AAC5F,sDAAsD;AACtD,MAAM,CAAC,MAAM,8BAA8B,GAAG,uBAAuB,CAAC;AAEtE,6DAA6D;AAC7D,MAAM,CAAC,MAAM,yBAAy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rD,SAAS,CAAC,IAAI,CAAC,wBAAwB,CAAC,GAAG,CAAC,IAAI,EAAE,EAAE,GAAG,CAAC,CAAC,CAAC;IAC5D,CAAC;IACD,SAAS,CAAC,IAAI,CAAC,gBAAgB,CAAC,OAAO,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC,CAAC;IACxD,OAAO,MAAM,CAAC,MAAM,CAClB,CAAC,GAAG,IAAI,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAC,CAAC,CAC9E,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Framework database names (configurable-framework-db, ADR-0037): the single source of the
|
|
3
|
+
* four `<namespace>_{app,identity,authz,audit}` names and the namespaced account defaults.
|
|
4
|
+
* Pure + zero-dep (ADR-0003).
|
|
5
|
+
*/
|
|
6
|
+
export type { FrameworkSchemas, FrameworkSchemaNameSet, MigrationSchemasContext, } from './framework-schemas.js';
|
|
7
|
+
export { DEFAULT_FRAMEWORK_DB_NAMESPACE, FRAMEWORK_DB_NAMESPACE_ENV, FRAMEWORK_DB_NAMESPACE_PATTERN, FRAMEWORK_SCHEMA_SUFFIXES, FrameworkSchemaConfigError, frameworkAccountName, resolveFrameworkSchemas, SCHEMA_NAME_PATTERN, assertSchemaName, assertFrameworkSchemaNames, frameworkSchemasForMigration, DEFAULT_MIGRATION_LEDGER_TABLE, MIGRATION_LEDGER_TABLE_PATTERN, resolveMigrationLedgerLocation, LEDGER_DATABASE_NAME_PATTERN, assertLedgerDatabaseName, migrationLedgerCandidates, } from './framework-schemas.js';
|
|
8
|
+
export type { MigrationLedgerLocation } from './framework-schemas.js';
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/database/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,YAAY,EACV,gBAAgB,EAChB,sBAAsB,EACtB,uBAAuB,GACxB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EACL,8BAA8B,EAC9B,0BAA0B,EAC1B,8BAA8B,EAC9B,yBAAyB,EACzB,0BAA0B,EAC1B,oBAAoB,EACpB,uBAAuB,EAEvB,mBAAmB,EACnB,gBAAgB,EAChB,0BAA0B,EAC1B,4BAA4B,EAE5B,8BAA8B,EAC9B,8BAA8B,EAC9B,8BAA8B,EAG9B,4BAA4B,EAC5B,wBAAwB,EACxB,yBAAyB,GAC1B,MAAM,wBAAwB,CAAC;AAChC,YAAY,EAAE,uBAAuB,EAAE,MAAM,wBAAwB,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Framework database names (configurable-framework-db, ADR-0037): the single source of the
|
|
3
|
+
* four `<namespace>_{app,identity,authz,audit}` names and the namespaced account defaults.
|
|
4
|
+
* Pure + zero-dep (ADR-0003).
|
|
5
|
+
*/
|
|
6
|
+
export { DEFAULT_FRAMEWORK_DB_NAMESPACE, FRAMEWORK_DB_NAMESPACE_ENV, FRAMEWORK_DB_NAMESPACE_PATTERN, FRAMEWORK_SCHEMA_SUFFIXES, FrameworkSchemaConfigError, frameworkAccountName, resolveFrameworkSchemas,
|
|
7
|
+
// Phase 4: caller-supplied name checks + the migration fallback helper (append-only).
|
|
8
|
+
SCHEMA_NAME_PATTERN, assertSchemaName, assertFrameworkSchemaNames, frameworkSchemasForMigration,
|
|
9
|
+
// Phase 6: the one migration-ledger location rule (migrate CLI + runtime boot check).
|
|
10
|
+
DEFAULT_MIGRATION_LEDGER_TABLE, MIGRATION_LEDGER_TABLE_PATTERN, resolveMigrationLedgerLocation,
|
|
11
|
+
// G-007 QA P1/P2-3: the boot check's candidate ledger locations and the operator ledger-name
|
|
12
|
+
// check (append-only).
|
|
13
|
+
LEDGER_DATABASE_NAME_PATTERN, assertLedgerDatabaseName, migrationLedgerCandidates, } from './framework-schemas.js';
|
|
14
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/database/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAOH,OAAO,EACL,8BAA8B,EAC9B,0BAA0B,EAC1B,8BAA8B,EAC9B,yBAAyB,EACzB,0BAA0B,EAC1B,oBAAoB,EACpB,uBAAuB;AACvB,sFAAsF;AACtF,mBAAmB,EACnB,gBAAgB,EAChB,0BAA0B,EAC1B,4BAA4B;AAC5B,sFAAsF;AACtF,8BAA8B,EAC9B,8BAA8B,EAC9B,8BAA8B;AAC9B,6FAA6F;AAC7F,uBAAuB;AACvB,4BAA4B,EAC5B,wBAAwB,EACxB,yBAAyB,GAC1B,MAAM,wBAAwB,CAAC"}
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AAGH,cAAc,qBAAqB,CAAC;AACpC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,oBAAoB,CAAC;AACnC,cAAc,iBAAiB,CAAC;AAChC,cAAc,0BAA0B,CAAC;AACzC,cAAc,kBAAkB,CAAC;AACjC,cAAc,uBAAuB,CAAC;AACtC,cAAc,oBAAoB,CAAC;AAGnC,cAAc,qBAAqB,CAAC;AAKpC,cAAc,kBAAkB,CAAC;AACjC,cAAc,oBAAoB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AAGH,cAAc,qBAAqB,CAAC;AACpC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,oBAAoB,CAAC;AACnC,cAAc,iBAAiB,CAAC;AAChC,cAAc,0BAA0B,CAAC;AACzC,cAAc,kBAAkB,CAAC;AACjC,cAAc,uBAAuB,CAAC;AACtC,cAAc,oBAAoB,CAAC;AAGnC,cAAc,qBAAqB,CAAC;AAKpC,cAAc,kBAAkB,CAAC;AACjC,cAAc,oBAAoB,CAAC;AAGnC,cAAc,qBAAqB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -48,4 +48,6 @@ export * from './metadata/index.js';
|
|
|
48
48
|
// signatures + the query/command service factories + command-pipeline order.
|
|
49
49
|
export * from './admin/index.js';
|
|
50
50
|
export * from './storage/index.js';
|
|
51
|
+
// Framework database names (configurable-framework-db, ADR-0037) — append-only.
|
|
52
|
+
export * from './database/index.js';
|
|
51
53
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AAEH,kEAAkE;AAClE,cAAc,qBAAqB,CAAC;AACpC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,oBAAoB,CAAC;AACnC,cAAc,iBAAiB,CAAC;AAChC,cAAc,0BAA0B,CAAC;AACzC,cAAc,kBAAkB,CAAC;AACjC,cAAc,uBAAuB,CAAC;AACtC,cAAc,oBAAoB,CAAC;AAEnC,mFAAmF;AACnF,cAAc,qBAAqB,CAAC;AAEpC,4EAA4E;AAC5E,iFAAiF;AACjF,6EAA6E;AAC7E,cAAc,kBAAkB,CAAC;AACjC,cAAc,oBAAoB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AAEH,kEAAkE;AAClE,cAAc,qBAAqB,CAAC;AACpC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,oBAAoB,CAAC;AACnC,cAAc,iBAAiB,CAAC;AAChC,cAAc,0BAA0B,CAAC;AACzC,cAAc,kBAAkB,CAAC;AACjC,cAAc,uBAAuB,CAAC;AACtC,cAAc,oBAAoB,CAAC;AAEnC,mFAAmF;AACnF,cAAc,qBAAqB,CAAC;AAEpC,4EAA4E;AAC5E,iFAAiF;AACjF,6EAA6E;AAC7E,cAAc,kBAAkB,CAAC;AACjC,cAAc,oBAAoB,CAAC;AAEnC,gFAAgF;AAChF,cAAc,qBAAqB,CAAC"}
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Framework database names (configurable-framework-db, spec §5.A, §10, §13; ADR-0037).
|
|
4
|
+
*
|
|
5
|
+
* The single source of the four framework database names. `SHIELD_DB_NAMESPACE` renames all
|
|
6
|
+
* four together (`beta` ⇒ `beta_app`, `beta_identity`, `beta_authz`, `beta_audit`); unset keeps
|
|
7
|
+
* today's `shield_*` names. Pure and zero-dep (ADR-0003): the caller passes an env-like record,
|
|
8
|
+
* nothing here reads `process.env`.
|
|
9
|
+
*
|
|
10
|
+
* SECURITY: fail closed (Shield #1) — a set-but-empty or non-matching namespace throws; a
|
|
11
|
+
* failure never returns a partial set. Names reach SQL only after this validation, and the
|
|
12
|
+
* pattern admits no quote, backtick, dot or whitespace.
|
|
13
|
+
*/
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.LEDGER_DATABASE_NAME_PATTERN = exports.MIGRATION_LEDGER_TABLE_PATTERN = exports.DEFAULT_MIGRATION_LEDGER_TABLE = exports.SCHEMA_NAME_PATTERN = exports.FrameworkSchemaConfigError = exports.FRAMEWORK_SCHEMA_SUFFIXES = exports.FRAMEWORK_DB_NAMESPACE_PATTERN = exports.DEFAULT_FRAMEWORK_DB_NAMESPACE = exports.FRAMEWORK_DB_NAMESPACE_ENV = void 0;
|
|
16
|
+
exports.resolveFrameworkSchemas = resolveFrameworkSchemas;
|
|
17
|
+
exports.frameworkAccountName = frameworkAccountName;
|
|
18
|
+
exports.assertSchemaName = assertSchemaName;
|
|
19
|
+
exports.assertFrameworkSchemaNames = assertFrameworkSchemaNames;
|
|
20
|
+
exports.frameworkSchemasForMigration = frameworkSchemasForMigration;
|
|
21
|
+
exports.assertLedgerDatabaseName = assertLedgerDatabaseName;
|
|
22
|
+
exports.resolveMigrationLedgerLocation = resolveMigrationLedgerLocation;
|
|
23
|
+
exports.migrationLedgerCandidates = migrationLedgerCandidates;
|
|
24
|
+
/** The env variable that selects the namespace. */
|
|
25
|
+
exports.FRAMEWORK_DB_NAMESPACE_ENV = 'SHIELD_DB_NAMESPACE';
|
|
26
|
+
/** The namespace used when `SHIELD_DB_NAMESPACE` is unset (today's names). */
|
|
27
|
+
exports.DEFAULT_FRAMEWORK_DB_NAMESPACE = 'shield';
|
|
28
|
+
/**
|
|
29
|
+
* Allowed namespace. `<ns>_identity`, the longest name, stays within MariaDB's 64-character
|
|
30
|
+
* identifier limit (55 + 9).
|
|
31
|
+
*/
|
|
32
|
+
// Letters and digits only (spec v3): `_` is a one-character wildcard in MariaDB database-level
|
|
33
|
+
// grants, so a namespace containing `_` could make one environment's grants match another's
|
|
34
|
+
// databases (`team_a_app` also matches `team1a_app`).
|
|
35
|
+
exports.FRAMEWORK_DB_NAMESPACE_PATTERN = /^[a-z][a-z0-9]{0,54}$/;
|
|
36
|
+
/** The fixed database suffixes, one per framework module. */
|
|
37
|
+
exports.FRAMEWORK_SCHEMA_SUFFIXES = Object.freeze({
|
|
38
|
+
app: '_app',
|
|
39
|
+
identity: '_identity',
|
|
40
|
+
authz: '_authz',
|
|
41
|
+
audit: '_audit',
|
|
42
|
+
});
|
|
43
|
+
/** Thrown for an invalid framework database configuration. Never carries a secret. */
|
|
44
|
+
class FrameworkSchemaConfigError extends Error {
|
|
45
|
+
constructor(message) {
|
|
46
|
+
super(message);
|
|
47
|
+
this.name = 'FrameworkSchemaConfigError';
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
exports.FrameworkSchemaConfigError = FrameworkSchemaConfigError;
|
|
51
|
+
function namesFor(namespace, isDefault) {
|
|
52
|
+
return Object.freeze({
|
|
53
|
+
namespace,
|
|
54
|
+
app: `${namespace}${exports.FRAMEWORK_SCHEMA_SUFFIXES.app}`,
|
|
55
|
+
identity: `${namespace}${exports.FRAMEWORK_SCHEMA_SUFFIXES.identity}`,
|
|
56
|
+
authz: `${namespace}${exports.FRAMEWORK_SCHEMA_SUFFIXES.authz}`,
|
|
57
|
+
audit: `${namespace}${exports.FRAMEWORK_SCHEMA_SUFFIXES.audit}`,
|
|
58
|
+
isDefault,
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Resolve the four framework database names from an env-like record.
|
|
63
|
+
* @throws FrameworkSchemaConfigError when `SHIELD_DB_NAMESPACE` is set but empty or invalid.
|
|
64
|
+
*/
|
|
65
|
+
function resolveFrameworkSchemas(env) {
|
|
66
|
+
const raw = env[exports.FRAMEWORK_DB_NAMESPACE_ENV];
|
|
67
|
+
if (raw === undefined)
|
|
68
|
+
return namesFor(exports.DEFAULT_FRAMEWORK_DB_NAMESPACE, true);
|
|
69
|
+
if (!exports.FRAMEWORK_DB_NAMESPACE_PATTERN.test(raw)) {
|
|
70
|
+
// The value is not echoed: it is config, not a secret, but the message stays a fixed shape.
|
|
71
|
+
throw new FrameworkSchemaConfigError(`${exports.FRAMEWORK_DB_NAMESPACE_ENV} is set but ${raw === '' ? 'empty' : 'invalid'}; ` +
|
|
72
|
+
`expected ${exports.FRAMEWORK_DB_NAMESPACE_PATTERN.source} (unset it for the default "${exports.DEFAULT_FRAMEWORK_DB_NAMESPACE}")`);
|
|
73
|
+
}
|
|
74
|
+
// An explicit default namespace is the default: same names AND the same rules (ledger, old
|
|
75
|
+
// migrate fallback), so `SHIELD_DB_NAMESPACE=shield` never behaves differently from unset.
|
|
76
|
+
return namesFor(raw, raw === exports.DEFAULT_FRAMEWORK_DB_NAMESPACE);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* The DB account name for a matrix account role under a namespace: `<ns>_<role>`
|
|
80
|
+
* (`frameworkAccountName('beta', 'core')` ⇒ `beta_core`). Roles are the matrix account names
|
|
81
|
+
* without the `shield_` prefix (`core`, `migrate`, `audit_ops`, ...).
|
|
82
|
+
*/
|
|
83
|
+
function frameworkAccountName(namespace, role) {
|
|
84
|
+
return `${namespace}_${role}`;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* A database name a caller may hand to framework SQL (`opts.schema`, `forRoot({ schemas })`):
|
|
88
|
+
* lower-case letters, digits and `_`, 1..64 characters (MariaDB's identifier limit). Names come
|
|
89
|
+
* from the resolver or from operator config, both lower-case (MariaDB database names are
|
|
90
|
+
* case-sensitive on Linux, so a mixed-case name is refused rather than guessed). It admits no
|
|
91
|
+
* backtick, quote, dot, whitespace or `;`, so a name that passes can be backtick-quoted safely.
|
|
92
|
+
*/
|
|
93
|
+
exports.SCHEMA_NAME_PATTERN = /^[a-z0-9_]{1,64}$/;
|
|
94
|
+
/**
|
|
95
|
+
* Check a caller-supplied database name before it reaches SQL (configurable-framework-db,
|
|
96
|
+
* spec §11). Returns the name unchanged.
|
|
97
|
+
* @param label names the setting in the error (e.g. `opts.schema`); the value is never echoed.
|
|
98
|
+
* @throws FrameworkSchemaConfigError when the name is not a string matching
|
|
99
|
+
* {@link SCHEMA_NAME_PATTERN}.
|
|
100
|
+
*/
|
|
101
|
+
function assertSchemaName(name, label) {
|
|
102
|
+
if (typeof name !== 'string' || !exports.SCHEMA_NAME_PATTERN.test(name)) {
|
|
103
|
+
throw new FrameworkSchemaConfigError(`${label} is not a valid database name; expected ${exports.SCHEMA_NAME_PATTERN.source}`);
|
|
104
|
+
}
|
|
105
|
+
return name;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Check all four names of a caller-supplied set (`forRoot({ schemas })`, a store's `schemas`
|
|
109
|
+
* argument, `ctx.schemas`) with {@link assertSchemaName}. Returns the same object.
|
|
110
|
+
* @throws FrameworkSchemaConfigError naming `<label>.<field>` for the first bad name.
|
|
111
|
+
*/
|
|
112
|
+
function assertFrameworkSchemaNames(schemas, label) {
|
|
113
|
+
if (typeof schemas !== 'object' || schemas === null) {
|
|
114
|
+
throw new FrameworkSchemaConfigError(`${label} must be an object with app, identity, authz and audit database names`);
|
|
115
|
+
}
|
|
116
|
+
for (const key of ['app', 'identity', 'authz', 'audit']) {
|
|
117
|
+
assertSchemaName(schemas[key], `${label}.${key}`);
|
|
118
|
+
}
|
|
119
|
+
return schemas;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* The framework database names for a framework migration's `up`/`down` (spec v2 §5.H, decided
|
|
123
|
+
* Q2). `@astram/guard` and `@astram/identity` do not depend on `@astram/migrate`, so their
|
|
124
|
+
* migrations may run under an older CLI that passes no `ctx.schemas`:
|
|
125
|
+
* - `ctx.schemas` present (current `@astram/migrate`) ⇒ those names, validated;
|
|
126
|
+
* - absent and `SHIELD_DB_NAMESPACE` unset (or `shield`) ⇒ the default `shield_*` names (today's
|
|
127
|
+
* behaviour);
|
|
128
|
+
* - absent and `SHIELD_DB_NAMESPACE` set to any other value ⇒ throws before any DDL, telling the
|
|
129
|
+
* operator to upgrade `@astram/migrate`.
|
|
130
|
+
*
|
|
131
|
+
* Usage in a migration file: `const s = frameworkSchemasForMigration(ctx, process.env);`.
|
|
132
|
+
* Pure: the caller passes the env.
|
|
133
|
+
* @throws FrameworkSchemaConfigError as above, or for an invalid name in `ctx.schemas`.
|
|
134
|
+
*/
|
|
135
|
+
function frameworkSchemasForMigration(ctx, env) {
|
|
136
|
+
const given = ctx?.schemas;
|
|
137
|
+
if (given !== undefined)
|
|
138
|
+
return assertFrameworkSchemaNames(given, 'ctx.schemas');
|
|
139
|
+
const ns = env[exports.FRAMEWORK_DB_NAMESPACE_ENV];
|
|
140
|
+
if (ns !== undefined && ns !== exports.DEFAULT_FRAMEWORK_DB_NAMESPACE) {
|
|
141
|
+
throw new FrameworkSchemaConfigError(`${exports.FRAMEWORK_DB_NAMESPACE_ENV} is set but this @astram/migrate does not pass ctx.schemas; ` +
|
|
142
|
+
'upgrade @astram/migrate');
|
|
143
|
+
}
|
|
144
|
+
return namesFor(exports.DEFAULT_FRAMEWORK_DB_NAMESPACE, true);
|
|
145
|
+
}
|
|
146
|
+
/** The `@astram/migrate` ledger table when `APP_MIGRATION_TABLE` is unset. */
|
|
147
|
+
exports.DEFAULT_MIGRATION_LEDGER_TABLE = 'schema_migrations';
|
|
148
|
+
/** A ledger table name: a plain identifier, safe to backtick-quote. */
|
|
149
|
+
exports.MIGRATION_LEDGER_TABLE_PATTERN = /^[A-Za-z0-9_$]{1,64}$/;
|
|
150
|
+
/**
|
|
151
|
+
* An operator-supplied ledger database name (`DDL_DB_NAME`, or a migrate job's `APP_DB_NAME` in
|
|
152
|
+
* default mode): any plain MariaDB identifier, mixed case included, so a name that worked with
|
|
153
|
+
* earlier `@astram/migrate` releases keeps working. Letters, digits and `_` only, 1..64: no
|
|
154
|
+
* backtick, quote, dot, whitespace or `;`, so it is safe to backtick-quote or single-quote.
|
|
155
|
+
* Resolver-produced names stay on the stricter {@link SCHEMA_NAME_PATTERN}.
|
|
156
|
+
*/
|
|
157
|
+
exports.LEDGER_DATABASE_NAME_PATTERN = /^[A-Za-z0-9_]{1,64}$/;
|
|
158
|
+
/**
|
|
159
|
+
* Check an operator-supplied ledger database name before it reaches SQL. Returns it unchanged.
|
|
160
|
+
* @param label names the setting in the error (e.g. `DDL_DB_NAME`); the value is never echoed.
|
|
161
|
+
* @throws FrameworkSchemaConfigError when the name does not match
|
|
162
|
+
* {@link LEDGER_DATABASE_NAME_PATTERN}.
|
|
163
|
+
*/
|
|
164
|
+
function assertLedgerDatabaseName(name, label) {
|
|
165
|
+
if (typeof name !== 'string' || !exports.LEDGER_DATABASE_NAME_PATTERN.test(name)) {
|
|
166
|
+
throw new FrameworkSchemaConfigError(`${label} is not a valid database name; expected ${exports.LEDGER_DATABASE_NAME_PATTERN.source}`);
|
|
167
|
+
}
|
|
168
|
+
return name;
|
|
169
|
+
}
|
|
170
|
+
function ledgerTable(env) {
|
|
171
|
+
const table = env.APP_MIGRATION_TABLE ?? exports.DEFAULT_MIGRATION_LEDGER_TABLE;
|
|
172
|
+
if (!exports.MIGRATION_LEDGER_TABLE_PATTERN.test(table)) {
|
|
173
|
+
throw new FrameworkSchemaConfigError(`APP_MIGRATION_TABLE is not a valid table name; expected ${exports.MIGRATION_LEDGER_TABLE_PATTERN.source}`);
|
|
174
|
+
}
|
|
175
|
+
return table;
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* The ledger-location rule of the migrate CLI (configurable-framework-db spec 5.G):
|
|
179
|
+
* - namespace set ⇒ always `<ns>_app`; a `DDL_DB_NAME` other than `<ns>_app` throws;
|
|
180
|
+
* - namespace unset (or `shield`) ⇒ `DDL_DB_NAME`, else the default app database. (The CLI adds
|
|
181
|
+
* its released fallback to the job's `APP_DB_NAME` before `shield_app`.)
|
|
182
|
+
* Table: `APP_MIGRATION_TABLE`, else `schema_migrations`. The runtime boot check does not use this
|
|
183
|
+
* single location: it tries {@link migrationLedgerCandidates}. Pure: the caller passes the env.
|
|
184
|
+
* @throws FrameworkSchemaConfigError naming the variable on a conflicting or invalid name.
|
|
185
|
+
*/
|
|
186
|
+
function resolveMigrationLedgerLocation(schemas, env) {
|
|
187
|
+
const ddl = env.DDL_DB_NAME;
|
|
188
|
+
let database;
|
|
189
|
+
if (schemas.isDefault) {
|
|
190
|
+
database =
|
|
191
|
+
ddl === undefined
|
|
192
|
+
? assertSchemaName(schemas.app, 'ledger')
|
|
193
|
+
: assertLedgerDatabaseName(ddl, 'DDL_DB_NAME');
|
|
194
|
+
}
|
|
195
|
+
else {
|
|
196
|
+
if (ddl !== undefined && ddl !== schemas.app) {
|
|
197
|
+
throw new FrameworkSchemaConfigError(`DDL_DB_NAME must be ${schemas.app} or unset when ` +
|
|
198
|
+
`${exports.FRAMEWORK_DB_NAMESPACE_ENV}=${schemas.namespace}: the migration ledger lives in ` +
|
|
199
|
+
`${schemas.app} (spec 5.G).`);
|
|
200
|
+
}
|
|
201
|
+
database = schemas.app;
|
|
202
|
+
}
|
|
203
|
+
return { database, table: ledgerTable(env) };
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Where the runtime boot check looks for the ledger, in order (G-007 QA P1). A standard scaffold
|
|
207
|
+
* runs `astram-migrate` with the server's env, so in default mode migrate writes the ledger to
|
|
208
|
+
* `DDL_DB_NAME`, else the job's `APP_DB_NAME`, else `shield_app`; the boot check cannot know
|
|
209
|
+
* which env the migrate job had, so it tries each:
|
|
210
|
+
* - namespace set ⇒ only `<ns>_app` (as {@link resolveMigrationLedgerLocation}; `APP_DB_NAME`
|
|
211
|
+
* is ignored, a conflicting `DDL_DB_NAME` throws);
|
|
212
|
+
* - namespace unset (or `shield`) ⇒ `DDL_DB_NAME`, the runtime `APP_DB_NAME`, then the default
|
|
213
|
+
* app database; unset or blank values skipped, duplicates dropped.
|
|
214
|
+
* The caller uses the first location whose ledger table exists and must still find every shipped
|
|
215
|
+
* id there; when none exists it fails closed naming every location. Pure: the caller passes the
|
|
216
|
+
* env. Returns a frozen, non-empty list.
|
|
217
|
+
* @throws FrameworkSchemaConfigError naming the variable on a conflicting or invalid name.
|
|
218
|
+
*/
|
|
219
|
+
function migrationLedgerCandidates(schemas, env) {
|
|
220
|
+
if (!schemas.isDefault) {
|
|
221
|
+
return Object.freeze([Object.freeze(resolveMigrationLedgerLocation(schemas, env))]);
|
|
222
|
+
}
|
|
223
|
+
const table = ledgerTable(env);
|
|
224
|
+
const databases = [];
|
|
225
|
+
for (const key of ['DDL_DB_NAME', 'APP_DB_NAME']) {
|
|
226
|
+
const raw = env[key];
|
|
227
|
+
if (raw === undefined || raw.trim() === '')
|
|
228
|
+
continue;
|
|
229
|
+
databases.push(assertLedgerDatabaseName(raw.trim(), key));
|
|
230
|
+
}
|
|
231
|
+
databases.push(assertSchemaName(schemas.app, 'ledger'));
|
|
232
|
+
return Object.freeze([...new Set(databases)].map((database) => Object.freeze({ database, table })));
|
|
233
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Framework database names (configurable-framework-db, ADR-0037): the single source of the
|
|
4
|
+
* four `<namespace>_{app,identity,authz,audit}` names and the namespaced account defaults.
|
|
5
|
+
* Pure + zero-dep (ADR-0003).
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.migrationLedgerCandidates = exports.assertLedgerDatabaseName = exports.LEDGER_DATABASE_NAME_PATTERN = exports.resolveMigrationLedgerLocation = exports.MIGRATION_LEDGER_TABLE_PATTERN = exports.DEFAULT_MIGRATION_LEDGER_TABLE = exports.frameworkSchemasForMigration = exports.assertFrameworkSchemaNames = exports.assertSchemaName = exports.SCHEMA_NAME_PATTERN = exports.resolveFrameworkSchemas = exports.frameworkAccountName = exports.FrameworkSchemaConfigError = exports.FRAMEWORK_SCHEMA_SUFFIXES = exports.FRAMEWORK_DB_NAMESPACE_PATTERN = exports.FRAMEWORK_DB_NAMESPACE_ENV = exports.DEFAULT_FRAMEWORK_DB_NAMESPACE = void 0;
|
|
9
|
+
var framework_schemas_js_1 = require("./framework-schemas.js");
|
|
10
|
+
Object.defineProperty(exports, "DEFAULT_FRAMEWORK_DB_NAMESPACE", { enumerable: true, get: function () { return framework_schemas_js_1.DEFAULT_FRAMEWORK_DB_NAMESPACE; } });
|
|
11
|
+
Object.defineProperty(exports, "FRAMEWORK_DB_NAMESPACE_ENV", { enumerable: true, get: function () { return framework_schemas_js_1.FRAMEWORK_DB_NAMESPACE_ENV; } });
|
|
12
|
+
Object.defineProperty(exports, "FRAMEWORK_DB_NAMESPACE_PATTERN", { enumerable: true, get: function () { return framework_schemas_js_1.FRAMEWORK_DB_NAMESPACE_PATTERN; } });
|
|
13
|
+
Object.defineProperty(exports, "FRAMEWORK_SCHEMA_SUFFIXES", { enumerable: true, get: function () { return framework_schemas_js_1.FRAMEWORK_SCHEMA_SUFFIXES; } });
|
|
14
|
+
Object.defineProperty(exports, "FrameworkSchemaConfigError", { enumerable: true, get: function () { return framework_schemas_js_1.FrameworkSchemaConfigError; } });
|
|
15
|
+
Object.defineProperty(exports, "frameworkAccountName", { enumerable: true, get: function () { return framework_schemas_js_1.frameworkAccountName; } });
|
|
16
|
+
Object.defineProperty(exports, "resolveFrameworkSchemas", { enumerable: true, get: function () { return framework_schemas_js_1.resolveFrameworkSchemas; } });
|
|
17
|
+
// Phase 4: caller-supplied name checks + the migration fallback helper (append-only).
|
|
18
|
+
Object.defineProperty(exports, "SCHEMA_NAME_PATTERN", { enumerable: true, get: function () { return framework_schemas_js_1.SCHEMA_NAME_PATTERN; } });
|
|
19
|
+
Object.defineProperty(exports, "assertSchemaName", { enumerable: true, get: function () { return framework_schemas_js_1.assertSchemaName; } });
|
|
20
|
+
Object.defineProperty(exports, "assertFrameworkSchemaNames", { enumerable: true, get: function () { return framework_schemas_js_1.assertFrameworkSchemaNames; } });
|
|
21
|
+
Object.defineProperty(exports, "frameworkSchemasForMigration", { enumerable: true, get: function () { return framework_schemas_js_1.frameworkSchemasForMigration; } });
|
|
22
|
+
// Phase 6: the one migration-ledger location rule (migrate CLI + runtime boot check).
|
|
23
|
+
Object.defineProperty(exports, "DEFAULT_MIGRATION_LEDGER_TABLE", { enumerable: true, get: function () { return framework_schemas_js_1.DEFAULT_MIGRATION_LEDGER_TABLE; } });
|
|
24
|
+
Object.defineProperty(exports, "MIGRATION_LEDGER_TABLE_PATTERN", { enumerable: true, get: function () { return framework_schemas_js_1.MIGRATION_LEDGER_TABLE_PATTERN; } });
|
|
25
|
+
Object.defineProperty(exports, "resolveMigrationLedgerLocation", { enumerable: true, get: function () { return framework_schemas_js_1.resolveMigrationLedgerLocation; } });
|
|
26
|
+
// G-007 QA P1/P2-3: the boot check's candidate ledger locations and the operator ledger-name
|
|
27
|
+
// check (append-only).
|
|
28
|
+
Object.defineProperty(exports, "LEDGER_DATABASE_NAME_PATTERN", { enumerable: true, get: function () { return framework_schemas_js_1.LEDGER_DATABASE_NAME_PATTERN; } });
|
|
29
|
+
Object.defineProperty(exports, "assertLedgerDatabaseName", { enumerable: true, get: function () { return framework_schemas_js_1.assertLedgerDatabaseName; } });
|
|
30
|
+
Object.defineProperty(exports, "migrationLedgerCandidates", { enumerable: true, get: function () { return framework_schemas_js_1.migrationLedgerCandidates; } });
|
package/dist-cjs/index.js
CHANGED
|
@@ -64,3 +64,5 @@ __exportStar(require("./metadata/index.js"), exports);
|
|
|
64
64
|
// signatures + the query/command service factories + command-pipeline order.
|
|
65
65
|
__exportStar(require("./admin/index.js"), exports);
|
|
66
66
|
__exportStar(require("./storage/index.js"), exports);
|
|
67
|
+
// Framework database names (configurable-framework-db, ADR-0037) — append-only.
|
|
68
|
+
__exportStar(require("./database/index.js"), exports);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@astram/core",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.7.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Astram framework — sealed security core (Identity, Authentication, Session, Risk, Authorization, Audit). ZERO framework deps — the portable decision brain (ADR-0003). The frozen public contract consumers build against; installed, not edited (ADR-0023).",
|
|
6
6
|
"type": "module",
|