Skip to content

Commit

Permalink
feat: sqlite support
Browse files Browse the repository at this point in the history
  • Loading branch information
Norman Meier committed Jan 28, 2022
1 parent ab0fda2 commit 46f99ef
Show file tree
Hide file tree
Showing 5 changed files with 1,009 additions and 0 deletions.
57 changes: 57 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ 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`):

```sql
Expand Down Expand Up @@ -48,6 +50,61 @@ queries := pg.NewQueries("blocks")
ds := sqlds.NewDatastore(mydb, queries)
```

### SQLite

The [SQLite](https://sqlite.org) wrapper tries to create the table automatically

Prefix scans are optimized by using GLOB

Import and use in your application:

```go
import (
"github.com/ipfs/go-ds-sql"
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)
}
}
```

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

### SQLCipher

The SQLite wrapper also supports the [SQLCipher](https://www.zetetic.net/sqlcipher/) extension

Import and use in your application:

```go
import (
"github.com/ipfs/go-ds-sql"
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")
}

ds, err := opts.Create()
if err != nil {
panic(err)
}
}
```

## API

[GoDoc Reference](https://godoc.org/github.com/ipfs/go-ds-sql)
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ go 1.16
require (
github.com/ipfs/go-datastore v0.5.1
github.com/lib/pq v1.3.0
github.com/mattn/go-sqlite3 v1.14.9
github.com/pkg/errors v0.9.1
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/lib/pq v1.3.0 h1:/qkRGz8zljWiDcFvgpwUpwIAPu3r07TDvs3Rws+o/pU=
github.com/lib/pq v1.3.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/mattn/go-sqlite3 v1.14.9 h1:10HX2Td0ocZpYEjhilsuo6WWtUqttj2Kb0KtD86/KYA=
github.com/mattn/go-sqlite3 v1.14.9/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
Loading

0 comments on commit 46f99ef

Please sign in to comment.