Coder Social home page Coder Social logo

xyproto / simplehstore Goto Github PK

View Code? Open in Web Editor NEW
54.0 6.0 1.0 386 KB

:convenience_store: Easy way to use a PostgreSQL database (and the HSTORE feature) from Go

License: BSD 3-Clause "New" or "Revised" License

Go 99.54% Shell 0.46%
hstore strings hashmap go key-value postgresql-database postgresql

simplehstore's Introduction

simplehstore

Build GoDoc License Go Report Card

Easy way to use a PostgreSQL database (and the HSTORE feature) from Go.

Online API Documentation

godoc.org

Features and limitations

  • Requires PostgreSQL 9.1 or later.
  • Requires Go 1.10 or later.
  • Supports simple use of lists, hashmaps, sets and key/values.
  • Deals mainly with strings.
  • Uses the pq package.
  • Modeled after simpleredis.
  • Uses SQL queries with HSTORE for the KeyValue and HashMap types.
  • Uses regular SQL for the List and Set types.

Sample usage

package main

import (
    "log"

    db "github.com/xyproto/simplehstore"
)

func main() {
    // Check if the local db service is up
    if err := db.TestConnection(); err != nil {
        log.Fatalln("Could not connect to local database. Is the service up and running?")
    }

    // Create a Host, connect to the local db server
    host := db.New()

    // Connecting to a different host/port
    //host := db.NewHost("server:5432/db")

    // Connect to a different db host/port, with a username and password
    // host := db.NewHost("username:password@server/db")

    // Close the connection when the function returns
    defer host.Close()

    // Create a list named "greetings"
    list, err := db.NewList(host, "greetings")
    if err != nil {
        log.Fatalln("Could not create list!")
    }

    // Add "hello" to the list, check if there are errors
    if list.Add("hello") != nil {
        log.Fatalln("Could not add an item to list!")
    }

    // Get the last item of the list
    if item, err := list.GetLast(); err != nil {
        log.Fatalln("Could not fetch the last item from the list!")
    } else {
        log.Println("The value of the stored item is:", item)
    }

    // Remove the list
    if list.Remove() != nil {
        log.Fatalln("Could not remove the list!")
    }
}

Testing

  • A PostgreSQL server must be up and running locally for go test to work, and a database named test must exist.

License, author and version

simplehstore's People

Contributors

dependabot[bot] avatar vgoltv avatar xyproto avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Forkers

gebi

simplehstore's Issues

Can't create a database with an hyphen in its name "foo-bar"

func (host *Host) createDatabase() error {

    if _, err := host.db.Exec("CREATE DATABASE " + host.dbname + " WITH ENCODING '" + encoding + "'"); err != nil {

returns err : Could not create database avisonline-dev: pq: syntax error at or near "-" for "foo-bar"

Adding single quotes saves the day.

Perhaps using fmt.Sprintf() to avoid the easy to miss '" +?

    if _, err := host.db.Exec(fmt.Sprintf("CREATE DATABASE '%s' WITH ENCODING '%s'", host.dbname, encoding)); err != nil {

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.