47 lines
1.9 KiB
Markdown
47 lines
1.9 KiB
Markdown
# JSONDB
|
|
|
|
JSONDB is a simple and very accessible database based on JSON files. Its niche is small projects that interact with the database exclusively through a single instance (simultaneous write access by multiple instances will lead to loss of data integrity). Basically, it is an in-memory key-value store that stores data locally in JSON files. If data is changed (in memory), these changes are also made in the file system. The big advantage in contrast to e.g. SQLite databases is that the data is available in plain text and thus extremely accessible for other programs. On the other hand, any logic for accessing the databases is missing or must be implemented by the user himself.
|
|
|
|
## Pros
|
|
|
|
+ JSONDB doesn't care what data you put in it (string, int, struct, etc).
|
|
+ All data is always in memory, resulting in very short access times when reading.
|
|
+ All data is stored as simple JSON files in the file system
|
|
|
|
## Cons
|
|
|
|
+ To avoid loss of data integrity, only one program instance should access the database at a time
|
|
+ As databases grow, write times increase as well, since the entire database is written to the file system each time
|
|
+ In addition, (too) large databases can have a negative impact on system performance, since memory consumption naturally grows in parallel with the database size
|
|
|
|
## Usage
|
|
|
|
```go
|
|
import (
|
|
"git.0x0001f346.de/andreas/jsondb"
|
|
)
|
|
|
|
type Car struct {
|
|
Manufacturer string
|
|
Model string
|
|
Horsepower int
|
|
WeightInKG int
|
|
}
|
|
|
|
jsondb.SetLocationOfCluster("/mnt/storage/databases") // optional, default is the path to the executable
|
|
jsondb.SetNameOfCluster("iLikeJSONFiles") // optional, default is "jsondb"
|
|
jsondb.LoadCluster()
|
|
jsondb.CreateDatabase("Cars")
|
|
jsondb.SetKeyInDatabase(
|
|
"Cars",
|
|
"My dream car",
|
|
Car{
|
|
Manufacturer: "Toyota",
|
|
Model: "GR86",
|
|
Horsepower: 234,
|
|
WeightInKG: 1271,
|
|
},
|
|
)
|
|
|
|
myDreamCar, _ := jsondb.GetKeyFromDatabase("Cars", "My dream car") // returns (interface{}, err)
|
|
``` |