Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support libsql/Turso #6666

Open
danawoodman opened this issue Oct 31, 2023 · 16 comments
Open

Support libsql/Turso #6666

danawoodman opened this issue Oct 31, 2023 · 16 comments
Assignees
Labels
type:feature_request feature request

Comments

@danawoodman
Copy link

danawoodman commented Oct 31, 2023

Describe the feature

Unless I am mistaken, Gorm does not support a network connected SQLite database, specifically libsql / Turso

I've had success connecting to libsql/Turso with Go's built-in database/sql as well as sqlx, but I get the following error when attempting with Gorm and I see no docs on how to do this, so I'm assuming it isn't supported.

unable to open database file: no such file or directory

Perhaps integration with libsql-client-go, which allows support in the above mentioned tools, could be integrated?

Motivation

The ability to connect to any libsql SQLite database, specifically Turso

Related Issues

I could not find anything in issues or discussions.

@ekristen
Copy link

ekristen commented Nov 1, 2023

I'll offer to test. I would try and write it but I suspect that @jinzhu will be about 1000% faster if they have the time.

@ekristen
Copy link

ekristen commented Nov 1, 2023

I actually made this work locally by cloning the sqlite driver and replacing just the imported library under the hood from

_ "github.com/mattn/go-sqlite3"

to

_ "github.com/libsql/libsql-client-go/libsql"

I'll create a fork or repo as an example later today.

@ekristen
Copy link

ekristen commented Nov 1, 2023

@danawoodman give this a shot -- https://github.com/ekristen/gorm-libsql -- happy to contribute my changes back to go-gorm

Note: DSN must contain the prefix libsql:// or a few different options that libsql library underneath expects.

gorm.Open(libsql.Open(dsn), config)

@jinzhu
Copy link
Member

jinzhu commented Nov 2, 2023

I think we can make it works like this example:

https://gorm.io/docs/connecting_to_the_database.html#Customize-Driver

@ekristen
Copy link

ekristen commented Nov 2, 2023

If you could work up an example that'd be great.

@danawoodman
Copy link
Author

Anyone have some success with this?

@ekristen
Copy link

This doesn't seem to work with sqlite.

@ytsruh
Copy link

ytsruh commented Mar 10, 2024

Just wondered if anybody has been working on this? I tried @ekristen library mentioned earlier and updated it to the new Turso driver but I was getting a nil pointer deference whenever I try to access the database. I could get a connection successfully & use AutoMigrate successfully but it seems the connection is being closed after each use.

If anyone can provide some general guidance/direction on this then I'm happy to work on it. I'm no DB or Gorm genious but I'm sure I could have a good stab at it.

@ekristen
Copy link

I made it work by creating this https://github.com/ekristen/gorm-libsql, theoretically the customize driver should work but I haven't had any luck. ekristen/gorm-libsql@74475fc...master

@ytsruh
Copy link

ytsruh commented Mar 11, 2024

Hi all, I've had a go at this and you can find the PR here - go-gorm/sqlite#185

You use it the same way as you can with the Postgres one:

db, err = gorm.Open(sqlite.New(sqlite.Config{
		DriverName: "libsql",
		DSN:       <url including authToken>,
	}), &gorm.Config{})

I'm 99.99% sure this will work as expected and I've tried a fair few combinations of CRUD and its working for me. Could somebody test it and let me know please? I'll also post in the Turso Discord as well.

If this PR is successful then I'll also update the docs for Gorm

@ytsruh
Copy link

ytsruh commented Mar 12, 2024

Right this is now working as expected & all tests are passing.

You'll need to add the import of _ "github.com/tursodatabase/libsql-client-go/libsql" with the code example below but this is now fully working with a Turso remote DB :)

@asilvadesigns
Copy link

👀

@pMatt1988
Copy link

Right this is now working as expected & all tests are passing.

You'll need to add the import of _ "github.com/tursodatabase/libsql-client-go/libsql" with the code example below but this is now fully working with a Turso remote DB :)

@ytsruh
I just tried this out and it works a treat. Thanks!

@ytsruh
Copy link

ytsruh commented Apr 10, 2024

Small update on the PR. Seems it introduces possible corruptions issues with its implementation. I've not had feedback from the maintainers but to prevent a clash of drivers I'm going to break this out into its own package and publish myself.

This will prevent a breaking change to the current SQLite driver but allow us to use it with Turso & libsql

@ytsruh
Copy link

ytsruh commented Apr 21, 2024

Hi all, small update to say that I've published a version/fork of the SQLite driver that supports both embedded replicas and the standard (non cGo) features as well. available here : https://github.com/ytsruh/gorm-libsql

go get -u github.com/ytsruh/gorm-libsql this should work & instructions are in the Readme

@sejoalfaro
Copy link

I think it can be closed by the following PR go-gorm/sqlite#185.
That PR allow us to use gorm with turso.
And ty @ytsruh for your contribution 🚀.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:feature_request feature request
Projects
None yet
Development

No branches or pull requests

7 participants