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
D1 beta support #329
D1 beta support #329
Conversation
Deploying with Cloudflare Pages
|
a788cc1
to
3ea70af
Compare
3ea70af
to
f915a75
Compare
63cc329
to
14402c9
Compare
14402c9
to
3ac32b9
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! 🎉 Added a few comments, mostly around hiding internal methods from users' workers.
Closes #277 |
You may have already fixed this. Browsing the code I couldn't tell. I made a duplicate of this branch for myself just for testing purposes because if you use memory (and possibly persist) in testing:
What happens is the data is wiped because the plugin asks for the DB again... but an in memory DB would just create a new one, and I think sometimes there are issues even with a persist db. My patch works but isn't great just to show you how I fixed it. But I figured it would be something good to know before this is merged. |
That's a good point, @CraigglesO... Each test should start from a clean slate (if you're using in-memory DBs, at least), but Does the same problem occur with KV or R2? I wonder if it's something boneheaded I've done with the D1 stuff or a problem more generally... |
So KV and R2 pull data in from the storage class. Currently in the case of memory (and I'd argue for persistence as well), it is creating a brand new If you cache the instance for later fetches that should work fine. You can look to DurableObjects for how to cache and cleanup on each reload, and on dispose. No worries, this project has become somewhat monolithic. I had a terrible first PR but this one looks great otherwise. @mrbbot - correct me if I'm wrong here please. |
3ac32b9
to
0252dd0
Compare
46d8eda
to
75a08f3
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! ✅ Couple tiny things then let's get this released 🎉 I'm going to start writing the changelog/docs for 2.9.0
now. 👍
```js | ||
import { BetaDatabase } from "@miniflare/d1"; | ||
import { MemoryStorage } from "@miniflare/storage-memory"; | ||
const db = new BetaDatabase(new MemoryStorage()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BetaDatabase
doesn't take a Storage
anymore. 🙁
await db.fetch("/execute", { | ||
method: "POST", | ||
body: JSON.stringify({ | ||
sql: `CREATE TABLE my_table (cid INTEGER PRIMARY KEY, name TEXT NOT NULL);`, | ||
}), | ||
}); | ||
const response = await db.fetch("/query", { | ||
method: "POST", | ||
body: JSON.stringify({ | ||
sql: `SELECT * FROM sqlite_schema`, | ||
}), | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BetaDatabase
doesn't actually support these functions anymore right?
This adds
@miniflare/d1
package:Statement
class, acts similarly to https://github.com/WiseLibs/better-sqlite3/blob/master/docs/api.md#class-statement but is entirely lazy, only truly gets prepared & bound when.all()
,.first()
,.run()
or.raw()
are called.BetaDatabase
class, acts as a D1 binding within MiniflarecreateSQLiteDB
returns abetter-sqlite3
DB instance for manipulating an in-memory or on-disk Sqlite3 database. Used in Wrangler for running commands against local DBs.getSQLiteNativeBindingLocation
returns the location of thebetter_sqlite3.node
file, which may not be installed in the current project.Note this uses
npx-import
(https://npx-import.pages.dev) to lazily load better-sqlite3, otherwise every installation of miniflare would add ~30s to installation (includingnpx wrangler ...
)On a M1 Pro ^