This repository demonstrates how to create a dynamic database connection in a Go application by changing a single flag in the environment configuration. It includes implementations for both SQLite and CockroachDB.
In this project, we've implemented a dynamic database connection mechanism that allows developers to easily switch between SQLite and CockroachDB with minimal changes. This is achieved by utilizing environment variables to specify the desired database type.
- Dynamic database connection switching
- SQLite and CockroachDB implementations
- Clear separation of concerns for database interactions
To run this project, you need the following:
- Go programming language
go-sqlite3
package for SQLite support (optional)- CockroachDB (if using CockroachDB)
Follow these steps to set up and run the project:
-
Clone the repository:
git clone https://github.com/guptaaashutosh/Dynamic_Database_Connection_in_Go_SQLite_or_CockroachDB.git cd Dynamic_Database_Connection_in_Go_SQLite_or_CockroachDB
-
Install Dependencies:
go mod tidy
or,
-
If using SQLite, install the go-sqlite3 package:
go get github.com/mattn/go-sqlite3
-
Install the CockroachDB driver for Go (refer to official documentation for specific instructions).
https://www.cockroachlabs.com/docs/v24.1/install-cockroachdb-windows.html
-
Create
.config
folder : create.env
file inside it and configure as per.env.example
file.
Dynamic_Database_Connection_in_Go_SQLite_or_CockroachDB/
├── .config/
│ └── .env
├── .env.example
-
Set the
DB_CONNECTION
type environment variable to either "sqlite" or "cockroachdb" as per your requirement.
The dynamic database connection is controlled by theDB_CONNECTION
environment variable. Set this variable tosqlite
orcockroachdb
to choose the database type.DB_CONNECTION="sqlite"
-
Run the Application:
go run cmd/main.go
- Flexibility: Switch between databases based on your needs (development, testing, production).
- Reusability: Database logic remains consistent regardless of the underlying database.
- Maintainability: Code is easier to understand and maintain with a clear separation of concerns.
- This example provides a basic structure. Adapt it to your specific database interactions and error handling.
- Consider adding support for additional database types if needed.
This readme provides a starting point for creating a dynamic and flexible database connection system in your Go applications.