If you’re looking for a convenient and efficient way to manage your databases in Go, then you’ll love GoLobby ORM. This lightweight yet powerful Object-Relational Mapper (ORM) provides developers with a user-friendly and customizable interface to interact with databases without the fuss of raw SQL code.
Table of Contents
- Features
- Introduction
- Creating a new Entity
- Initializing ORM
- Fetching an entity from a database
- Saving entities or Insert/Update
- Using raw SQL
- Deleting entities
- Relationships
- Query Builder
- Database Validations
Features
- Elegant and easy-to-use APIs with the help of Generics
- Type safety
- Reflection at startup for fast runtime performance
- No code generation needed!
- Powerful query builder
- Supports various relationship types: One-to-One, One-to-Many, Many-to-Many
Introduction
GoLobby ORM is designed to streamline database interactions, allowing developers to focus more on building robust applications rather than dealing with cumbersome SQL queries. Each database table corresponds to an Entity, which can be manipulated via elegant APIs.
Creating a New Entity
To illustrate creating a new entity, let’s think of this process as crafting a new book. Each book has specific elements, like a title, an author, and publication details, just like our entity has fields. Here’s how to set it up:
package main
import "github.com/golobby/orm"
// User entity
type User struct {
ID int64
FirstName string
LastName string
Email string
orm.Timestamps
}
// ConfigureEntity sets up the User table configuration
func (u User) ConfigureEntity(e *orm.EntityConfigurator) {
e.Table("users")
}
In this code:
- A new User type is created just like a book with specific attributes.
- The ConfigureEntity method helps to define the “users” table associated with these attributes.
Initializing ORM
Before using your entities, you need to initialize the GoLobby ORM by setting up the database connection:
func main() {
err := orm.Initialize(orm.ConnectionConfig{
Name: "default",
Driver: "sqlite3",
ConnectionString: ":memory:",
DatabaseValidations: true,
})
if err != nil {
panic(err)
}
}
This step is akin to unlocking a new book before reading, ensuring that the ORM is ready for action!
Fetching an Entity from a Database
Once your ORM is set up, fetching an entity from the database is straightforward. Think of this as retrieving a specific book from a vast library:
user, err := orm.Find[User](1)
The Find method lets you specify the primary key, retrieving the corresponding user data as if you were pulling the right book from the shelf.
Saving Entities or Insert/Update
Saving an entity using GoLobby ORM is just like deciding to write or update a book in the library. The ORM will know whether to insert a new record or update an existing one based on the provided primary key:
user.Email = "jack@mail.com"
err := orm.Save(user)
This line will update the email of the user. If the ID was zero, it would insert a new book instead!
Using Raw SQL
If you ever find a need to step outside the bounds of ORM-generated SQL, you can flex your muscle and write raw SQL like an experienced librarian referencing archival texts:
users, err := orm.QueryRaw[User]("SELECT * FROM users")
This provides a great balance, letting you tap into the power of SQL when necessary.
Deleting Entities
Deleting entities is as simple as checking a specific book back into the library:
err := orm.Delete(user)
You can effortlessly remove the entity and free up the space for newer entries.
Relationships
A library isn’t just about individual books. Often, you have authors with multiple works or a series of thrillers bound together! GoLobby ORM supports various relationship types, including:
- HasMany
- HasOne
- BelongsTo
- BelongsToMany
Query Builder
GoLobby ORM offers a robust query builder to help you create complex queries with ease. It’s like having a set of tools to craft the perfect index for your library:
posts, err := orm.Query[Post]().Where("author_id", 1).All()
This method can help you get all posts from a specific author efficiently!
Database Validations
GoLobby ORM can also validate your database state against your entities, ensuring everything is in sync. This is crucial to avoid any surprises when your application is live:
orm.Initialize(
orm.ConnectionConfig{
Name: "default",
DatabaseValidations: true,
})
Once this is enabled, ORM checks that all necessary tables and columns are present in the database.
Troubleshooting
Sometimes, you might encounter issues while using GoLobby ORM. Here are a few troubleshooting tips:
- Ensure your database server is running and accessible.
- Check for any connection string issues.
- Validate that your entities and database schema are in sync by enabling DatabaseValidations.
For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.
Conclusion
At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

