@xubylele/schema-forge 0.3.1
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/LICENSE +15 -0
- package/README.md +322 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +1179 -0
- package/package.json +53 -0
package/LICENSE
ADDED
|
@@ -0,0 +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.
|
package/README.md
ADDED
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
# SchemaForge
|
|
2
|
+
|
|
3
|
+
A modern CLI tool for database schema management with a clean DSL and automatic SQL migration generation.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Simple DSL** - Define your schema with a clean, intuitive syntax
|
|
8
|
+
- **Migration Generation** - Automatically generate SQL migrations from schema changes
|
|
9
|
+
- **State Tracking** - Built-in state management to track your schema evolution
|
|
10
|
+
- **Type Safety** - Validates your schema before generating SQL
|
|
11
|
+
- **Postgres/Supabase** - Currently supports PostgreSQL and Supabase
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
Install globally via npm:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install -g @xubylele/schema-forge
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Or use directly with npx (no installation required):
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
npx @xubylele/schema-forge init
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
> **Note for contributors:** This is a scoped package. To publish, use `npm publish --access public` or run `npm run publish:public`.
|
|
28
|
+
|
|
29
|
+
## Development
|
|
30
|
+
|
|
31
|
+
Clone the repository and install dependencies:
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
git clone <repository-url>
|
|
35
|
+
cd schema-forge
|
|
36
|
+
npm install
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Build the project:
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
npm run build
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Run in development mode:
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
npm run dev -- [command]
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Run tests:
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
npm test
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Getting Started
|
|
58
|
+
|
|
59
|
+
Here's a quick walkthrough to get started with SchemaForge:
|
|
60
|
+
|
|
61
|
+
### 1. Initialize a new project
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
schemaforge init
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
This creates:
|
|
68
|
+
|
|
69
|
+
- `schemaforge/schema.sf` - Your schema definition file
|
|
70
|
+
- `schemaforge/config.json` - Project configuration
|
|
71
|
+
- `schemaforge/state.json` - State tracking file
|
|
72
|
+
- `supabase/migrations/` - Directory for generated migrations
|
|
73
|
+
|
|
74
|
+
### 2. Define your schema
|
|
75
|
+
|
|
76
|
+
Edit `schemaforge/schema.sf`:
|
|
77
|
+
|
|
78
|
+
```sql
|
|
79
|
+
# SchemaForge schema definition
|
|
80
|
+
|
|
81
|
+
table users {
|
|
82
|
+
id uuid pk
|
|
83
|
+
email varchar unique
|
|
84
|
+
name text
|
|
85
|
+
created_at timestamptz default now()
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
table posts {
|
|
89
|
+
id uuid pk
|
|
90
|
+
user_id uuid fk users.id
|
|
91
|
+
title varchar
|
|
92
|
+
content text
|
|
93
|
+
published boolean default false
|
|
94
|
+
created_at timestamptz default now()
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### 3. Generate your first migration
|
|
99
|
+
|
|
100
|
+
```bash
|
|
101
|
+
schemaforge generate
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
This generates a timestamped SQL migration file with CREATE TABLE statements and updates the state file.
|
|
105
|
+
|
|
106
|
+
### 4. Make schema changes
|
|
107
|
+
|
|
108
|
+
Edit `schemaforge/schema.sf` to add a new column:
|
|
109
|
+
|
|
110
|
+
```sql
|
|
111
|
+
table users {
|
|
112
|
+
id uuid pk
|
|
113
|
+
email varchar unique
|
|
114
|
+
name text
|
|
115
|
+
avatar_url text # New column!
|
|
116
|
+
created_at timestamptz default now()
|
|
117
|
+
}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### 5. Generate a migration for the changes
|
|
121
|
+
|
|
122
|
+
```bash
|
|
123
|
+
schemaforge generate --name "add user avatar"
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
This generates a new migration file with ALTER TABLE statements.
|
|
127
|
+
|
|
128
|
+
### 6. Check for pending changes
|
|
129
|
+
|
|
130
|
+
```bash
|
|
131
|
+
schemaforge diff
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
If your schema matches the state file, you'll see "No changes detected". If there are changes, it will display the SQL that would be generated.
|
|
135
|
+
|
|
136
|
+
## Commands
|
|
137
|
+
|
|
138
|
+
### `schemaforge init`
|
|
139
|
+
|
|
140
|
+
Initialize a new SchemaForge project in the current directory.
|
|
141
|
+
|
|
142
|
+
```bash
|
|
143
|
+
schemaforge init
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
Creates the necessary directory structure and configuration files.
|
|
147
|
+
|
|
148
|
+
### `schemaforge generate`
|
|
149
|
+
|
|
150
|
+
Generate SQL migration from schema changes.
|
|
151
|
+
|
|
152
|
+
```bash
|
|
153
|
+
schemaforge generate [--name "migration description"]
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
**Options:**
|
|
157
|
+
|
|
158
|
+
- `--name` - Optional name for the migration (default: "migration")
|
|
159
|
+
|
|
160
|
+
Compares your current schema with the tracked state, generates SQL for any changes, and updates the state file.
|
|
161
|
+
|
|
162
|
+
### `schemaforge diff`
|
|
163
|
+
|
|
164
|
+
Compare your schema with the current state without generating files.
|
|
165
|
+
|
|
166
|
+
```bash
|
|
167
|
+
schemaforge diff
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
Shows what SQL would be generated if you ran `generate`. Useful for previewing changes.
|
|
171
|
+
|
|
172
|
+
## Schema DSL Format
|
|
173
|
+
|
|
174
|
+
Schemas are defined using the `.sf` format with a clean, readable syntax.
|
|
175
|
+
|
|
176
|
+
### Basic Syntax
|
|
177
|
+
|
|
178
|
+
```sql
|
|
179
|
+
# Comments start with # or //
|
|
180
|
+
|
|
181
|
+
table table_name {
|
|
182
|
+
column_name column_type [modifiers...]
|
|
183
|
+
}
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
### Supported Column Types
|
|
187
|
+
|
|
188
|
+
- `uuid` - UUID/GUID
|
|
189
|
+
- `varchar` - Variable-length string
|
|
190
|
+
- `text` - Long text
|
|
191
|
+
- `int` - Integer
|
|
192
|
+
- `boolean` - Boolean value
|
|
193
|
+
- `timestamptz` - Timestamp with timezone
|
|
194
|
+
- `date` - Date without time
|
|
195
|
+
|
|
196
|
+
### Column Modifiers
|
|
197
|
+
|
|
198
|
+
- `pk` - Primary key
|
|
199
|
+
- `unique` - Unique constraint
|
|
200
|
+
- `nullable` - Allow NULL values (columns are NOT NULL by default)
|
|
201
|
+
- `default <value>` - Default value (e.g., `default now()`, `default false`, `default 0`)
|
|
202
|
+
- `fk <table>.<column>` - Foreign key reference (e.g., `fk users.id`)
|
|
203
|
+
|
|
204
|
+
### Examples
|
|
205
|
+
|
|
206
|
+
#### Simple table
|
|
207
|
+
|
|
208
|
+
```sql
|
|
209
|
+
table users {
|
|
210
|
+
id uuid pk
|
|
211
|
+
email varchar unique
|
|
212
|
+
name text
|
|
213
|
+
created_at timestamptz default now()
|
|
214
|
+
}
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
#### Table with foreign keys
|
|
218
|
+
|
|
219
|
+
```sql
|
|
220
|
+
table posts {
|
|
221
|
+
id uuid pk
|
|
222
|
+
author_id uuid fk users.id
|
|
223
|
+
title varchar
|
|
224
|
+
content text
|
|
225
|
+
published boolean default false
|
|
226
|
+
created_at timestamptz default now()
|
|
227
|
+
}
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
#### Table with nullable columns
|
|
231
|
+
|
|
232
|
+
```sql
|
|
233
|
+
table profiles {
|
|
234
|
+
id uuid pk
|
|
235
|
+
user_id uuid fk users.id
|
|
236
|
+
bio text nullable
|
|
237
|
+
avatar_url text nullable
|
|
238
|
+
website varchar nullable
|
|
239
|
+
updated_at timestamptz default now()
|
|
240
|
+
}
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
## Project Structure
|
|
244
|
+
|
|
245
|
+
```bash
|
|
246
|
+
your-project/
|
|
247
|
+
├── schemaforge/
|
|
248
|
+
│ ├── schema.sf # Your schema definition (edit this!)
|
|
249
|
+
│ ├── config.json # Project configuration
|
|
250
|
+
│ └── state.json # State tracking (auto-generated)
|
|
251
|
+
└── supabase/
|
|
252
|
+
└── migrations/ # Generated SQL migrations
|
|
253
|
+
├── 20240101120000-initial.sql
|
|
254
|
+
└── 20240101120100-add-user-avatar.sql
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
## Configuration
|
|
258
|
+
|
|
259
|
+
The `schemaforge/config.json` file contains project configuration:
|
|
260
|
+
|
|
261
|
+
```json
|
|
262
|
+
{
|
|
263
|
+
"provider": "supabase",
|
|
264
|
+
"outputDir": "supabase/migrations",
|
|
265
|
+
"schemaFile": "schemaforge/schema.sf",
|
|
266
|
+
"stateFile": "schemaforge/state.json",
|
|
267
|
+
"sql": {
|
|
268
|
+
"uuidDefault": "gen_random_uuid()",
|
|
269
|
+
"timestampDefault": "now()"
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
## Supported Databases
|
|
275
|
+
|
|
276
|
+
Currently supports:
|
|
277
|
+
|
|
278
|
+
- PostgreSQL (`postgres`)
|
|
279
|
+
- Supabase (`supabase`)
|
|
280
|
+
|
|
281
|
+
## Development Workflow
|
|
282
|
+
|
|
283
|
+
A typical development workflow looks like this:
|
|
284
|
+
|
|
285
|
+
1. **Initialize** - `schemaforge init` (one time)
|
|
286
|
+
2. **Edit schema** - Modify `schemaforge/schema.sf`
|
|
287
|
+
3. **Preview changes** - `schemaforge diff` (optional)
|
|
288
|
+
4. **Generate migration** - `schemaforge generate --name "description"`
|
|
289
|
+
5. **Apply migration** - Run the generated SQL against your database
|
|
290
|
+
6. **Repeat** - Continue editing and generating migrations as needed
|
|
291
|
+
|
|
292
|
+
## Tips
|
|
293
|
+
|
|
294
|
+
- Use descriptive migration names with `--name` to make your migration history readable
|
|
295
|
+
- Run `diff` before `generate` to preview what SQL will be created
|
|
296
|
+
- Commit your schema files and migrations to version control
|
|
297
|
+
- The state file tracks your schema evolution - don't edit it manually
|
|
298
|
+
|
|
299
|
+
## Releasing
|
|
300
|
+
|
|
301
|
+
Schema Forge uses automated releases via GitHub Actions and [Changesets](https://github.com/changesets/changesets).
|
|
302
|
+
|
|
303
|
+
When contributing changes, create a changeset:
|
|
304
|
+
|
|
305
|
+
```bash
|
|
306
|
+
npx changeset
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
Once your PR is merged to `main`, the release workflow automatically:
|
|
310
|
+
|
|
311
|
+
- Bumps the version
|
|
312
|
+
- Updates the CHANGELOG
|
|
313
|
+
- Creates a git tag
|
|
314
|
+
- Publishes to npm
|
|
315
|
+
|
|
316
|
+
No manual steps required! See [docs/releasing.md](docs/releasing.md) for detailed documentation.
|
|
317
|
+
|
|
318
|
+
For detailed guidelines on contributing and automated releases, see [CONTRIBUTING.md](CONTRIBUTING.md) and [docs/releasing.md](docs/releasing.md).
|
|
319
|
+
|
|
320
|
+
## License
|
|
321
|
+
|
|
322
|
+
ISC
|
package/dist/cli.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
#!/usr/bin/env node
|