|
| 1 | +# MangoSQL |
| 2 | + |
| 3 | +## Description |
| 4 | + |
| 5 | +MangoSQL is a fresh and juicy SQL code generator. |
| 6 | + |
| 7 | +1. You provide your database schema (.sql files) |
| 8 | +2. You run MangoSQL cli to generate a client with type-safe interfaces and queries |
| 9 | +3. You write application code that calls the generated code |
| 10 | + |
| 11 | +This is not an ORM, and you can easily inspect the code generated. |
| 12 | +This is inspired by [SQLC](https://github.com/sqlc-dev/sqlc) but pushes the idea farther by supporting relations and dynamic queries. |
| 13 | + |
| 14 | +## Features |
| 15 | + |
| 16 | +* **Convenient**: Generate all the structs for you |
| 17 | +* **Flexible**: Provide a way to run dynamic queries (pagination, search, ...) |
| 18 | +* **Time Saver**: All the basic queries (CRUD) are auto-generated from your schema, nothing to declare |
| 19 | +* **Performant**: Support batching out of the box |
| 20 | +* **Safe**: All the SQL queries use prepared statement |
| 21 | +* **Consistency**: Easy to use transaction API |
| 22 | + |
| 23 | +## Status |
| 24 | + |
| 25 | +This is WIP, features are still not complete and may change |
| 26 | + |
| 27 | +Goals: |
| 28 | +* Handle custom relations (join, aggregations, ...) |
| 29 | +* Handle views |
| 30 | +* Support Mysql/MariaDB/Sqlite3 |
| 31 | +* Better CLI |
| 32 | + |
| 33 | +## Example |
| 34 | + |
| 35 | +So let's see what it means in reality. For the following: |
| 36 | +```sql |
| 37 | +CREATE TABLE users ( |
| 38 | + id UUID PRIMARY KEY, |
| 39 | + email VARCHAR(64) NOT NULL, |
| 40 | + name VARCHAR(64) NOT NULL, |
| 41 | + created_at TIMESTAMP NOT NULL DEFAULT NOW(), |
| 42 | + updated_at TIMESTAMP NOT NULL DEFAULT NOW(), |
| 43 | + deleted_at TIMESTAMP DEFAULT NULL |
| 44 | +); |
| 45 | +``` |
| 46 | + |
| 47 | +Execute the following command to automatically generate a `database/client.go` |
| 48 | +```sh |
| 49 | +mangosql --output=database schema.sql |
| 50 | +``` |
| 51 | + |
| 52 | +This is all you need to do, now the client can be used in your code |
| 53 | +```go |
| 54 | +sqlDb, err := sqlx.Connect("postgres", "Postgres Connection URL") |
| 55 | +if err != nil { |
| 56 | + panic(err) |
| 57 | +} |
| 58 | + |
| 59 | +db := database.New(sqlDb) |
| 60 | + |
| 61 | +// then you can use it and make queries or transactions |
| 62 | + |
| 63 | +// Handle crud operation |
| 64 | +user, err := db.User.Create(database.UserCreate{ |
| 65 | + Name: "user1", |
| 66 | + Email: "user1@email.com" |
| 67 | +}) |
| 68 | + |
| 69 | +// Handle transactions |
| 70 | +err := db.Transaction(func(tx *database.DBClient) error { |
| 71 | + // ... |
| 72 | +}) |
| 73 | + |
| 74 | +// Handle dynamic clause (filters, pagination, ...) |
| 75 | +users, err := db.User.Where(func(cond database.SelectBuilder) database.SelectBuilder { |
| 76 | + return cond.Where("name ILIKE $1", "%user%").Offset(0).Limit(20) |
| 77 | +}) |
| 78 | + |
| 79 | +// Handle Batching |
| 80 | +ids, err := db.User.UpsertMany([]database.UserUpdate{ |
| 81 | + {Email: "usernew@localhost", Name: "usernew"}, // this entry will be inserted |
| 82 | + {Id: id, Email: "user1-updated", Name: "user1-updated"}, // this entry will be updated |
| 83 | +}) |
| 84 | + |
| 85 | +// ... |
| 86 | +``` |
| 87 | + |
| 88 | +## API |
| 89 | + |
| 90 | +Here is the list of all the autogenerated methods for your tables: |
| 91 | + |
| 92 | +## Getters |
| 93 | +* db.{Table}.Count() |
| 94 | +* db.{Table}.CountWhere(condition) |
| 95 | +* db.{Table}.All(offset, limit) |
| 96 | +* db.{Table}.Where(condition) |
| 97 | +* db.{Table}.GetById(id) |
| 98 | + |
| 99 | +## Mutations |
| 100 | +* db.{Table}.Create(input) |
| 101 | +* db.{Table}.Update(input) |
| 102 | +* db.{Table}.Upsert(input) |
| 103 | +* db.{Table}.DeleteSoft(id) |
| 104 | +* db.{Table}.DeleteHard(id) |
| 105 | + |
| 106 | +## Batch Mutations |
| 107 | +* db.{Table}.CreateMany(inputs) |
| 108 | +* db.{Table}.UpdateMany(inputs) |
| 109 | +* db.{Table}.UpsertMany(inputs) |
0 commit comments