@fabrika/cli 0.9.0-dev.21 → 0.9.0-dev.23

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": "@fabrika/cli",
3
- "version": "0.9.0-dev.21",
3
+ "version": "0.9.0-dev.23",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",
@@ -31,20 +31,20 @@
31
31
  },
32
32
  "dependencies": {
33
33
  "zod": "^3.25.76",
34
- "@fabrika/app": "0.9.0-dev.21",
35
- "@fabrika/auth-core": "0.9.0-dev.21",
36
- "@fabrika/control-contract": "0.9.0-dev.21",
37
- "@fabrika/operations-contract": "0.9.0-dev.21",
38
- "@fabrika/provider-contract": "0.9.0-dev.21",
39
- "@fabrika/db": "0.9.0-dev.21",
40
- "@fabrika/platform": "0.9.0-dev.21",
41
- "@fabrika/platform-node": "0.9.0-dev.21",
42
- "@fabrika/check": "0.9.0-dev.21"
34
+ "@fabrika/app": "0.9.0-dev.23",
35
+ "@fabrika/auth-core": "0.9.0-dev.23",
36
+ "@fabrika/control-contract": "0.9.0-dev.23",
37
+ "@fabrika/operations-contract": "0.9.0-dev.23",
38
+ "@fabrika/provider-contract": "0.9.0-dev.23",
39
+ "@fabrika/db": "0.9.0-dev.23",
40
+ "@fabrika/platform": "0.9.0-dev.23",
41
+ "@fabrika/platform-node": "0.9.0-dev.23",
42
+ "@fabrika/check": "0.9.0-dev.23"
43
43
  },
44
44
  "peerDependencies": {
45
- "@fabrika/builder": "0.9.0-dev.21",
46
- "@fabrika/provider-cloudflare": "0.9.0-dev.21",
47
- "@fabrika/provider-zerops": "0.9.0-dev.21"
45
+ "@fabrika/builder": "0.9.0-dev.23",
46
+ "@fabrika/provider-cloudflare": "0.9.0-dev.23",
47
+ "@fabrika/provider-zerops": "0.9.0-dev.23"
48
48
  },
