refactor into an importable library and keep an example
parent
cdc8fc4e5e
commit
cfd22dfc9c
|
@ -1,4 +1,4 @@
|
|||
package libsqlDB
|
||||
package libsqldb
|
||||
|
||||
import (
|
||||
"database/sql"
|
|
@ -1,6 +1,6 @@
|
|||
//go:build !windows
|
||||
|
||||
package libsqlDB
|
||||
package libsqldb
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
@ -26,6 +26,8 @@ var syncInterval = 200 * time.Millisecond
|
|||
func NewLibSqlDB(primaryUrl string, migrationFiles embed.FS, opts ...Options) (*LibSqlDB, error) {
|
||||
l := libSqlDefaults()
|
||||
|
||||
_migrationFiles = migrationFiles
|
||||
|
||||
for _, option := range opts {
|
||||
err := option(l)
|
||||
if err != nil {
|
|
@ -1,18 +1,24 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"fmt"
|
||||
"git.sa.vin/payne8/libsqldb"
|
||||
"os"
|
||||
)
|
||||
|
||||
//go:embed migrations/*.sql
|
||||
var migrationFiles embed.FS
|
||||
|
||||
func main() {
|
||||
primaryUrl := os.Getenv("LIBSQL_DATABASE_URL")
|
||||
authToken := os.Getenv("LIBSQL_AUTH_TOKEN")
|
||||
|
||||
tdb, err := libsqlDB.NewLibSqlDB(
|
||||
tdb, err := libsqldb.NewLibSqlDB(
|
||||
primaryUrl,
|
||||
libsqlDB.WithAuthToken(authToken),
|
||||
libsqlDB.WithLocalDBName("local.db"),
|
||||
migrationFiles,
|
||||
libsqldb.WithAuthToken(authToken),
|
||||
libsqldb.WithLocalDBName("local.db"),
|
||||
)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "failed to open db %s: %s", primaryUrl, err)
|
|
@ -1,6 +1,6 @@
|
|||
//go:build windows
|
||||
|
||||
package libsqlDB
|
||||
package libsqldb
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
@ -8,7 +8,7 @@ import (
|
|||
"fmt"
|
||||
"time"
|
||||
|
||||
multierror "github.com/hashicorp/go-multierror"
|
||||
"github.com/hashicorp/go-multierror"
|
||||
_ "github.com/tursodatabase/libsql-client-go/libsql"
|
||||
)
|
||||
|
||||
|
@ -26,6 +26,8 @@ type LibSqlDB struct {
|
|||
func NewLibSqlDB(primaryUrl string, migrationFiles embed.FS, opts ...Options) (*LibSqlDB, error) {
|
||||
l := &LibSqlDB{}
|
||||
|
||||
_migrationFiles = migrationFiles
|
||||
|
||||
for _, option := range opts {
|
||||
err := option(l)
|
||||
if err != nil {
|
Loading…
Reference in New Issue