Skip to content

Commit

Permalink
Add info on how to connect to something like Turso
Browse files Browse the repository at this point in the history
  • Loading branch information
Piszmog authored Nov 30, 2024
1 parent fbc795e commit be8ba8d
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,54 @@ migrate create -ext sql -dir db/migrations <name of migration>

This package can be easily update to use `sqlx` as well.

#### Example Connection to Turso

If you want to connect to a remote Database, like [Turso](https://turso.tech/), you can create a struct that implements `Database`.

```golang
package db

import (
"database/sql"
"log/slog"

"go-htmx-template/db/queries"
_ "github.com/tursodatabase/libsql-client-go/libsql"
)

type RemoteDB struct {
logger *slog.Logger
db *sql.DB
queries *queries.Queries
}

var _ Database = (*RemoteDB)(nil)

func (d *RemoteDB) DB() *sql.DB {
return d.db
}

func (d *RemoteDB) Queries() *queries.Queries {
return d.queries
}

func (d *RemoteDB) Logger() *slog.Logger {
return d.logger
}

func (d *RemoteDB) Close() error {
return d.db.Close()
}

func newRemoteDB(logger *slog.Logger, name string, token string) (*RemoteDB, error) {
db, err := sql.Open("libsql", "libsql://"+name+".turso.io?authToken="+token)
if err != nil {
return nil, err
}
return &RemoteDB{logger: logger, db: db, queries: queries.New(db)}, nil
}
```

### Dist

This is where your assets live. Any Javascript, images, or styling needs to go in the
Expand Down

0 comments on commit be8ba8d

Please sign in to comment.