Giter VIP home page Giter VIP logo

go-ds-sql's Introduction

SQL Datastore

CircleCI Coverage Standard README GoDoc golang version Go Report Card

An implementation of the datastore interface that can be backed by any sql database.

Install

go get github.com/ipfs/go-ds-sql

Usage

PostgreSQL

Ensure a database is created and a table exists with key and data columns. For example, in PostgreSQL you can create a table with the following structure (replacing table_name with the name of the table the datastore will use - by default this is blocks):

CREATE TABLE IF NOT EXISTS table_name (key TEXT NOT NULL UNIQUE, data BYTEA)

It's recommended to create an index on the key column that is optimised for prefix scans. For example, in PostgreSQL you can create a text_pattern_ops index on the table:

CREATE INDEX IF NOT EXISTS table_name_key_text_pattern_ops_idx ON table_name (key text_pattern_ops)

Import and use in your application:

import (
	"database/sql"
	"github.com/ipfs/go-ds-sql"
	pg "github.com/ipfs/go-ds-sql/postgres"
)

mydb, _ := sql.Open("yourdb", "yourdbparameters")

// Implement the Queries interface for your SQL impl.
// ...or use the provided PostgreSQL queries
queries := pg.NewQueries("blocks")

ds := sqlds.NewDatastore(mydb, queries)

SQLite

The SQLite wrapper tries to create the table automatically

Prefix scans are optimized by using GLOB

Import and use in your application:

package main

import (
	sqliteds "github.com/ipfs/go-ds-sql/sqlite"
	_ "github.com/mattn/go-sqlite3"
)

func main() {
	opts := &sqliteds.Options{
		DSN: "db.sqlite",
	}

	ds, err := opts.Create()
	if err != nil {
		panic(err)
	}
	defer func() {
		if err := ds.Close(); err != nil {
			panic(err)
		}
	}()
}

If no DSN is specified, an unique in-memory database will be created

SQLCipher

The SQLite wrapper also supports the SQLCipher extension

Import and use in your application:

package main

import (
	sqliteds "github.com/ipfs/go-ds-sql/sqlite"
	_ "github.com/mutecomm/go-sqlcipher/v4"
)

func main() {
	opts := &sqliteds.Options{
		DSN: "encdb.sqlite",
		Key: ([]byte)("32_very_secure_bytes_0123456789a"),
	}

	ds, err := opts.Create()
	if err != nil {
		panic(err)
	}
	defer func() {
		if err := ds.Close(); err != nil {
			panic(err)
		}
	}()
}

API

GoDoc Reference

Contribute

Feel free to dive in! Open an issue or submit PRs.

License

MIT

go-ds-sql's People

Contributors

alanshaw avatar hsanjuan avatar i-norden avatar ipfs-mgmt-read-write[bot] avatar marten-seemann avatar masih avatar n0izn0iz avatar rmulhol avatar stebalien avatar web-flow avatar web3-bot avatar whyrusleeping avatar willscott 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

Watchers

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

go-ds-sql's Issues

Server-side ordering

We should be using SQL's "order-by" for basic key/value ordering instead of sorting and buffering client-side.

Sql Datastore Should Allow For Migrations

Right now to be able to use this repo requires running migrations outside of the datastore. Since the datastore includes a postgres implementation, the postgres implementation should have the ability to initialize the database with migrations when connecting

gx release needed after latest PR

After undoing the gx-ification of the dependencies on PR I forgot to re-gx-ify the package itself, so the hash for 1.0.0 is currently incorrect. I think the quickest/cleanest way to fix this is have one of the maintainers perform a gx patch. Sorry for the trouble.

Use standard README

We should use the standard readme that we use in all other go-ipfs packages.

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.