@xubylele/schema-forge-core 1.0.0 → 1.0.3

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.
Files changed (3) hide show
  1. package/LICENSE +15 -15
  2. package/README.md +254 -254
  3. package/package.json +36 -32
package/LICENSE CHANGED
@@ -1,15 +1,15 @@
1
- ISC License
2
-
3
- Copyright (c) 2025-2026 Xuby
4
-
5
- Permission to use, copy, modify, and/or distribute this software for any
6
- purpose with or without fee is hereby granted, provided that the above
7
- copyright notice and this permission notice appear in all copies.
8
-
9
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10
- WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
- MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12
- ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
- WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14
- ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15
- OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1
+ ISC License
2
+
3
+ Copyright (c) 2025-2026 Xuby
4
+
5
+ Permission to use, copy, modify, and/or distribute this software for any
6
+ purpose with or without fee is hereby granted, provided that the above
7
+ copyright notice and this permission notice appear in all copies.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
package/README.md CHANGED
@@ -1,254 +1,254 @@
1
- # Schema Forge Core
2
-
3
- `@xubylele/schema-forge-core` is the deterministic engine behind Schema Forge. It contains the full domain logic for parsing, diffing, validating, and generating SQL from declarative schema definitions.
4
-
5
- The core is framework-agnostic and built around pure, predictable transformations:
6
-
7
- * **Parse** the `.sf` DSL format
8
- * **Normalize** schema into a state model
9
- * **Diff** schema versions to detect structural changes
10
- * **Validate** schema definitions and destructive changes
11
- * **Generate** SQL migration statements
12
- * **Import** and reconstruct schema from existing SQL migrations
13
-
14
- Same input → same output.
15
-
16
- ---
17
-
18
- ## Philosophy
19
-
20
- * Declarative schema definition
21
- * Deterministic transformations
22
- * Framework agnostic
23
- * Pure domain layer
24
- * Explicit state modeling
25
-
26
- The core contains no hidden side effects. All structural changes are derived from explicit schema input and state.
27
-
28
- ---
29
-
30
- ## Module Boundaries
31
-
32
- ### Core (Pure)
33
-
34
- Environment-agnostic logic:
35
-
36
- * Parsing DSL
37
- * Schema normalization
38
- * Diff engine
39
- * Validation rules
40
- * SQL generation
41
- * SQL parsing to operations
42
- * State modeling
43
-
44
- These modules can run in Node.js or browser environments.
45
-
46
- ### Node Utilities (I/O Helpers)
47
-
48
- Node-only helpers for CLI or tooling environments:
49
-
50
- * File system utilities
51
- * Project path resolution
52
- * Migration file loading
53
-
54
- These require Node.js.
55
-
56
- ---
57
-
58
- ## Installation
59
-
60
- ```bash
61
- npm install @xubylele/schema-forge-core
62
- ```
63
-
64
- ---
65
-
66
- ## Basic Usage
67
-
68
- ### Parse → Diff → Generate SQL
69
-
70
- ```ts
71
- import {
72
- parseSchema,
73
- schemaToState,
74
- diffSchemas,
75
- generateSql
76
- } from "@xubylele/schema-forge-core"
77
-
78
- const schemaSource = `
79
- table users {
80
- id uuid pk
81
- email varchar unique
82
- created_at timestamptz
83
- }
84
- `
85
-
86
- // Parse DSL into schema model
87
- const schema = parseSchema(schemaSource)
88
-
89
- // Convert schema into normalized state
90
- const state = await schemaToState(schema)
91
-
92
- const nextSchemaSource = `
93
- table users {
94
- id uuid pk
95
- email varchar unique
96
- name varchar
97
- created_at timestamptz
98
- }
99
- `
100
-
101
- const nextSchema = parseSchema(nextSchemaSource)
102
-
103
- // Compute structural diff
104
- const diff = diffSchemas(state, nextSchema)
105
-
106
- // Generate SQL for provider
107
- const sql = generateSql(diff, "postgres")
108
-
109
- console.log(sql)
110
- // ALTER TABLE users ADD COLUMN name varchar;
111
- ```
112
-
113
- ---
114
-
115
- ## Schema Validation
116
-
117
- ```ts
118
- import {
119
- validateSchema,
120
- validateSchemaChanges,
121
- toValidationReport
122
- } from "@xubylele/schema-forge-core"
123
-
124
- const schema = parseSchema(schemaSource)
125
-
126
- // Structural validation (throws on invalid schema)
127
- validateSchema(schema)
128
-
129
- // Destructive change validation
130
- const findings = validateSchemaChanges(state, nextSchema)
131
- const report = toValidationReport(findings)
132
-
133
- report.errors.forEach(f => {
134
- console.error(`[error] ${f.code}: ${f.message}`)
135
- })
136
-
137
- report.warnings.forEach(f => {
138
- console.warn(`[warning] ${f.code}: ${f.message}`)
139
- })
140
- ```
141
-
142
- ---
143
-
144
- ## SQL Import (Reverse Engineering)
145
-
146
- ```ts
147
- import {
148
- loadMigrationSqlInput,
149
- parseMigrationSql,
150
- applySqlOps,
151
- schemaToDsl
152
- } from "@xubylele/schema-forge-core"
153
-
154
- const inputs = await loadMigrationSqlInput("./migrations")
155
-
156
- for (const input of inputs) {
157
- const parseResult = parseMigrationSql(input.sql)
158
-
159
- if (parseResult.warnings.length > 0) {
160
- console.warn(parseResult.warnings)
161
- }
162
-
163
- const result = applySqlOps(parseResult.ops)
164
- const dsl = schemaToDsl(result.schema)
165
-
166
- console.log(dsl)
167
- }
168
- ```
169
-
170
- ---
171
-
172
- ## Supported Providers
173
-
174
- * `postgres`
175
-
176
- Additional providers may be implemented in higher-level packages.
177
-
178
- ---
179
-
180
- ## Core API (High-Level)
181
-
182
- ### Parsing & State
183
-
184
- * `parseSchema(source: string): DatabaseSchema`
185
- * `schemaToState(schema: DatabaseSchema): Promise<StateFile>`
186
- * `loadState(filePath: string): Promise<StateFile>`
187
- * `saveState(filePath: string, state: StateFile): Promise<void>`
188
-
189
- ### Diff Engine
190
-
191
- * `diffSchemas(state: StateFile, schema: DatabaseSchema): DiffResult`
192
-
193
- ### Validation
194
-
195
- * `validateSchema(schema: DatabaseSchema): void`
196
- * `validateSchemaChanges(state: StateFile, schema: DatabaseSchema): Finding[]`
197
- * `toValidationReport(findings: Finding[]): ValidationReport`
198
-
199
- ### SQL Generation
200
-
201
- * `generateSql(diff: DiffResult, provider: Provider, config?: SqlConfig): string`
202
-
203
- ### SQL Parsing & Reconstruction
204
-
205
- * `parseMigrationSql(sql: string): ParseResult`
206
- * `applySqlOps(ops: SqlOp[]): ApplySqlOpsResult`
207
- * `schemaToDsl(schema: DatabaseSchema): string`
208
-
209
- Full API surface is available via TypeScript exports.
210
-
211
- ---
212
-
213
- ## Type Exports
214
-
215
- All domain types are exported for integration:
216
-
217
- * `DatabaseSchema`, `Table`, `Column`, `ForeignKey`
218
- * `StateFile`, `StateTable`, `StateColumn`
219
- * `DiffResult`, `Operation`
220
- * `SqlOp`, `ParseResult`, `ApplySqlOpsResult`
221
- * `Finding`, `ValidationReport`, `Severity`
222
- * `Provider`, `SqlConfig`
223
-
224
- ---
225
-
226
- ## Architecture
227
-
228
- ```
229
- DSL (.sf)
230
-
231
- Parser → DatabaseSchema
232
-
233
- State Normalization → StateFile
234
-
235
- Diff Engine → Operations
236
-
237
- SQL Generator → Migration SQL
238
- ```
239
-
240
- ---
241
-
242
- ## Development
243
-
244
- ```bash
245
- npm install
246
- npm run build
247
- npm test
248
- ```
249
-
250
- ---
251
-
252
- ## License
253
-
254
- ISC
1
+ # Schema Forge Core
2
+
3
+ `@xubylele/schema-forge-core` is the deterministic engine behind Schema Forge. It contains the full domain logic for parsing, diffing, validating, and generating SQL from declarative schema definitions.
4
+
5
+ The core is framework-agnostic and built around pure, predictable transformations:
6
+
7
+ * **Parse** the `.sf` DSL format
8
+ * **Normalize** schema into a state model
9
+ * **Diff** schema versions to detect structural changes
10
+ * **Validate** schema definitions and destructive changes
11
+ * **Generate** SQL migration statements
12
+ * **Import** and reconstruct schema from existing SQL migrations
13
+
14
+ Same input → same output.
15
+
16
+ ---
17
+
18
+ ## Philosophy
19
+
20
+ * Declarative schema definition
21
+ * Deterministic transformations
22
+ * Framework agnostic
23
+ * Pure domain layer
24
+ * Explicit state modeling
25
+
26
+ The core contains no hidden side effects. All structural changes are derived from explicit schema input and state.
27
+
28
+ ---
29
+
30
+ ## Module Boundaries
31
+
32
+ ### Core (Pure)
33
+
34
+ Environment-agnostic logic:
35
+
36
+ * Parsing DSL
37
+ * Schema normalization
38
+ * Diff engine
39
+ * Validation rules
40
+ * SQL generation
41
+ * SQL parsing to operations
42
+ * State modeling
43
+
44
+ These modules can run in Node.js or browser environments.
45
+
46
+ ### Node Utilities (I/O Helpers)
47
+
48
+ Node-only helpers for CLI or tooling environments:
49
+
50
+ * File system utilities
51
+ * Project path resolution
52
+ * Migration file loading
53
+
54
+ These require Node.js.
55
+
56
+ ---
57
+
58
+ ## Installation
59
+
60
+ ```bash
61
+ npm install @xubylele/schema-forge-core
62
+ ```
63
+
64
+ ---
65
+
66
+ ## Basic Usage
67
+
68
+ ### Parse → Diff → Generate SQL
69
+
70
+ ```ts
71
+ import {
72
+ parseSchema,
73
+ schemaToState,
74
+ diffSchemas,
75
+ generateSql
76
+ } from "@xubylele/schema-forge-core"
77
+
78
+ const schemaSource = `
79
+ table users {
80
+ id uuid pk
81
+ email varchar unique
82
+ created_at timestamptz
83
+ }
84
+ `
85
+
86
+ // Parse DSL into schema model
87
+ const schema = parseSchema(schemaSource)
88
+
89
+ // Convert schema into normalized state
90
+ const state = await schemaToState(schema)
91
+
92
+ const nextSchemaSource = `
93
+ table users {
94
+ id uuid pk
95
+ email varchar unique
96
+ name varchar
97
+ created_at timestamptz
98
+ }
99
+ `
100
+
101
+ const nextSchema = parseSchema(nextSchemaSource)
102
+
103
+ // Compute structural diff
104
+ const diff = diffSchemas(state, nextSchema)
105
+
106
+ // Generate SQL for provider
107
+ const sql = generateSql(diff, "postgres")
108
+
109
+ console.log(sql)
110
+ // ALTER TABLE users ADD COLUMN name varchar;
111
+ ```
112
+
113
+ ---
114
+
115
+ ## Schema Validation
116
+
117
+ ```ts
118
+ import {
119
+ validateSchema,
120
+ validateSchemaChanges,
121
+ toValidationReport
122
+ } from "@xubylele/schema-forge-core"
123
+
124
+ const schema = parseSchema(schemaSource)
125
+
126
+ // Structural validation (throws on invalid schema)
127
+ validateSchema(schema)
128
+
129
+ // Destructive change validation
130
+ const findings = validateSchemaChanges(state, nextSchema)
131
+ const report = toValidationReport(findings)
132
+
133
+ report.errors.forEach(f => {
134
+ console.error(`[error] ${f.code}: ${f.message}`)
135
+ })
136
+
137
+ report.warnings.forEach(f => {
138
+ console.warn(`[warning] ${f.code}: ${f.message}`)
139
+ })
140
+ ```
141
+
142
+ ---
143
+
144
+ ## SQL Import (Reverse Engineering)
145
+
146
+ ```ts
147
+ import {
148
+ loadMigrationSqlInput,
149
+ parseMigrationSql,
150
+ applySqlOps,
151
+ schemaToDsl
152
+ } from "@xubylele/schema-forge-core"
153
+
154
+ const inputs = await loadMigrationSqlInput("./migrations")
155
+
156
+ for (const input of inputs) {
157
+ const parseResult = parseMigrationSql(input.sql)
158
+
159
+ if (parseResult.warnings.length > 0) {
160
+ console.warn(parseResult.warnings)
161
+ }
162
+
163
+ const result = applySqlOps(parseResult.ops)
164
+ const dsl = schemaToDsl(result.schema)
165
+
166
+ console.log(dsl)
167
+ }
168
+ ```
169
+
170
+ ---
171
+
172
+ ## Supported Providers
173
+
174
+ * `postgres`
175
+
176
+ Additional providers may be implemented in higher-level packages.
177
+
178
+ ---
179
+
180
+ ## Core API (High-Level)
181
+
182
+ ### Parsing & State
183
+
184
+ * `parseSchema(source: string): DatabaseSchema`
185
+ * `schemaToState(schema: DatabaseSchema): Promise<StateFile>`
186
+ * `loadState(filePath: string): Promise<StateFile>`
187
+ * `saveState(filePath: string, state: StateFile): Promise<void>`
188
+
189
+ ### Diff Engine
190
+
191
+ * `diffSchemas(state: StateFile, schema: DatabaseSchema): DiffResult`
192
+
193
+ ### Validation
194
+
195
+ * `validateSchema(schema: DatabaseSchema): void`
196
+ * `validateSchemaChanges(state: StateFile, schema: DatabaseSchema): Finding[]`
197
+ * `toValidationReport(findings: Finding[]): ValidationReport`
198
+
199
+ ### SQL Generation
200
+
201
+ * `generateSql(diff: DiffResult, provider: Provider, config?: SqlConfig): string`
202
+
203
+ ### SQL Parsing & Reconstruction
204
+
205
+ * `parseMigrationSql(sql: string): ParseResult`
206
+ * `applySqlOps(ops: SqlOp[]): ApplySqlOpsResult`
207
+ * `schemaToDsl(schema: DatabaseSchema): string`
208
+
209
+ Full API surface is available via TypeScript exports.
210
+
211
+ ---
212
+
213
+ ## Type Exports
214
+
215
+ All domain types are exported for integration:
216
+
217
+ * `DatabaseSchema`, `Table`, `Column`, `ForeignKey`
218
+ * `StateFile`, `StateTable`, `StateColumn`
219
+ * `DiffResult`, `Operation`
220
+ * `SqlOp`, `ParseResult`, `ApplySqlOpsResult`
221
+ * `Finding`, `ValidationReport`, `Severity`
222
+ * `Provider`, `SqlConfig`
223
+
224
+ ---
225
+
226
+ ## Architecture
227
+
228
+ ```
229
+ DSL (.sf)
230
+
231
+ Parser → DatabaseSchema
232
+
233
+ State Normalization → StateFile
234
+
235
+ Diff Engine → Operations
236
+
237
+ SQL Generator → Migration SQL
238
+ ```
239
+
240
+ ---
241
+
242
+ ## Development
243
+
244
+ ```bash
245
+ npm install
246
+ npm run build
247
+ npm test
248
+ ```
249
+
250
+ ---
251
+
252
+ ## License
253
+
254
+ ISC
package/package.json CHANGED
@@ -1,33 +1,37 @@
1
- {
2
- "name": "@xubylele/schema-forge-core",
3
- "version": "1.0.0",
4
- "description": "Core engine for Schema Forge DSL parsing, diffing and SQL generation",
5
- "license": "ISC",
6
- "author": "xubylele <diazxavier27@gmail.com>",
7
- "type": "module",
8
- "main": "dist/index.js",
9
- "types": "dist/index.d.ts",
10
- "exports": {
11
- ".": {
12
- "import": "./dist/index.js",
13
- "types": "./dist/index.d.ts"
14
- }
15
- },
16
- "files": [
17
- "dist"
18
- ],
19
- "publishConfig": {
20
- "access": "public"
21
- },
22
- "scripts": {
23
- "build": "tsc",
24
- "test": "vitest run",
25
- "dev": "tsc -w"
26
- },
27
- "devDependencies": {
28
- "@changesets/cli": "^2.29.8",
29
- "@types/node": "^20.0.0",
30
- "typescript": "^5.0.0",
31
- "vitest": "^4.0.18"
32
- }
1
+ {
2
+ "name": "@xubylele/schema-forge-core",
3
+ "version": "1.0.3",
4
+ "description": "Core engine for Schema Forge DSL parsing, diffing and SQL generation",
5
+ "license": "ISC",
6
+ "author": "xubylele <diazxavier27@gmail.com>",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/xubylele/schema-forge-core.git"
10
+ },
11
+ "type": "module",
12
+ "main": "dist/index.js",
13
+ "types": "dist/index.d.ts",
14
+ "exports": {
15
+ ".": {
16
+ "import": "./dist/index.js",
17
+ "types": "./dist/index.d.ts"
18
+ }
19
+ },
20
+ "files": [
21
+ "dist"
22
+ ],
23
+ "publishConfig": {
24
+ "access": "public"
25
+ },
26
+ "scripts": {
27
+ "build": "tsc",
28
+ "test": "vitest run",
29
+ "dev": "tsc -w"
30
+ },
31
+ "devDependencies": {
32
+ "@changesets/cli": "^2.29.8",
33
+ "@types/node": "^20.0.0",
34
+ "typescript": "^5.0.0",
35
+ "vitest": "^4.0.18"
36
+ }
33
37
  }