File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ import { migrations } from './migrations';
1212import { seeders } from './seeders' ;
1313
1414import dotenv from 'dotenv' ;
15+ import { installSqlitePragmas } from './sqlite-pragmas.js' ;
1516dotenv . config ( { quiet : true } ) ;
1617
1718// possible values: local, test
@@ -40,10 +41,12 @@ if (nodeEnv === 'test') {
4041}
4142
4243export const sequelize = new Sequelize ( config [ dbConfigKey ] ) ;
44+ installSqlitePragmas ( sequelize ) ;
4345
4446const mirrorConfig =
4547 ( process . env . NODE_ENV || 'local' ) === 'local' ? 'mirror' : 'mirrorTest' ;
4648export const sequelizeMirror = new Sequelize ( config [ mirrorConfig ] ) ;
49+ installSqlitePragmas ( sequelizeMirror ) ;
4750
4851// Snapshot of whether V1 MIRROR_DB was fully configured at module-load time.
4952// Captured alongside sequelizeMirror construction so mirrorDBEnabled() stays
Original file line number Diff line number Diff line change 1+ const SQLITE_PRAGMAS = [
2+ 'PRAGMA synchronous = NORMAL' ,
3+ 'PRAGMA cache_size = -65536' ,
4+ 'PRAGMA temp_store = MEMORY' ,
5+ 'PRAGMA mmap_size = 268435456' ,
6+ ] ;
7+
8+ const applySqlitePragmas = async ( connection ) => {
9+ if ( ! connection || typeof connection . exec !== 'function' ) {
10+ return ;
11+ }
12+
13+ await new Promise ( ( resolve , reject ) => {
14+ connection . exec ( SQLITE_PRAGMAS . join ( '; ' ) , ( error ) => {
15+ if ( error ) {
16+ reject ( error ) ;
17+ return ;
18+ }
19+ resolve ( ) ;
20+ } ) ;
21+ } ) ;
22+ } ;
23+
24+ export const installSqlitePragmas = ( sequelize ) => {
25+ if ( sequelize . getDialect ( ) !== 'sqlite' ) {
26+ return ;
27+ }
28+
29+ const configuredConnections = new WeakSet ( ) ;
30+ const { connectionManager } = sequelize ;
31+ const getConnection = connectionManager . getConnection . bind ( connectionManager ) ;
32+
33+ connectionManager . getConnection = async ( ...args ) => {
34+ const connection = await getConnection ( ...args ) ;
35+
36+ if ( ! configuredConnections . has ( connection ) ) {
37+ await applySqlitePragmas ( connection ) ;
38+ configuredConnections . add ( connection ) ;
39+ }
40+
41+ return connection ;
42+ } ;
43+ } ;
Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ import { migrations } from './migrations';
1111import { seeders } from './seeders' ;
1212
1313import dotenv from 'dotenv' ;
14+ import { installSqlitePragmas } from '../sqlite-pragmas.js' ;
1415dotenv . config ( { quiet : true } ) ;
1516
1617// possible values: local, test
@@ -39,6 +40,7 @@ if (nodeEnv === 'test') {
3940}
4041
4142export const sequelizeV2 = new Sequelize ( config [ dbConfigKey ] ) ;
43+ installSqlitePragmas ( sequelizeV2 ) ;
4244
4345// Determine if MySQL mirror is configured by checking the actual config values
4446// This allows MySQL mirror to work in any environment (local, test, production)
@@ -80,6 +82,7 @@ const isMysqlMirrorConfiguredForReconnectV2 = () => {
8082} ;
8183
8284export const sequelizeV2Mirror = new Sequelize ( config [ mirrorConfig ] ) ;
85+ installSqlitePragmas ( sequelizeV2Mirror ) ;
8386
8487// Snapshot of "is the mirror Sequelize instance actually pointing at MySQL"
8588// taken at module-load time, alongside sequelizeV2Mirror construction.
You can’t perform that action at this time.
0 commit comments