Support for sqlite in memory databases
Julien Capellari
To speedup tests, instead of using a sqlite file database, we should use an in memory temporary database. This is possible using
better-sqlite3
by passing :memory:
instead of a filename.I tried with strapi 4.25.0 and better-sqlite3 10.0.0 by writing the following config :
```typescript
module.exports = () => ({
connection: {
client: 'sqlite',
connection: {
filename: ':memory:',
}
},
});
```
But it fails with the error
SqliteError: unable to open database file
, because the filename is resolved using path.resolve
.Yunus Emre Zengin
This is definitely a hack but I think you can fool strapi with a proxy trick. While starting strapi wrap the connection config with a proxy:
const strapiInst = Strapi({ distDir: 'dist' });
const dbConn = strapiInst.config.get('database.connection');
const origConnection = dbConn.connection;
dbConn.connection = new Proxy(origConnection, {
set(target: any, p: string | symbol, newValue: any, receiver: any) {
target[p] = p === 'filename' ? ':memory:' : newValue;
return true;
},
});
strapiInst.config.set('database.connection', dbConn);
await strapiInst.load();
ABN Software
Yunus Emre Zengin I plan to use Sqlite in-memory for storing project data as well. Do you think the following sequence will break? 1. Load the sqlite .db file to memory 2. From there on, Strapi only talks to this in-memory database 3. Periodically sync this in-memory database back to disk. Thanks for tips.
Yunus Emre Zengin
ABN Software When it comes to software, it's hard to say "it won't break" 😄. However, the idea of synchronizing in memory data seems a bit risky to me. In this case, whenever you encounter an issue, you could lose the data in memory.