Query syntax with Turso (in NextJS context)
Hey there,
I guess I'm missing something somewhere.
I'm trying drizzle with libsql driver. When used locally with sqlite database, everything is working fine, but if I want to connect to my turso database, I can successfully get data using the classic ORM apis (
db.select().from(tests).limit(1)?.[0]
), but I can't get relational query syntax to work (db.query.tests.findFirst()
). It always return undefined
.
Any clue about what I missed ?5 Replies
I leave the info here just in case someone is finding this issue:
NextJS has a controversial way to manage cache by overriding the
fetch
JS API. It caches request to app pages, including the data retrieval using fetch
.
So, using the local sqlite file (file:/
), drizzle/libsql is using conventional sqlite driver to connect to database and query and everything works fine.
But when it comes to use turso
driver to connect cloud hosted database, then it uses turso APIs to query the database with http requests using fetch
. If I make my app bypass the cache or apply TTL, then I can get the correct data.Didn't Next.js stopped monkey-patching cache from some recent versions?
https://nextjs.org/docs/app/api-reference/functions/fetch
Apparently, no. But as I stated, I read and heard that this design isn't welcomed by users nor popular lib devs since it changes the behavior of node fundamental API.
Functions: fetch | Next.js
API reference for the extended fetch function.
Maybe this is in plans for a future release.
It'll opt-in in v15, so back to normal
https://nextjs.org/blog/next-15-rc
Next.js 15 RC
The Next.js 15 Release Candidate (RC) is now available. This early version allows you to test the latest features before the upcoming stable release.