From be8ba8d13dd719025104b7409fb15b85ba2dbeed Mon Sep 17 00:00:00 2001 From: Randell Callahan Date: Sat, 30 Nov 2024 09:33:29 -0700 Subject: [PATCH] Add info on how to connect to something like Turso --- README.md | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/README.md b/README.md index 37f067e..626b735 100644 --- a/README.md +++ b/README.md @@ -171,6 +171,54 @@ migrate create -ext sql -dir db/migrations 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