bazis 0.98.11 → 0.98.12

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.
@@ -232,7 +232,7 @@ export class CodexAppServerClient implements CodexClient {
232
232
  if (method === "account/updated") this.modelCache = undefined;
233
233
  }, () => { this.clearLogin(); this.modelCache = undefined; });
234
234
  const initialized = await client.request("initialize", {
235
- clientInfo: { name: "bazis", title: "Bazis", version: "0.98.11" }, capabilities: { experimentalApi: true },
235
+ clientInfo: { name: "bazis", title: "Bazis", version: "0.98.12" }, capabilities: { experimentalApi: true },
236
236
  }, 15_000, signal);
237
237
  if (resolve(text(initialized.codexHome, 4096)) !== this.paths.home) throw new CodexError("PROTOCOL_ERROR");
238
238
  client.notify("initialized");
@@ -154,18 +154,22 @@ export function defineConfig(prefixOrSchema: string | ConfigSchema<Defaults>, ma
154
154
  const values = new Map<string, ConfigValueType>();
155
155
  const inspection: ConfigInspection[] = [];
156
156
  const issues: string[] = [];
157
+ // A wrong value names what came and where to change it: the variable that
158
+ // supplied it, or the variables that can override the value from the code.
159
+ const got = (raw: unknown) => raw === undefined ? "" : `, got ${JSON.stringify(String(raw)).slice(0, 80)}`;
160
+ const source = (key: string, selected: { readonly name: string } | undefined) => ` (${selected ? selected.name : names.get(key)!.join(", ")})`;
157
161
  for (const key of keys) {
158
162
  const base = schema.default[key]!;
159
163
  const override = schema[environment]?.[key];
160
164
  const cell = override ?? base;
161
165
  const type = isSecret(base) ? "secret" : isEnum(base) ? typeof base.default : typeof base;
162
- const candidates: { raw: string; source: string; priority: number }[] = [];
166
+ const candidates: { raw: string; source: string; priority: number; name: string }[] = [];
163
167
  for (const name of names.get(key)!) {
164
168
  const sourceKey = name.slice(ENV_PREFIX.length).toLowerCase().replaceAll("__", ".");
165
169
  const raw = configuration.get(sourceKey);
166
170
  if (raw === undefined) continue;
167
171
  const origin = configuration.origin(sourceKey);
168
- candidates.push({ raw, ...origin });
172
+ candidates.push({ raw, ...origin, name });
169
173
  }
170
174
  const priority = Math.max(...candidates.map(candidate => candidate.priority));
171
175
  const winners = candidates.filter(candidate => candidate.priority === priority);
@@ -178,17 +182,17 @@ export function defineConfig(prefixOrSchema: string | ConfigSchema<Defaults>, ma
178
182
  else value = new Secret(raw);
179
183
  } else if (type === "number") {
180
184
  const parsed = Number(raw);
181
- if (raw === undefined || (typeof raw === "string" && !raw.trim()) || !Number.isFinite(parsed)) issues.push(`${fullKey(key)} — expected a finite number`);
185
+ if (raw === undefined || (typeof raw === "string" && !raw.trim()) || !Number.isFinite(parsed)) issues.push(`${fullKey(key)} — expected a finite number${got(raw)}${source(key, selected)}`);
182
186
  else value = parsed;
183
187
  } else if (type === "boolean") {
184
188
  const normalized = String(raw).toLowerCase();
185
189
  if (normalized === "true" || normalized === "1") value = true;
186
190
  else if (normalized === "false" || normalized === "0") value = false;
187
- else issues.push(`${fullKey(key)} — expected a boolean`);
191
+ else issues.push(`${fullKey(key)} — expected a boolean (true, false, 1, 0)${got(raw)}${source(key, selected)}`);
188
192
  } else if (typeof raw === "string") value = raw;
189
193
  else issues.push(`${fullKey(key)} — expected a string`);
190
194
  if (value === undefined) continue;
191
- if (isEnum(base) && !base.values.includes(value as string | number)) issues.push(`${fullKey(key)} — value is not one of the declared enum values`);
195
+ if (isEnum(base) && !base.values.includes(value as string | number)) issues.push(`${fullKey(key)} — ${JSON.stringify(value)} is not allowed, use one of: ${base.values.join(", ")}${source(key, selected)}`);
192
196
  try {
193
197
  const issue = validators[key]?.(value);
194
198
  if (issue) issues.push(`${fullKey(key)} — ${issue}`);
@@ -1,4 +1,4 @@
1
- import { InfraError, reader, requireValue, postgresConnectionOptions, type InfraConnector, type PostgresConfigShape } from "../infra";
1
+ import { errorMessage, InfraError, reader, requireValue, postgresConnectionOptions, type InfraConnector, type PostgresConfigShape } from "../infra";
2
2
  import type { AppConfig, ConfigRegistry } from "../kernel";
3
3
  import { postgres, type DatabaseProvider, type PostgresServerTimeouts } from "../../library/orm";
4
4
  import { DATABASE_PROVIDER } from "./DATABASE_PROVIDER";
@@ -86,16 +86,26 @@ export function ormBazisConnect<T extends PostgresOrmConfigShape>(config: AppCon
86
86
  export function ormBazisConnect(
87
87
  config: AppConfig<PostgresOrmConfigShape>,
88
88
  ): InfraConnector<DatabaseProvider> {
89
+ // Where the connection goes, for the startup error; never the password.
90
+ let target = "PostgreSQL";
89
91
  return {
90
92
  token: DATABASE_PROVIDER,
91
93
  config,
92
94
  phase: DATABASE_PHASE,
93
95
  create(configs) {
96
+ const c = reader(config, configs);
97
+ target = `PostgreSQL at ${String(c.get("host"))}:${String(c.get("port"))} (database "${String(c.get("database"))}", user "${String(c.get("username"))}")`;
94
98
  return buildPostgresProvider(config, configs);
95
99
  },
96
100
  async connect(provider, signal) {
97
- if (!(await provider.ping(signal))) {
98
- throw new InfraError('Infra connector "database": postgres is not reachable.');
101
+ if (provider.probe === undefined) {
102
+ if (!(await provider.ping(signal))) throw new InfraError(`Infra connector "database": cannot connect to ${target}.`);
103
+ return;
104
+ }
105
+ try {
106
+ await provider.probe(signal);
107
+ } catch (error) {
108
+ throw new InfraError(`Infra connector "database": cannot connect to ${target}: ${connectionFailure(error)}`);
99
109
  }
100
110
  },
101
111
  async dispose(provider) {
@@ -106,3 +116,12 @@ export function ormBazisConnect(
106
116
  },
107
117
  };
108
118
  }
119
+
120
+ /** The driver's reason with its code: `password authentication failed for user "app" (28P01)`. */
121
+ function connectionFailure(error: unknown): string {
122
+ const details = error as { errno?: unknown; code?: unknown };
123
+ const code = typeof details.errno === "string" && details.errno ? details.errno
124
+ : typeof details.code === "string" && details.code ? details.code : undefined;
125
+ const message = errorMessage(error);
126
+ return code === undefined || message.includes(code) ? message : `${message} (${code})`;
127
+ }
@@ -409,13 +409,17 @@ export class PostgresProvider implements DatabaseProvider {
409
409
 
410
410
  async ping(signal?: AbortSignal): Promise<boolean> {
411
411
  try {
412
- await withPostgresScopeOptions({ signal }, () => this.pooledOperation((session) => this.runQuery(session, "SELECT 1", [])));
412
+ await this.probe(signal);
413
413
  return true;
414
414
  } catch {
415
415
  return false;
416
416
  }
417
417
  }
418
418
 
419
+ async probe(signal?: AbortSignal): Promise<void> {
420
+ await withPostgresScopeOptions({ signal }, () => this.pooledOperation((session) => this.runQuery(session, "SELECT 1", [])));
421
+ }
422
+
419
423
  async introspect(): Promise<IntrospectedSchema> {
420
424
  const tableRows = await this.query(
421
425
  `SELECT table_schema, table_name
@@ -96,6 +96,7 @@ export function createObservedProvider(provider: DatabaseProvider, observe: Tran
96
96
  afterCommit: provider.afterCommit?.bind(provider),
97
97
  afterRollback: provider.afterRollback?.bind(provider),
98
98
  ping: (signal?: AbortSignal): Promise<boolean> => observe(() => provider.ping(signal)),
99
+ probe: provider.probe ? (signal?: AbortSignal): Promise<void> => observe(() => provider.probe!(signal)) : undefined,
99
100
  introspect: () => observe(() => provider.introspect()),
100
101
  schemaAdmissionCapability: schema ? {
101
102
  version: schema.version,
@@ -178,6 +178,11 @@ export interface DatabaseProvider {
178
178
  afterRollback?(callback: TransactionCallback): void;
179
179
  /** Connection check for the health check. */
180
180
  ping(signal?: AbortSignal): Promise<boolean>;
181
+ /**
182
+ * Connection check that rejects with the driver's error instead of `false`,
183
+ * so a startup failure can say why (wrong password, missing database, …).
184
+ */
185
+ probe?(signal?: AbortSignal): Promise<void>;
181
186
  /** Introspection of the actual database schema (for the auto-migration). */
182
187
  introspect(): Promise<IntrospectedSchema>;
183
188
  /**
@@ -103,6 +103,7 @@ export function withRetry(provider: DatabaseProvider, options?: ExecutionStrateg
103
103
  afterCommit: provider.afterCommit ? (callback) => provider.afterCommit!(callback) : undefined,
104
104
  afterRollback: provider.afterRollback ? (callback) => provider.afterRollback!(callback) : undefined,
105
105
  ping: (signal) => provider.ping(signal),
106
+ probe: provider.probe ? (signal) => provider.probe!(signal) : undefined,
106
107
  introspect: () => provider.introspect(),
107
108
  schemaAdmissionCapability: provider.schemaAdmissionCapability,
108
109
  withMigrationLock: provider.withMigrationLock ? (work) => provider.withMigrationLock!(work) : undefined,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bazis",
3
- "version": "0.98.11",
3
+ "version": "0.98.12",
4
4
  "description": "Modular backend framework for Bun in TypeScript: DI wired by codegen, HTTP, a PostgreSQL ORM, JWT, WebSocket, agents and a CLI.",
5
5
  "license": "MIT",
6
6
  "author": "Alexander Alenin",