Hey there! Hope everyone’s having a good day. I’m running into some issues and could really use your help figuring out what’s going wrong.
So I normally use these SQLite pragmas in my apps.
PRAGMA journal_mode = WAL; // Could this be the troublemaker?
PRAGMA synchronous = NORMAL;
PRAGMA busy_timeout = 5000;
PRAGMA cache_size = -20000;
PRAGMA foreign_keys = ON;
PRAGMA auto_vacuum = INCREMENTAL;
PRAGMA temp_store = MEMORY;
PRAGMA mmap_size = 2147483648;
PRAGMA page_size = 8192;
Now, I don’t think these pragmas are causing problems with LiteFS, but I’d love to double-check that ALL of them are actually compatible. Maybe I’m missing something? If they’re not the issue, then I’m pretty sure it has something to do with my LiteFS configuration.
To keep things clean and avoid technical debt, I set up my database connection like this. Some of these pragmas need to run every time the app starts, so here’s what I do:
import { open } from 'sqlite';
import sqlite3 from 'sqlite3';
export const dbPromise = open({
filename: process.env.NODE_ENV == 'production' ? '/app/src/database/database.sqlite' : './src/database/database.sqlite',
driver: sqlite3.Database,
});
export async function optimizeDatabase() {
const db = await dbPromise;
await db.exec(`
PRAGMA journal_mode = WAL;
PRAGMA synchronous = NORMAL;
PRAGMA busy_timeout = 5000;
PRAGMA cache_size = -20000;
PRAGMA foreign_keys = ON;
PRAGMA auto_vacuum = INCREMENTAL;
PRAGMA temp_store = MEMORY;
PRAGMA mmap_size = 2147483648;
PRAGMA page_size = 8192;
`);
}
Then when my app starts up, I run this:
try {
const { optimizeDatabase } = require(
process.env.NODE_ENV == 'production' ? '/app/src/db.ts' : './db.ts'
);
optimizeDatabase().catch(console.error);
} catch (error) {
console.error("Failed to import database module:", error);
}
And then anywhere in my app I can just do queries like this:
const { dbPromise } = require(
process.env.NODE_ENV == 'production' ? '/app/src/db.ts' : '../../db.ts'
);
const db = await dbPromise;
await db.run("DELETE FROM stats WHERE name_stat LIKE '%_visitor_tracking' OR name_stat LIKE '%_session_tracking'");
The thing is, Im pretty sure the problem is actually in my LiteFS configuration, or I believe so LiteFS: Standard PRAGMA optimize usage?
I’ll share that config in my next comment .
Also, quick question - if LiteFS is meant to only be accessible from a specific location, please let me know! I’m doing a rewrite of my application right now and I need the database to live in a specific directory that’s different from whatever the default is.
Anyone have ideas on where I should be looking to debug this? Thanks so much in advance - really appreciate any help you can give me!