Skip to content

lib-x/enturso

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

enturso

enturso provides an ent driver adapter for Turso's Go SQLite-compatible driver.

It does not register over the global sqlite3 driver name. Instead, it opens Turso through its native database/sql driver name, turso, and exposes dialect.SQLite to ent.

Installation

go get github.com/lib-x/enturso

Usage

Generate normal ent SQL code, then wire the generated client with ent.Driver.

package main

import (
	"context"
	"log"

	"github.com/lib-x/enturso"
	entursomigrate "github.com/lib-x/enturso/migrate"

	"your-module/ent"
	entmigrate "your-module/ent/migrate"
)

func main() {
	ctx := context.Background()

	drv, err := enturso.Open("./data.db?_busy_timeout=10000")
	if err != nil {
		log.Fatal(err)
	}
	defer drv.Close()

	client := ent.NewClient(ent.Driver(drv))
	defer client.Close()

	if err := entursomigrate.NewSchema(drv, entmigrate.Tables).Create(ctx); err != nil {
		log.Fatal(err)
	}
}

For tests or temporary databases:

drv, err := enturso.Open(":memory:")

In-memory databases are automatically limited to one open connection so the same transient database is reused across operations.

Migration

Use github.com/lib-x/enturso/migrate instead of client.Schema.Create.

Current Turso Go bindings do not support every SQLite inspection pragma used by ent's Atlas migrator, including PRAGMA foreign_key_check. The migrate helper uses generated ent/migrate.Tables metadata and performs create-only schema initialization for missing tables and indexes.

Sync Databases

For Turso sync databases, create the *sql.DB with tursogo and wrap it with enturso.OpenDB.

import turso "turso.tech/database/tursogo"

syncDB, err := turso.NewTursoSyncDb(ctx, turso.TursoSyncDbConfig{
	Path:      "./local.db",
	RemoteUrl: "https://<db>.<region>.turso.io",
	AuthToken: "<auth-token>",
})
if err != nil {
	log.Fatal(err)
}

sqlDB, err := syncDB.Connect(ctx)
if err != nil {
	log.Fatal(err)
}

drv := enturso.OpenDB(sqlDB, enturso.WithMaxOpenConns(1))
client := ent.NewClient(ent.Driver(drv))

enturso.Open enables PRAGMA foreign_keys=ON for each connection it creates. When wrapping an existing *sql.DB with OpenDB, configure and initialize that pool consistently with your Turso sync setup.

About

Turso driver for ent orm

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages