WiseLibs / better-sqlite3

The fastest and simplest library for SQLite3 in Node.js.
MIT License
5.26k stars 391 forks source link

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

Open aslushnikov opened 8 months ago

aslushnikov commented 8 months ago

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

Prinzhorn commented 8 months ago

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.

aslushnikov commented 8 months ago

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.