terradb 0.4.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.
- package/LICENSE +21 -0
- package/README.md +175 -0
- package/dist/index.js +29731 -0
- package/package.json +93 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Terra Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
# terradb
|
|
2
|
+
|
|
3
|
+
Declarative schema management for PostgreSQL and SQLite.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install -g terradb
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
### PostgreSQL
|
|
14
|
+
|
|
15
|
+
```sql
|
|
16
|
+
-- schema.sql
|
|
17
|
+
CREATE TABLE users (
|
|
18
|
+
id SERIAL PRIMARY KEY,
|
|
19
|
+
email VARCHAR(255) NOT NULL UNIQUE
|
|
20
|
+
);
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
export DATABASE_URL="postgres://user:password@localhost:5432/mydb"
|
|
25
|
+
terradb plan # preview changes
|
|
26
|
+
terradb apply # apply changes
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### SQLite
|
|
30
|
+
|
|
31
|
+
```sql
|
|
32
|
+
-- schema.sql
|
|
33
|
+
CREATE TABLE users (
|
|
34
|
+
id INTEGER PRIMARY KEY,
|
|
35
|
+
email TEXT NOT NULL UNIQUE
|
|
36
|
+
);
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
export DATABASE_URL="sqlite:///path/to/database.db"
|
|
41
|
+
terradb plan
|
|
42
|
+
terradb apply
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## How It Works
|
|
46
|
+
|
|
47
|
+
1. Write your desired schema as CREATE statements
|
|
48
|
+
2. Run `terradb plan` to see what changes are needed
|
|
49
|
+
3. Run `terradb apply` to execute the changes
|
|
50
|
+
|
|
51
|
+
terradb compares your schema file against the current database state and generates the necessary ALTER/DROP/CREATE statements.
|
|
52
|
+
|
|
53
|
+
## Configuration
|
|
54
|
+
|
|
55
|
+
### PostgreSQL
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
export DATABASE_URL="postgres://user:password@localhost:5432/mydb"
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Or individual variables:
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
export DB_HOST=localhost
|
|
65
|
+
export DB_PORT=5432
|
|
66
|
+
export DB_NAME=mydb
|
|
67
|
+
export DB_USER=postgres
|
|
68
|
+
export DB_PASSWORD=password
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### SQLite
|
|
72
|
+
|
|
73
|
+
```bash
|
|
74
|
+
export DATABASE_URL="sqlite:///path/to/database.db"
|
|
75
|
+
# or
|
|
76
|
+
export DATABASE_URL="/path/to/database.db"
|
|
77
|
+
# or in-memory
|
|
78
|
+
export DATABASE_URL=":memory:"
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## Feature Support
|
|
82
|
+
|
|
83
|
+
| Feature | PostgreSQL | SQLite |
|
|
84
|
+
|---------|------------|--------|
|
|
85
|
+
| Tables & Columns | Yes | Yes |
|
|
86
|
+
| Primary Keys | Yes | Yes |
|
|
87
|
+
| Foreign Keys | Yes | Yes |
|
|
88
|
+
| Indexes | Yes | Yes |
|
|
89
|
+
| Unique Constraints | Yes | Yes |
|
|
90
|
+
| Check Constraints | Yes | Yes |
|
|
91
|
+
| Views | Yes | Yes |
|
|
92
|
+
| ENUM Types | Yes | No |
|
|
93
|
+
| Sequences | Yes | No |
|
|
94
|
+
| Functions | Yes | No |
|
|
95
|
+
| Procedures | Yes | No |
|
|
96
|
+
| Triggers | Yes | No |
|
|
97
|
+
| Materialized Views | Yes | No |
|
|
98
|
+
| Schemas | Yes | No |
|
|
99
|
+
| Extensions | Yes | No |
|
|
100
|
+
|
|
101
|
+
SQLite uses table recreation for schema changes that ALTER TABLE doesn't support (column type changes, constraint modifications, etc.).
|
|
102
|
+
|
|
103
|
+
## Commands
|
|
104
|
+
|
|
105
|
+
```bash
|
|
106
|
+
terradb plan # Preview changes
|
|
107
|
+
terradb plan -f custom.sql # Use custom schema file
|
|
108
|
+
terradb apply # Apply changes
|
|
109
|
+
terradb apply -f custom.sql # Apply from custom file
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## Examples
|
|
113
|
+
|
|
114
|
+
### Constraints
|
|
115
|
+
|
|
116
|
+
```sql
|
|
117
|
+
-- Primary keys
|
|
118
|
+
id SERIAL PRIMARY KEY -- PostgreSQL
|
|
119
|
+
id INTEGER PRIMARY KEY -- SQLite
|
|
120
|
+
|
|
121
|
+
-- Foreign keys
|
|
122
|
+
CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
|
|
123
|
+
|
|
124
|
+
-- Check constraints
|
|
125
|
+
CONSTRAINT check_positive CHECK (quantity > 0)
|
|
126
|
+
|
|
127
|
+
-- Unique constraints
|
|
128
|
+
CONSTRAINT unique_email UNIQUE (email)
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### Indexes
|
|
132
|
+
|
|
133
|
+
```sql
|
|
134
|
+
CREATE INDEX idx_email ON users (email);
|
|
135
|
+
CREATE INDEX idx_active ON users (email) WHERE active = true; -- partial index
|
|
136
|
+
CREATE UNIQUE INDEX idx_unique_email ON users (email);
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
### PostgreSQL-only Features
|
|
140
|
+
|
|
141
|
+
```sql
|
|
142
|
+
-- ENUM types
|
|
143
|
+
CREATE TYPE status AS ENUM ('pending', 'active', 'inactive');
|
|
144
|
+
|
|
145
|
+
-- Views
|
|
146
|
+
CREATE VIEW active_users AS SELECT * FROM users WHERE active = true;
|
|
147
|
+
CREATE MATERIALIZED VIEW user_stats AS SELECT COUNT(*) FROM users;
|
|
148
|
+
|
|
149
|
+
-- Functions
|
|
150
|
+
CREATE FUNCTION add(a INT, b INT) RETURNS INT AS $$ SELECT a + b $$ LANGUAGE SQL;
|
|
151
|
+
|
|
152
|
+
-- Sequences
|
|
153
|
+
CREATE SEQUENCE custom_seq START 1000 INCREMENT 1;
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
## Development
|
|
157
|
+
|
|
158
|
+
Requires [Bun](https://bun.sh):
|
|
159
|
+
|
|
160
|
+
```bash
|
|
161
|
+
git clone https://github.com/elitan/terradb.git
|
|
162
|
+
cd terradb
|
|
163
|
+
bun install
|
|
164
|
+
|
|
165
|
+
# PostgreSQL tests
|
|
166
|
+
docker compose up -d
|
|
167
|
+
bun test
|
|
168
|
+
|
|
169
|
+
# SQLite tests (no docker needed)
|
|
170
|
+
bun test src/test/sqlite/
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
## License
|
|
174
|
+
|
|
175
|
+
MIT
|