Giter Club home page Giter Club logo

favdb's Introduction

favdb: User Favorite DB handle in go (memory, GitHub issue, and PostgreSQL)

GitHub license GoDocGo

fav is a package to help you setup a single entry fo MemoryDB, Github Issue, PostgresSQL DB with user favorite.

The database must be simple and ease to use.

User1 -> Fav1, Fav2 ...
User2 -> Fav3

Install

go get github.com/kkdai/favdb

Usage

refer example/github-issuefor more detail. GITHUB_URL is a string combine with "USE/REPO/GITHU_TOKEN".

var DB favdb.UserFavData

func main() {
    // PostgresSQL connect string
    url := os.Getenv("DATABASE_URL")
    // `GITHUB_URL` is a string combine with "USE/REPO/GITHU_TOKEN".
    gitUrl := os.Getenv("GITHUB_URL")
    if url != "" {
        // Use PostgresSQL as DB.
        DB = favdb.NewPGSql(url)
    } else if gitUrl != "" {
        // Use Github Issue as DB.
        DB = favdb.NewGithubDB(gitUrl)
    } else {
        //Use memory as DB
        DB = favdb.NewMemDB()
    }

    addBookmarkArticle("title1", "Fav1")
    addBookmarkArticle("title1", "Fav2")
}

func addBookmarkArticle(user, fav string) {
    newFavoriteArticle := fav
    newUser := favdb.UserFavorite{
        UserId:    user,
        Favorites: []string{newFavoriteArticle},
    }
    if record, err := DB.Get(user); err != nil {
        //User data is not created, create a new one
        DB.Add(newUser)
    } else {
        //Record found, update it
        oldRecords := record.Favorites
        if exist, idx := favdb.InArray(newFavoriteArticle, oldRecords); exist == true {
            oldRecords = favdb.RemoveStringItem(oldRecords, idx)
        } else {
            oldRecords = append(oldRecords, newFavoriteArticle)
        }
        record.Favorites = oldRecords
        DB.Update(record)
    }
}

If you want to run it directly, just run

Github Issue

    go install github.com/kkdai/photomgr/example/github_issue

Contribute

Please open up an issue on GitHub before you put a lot efforts on pull request. The code submitting to PR must be filtered with gofmt

License

This package is licensed under MIT license. See LICENSE for details.

favdb's People

Contributors

kkdai avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  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.