Git Product home page Git Product logo

safe-csv-db's Introduction

safe-csv-db

Thread safe database using only CSV

https://pkg.go.dev/github.com/zenpk/safe-csv-db

Functions

This package supports these functions

  • All: SELECT *
  • Select: SELECT * BY LIMIT 1
  • SelectAll: SELECT * BY
  • Insert: Insert one row
  • InsertAll: Insert multiple rows
  • Update: Update one row by some value
  • UpdateAll: Update all rows by some value
  • Delete: Delete one row by some value
  • DeleteAll: Delete all rows by some value

It is recommended that every row has a unique id

All values are stored as string

For detailed API please refer to the go doc

Usage

Basic

// define your RecordType struct
type My struct{
    Id int64
    Name string
}

// implement the RecordType interface
func (m My) ToRow() ([]string, error) {
    row := make([]string, 2)
    row[0] = strconv.FormatInt(m.Id, 10)
    row[1] = m.Name
    return row, nil
}

func (m My) FromRow(row []string) (RecordType, error) {
    if len(row) < 2 {
        return nil, errors.New("out of range")
    }
    id, err := strconv.ParseInt(row[0], 10, 64)
    if err != nil {
        return nil, err
    }
    record := My{
        Id:   id,
        Name: row[1],
    }
    return record, nil
}

tableMy, err := OpenTable("./my.csv", My{})
defer tableMy.Close()

go func() {
    if err := tableMy.ListenChange(); err != nil {
        panic(err)
    }
}()

record := My{
    Id:   1,
    Name: "abc",
}
if err := tableMy.Insert(record); err != nil {
    panic(err)
}

Call from other functions

func InitDb(preparing chan<- struct{}, stop <-chan struct{}) {
    tableUser, err := OpenTable("./users.csv", User{})
    defer tableUser.Close()

    go func() {
        if err := tableUser.ListenChange(); err != nil {
            panic(err)
        }
    }()

    tableArticle, err := OpenTable("./articles.csv", Article{})
    defer tableArticle.Close()

    go func() {
        if err := tableArticle.ListenChange(); err != nil {
            panic(err)
        }
    }()

    close(preparing)
    <- stop
}

func main() {
    preparing := make(chan struct{})
    stop := make(chan struct{})
    go InitDb(preparing, stop)
    <- preparing
    // do something
    close(stop)
}

safe-csv-db's People

Contributors

zenpk avatar

Watchers

 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.