From 8971c0ad13c287eb21c7a63341dd5c48c1b6ed06 Mon Sep 17 00:00:00 2001 From: Isaac Date: Tue, 20 Jun 2023 20:59:08 +0100 Subject: [PATCH] perf(sqlite): run `optimize` every 6h --- src/client.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/client.js b/src/client.js index 23834a2..96d04b0 100644 --- a/src/client.js +++ b/src/client.js @@ -11,6 +11,7 @@ const { join } = require('path'); const YAML = require('yaml'); const TicketManager = require('./lib/tickets/manager'); const sqliteMiddleware = require('./lib/middleware/prisma-sqlite'); +const ms = require('ms'); module.exports = class Client extends FrameworkClient { constructor(config, log) { @@ -53,13 +54,19 @@ module.exports = class Client extends FrameworkClient { async login(token) { /** @type {PrismaClient} */ this.prisma = new PrismaClient(); + if (process.env.DB_PROVIDER === 'sqlite') { + // rewrite queries that use unsupported features this.prisma.$use(sqliteMiddleware); - // make sqlite faster, - // and the missing parentheses are not a mistake, `$queryRaw` is a tagged template literal + // make sqlite faster (missing parentheses are not a mistake, `$queryRaw` is a tagged template literal) this.log.debug(await this.prisma.$queryRaw`PRAGMA journal_mode=WAL;`); // https://www.sqlite.org/wal.html this.log.debug(await this.prisma.$queryRaw`PRAGMA synchronous=normal;`); // https://www.sqlite.org/pragma.html#pragma_synchronous + + setInterval(async () => { + this.log.debug(await this.prisma.$queryRaw`PRAGMA optimize;`); // https://www.sqlite.org/pragma.html#pragma_optimize + }, ms('6h')); } + this.keyv = new Keyv(); return super.login(token); }