Code Monkey home page Code Monkey logo

lytedb's Introduction

lytedb

lytedb is an easy to use on-disk database for Go applications. Data is divided into collections and accessed by a key. This package leverages bolt and the standard lib encoding/gob to handle storage and serialization. Read about motivations here.

Storing/retrieving structs on disk should be this easy:

// add a new user under the key 'user-id'
lytedb.Add("user-id", User{Age: 22, Name: "Andres"})

// our user struct
var user User

// write value assigned to 'user-id' onto our struct
lytedb.Get("user-id", &user)

Usage

Open DB

Simply supply a path to a file. If no file exists, it will be created automatically. Remember that this will obtain a file lock on the specified file so no other process can open it.

db, err := lyte.New("/path/to/file.db")
if err != nil {
    // handle
}
defer db.Close()

Collections

Data can be divided into different groups called collections. Collections are a convenient way to seperate data while maintaining unique keys. Meaning you can have entries with the same key as long as they are in different collections.

userDB, err := db.Collection("users")

userDB.Add("user1", user)

Add/Get data

lytedb uses collections to seperate data but also uses a "main" collection for general storage. Remember that the db encodes values so only exported values get written.

type User struct {
    ID string
    Name string
    Age int
}

user := User{
    ID: "Hello World",
    Name: "Napoleon",
    Age: 25,
}

db.Add("napoleon", user)

Similarly, getting values writes back onto a struct. This is why values need to be exported so that the underlying encoding package (encoding/gob) can read/write from structs.

The Get method must recieve a pointer to the struct to write onto.

var user User
db.Get("napoleon", &user)

lytedb's People

Contributors

andresoro avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.