@uncutsql/core 0.0.0-stage → 0.2.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
@@ -1,3 +1,88 @@
1
- # Temporary Holding Version
1
+ # @uncutsql/core
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ The dialect-independent heart of [UncutSQL](../../README.md): the database model, schema
4
+ comparison, migration planning, and the contracts that dialects, drivers and code generators
5
+ implement.
6
+
7
+ **You do not need this package to use UncutSQL.** The CLI depends on it. Install it if you want
8
+ to:
9
+
10
+ - write a **dialect** for another database, or a **driver** for another client;
11
+ - write a **code generator** or a **mapper** for `uncutsql codegen`;
12
+ - compare schemas or plan migrations from your own code.
13
+
14
+ ```sh
15
+ npm install @uncutsql/core effect
16
+ ```
17
+
18
+ All operations return [Effect](https://effect.website) values. Core has no dependency other
19
+ than Effect: no SQL parser, filesystem or database access.
20
+
21
+ ## The pipeline
22
+
23
+ ```mermaid
24
+ flowchart LR
25
+ SQL[Schema SQL] -->|dialect: parse, toDatabaseSchema| Model[DatabaseSchema]
26
+ Model -->|core: diffDatabases, resolveMigrationDecisions| Diff[Diff]
27
+ Diff -->|core: planDatabaseMigration| Procedures[Logical procedures]
28
+ Procedures -->|dialect: lowerMigration, encodeMigration| Migration[Migration SQL]
29
+ Migration -->|driver: MigrationExecutor| DB[(Database)]
30
+ ```
31
+
32
+ - **`DatabaseSchema`** is a serializable description of namespaces, tables, columns,
33
+ constraints, indexes, views and triggers. `decodeDatabaseSchema` validates one restored from
34
+ JSON.
35
+ - **`diffDatabases(before, after)`** reports what changed.
36
+ - **`prepareMigrationDecisions`**, **`mergeMigrationDecisions`** and
37
+ **`resolveMigrationDecisions`** turn removals into explicit questions (drop, drop if empty,
38
+ rename) and validate the answers. Core never guesses a rename.
39
+ - **`planDatabaseMigration(diff)`** produces logical procedures (`CreateTable`, `AlterTable`,
40
+ `RenameTable`, …). Each dialect decides how to carry them out.
41
+
42
+ ```ts
43
+ import { Effect } from "effect";
44
+
45
+ import { planDatabaseMigration, resolveMigrationDecisions } from "@uncutsql/core";
46
+ import { dialect } from "@uncutsql/sqlite";
47
+
48
+ const migrationSql = (beforeSql: string, afterSql: string) =>
49
+ Effect.gen(function* () {
50
+ const before = yield* dialect.toDatabaseSchema(yield* dialect.parse(beforeSql), "before");
51
+ const after = yield* dialect.toDatabaseSchema(yield* dialect.parse(afterSql), "after");
52
+ const { diff } = yield* resolveMigrationDecisions(before, after);
53
+ const plan = yield* dialect.lowerMigration(planDatabaseMigration(diff), before, after);
54
+ return yield* dialect.encodeMigration(plan);
55
+ });
56
+ // migrationSql("create table t (a int)", "create table t (a int, b text)")
57
+ // BEGIN;
58
+ // ALTER TABLE "main"."t" ADD COLUMN "b" text;
59
+ // COMMIT;
60
+ ```
61
+
62
+ `resolveMigrationDecisions` fails with a `MigrationDecisionError` listing the questions when a
63
+ table or column was removed; pass the answered document as its third argument.
64
+
65
+ ## Extension contracts
66
+
67
+ | Contract | Implemented by | Loaded by the CLI from |
68
+ | ------------------- | ------------------------------- | ------------------------------------------ |
69
+ | `Dialect` | `@uncutsql/sqlite`, … | the `dialect` export (or default export) |
70
+ | `MigrationExecutor` | `@uncutsql/sqlite-driver`, … | the `executor` export (or default export) |
71
+ | `MapperPlugin` | `@uncutsql/typescript` | the `mapper` export (or default export) |
72
+ | `GeneratorPlugin` | `@uncutsql/generator-kysely`, … | the `generator` export (or default export) |
73
+
74
+ Point the configuration at your own module to use it:
75
+
76
+ ```json
77
+ {
78
+ "dialect": "./my-dialect.mjs",
79
+ "generators": { "docs": { "plugin": "./my-generator.mjs", "mapper": "app", "output": "./docs" } }
80
+ }
81
+ ```
82
+
83
+ A dialect can also provide `resolveSemanticSchema`, which derives the facts code generators
84
+ rely on (types, nullability, defaults, keys), and a `languageService` for the editor.
85
+
86
+ The full contracts, including migration decision documents and executor helpers, are described
87
+ in [DEVELOPMENT.md](DEVELOPMENT.md). The existing dialect and driver packages are the best
88
+ examples.