Skip to content

Commit bb2d2ea

Browse files
committed
docs: add project README
- Add project overview explaining sqlok as PostgreSQL schema manager - Document features: query builder, schema management, parameterized queries - Include quick start examples for SELECT, INSERT, schema definitions - Add database connection examples - Detail core architecture with package descriptions - Include development setup instructions and CI/CD info - List dependencies and contributing guidelines - Add roadmap for future features Fixes #8
1 parent 06eb675 commit bb2d2ea

1 file changed

Lines changed: 188 additions & 0 deletions

File tree

README.md

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
# sqlok
2+
3+
A Go library for PostgreSQL schema management and SQL query building.
4+
5+
## Overview
6+
7+
**sqlok** provides a fluent, type-safe API for building SQL queries and managing PostgreSQL schemas. It combines a query builder pattern with reflection-based schema introspection, making it easy to write database operations without manual SQL string concatenation.
8+
9+
## Features
10+
11+
- **Query Builder** - Fluent API for SELECT, INSERT, UPDATE, DELETE operations
12+
- **Schema Management** - Table, Field, and ForeignKey definitions with constraint support
13+
- **Parameterized Queries** - Safe against SQL injection via PostgreSQL placeholders (`$1`, `$2`, etc)
14+
- **Reflection-Based Tags** - Define schema constraints using Go struct tags (`primary_key`, `unique`, etc)
15+
- **CLI Interface** - Command-line tools for schema inspection and example generation
16+
- **Type-Safe** - Leverage Go's type system for compile-time safety
17+
18+
## Installation
19+
20+
```bash
21+
go get github.com/candango/sqlok
22+
```
23+
24+
### Requirements
25+
26+
- Go 1.24 or higher
27+
- PostgreSQL 12 or higher
28+
29+
## Quick Start
30+
31+
### Query Builder
32+
33+
```go
34+
package main
35+
36+
import "github.com/candango/sqlok"
37+
38+
// SELECT query
39+
sql, args := sqlok.Select("id", "name", "email").
40+
From("users").
41+
Where("id=$1", 1).
42+
Build()
43+
// sql: "SELECT id, name, email FROM users WHERE id=$1"
44+
// args: []any{1}
45+
46+
// INSERT query
47+
sql, args := sqlok.NewInsertBuiler().
48+
InsertInto("users").
49+
Columns("name", "email").
50+
Values("John", "john@example.com").
51+
Returning("id").
52+
Build()
53+
// sql: "INSERT INTO users (name, email) VALUES($1, $2) RETURNING id"
54+
// args: []any{"John", "john@example.com"}
55+
56+
// Complex WHERE with AND/OR
57+
sql, args := sqlok.Select("*").
58+
From("users").
59+
Where("age=$1", 18).
60+
And("status=$2", "active").
61+
Build()
62+
```
63+
64+
### Schema Definition
65+
66+
```go
67+
import "github.com/candango/sqlok/internal/schema"
68+
69+
table := &schema.Table{
70+
TableName: "users",
71+
Schema: "public",
72+
Fields: []*schema.Field{
73+
{FieldName: "id", Type: "BIGSERIAL", Primary: true},
74+
{FieldName: "name", Type: "VARCHAR(255)", Nullable: false},
75+
{FieldName: "email", Type: "VARCHAR(255)", Nullable: false},
76+
},
77+
}
78+
```
79+
80+
### Database Connection
81+
82+
```go
83+
import "github.com/candango/sqlok"
84+
85+
loader := sqlok.NewPostgresLoader("postgresql://user:password@localhost/dbname", ctx)
86+
if err := loader.Connect(); err != nil {
87+
log.Fatal(err)
88+
}
89+
defer loader.Disconnect()
90+
91+
if err := loader.Load(); err != nil {
92+
log.Fatal(err)
93+
}
94+
95+
tables := loader.Tables()
96+
```
97+
98+
## Architecture
99+
100+
### Core Packages
101+
102+
- **`builder.go`** (525 LOC) - Query builder implementations
103+
- `QueryBuilder` interface
104+
- `SelectBuilder`, `InsertBuilder`, `UpdateBuilder`, `DeleteBuilder`
105+
- Join and condition helpers (`And`, `Or`)
106+
107+
- **`sqlok.go`** (159 LOC) - Database connection and schema loading
108+
- `DatabaseLoader` interface
109+
- `PostgresLoader` implementation
110+
- Context management
111+
112+
- **`schema/`** - Schema definitions
113+
- `Table` - Represents a database table
114+
- `Field` - Represents a table column
115+
- `ForeignKey` - Represents foreign key constraints with reference options
116+
117+
- **`cli/`** - Command-line interface
118+
- `root.go` - Main CLI command
119+
- `database.go` - Database operations
120+
- `init.go` - Schema initialization
121+
- `example.go` - Example code generation
122+
123+
- **`mapper.go`** - Result mapping (in development)
124+
125+
- **`namefmt.go`** - Name formatting utilities
126+
127+
## Development
128+
129+
### Running Tests
130+
131+
```bash
132+
make test
133+
```
134+
135+
Tests use PostgreSQL with connection credentials from environment:
136+
- Host: `localhost:5432`
137+
- User: `sqlok`
138+
- Password: Set via `PGSQL_SQLOK_PASSWORD` environment variable
139+
140+
### CI/CD Pipeline
141+
142+
GitHub Actions automatically tests against:
143+
- Go 1.23
144+
- Go 1.24
145+
- Go 1.25
146+
147+
### Project Structure
148+
149+
```
150+
.
151+
cmd/sqlok/ # CLI entry point
152+
internal/
153+
├── builder.go # Query builder (core)
154+
├── sqlok.go # DB connection
155+
├── schema/ # Schema definitions
156+
├── cli/ # CLI commands
157+
└── ...
158+
dummy/ # Example models and tests
159+
scripts/postgres/ # Database setup scripts
160+
makefile # Build targets
161+
```
162+
163+
## Dependencies
164+
165+
- **[pgx/v5](https://github.com/jackc/pgx)** - PostgreSQL driver
166+
- **[cobra](https://github.com/spf13/cobra)** - CLI framework
167+
- **[logrus](https://github.com/sirupsen/logrus)** - Structured logging
168+
- **[testify](https://github.com/stretchr/testify)** - Testing utilities
169+
170+
## License
171+
172+
See [LICENSE](LICENSE) file.
173+
174+
## Contributing
175+
176+
Contributions are welcome! Please ensure tests pass before submitting pull requests.
177+
178+
```bash
179+
make test
180+
```
181+
182+
## Roadmap
183+
184+
- [ ] Complete `mapper.go` for result scanning
185+
- [ ] Add UPDATE and DELETE builders
186+
- [ ] Support for additional databases (MySQL, SQLite)
187+
- [ ] Query optimization and performance analysis
188+
- [ ] Extended documentation and examples

0 commit comments

Comments
 (0)