Skip to content

Simple to use local JSON database. Powered by Go. Inspired in lowdb for Javascript

License

Notifications You must be signed in to change notification settings

valerianomacuri/lowdb

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

lowdb Go Reference

Simple to use local JSON database. Powered by Go. Inspired in lowdb for Javascript

db.Data.Posts = append(
	db.Data.Posts, 
	Data.Posts{ ID: 1, Title: "lowdb is awesome"},
	)
// db.json
{
  "posts": [
    { "id": 1, "title": "lowdb is awesome" }
  ]
}

Install

go get github.com/valerianomacuri/lowdb

Usage

touch db.json && echo "{}" > db.json
// db.json
{}
package main

import (
	"github.com/valerianomacuri/lowdb/adapters"
	"github.com/valerianomacuri/lowdb/low"
)

// Define the data structure for database
type Data struct {
	Posts []Post `json:"posts"`
}
type Post struct {
	ID    uint64 `json:"id"`
	Title string `json:"title"`
}

func main() {
	adapter := adapters.NewJSONFile[Data]("db.json")
	db := low.New[Data](adapter)
	// Read data from JSON file, this will set db.Data content
	db.Read()

	// Finally write db.Data content to file
	defer db.Write()

	// Append a post into posts array
	db.Data.Posts = append(
		db.Data.Posts,
		Post{ID: 1, Title: "lowdb is awesome"},
	)
}

Help us