@barishnamazov/gsql 0.1.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 +203 -0
- package/dist/cli.js +174 -0
- package/dist/index.js +33 -0
- package/package.json +50 -0
- package/src/cli.ts +206 -0
- package/src/compiler.ts +73 -0
- package/src/generator.ts +547 -0
- package/src/index.ts +11 -0
- package/src/lexer.ts +636 -0
- package/src/parser.ts +1241 -0
- package/src/types.ts +165 -0
package/README.md
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
# GSQL - Generic SQL
|
|
2
|
+
|
|
3
|
+
**Parametric polymorphism for SQL schemas**
|
|
4
|
+
|
|
5
|
+
GSQL is a domain-specific language that brings the power of generics/templates to database schemas. Define common patterns once, instantiate them anywhere.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- **Concepts**: Generic schema templates with type parameters
|
|
10
|
+
- **Mixins**: Compose reusable schema fragments
|
|
11
|
+
- **Template variables**: Automatic field name expansion
|
|
12
|
+
- **Per-instance indexes**: Add indexes after instantiation
|
|
13
|
+
- **Type-safe foreign keys**: Proper FK constraints for polymorphic patterns
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install @barishnamazov/gsql
|
|
19
|
+
# or
|
|
20
|
+
bun install @barishnamazov/gsql
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
### Command Line
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
# Compile a GSQL file to SQL
|
|
29
|
+
gsql compile schema.gsql -o schema.sql
|
|
30
|
+
|
|
31
|
+
# Output to stdout
|
|
32
|
+
gsql compile schema.gsql
|
|
33
|
+
|
|
34
|
+
# Show help
|
|
35
|
+
gsql --help
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### As a Library
|
|
39
|
+
|
|
40
|
+
```typescript
|
|
41
|
+
import { compile, compileToSQL } from "@barishnamazov/gsql";
|
|
42
|
+
|
|
43
|
+
// Get detailed result
|
|
44
|
+
const result = compile(source);
|
|
45
|
+
if (result.success) {
|
|
46
|
+
console.log(result.sql);
|
|
47
|
+
} else {
|
|
48
|
+
console.error(result.errors);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Or just get SQL (throws on error)
|
|
52
|
+
const sql = compileToSQL(source);
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Quick Example
|
|
56
|
+
|
|
57
|
+
```gsql
|
|
58
|
+
// Reusable timestamp pattern
|
|
59
|
+
schema Timestamps {
|
|
60
|
+
created_at timestamptz nonull default(NOW());
|
|
61
|
+
updated_at timestamptz nonull default(NOW());
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Generic concept for announcements
|
|
65
|
+
concept Announcing<Target> {
|
|
66
|
+
schema Announcements mixin Timestamps {
|
|
67
|
+
id serial pkey;
|
|
68
|
+
{Target}_id integer nonull ref(Target.id) ondelete(cascade);
|
|
69
|
+
message text nonull;
|
|
70
|
+
|
|
71
|
+
index({Target}_id);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Concrete tables
|
|
76
|
+
schema Posts {
|
|
77
|
+
id serial pkey;
|
|
78
|
+
title varchar(255);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
posts = Posts;
|
|
82
|
+
post_announcements = Announcing<posts[post]>;
|
|
83
|
+
|
|
84
|
+
// Per-instance indexes
|
|
85
|
+
index(post_announcements, created_at);
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
This generates proper SQL with:
|
|
89
|
+
|
|
90
|
+
- A `posts` table
|
|
91
|
+
- A `post_announcements` table with a `post_id` foreign key
|
|
92
|
+
- Proper indexes and constraints
|
|
93
|
+
|
|
94
|
+
## Syntax Reference
|
|
95
|
+
|
|
96
|
+
### Schemas
|
|
97
|
+
|
|
98
|
+
```gsql
|
|
99
|
+
schema Name mixin Mixin1, Mixin2 {
|
|
100
|
+
column_name type constraint1 constraint2;
|
|
101
|
+
index(column1, column2) unique;
|
|
102
|
+
check(expression);
|
|
103
|
+
trigger name before update on each row execute function fn();
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### Concepts
|
|
108
|
+
|
|
109
|
+
```gsql
|
|
110
|
+
concept Name<TypeParam1, TypeParam2> {
|
|
111
|
+
enum status { active; inactive; }
|
|
112
|
+
|
|
113
|
+
schema Table {
|
|
114
|
+
{TypeParam1}_id integer ref(TypeParam1.id);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
**Template Variables:** Use uppercase type parameter names in curly braces (e.g., `{Target}_id`, `{Author}_id`).
|
|
120
|
+
|
|
121
|
+
### Instantiation
|
|
122
|
+
|
|
123
|
+
```gsql
|
|
124
|
+
// Simple
|
|
125
|
+
table_name = SchemaOrConcept;
|
|
126
|
+
|
|
127
|
+
// With type arguments and aliases
|
|
128
|
+
table_name = Concept<other_table[alias]>;
|
|
129
|
+
|
|
130
|
+
// Multiple outputs
|
|
131
|
+
table1, table2 = ConceptWithMultipleSchemas<type_arg>;
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
**Aliases:** When instantiating a concept:
|
|
135
|
+
|
|
136
|
+
- **With alias:** `exams[examHello]` → uses `examHello_id` (preserves alias as-is)
|
|
137
|
+
- **Without alias:** `authors` → uses `author_id` (snake_cased from parameter name `Author`)
|
|
138
|
+
|
|
139
|
+
Example:
|
|
140
|
+
|
|
141
|
+
```gsql
|
|
142
|
+
concept Announcing<Target, Author> {
|
|
143
|
+
schema Announcements {
|
|
144
|
+
{Target}_id integer nonull ref(Target.id);
|
|
145
|
+
{Author}_id integer nonull ref(Author.id);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
schema Exams { id serial pkey; }
|
|
150
|
+
schema Authors { id serial pkey; }
|
|
151
|
+
|
|
152
|
+
exams = Exams;
|
|
153
|
+
authors = Authors;
|
|
154
|
+
|
|
155
|
+
// Creates table with exam_id and author_id columns
|
|
156
|
+
// We don't need to alias authors, because the parameter name is Author
|
|
157
|
+
announcements = Announcing<exams[exam], authors>;
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### Data Types
|
|
161
|
+
|
|
162
|
+
- `serial`, `bigserial`
|
|
163
|
+
- `integer`, `bigint`, `smallint`
|
|
164
|
+
- `text`, `varchar(n)`, `char(n)`
|
|
165
|
+
- `boolean`
|
|
166
|
+
- `timestamptz`, `timestamp`, `date`, `time`
|
|
167
|
+
- `jsonb`, `json`
|
|
168
|
+
- `uuid`, `inet`, `citext`
|
|
169
|
+
- `decimal`, `numeric`, `real`
|
|
170
|
+
- `bytea`
|
|
171
|
+
|
|
172
|
+
### Constraints
|
|
173
|
+
|
|
174
|
+
- `pkey` - Primary key
|
|
175
|
+
- `nonull` - Not null
|
|
176
|
+
- `unique` - Unique constraint
|
|
177
|
+
- `default(value)` - Default value
|
|
178
|
+
- `ref(Table.column)` - Foreign key reference
|
|
179
|
+
- `ondelete(cascade|restrict|setnull|setdefault|noaction)`
|
|
180
|
+
- `check(expression)` - Check constraint
|
|
181
|
+
|
|
182
|
+
## Development
|
|
183
|
+
|
|
184
|
+
```bash
|
|
185
|
+
# Install dependencies
|
|
186
|
+
npm install
|
|
187
|
+
|
|
188
|
+
# Run tests
|
|
189
|
+
npm test
|
|
190
|
+
|
|
191
|
+
# Build
|
|
192
|
+
npm run build
|
|
193
|
+
|
|
194
|
+
# Lint
|
|
195
|
+
npm run lint
|
|
196
|
+
|
|
197
|
+
# Format
|
|
198
|
+
npm run format
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
## License
|
|
202
|
+
|
|
203
|
+
MIT
|