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

Add durable objects sqlite driver #3053

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

jmshal
Copy link

@jmshal jmshal commented Oct 7, 2024

This PR adds initial support for the SQLite capabilities in Cloudflare Durable Objects.

See #3052 for more info.

This is my first contribution to Drizzle - so any pointers are much appreciated. Unfortunately I have limited time right now, but I can see what I can do.

config?: string | Omit<MigrationConfig, 'migrationsFolder'>,
) {
db.dialect.migrate(migrations, db.session, {
...(typeof config !== 'string' && config),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will fail, as the sqlite dialect uses BEGIN/COMMIT, which are not allowed on DO sqlite.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious why the sqlite dialect migrate function doesn't use transactions within... but I haven't spent any time looking into it. I assume there might be a legitimate reason I'm just not seeing.

config: SQLiteTransactionConfig = {},
): T {
const tx = new DurableObjectSQLiteTransaction('sync', this.dialect, this, this.schema);
this.run(sql.raw(`begin${config?.behavior ? ' ' + config.behavior : ''}`));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will also fail. To do transactions, you have to use a function which is (so far) only documented on Discord:

this.ctx.storage.transactionSync(() => {...})

https://discord.com/channels/595317990191398933/773219443911819284/1292483777557172316

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @zwily! I hadn't gotten round to testing transactions yet. I've tested this API and it works perfectly. I also checked that it can be nested, and that works perfectly as well.

override transaction<T>(transaction: (tx: DurableObjectSQLiteTransaction<TFullSchema, TSchema>) => T): T {
const savepointName = `sp${this.nestedIndex}`;
const tx = new DurableObjectSQLiteTransaction('sync', this.dialect, this.session, this.schema, this.nestedIndex + 1);
this.session.run(sql.raw(`savepoint ${savepointName}`));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Savepoints also don't work. Have to use the API above.

@zwily
Copy link

zwily commented Oct 7, 2024

Cool - I updated my test app at https://github.com/zwily/test-drizzle-durable-objects to use the new migrate method.

@constantins2001
Copy link

constantins2001 commented Nov 12, 2024

Hi @jmshal, great work so far! What is the current state of this? Do you need any help moving this forward? I'm currently also working with the Workers DO SQL API and doing everything "by hand", which is quite unsustainable ^^

@jmshal
Copy link
Author

jmshal commented Nov 12, 2024

Hi @constantins2001. When I left this - it was all working, minus migrations. I've asked for comments about how that should work. I have ideas, but other than tweeting generally about supporting DO SQLite if they got enough likes, the official Drizzle team has not shown any interest in this PR or issue. I'm happy to work on this issue further, but I don't want to waste any time if existing maintainers/the Drizzle team don't agree with the direction it takes.

@constantins2001
Copy link

It seems like the API - or at least the typings - aren't fully sync.
For example things like this.drizzle.query.usersTable.findFirst() still return a promise. Is that intentional?

@joaocstro
Copy link

It seems like the API - or at least the typings - aren't fully sync. For example things like this.drizzle.query.usersTable.findFirst() still return a promise. Is that intentional?

You need to use .sync() at the end. Found this by reading the source code as this doesn't seem to be documented anywhere.

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

Successfully merging this pull request may close these issues.

4 participants