Skip to content

Latest commit

 

History

History
77 lines (61 loc) · 1.53 KB

README.md

File metadata and controls

77 lines (61 loc) · 1.53 KB

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