JsonDB
is a lightweight, file-based database management system implemented in Go. It provides an easy way to store, retrieve, and manage data in a JSON file format. This project is particularly useful for small applications that do not require the overhead of a full-fledged database management system.
- File-based storage: All data is stored in a single JSON file.
- Auto-incremented IDs: Automatically generate unique IDs for records.
- Category-based organization: Data is organized into categories, each with its own set of records.
- Hashing passwords: Provides built-in functionality to hash and verify passwords.
- Simple and extensible: The code is straightforward, making it easy to extend and customize.
- Go 1.18 or higher installed on your machine.
- Basic understanding of Go programming.
-
Clone the repository to your local machine:
git clone https://github.com/yourusername/jsondb-go.git cd jsondb-go
-
Build the project:
go build -o jsondb
-
Run the project:
go run main.go
main.go
: The main entry point of the application. Demonstrates how to use theJsonDB
to store and retrieve data.jsondb.go
: The core library containing all the logic for interacting with the JSON database.database/
: Directory where the JSON database file (database.json
) is stored.
The JsonDB
is initialized by specifying the filename where the data will be stored:
db := NewJsonDB("database.json")
You can add data to a category with an auto-incremented ID:
userID := db.SetDataWithAutoIncrement("user", map[string]interface{}{
"username": "Alice",
"password": HashPassword("password123"),
})
fmt.Printf("User added with ID: %d\n", userID)
This will store the user data under user/1
if it’s the first entry in the user
category.
You can retrieve data by specifying the category and ID:
user := db.GetData(fmt.Sprintf("user/%d", userID))
fmt.Printf("User %d: %v\n", userID, user)
This retrieves the user data stored under user/1
.
To delete a record, specify the category and ID:
db.DeleteData(fmt.Sprintf("user/%d", userID))
This deletes the user data stored under user/1
.
JsonDB
includes utility functions for hashing and verifying passwords:
hashedPassword := HashPassword("mysecretpassword")
fmt.Println("Hashed Password:", hashedPassword)
isValid := CheckPassword(hashedPassword, "mysecretpassword")
fmt.Println("Password is valid:", isValid)
The JSON database structure will look something like this:
{
"auto_increment": {
"user": 1
},
"user": {
"1": {
"username": "Alice",
"password": "ef92b778bafe771e89245b89ecbc08a44a4e166c06659911881f383d4473e94f"
}
}
}
This project is licensed under the MIT License. See the LICENSE
file for more details.