better-sqlite3 icon indicating copy to clipboard operation
better-sqlite3 copied to clipboard

fix: support passing `file://` URI when using custom-compile SQLite

Open aslushnikov opened this issue 2 years ago • 3 comments

I'm trying to use better-sqlite3 with an SQLite that is compiled with SQLITE_USE_URI=1:

const db = SQLite(`file:///foo/bar?vfs=myfs&mode=ro&immutable=1`);

This, however, doesn't work right now, since there's an erroneous assertion in the database creation.

With this patch, I can successfully connect to the database.

References https://github.com/WiseLibs/better-sqlite3/issues/483

aslushnikov avatar Nov 19 '23 08:11 aslushnikov

It might solve your problem in a hacky way but it does not feel like the correct solution since these things are not mutually exclusive. Checking if the directory exists makes sense regardless if you're using file:// or not.

> require('url').fileURLToPath('file:///foo/bar?vfs=myfs&mode=ro&immutable=1')
'/foo/bar'

I don't know what the right solution is, but I feel like the current logic conflicts with the use of file://. E.g. it allows some of the options like readonly. So there needs to be a "UR mode" that forbids other options or does some more sanity checks.

Prinzhorn avatar Nov 20 '23 06:11 Prinzhorn

I don't know what the right solution is, but I feel like the current logic conflicts with the use of file://. E.g. it allows some of the options like readonly. So there needs to be a "UR mode" that forbids other options or does some more sanity checks.

@Prinzhorn I agree that proper URI support would need to address these points, and this is discussed to some extend in https://github.com/WiseLibs/better-sqlite3/issues/483

However, some of the extensions actually rely on the "fake URI", like this one: https://github.com/mlin/sqlite_web_vfs:

const db = SQLite(`file:///__web__?vfs=web&mode=ro&immutable=1&web_uri=${encodeURIComponent(dbUrl)}`);

Do you think it'd be possible to have some escape hatch for these kind of use cases?

Thank you for your review.

aslushnikov avatar Nov 20 '23 10:11 aslushnikov