Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gschier/banister
Code-generated Go ORM that keeps you safe with types
https://github.com/gschier/banister
golang orm
Last synced: 21 days ago
JSON representation
Code-generated Go ORM that keeps you safe with types
- Host: GitHub
- URL: https://github.com/gschier/banister
- Owner: gschier
- Created: 2020-07-19T00:51:27.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2022-04-04T18:32:21.000Z (almost 3 years ago)
- Last Synced: 2024-11-09T20:39:10.613Z (2 months ago)
- Topics: golang, orm
- Language: Go
- Homepage:
- Size: 193 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Go Banister
===========`banister` provides access to relational databases in the safest way possible.
```go
// Example usage
package mainimport (
"database/sql"
_ "github.com/mattn/go-sqlite3"
"models"
)func main() {
db, _ := sql.Open("sqlite3", ":memory:?_fk=1")
store := models.NewStore(db, models.StoreConfig{
BlogPostConfig: models.BlogPostConfig{
HookPreInsert: func(m *models.User) {
m.CreatedAt = time.Now()
},
},
})user := store.Users.MustInsert(
models.Set.User.Username("superUser"),
models.Set.User.Email("[email protected]"),
)
user.Email = "[email protected]"
store.Users.MustUpdate(user)store.BlogPosts.MultInsert(
models.Set.BlogPost.Title("First Post!"),
models.Set.BlogPost.Slug("first-post"),
models.Set.BlogPost.Published(true),
models.Set.BlogPost.Content("Hello World!"),
models.Set.BlogPost.UserId(user.ID),
)// Fetch blog posts
drafts := store.BlogPosts.Filter(
models.Where.BlogPost.Published.False(),
).Sort(
models.OrderBy.BlogPost.CreatedAt.Desc,
).MustAll()
fmt.Printf("Found %d drafts", len(drafts))
}
``````go
// Define models and generate a new type-safe database client
package mainimport (
"github.com/gschier/banister"
"log"
)var User = banister.NewModel(
"User",
banister.NewAutoField("id"),
banister.NewTextField("email").Unique(),
banister.NewTextField("password"),
)var BlogPost = banister.NewModel(
"BlogPost",
banister.NewAutoField("id"),
banister.NewDateTimeField("created_at"),
banister.NewTextField("content"),
banister.NewTextField("slug").Unique(),
banister.NewTextField("title"),
banister.NewBooleanField("published"),
banister.NewForeignKeyField(User.Settings().Name).OnDelete(banister.OnDeleteSetNull),
)func main() {
config := &banister.GenerateConfig{
Backend: "postgres",
OutputDir: "./models",
PackageName: "models",
Models: []banister.Model{
models.User,
models.BlogPost,
},
}err := banister.Generate(config)
if err != nil {
log.Panicf("Failed to generate database client: %s", err)
}
}
```