@c9up/atlas 0.3.5 → 0.3.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.
@@ -21,10 +21,12 @@ type Constructor = new (...args: unknown[]) => unknown;
21
21
  export type { AtlasCommandClass } from "./contract.js";
22
22
 
23
23
  /**
24
- * Build the `atlas:check` command for the given models. Register it in
25
- * `reamrc.commands` (atlas has no global entity registry — list your models,
26
- * as in Lucid). Run it as `ream atlas:check`; `--warn` reports drift without a
27
- * non-zero exit (useful for an advisory CI step).
24
+ * Build the `atlas:check` command for the given models.
25
+ *
26
+ * Most applications do not call this: `@c9up/atlas/commands` ships the command
27
+ * already, reading the models from `verifySchema.entities`. Call it directly to
28
+ * verify a different set — pass a function when the list is only known once the
29
+ * application has booted.
28
30
  *
29
31
  * @example
30
32
  * // commands/atlas-check.ts
@@ -35,7 +37,7 @@ export type { AtlasCommandClass } from "./contract.js";
35
37
  * // run: ream atlas:check --warn
36
38
  */
37
39
  export function schemaCheckCommand(
38
- entities: readonly Constructor[],
40
+ entities: readonly Constructor[] | (() => readonly Constructor[]),
39
41
  ): AtlasCommandClass {
40
42
  return class SchemaCheck {
41
43
  static commandName = "atlas:check";
@@ -58,7 +60,18 @@ export function schemaCheckCommand(
58
60
  process.exitCode = 1;
59
61
  return;
60
62
  }
61
- const code = await runSchemaCheck(entities, db, getAtlasDialect());
63
+ // Resolved here, not at registration: the shipped command reads the
64
+ // models from `verifySchema.entities`, which only exists once the
65
+ // application has booted its config.
66
+ const models = typeof entities === "function" ? entities() : entities;
67
+ if (models.length === 0) {
68
+ console.error(
69
+ "[atlas:check] no models to verify — list them under `verifySchema.entities` in config/database.ts.",
70
+ );
71
+ process.exitCode = 1;
72
+ return;
73
+ }
74
+ const code = await runSchemaCheck(models, db, getAtlasDialect());
62
75
  // `--warn` downgrades drift to advisory (exit 0); default fails CI.
63
76
  if (code !== 0 && !this.warn) process.exitCode = code;
64
77
  }
@@ -13,7 +13,10 @@
13
13
  * container hooks).
14
14
  */
15
15
 
16
- import type { ConnectionConfig } from "../AtlasProvider.js";
16
+ import type {
17
+ AtlasDatabaseConfig,
18
+ ConnectionConfig,
19
+ } from "../AtlasProvider.js";
17
20
  import type { AsyncDatabaseConnection } from "../adapters/NapiDbAdapter.js";
18
21
  import { ConnectionManager } from "../ConnectionManager.js";
19
22
  import {
@@ -171,6 +174,86 @@ export function getDb(): AsyncDatabaseConnection | undefined {
171
174
  return instance;
172
175
  }
173
176
 
177
+ // The config the provider booted with. Commands need it: `migration:run` has to
178
+ // know where the migration files are, and Lucid answers that from the config
179
+ // too (`connection.config.migrations.paths`, read through `lucid.db`). Atlas has
180
+ // no framework container to read, so the provider records it here.
181
+ let databaseConfig:
182
+ | { config: AtlasDatabaseConfig; defaultName: string }
183
+ | undefined;
184
+
185
+ /** @internal Record the booted config (called by AtlasProvider). */
186
+ export function setDatabaseConfig(
187
+ config: AtlasDatabaseConfig,
188
+ defaultName: string,
189
+ ): void {
190
+ databaseConfig = { config, defaultName };
191
+ }
192
+
193
+ /**
194
+ * @internal Forget the config IF it is still the one `config` describes.
195
+ * Ownership-guarded for the same reason {@link clearDb} is: a second provider
196
+ * may have rebound it, and the older provider's shutdown must not clear the
197
+ * newer binding.
198
+ */
199
+ export function clearDatabaseConfig(config: AtlasDatabaseConfig): void {
200
+ if (databaseConfig?.config === config) databaseConfig = undefined;
201
+ }
202
+
203
+ /** The config the application booted with, or `undefined` before boot. */
204
+ export function getDatabaseConfig(): AtlasDatabaseConfig | undefined {
205
+ return databaseConfig?.config;
206
+ }
207
+
208
+ /** The default connection's name — Lucid `db.primaryConnectionName`. */
209
+ export function primaryConnectionName(): string {
210
+ return databaseConfig?.defaultName ?? "primary";
211
+ }
212
+
213
+ /**
214
+ * One connection's config, by name (default connection when unnamed).
215
+ *
216
+ * Read from the manager's node first — that is where Lucid keeps `migrations`,
217
+ * `seeders` and `schemaGeneration`, and the shape we mirror. The top level is
218
+ * the fallback, because the single-connection form IS the connection config,
219
+ * and because an app that set `migrations` there before these keys were
220
+ * per-connection must keep working.
221
+ */
222
+ export function connectionConfigFor(name?: string): ConnectionConfig {
223
+ const resolved = name ?? primaryConnectionName();
224
+ const top = databaseConfig?.config;
225
+ // The manager knows a connection once the provider has registered it; before
226
+ // that — a command running against a config nobody opened yet — the declared
227
+ // `connections` entry is the same information.
228
+ const declared =
229
+ manager.get(resolved)?.config ?? top?.connections?.[resolved];
230
+ if (top === undefined) return declared ?? {};
231
+ if (declared === undefined) return top;
232
+ return {
233
+ ...top,
234
+ ...declared,
235
+ // These three are merged a level down rather than replaced: the top level
236
+ // is the single-connection form of the same config, so a connection that
237
+ // names only its paths must not drop a `tableName` set beside it.
238
+ migrations: mergeOrUndefined(top.migrations, declared.migrations),
239
+ seeders: mergeOrUndefined(top.seeders, declared.seeders),
240
+ schemaGeneration: mergeOrUndefined(
241
+ top.schemaGeneration,
242
+ declared.schemaGeneration,
243
+ ),
244
+ };
245
+ }
246
+
247
+ /** `{...a, ...b}`, or `undefined` when neither side has anything to merge. */
248
+ function mergeOrUndefined<T extends object>(
249
+ base: T | undefined,
250
+ override: T | undefined,
251
+ ): T | undefined {
252
+ if (base === undefined) return override;
253
+ if (override === undefined) return base;
254
+ return { ...base, ...override };
255
+ }
256
+
174
257
  // The shared connection manager (Lucid `db.manager`) — the single owner of named
175
258
  // connections. Backs `BaseModel.connection = 'analytics'` so a model resolves a
176
259
  // non-default connection from a plain import (AdonisJS `static connection`).