GoAntiSqlPOC - SQL-Free Database Management in Go
This project demonstrates a proof of concept for a database solution in Go that uses zero SQL or text-based query languages. Instead, it leverages BadgerDB, a fast key-value store written in Go, to provide structured data storage with API-style access patterns.
Disclaimer ⚠️
This project is not clean architecture friendly, yet.
Core Features
- No SQL or Query Languages - Pure programmatic data access through Go functions
- Strong Schema Enforcement - Type safety via Go structs
- Relational-Like Data Modeling - Links between entities using IDs and indexes
- Clean API-Style Access - Simple function calls instead of writing queries
Project Structure
- main.go: Example usage demonstrating CRUD operations and relationships
- store/db.go: Database connection management with in-memory storage
- store/user.go: User entity operations (create, find, update, list)
- store/address.go: Address entity operations with user relationships
How It Works
Database Management
The project uses BadgerDB as the underlying storage engine. The store.InitDB() function sets up an in-memory database connection with a singleton pattern using sync.Once to ensure it's only initialized once.
Data Models
The project defines strongly typed Go structs to enforce schema:
User: Stores user information with ID, name, email, and passwordAddress: Stores address information with reference to a user via UserID
Data Access Pattern
Instead of SQL queries, all data access is done through simple function calls:
// Create a user
user, _ := store.CreateUser("Bob", "bob@example.com", "hunter2")
// Add an address
addr, _ := store.AddAddressToUser(user.ID, "123 Main St", "Paris", "75001")
// List addresses for a user
addresses, _ := store.ListAddressesByUserID(user.ID)
// Update user data
user.Name = "Bobby"
store.UpdateUser(user)
"Relational" Modeling
The project demonstrates relationship modeling between users and addresses:
- Each address has a
UserIDfield linking it to its owner - The
ListAddressesByUserIDfunction retrieves all addresses for a specific user - Updates to either entity maintain relationship integrity
Data Consistency
The project implements data consistency features:
- Email uniqueness enforcement using an email-to-ID index
- Transaction support for atomic operations
- Error handling for constraint violations
Terminal Demo
The main.go file provides a demonstration that showcases:
- User creation with validation
- Address management (create, read, update)
- Relationship handling (users have multiple addresses)
- Constraint enforcement (unique emails)
- Entity retrieval by ID and other fields
Running the Demo
go run main.go