@xubylele/schema-forge 1.7.0 → 1.8.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/README.md CHANGED
@@ -29,6 +29,19 @@ Or use directly with npx (no installation required):
29
29
  npx @xubylele/schema-forge init
30
30
  ```
31
31
 
32
+ ### Programmatic API
33
+
34
+ Use the programmatic API from Node (e.g. scripts, GitHub Actions) instead of invoking the CLI via shell:
35
+
36
+ ```js
37
+ const { generate, EXIT_CODES } = require('@xubylele/schema-forge/api');
38
+
39
+ const result = await generate({ name: 'MyMigration' });
40
+ if (result.exitCode !== EXIT_CODES.SUCCESS) process.exit(result.exitCode);
41
+ ```
42
+
43
+ **Exports:** `init`, `generate`, `diff`, `doctor`, `validate`, `introspect`, `importSchema` (each returns `Promise<RunResult>`), `RunResult` (`{ exitCode: number }`), `EXIT_CODES`, and option types (`GenerateOptions`, `DiffOptions`, etc.). Entrypoint: `@xubylele/schema-forge/api`. Exit code semantics: [docs/exit-codes.json](docs/exit-codes.json).
44
+
32
45
  ## Development
33
46
 
34
47
  Clone the repository and install dependencies:
@@ -298,18 +311,7 @@ Live mode options:
298
311
  - `--url` - PostgreSQL connection URL for live drift validation (fallback: `DATABASE_URL`)
299
312
  - `--schema` - Comma-separated schema names to introspect (default: `public`)
300
313
 
301
- In live mode, exit code `2` is used when drift is detected between `state.json` and the live database.
302
-
303
- Exit codes (also see [CI Behavior](#ci-behavior)):
304
-
305
- - `3` in CI environment if destructive findings detected
306
- - `1` if one or more `error` findings are detected
307
- - `0` if no `error` findings are detected (warnings alone do not fail)
308
-
309
- Exit codes:
310
-
311
- - `1` when one or more `error` findings are detected
312
- - `0` when no `error` findings are detected (warnings alone do not fail)
314
+ In live mode, exit code `2` is used when drift is detected between `state.json` and the live database. For all exit codes used by `validate`, see [Exit code standards](#exit-code-standards).
313
315
 
314
316
  ### `schema-forge doctor`
315
317
 
@@ -331,10 +333,7 @@ Options:
331
333
  - `--schema` - Comma-separated schema names to introspect (default: `public`)
332
334
  - `--json` - Output structured drift report JSON
333
335
 
334
- Exit codes:
335
-
336
- - `0` when no drift is detected (healthy)
337
- - `2` when drift is detected between `state.json` and live database schema
336
+ Exit codes: see [Exit code standards](#exit-code-standards) (doctor uses 0, 2).
338
337
 
339
338
  ### `schema-forge introspect`
340
339
 
@@ -362,16 +361,29 @@ CI mode is automatically activated when either environment variable is set:
362
361
  - `CI=true`
363
362
  - `CONTINUOUS_INTEGRATION=true`
364
363
 
365
- ### Exit Codes
364
+ ### Exit code standards
366
365
 
367
- SchemaForge uses specific exit codes for different scenarios:
366
+ SchemaForge uses specific exit codes for deterministic CI and script behavior. The following is the single source of truth.
368
367
 
369
- | Exit Code | Meaning |
370
- | --------- | ------- |
371
- | `0` | Success - no changes or no destructive operations detected |
372
- | `1` | Validation/general error - invalid DSL, operation declined, missing files, etc. |
373
- | `2` | Drift detected between expected state and live database schema |
374
- | `3` | **CI Destructive** - destructive operations detected in CI environment without `--force` |
368
+ | Exit Code | Name | Meaning |
369
+ | --------- | ---- | ------- |
370
+ | `0` | SUCCESS | No changes or no destructive operations detected |
371
+ | `1` | VALIDATION_ERROR | Invalid DSL, config errors, missing files, or operation declined (e.g. with `--safe`) |
372
+ | `2` | DRIFT_DETECTED | Drift between expected state and live database schema |
373
+ | `3` | CI_DESTRUCTIVE | Destructive operations detected in CI without `--force` |
374
+
375
+ **Per-command exit codes:**
376
+
377
+ | Command | Possible exit codes |
378
+ | ------- | -------------------- |
379
+ | `validate` | 0, 1, 2, 3 |
380
+ | `doctor` | 0, 2 |
381
+ | `diff`, `generate` | 0, 1, 3 |
382
+ | `init`, `import` | 0 |
383
+
384
+ (Global CLI errors, e.g. unknown command or missing config, exit with 1.)
385
+
386
+ A machine-readable exit code contract is available at [docs/exit-codes.json](docs/exit-codes.json). It includes a `version` field and an optional `commands` map for tooling.
375
387
 
376
388
  ### Destructive Operations in CI
377
389
 
@@ -429,6 +441,20 @@ schema-forge generate --safe
429
441
 
430
442
  This is useful for strict CI pipelines where all destructive changes must be reviewed and merged separately.
431
443
 
444
+ ### Using exit codes in CI
445
+
446
+ In CI, rely on the process exit code to fail the job when validation or safety checks fail. Example with a shell script:
447
+
448
+ ```bash
449
+ schema-forge validate --json
450
+ if [ $? -ne 0 ]; then
451
+ echo "Schema validation failed (exit code: $?)"
452
+ exit 1
453
+ fi
454
+ ```
455
+
456
+ When using the [schema-forge-action](https://github.com/xubylele/schema-forge-action), the action passes through the CLI exit code: a non-zero exit from Schema Forge fails the job with that same code, so no extra script is needed.
457
+
432
458
  ## Constraint Change Detection
433
459
 
434
460
  SchemaForge detects and generates migrations for:
package/dist/api.d.ts ADDED
@@ -0,0 +1,101 @@
1
+ interface DiffOptions {
2
+ safe?: boolean;
3
+ force?: boolean;
4
+ url?: string;
5
+ schema?: string;
6
+ }
7
+
8
+ interface DoctorOptions {
9
+ json?: boolean;
10
+ url?: string;
11
+ schema?: string;
12
+ }
13
+
14
+ interface GenerateOptions {
15
+ name?: string;
16
+ safe?: boolean;
17
+ force?: boolean;
18
+ }
19
+
20
+ interface ImportOptions {
21
+ out?: string;
22
+ }
23
+
24
+ interface IntrospectOptions {
25
+ url?: string;
26
+ schema?: string;
27
+ json?: boolean;
28
+ out?: string;
29
+ }
30
+
31
+ interface ValidateOptions {
32
+ json?: boolean;
33
+ url?: string;
34
+ schema?: string;
35
+ }
36
+
37
+ /**
38
+ * Exit codes used throughout the CLI for deterministic behavior
39
+ *
40
+ * @see SF-106 Standardize Exit Codes
41
+ */
42
+ declare const EXIT_CODES: {
43
+ /** Successful operation */
44
+ readonly SUCCESS: 0;
45
+ /** Validation error (invalid DSL syntax, config errors, missing files, etc.) */
46
+ readonly VALIDATION_ERROR: 1;
47
+ /** Drift detected - Reserved for future use when comparing actual DB state vs schema */
48
+ readonly DRIFT_DETECTED: 2;
49
+ /** Destructive operation detected in CI environment without --force */
50
+ readonly CI_DESTRUCTIVE: 3;
51
+ };
52
+
53
+ /**
54
+ * Programmatic API for Schema Forge.
55
+ * Use this entrypoint when integrating from Node (e.g. scripts, GitHub Actions)
56
+ * instead of invoking the CLI via shell.
57
+ *
58
+ * @example
59
+ * const { generate, EXIT_CODES } = require('@xubylele/schema-forge/api');
60
+ * const result = await generate({ name: 'MyMigration' });
61
+ * if (result.exitCode !== EXIT_CODES.SUCCESS) process.exit(result.exitCode);
62
+ */
63
+
64
+ /**
65
+ * Result of a programmatic command run. Exit codes match the CLI contract.
66
+ * @see docs/exit-codes.json
67
+ */
68
+ interface RunResult {
69
+ exitCode: number;
70
+ }
71
+ /**
72
+ * Initialize a new schema project in the current directory.
73
+ */
74
+ declare function init(): Promise<RunResult>;
75
+ /**
76
+ * Generate SQL migration from schema files.
77
+ */
78
+ declare function generate(options?: GenerateOptions): Promise<RunResult>;
79
+ /**
80
+ * Compare two schema versions and generate migration SQL (optionally against live DB).
81
+ */
82
+ declare function diff(options?: DiffOptions): Promise<RunResult>;
83
+ /**
84
+ * Check live database drift against state.
85
+ */
86
+ declare function doctor(options?: DoctorOptions): Promise<RunResult>;
87
+ /**
88
+ * Validate schema and optionally check for destructive changes or live drift.
89
+ */
90
+ declare function validate(options?: ValidateOptions): Promise<RunResult>;
91
+ /**
92
+ * Extract normalized live schema from PostgreSQL.
93
+ */
94
+ declare function introspect(options?: IntrospectOptions): Promise<RunResult>;
95
+ /**
96
+ * Import schema from SQL migrations.
97
+ * @param inputPath - Path to .sql file or migrations directory
98
+ */
99
+ declare function importSchema(inputPath: string, options?: ImportOptions): Promise<RunResult>;
100
+
101
+ export { type DiffOptions, type DoctorOptions, EXIT_CODES, type GenerateOptions, type ImportOptions, type IntrospectOptions, type RunResult, type ValidateOptions, diff, doctor, generate, importSchema, init, introspect, validate };