49
49
  "peerDependenciesMeta": {
50
50
  "@fabrika/builder": {
package/src/db.ts CHANGED
@@ -27,9 +27,10 @@ export const DB_USAGE = `fabrika db — an application's database schema: Postgr
27
27
  Usage:
28
28
  fabrika db new <slug> Write ${MIGRATIONS_DIR}/<UTC YYYYMMDDHHMMSS>_<slug>.sql; the slug is lowercase snake_case.
29
29
  fabrika db migrate Apply the framework's tables, then ${MIGRATIONS_DIR}, to the application's schema app_<id>.
30
- fabrika db codegen [--verify] Write ${SCHEMA_FILE} from the migrated schema; --verify only fails when it is stale.
30
+ fabrika db codegen [--verify] [--config=<path>] Write ${SCHEMA_FILE} from the migrated schema; --verify only fails when it is stale.
31
31
 
32
- Run from the application root. \`migrate\` and \`codegen\` read FABRIKA_APP_ID and FABRIKA_APP_DATABASE_URL from
32
+ Run from the application root. \`--config\` names the application's config, ${CONFIG_FILE} by default, as \`fabrika app
33
+ build\` takes it: its provider decides the dialect. \`migrate\` and \`codegen\` read FABRIKA_APP_ID and FABRIKA_APP_DATABASE_URL from
33
34
  the environment only. FABRIKA_APP_DATABASE_URL is the provisioning credential: \`migrate\` uses it only to create
34
35
  the application's roles (app_<id> owns the schema, run_<id> gets DML on its tables) and fails when it cannot create
35
36
  roles. It applies the files as app_<id>, holds a lock derived from the application id, runs each file in one
@@ -59,12 +60,22 @@ const refuseArguments = (command: string, argv: readonly string[]): void => {
59
60
  if (argv.length > 0) throw new Error(`db ${command} takes no argument ${argv[0]}\n\n${DB_USAGE}`)
60
61
  }
61
62
 
62
- /** The dialect `fabrika.config.ts` names, read as the checker reads it; Postgres without a config. */
63
- const dialectAt = async (cwd: string): Promise<ApplicationDialect> => {
64
- const config = await readFile(join(cwd, CONFIG_FILE), 'utf8').catch(() => undefined)
63
+ /** The dialect the config at `configPath` names, read as the checker reads `fabrika.config.ts`; Postgres without a config. */
64
+ const dialectAt = async (cwd: string, configPath: string = CONFIG_FILE): Promise<ApplicationDialect> => {
65
+ const config = await readFile(join(cwd, configPath), 'utf8').catch(() => undefined)
65
66
  return applicationDialect(new Map(config === undefined ? [] : [[CONFIG_FILE, config]]))
66
67
  }
67
68
 
69
+ const CONFIG_OPTION = '--config='
70
+
71
+ /** `--config=<path>`, relative to the application root, and the arguments besides it. */
72
+ const configOption = (argv: readonly string[]): { readonly config: string; readonly rest: readonly string[] } => {
73
+ const named = argv.filter((arg) => arg.startsWith(CONFIG_OPTION))
74
+ const config = named.at(-1)?.slice(CONFIG_OPTION.length)
75
+ if (named.length > 1 || config === '') throw new Error(`db: --config names one config file\n\n${DB_USAGE}`)
76
+ return { config: config ?? CONFIG_FILE, rest: argv.filter((arg) => !arg.startsWith(CONFIG_OPTION)) }
77
+ }
78
+
68
79
  const timestamp = (date: Date): string => date.toISOString().replace(/\.\d{3}Z$/, '').replaceAll(/[-:T]/g, '')
69
80
 
70
81
  const newMigration = async (argv: readonly string[], deps: DbCommandDependencies): Promise<void> => {
@@ -133,7 +144,7 @@ const postgresSchema = async (deps: DbCommandDependencies): Promise<GeneratedMod
133
144
  const migrations = applicationMigrations(required(deps.source, 'FABRIKA_APP_ID'), deps.cwd)
134
145
  const url = required(deps.source, 'FABRIKA_APP_DATABASE_URL')
135
146
  try {
136
- // The migrations' manifest: the start check refuses any database object no migration declares.
147
+ // The migrations' manifest: the check refuses any database object no migration declares.
137
148
  return await generateSchema(url, migrations.schema, migrations.objects())
138
149
  } catch (error) {
139
150
  throw reportable(error)
@@ -148,9 +159,10 @@ const sqliteSchema = async (cwd: string): Promise<GeneratedModule> =>
148
159
  ])
149
160
 
150
161
  const codegen = async (argv: readonly string[], deps: DbCommandDependencies): Promise<void> => {
151
- const verify = argv.includes('--verify')
152
- refuseArguments('codegen', argv.filter((arg) => arg !== '--verify'))
153
- const { source, leftOut } = (await dialectAt(deps.cwd)) === 'sqlite' ? await sqliteSchema(deps.cwd) : await postgresSchema(deps)
162
+ const { config, rest } = configOption(argv)
163
+ const verify = rest.includes('--verify')
164
+ refuseArguments('codegen', rest.filter((arg) => arg !== '--verify'))
165
+ const { source, leftOut } = (await dialectAt(deps.cwd, config)) === 'sqlite' ? await sqliteSchema(deps.cwd) : await postgresSchema(deps)
154
166
  const path = join(deps.cwd, SCHEMA_FILE)
155
167
  // Named on every run, --verify's too, so a column the module leaves out is never silent.
156
168
  for (const column of leftOut) deps.write(`${leftOutText(`${column.table}.${column.column}`, column.type, column.of)}\n`)
package/types/src/db.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export declare const DB_USAGE = "fabrika db \u2014 an application's database schema: Postgres, or SQLite on Cloudflare\n\nUsage:\n fabrika db new <slug> Write db/migrations/<UTC YYYYMMDDHHMMSS>_<slug>.sql; the slug is lowercase snake_case.\n fabrika db migrate Apply the framework's tables, then db/migrations, to the application's schema app_<id>.\n fabrika db codegen [--verify] Write src/db/schema.generated.ts from the migrated schema; --verify only fails when it is stale.\n\nRun from the application root. `migrate` and `codegen` read FABRIKA_APP_ID and FABRIKA_APP_DATABASE_URL from\nthe environment only. FABRIKA_APP_DATABASE_URL is the provisioning credential: `migrate` uses it only to create\nthe application's roles (app_<id> owns the schema, run_<id> gets DML on its tables) and fails when it cannot create\nroles. It applies the files as app_<id>, holds a lock derived from the application id, runs each file in one\ntransaction with lock_timeout 5000 ms and statement_timeout 60000 ms, and fails when a\nfile that already ran has changed: an applied migration is immutable, so a change is a new file.\n\nAn application whose fabrika.config.ts default-exports @fabrika/provider-cloudflare's defineApp runs on SQLite in its\nDurable Object, which applies the migrations itself when it boots: `migrate` refuses, `codegen` applies them to an\nin-memory database; neither needs a database URL.\n";
1
+ export declare const DB_USAGE = "fabrika db \u2014 an application's database schema: Postgres, or SQLite on Cloudflare\n\nUsage:\n fabrika db new <slug> Write db/migrations/<UTC YYYYMMDDHHMMSS>_<slug>.sql; the slug is lowercase snake_case.\n fabrika db migrate Apply the framework's tables, then db/migrations, to the application's schema app_<id>.\n fabrika db codegen [--verify] [--config=<path>] Write src/db/schema.generated.ts from the migrated schema; --verify only fails when it is stale.\n\nRun from the application root. `--config` names the application's config, fabrika.config.ts by default, as `fabrika app\nbuild` takes it: its provider decides the dialect. `migrate` and `codegen` read FABRIKA_APP_ID and FABRIKA_APP_DATABASE_URL from\nthe environment only. FABRIKA_APP_DATABASE_URL is the provisioning credential: `migrate` uses it only to create\nthe application's roles (app_<id> owns the schema, run_<id> gets DML on its tables) and fails when it cannot create\nroles. It applies the files as app_<id>, holds a lock derived from the application id, runs each file in one\ntransaction with lock_timeout 5000 ms and statement_timeout 60000 ms, and fails when a\nfile that already ran has changed: an applied migration is immutable, so a change is a new file.\n\nAn application whose fabrika.config.ts default-exports @fabrika/provider-cloudflare's defineApp runs on SQLite in its\nDurable Object, which applies the migrations itself when it boots: `migrate` refuses, `codegen` applies them to an\nin-memory database; neither needs a database URL.\n";
2
2
  export interface DbCommandDependencies {
3
3
  /** The application root. */
4
4
  readonly cwd: string;