Read an existing file on the filesystem without copying #226
-
I am trying to understand how I might use this to read an existing sqlite database in the user filesystem without copying the database into OPFS. I am trying to build an offline web app for reading sqlite files (specifically, mbtiles files). The files can be large (> 1GB) so I want to avoid reading them into memory. In an ideal world, we could use For supporting browsers that don't support The alternative is to streaming copy a I understand that SQLite needs to create other files for writing, but for just reading a database can it just use a single file handle to read the DB file? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
This will work. You could use any OPFS VFS with filesystem transparency (see feature table). The drawback is the time to copy a big file. If you want to access the user's file directly (without copying to OPFS), you'll need to write a custom VFS. I'm unsure whether you can get a FileSystemSyncAccessHandle on a picked file (i.e. outside OPFS) if you're not on a Chromium browser; my guess is that Firefox and Safari don't support it. Also note that FileSystemWritableFileStream isn't supported on Safari and Android Chrome. If you only want to read the user's database file then your custom VFS might work with a simple modification to OPFSAnyContextVFS. You could try replacing this code: wa-sqlite/src/examples/OPFSAnyContextVFS.js Lines 72 to 74 in 605100a with something like: file.fileHandle = getUsersFileSystemFileHandle(zName); where you would implement
Mostly yes, but it will generally still attempt to open other files to check that the database wasn't left in an inconsistent state. In the typical case these other files will be not present or empty. |
Beta Was this translation helpful? Give feedback.
-
Thank you @rhashimoto this is really helpful. Still investigating the best way to do this - the answer might just be "wait a year for better browser support". |
Beta Was this translation helpful? Give feedback.
This will work. You could use any OPFS VFS with filesystem transparency (see feature table). The drawback is the time to copy a big file.
If you want to access the user's file directly (without copying to OPFS), you'll need to write a custom VFS. I'm unsure whether you can get a FileSystemSyncAccessHandle on a picked file (i.e. outside OPFS) if you're not on a Chromium browser; my guess is that Firefox and Safari don't support it. Also note that FileSystemWritableFileStream isn't supported on Safari and Android Chrome.
If you on…