@@ -3,6 +3,14 @@ var P = require('bluebird');
33var fs = P . promisifyAll ( require ( 'fs' ) ) ;
44var sqlite3 = P . promisifyAll ( require ( 'sqlite3' ) ) ;
55
6+ var pragmas = [
7+ 'PRAGMA main.page_size = 4096' ,
8+ 'PRAGMA main.cache_size=10000' ,
9+ 'PRAGMA main.locking_mode=EXCLUSIVE' ,
10+ 'PRAGMA main.synchronous=NORMAL' ,
11+ 'PRAGMA main.journal_mode=WAL' ,
12+ 'PRAGMA main.cache_size=5000'
13+ ] ;
614var createTableQuery = 'CREATE TABLE IF NOT EXISTS data('
715 + 'title TEXT, revision INTEGER, body BLOB, namespace INTEGER'
816 + ', PRIMARY KEY(title ASC, revision DESC)'
@@ -14,16 +22,28 @@ var saveQuery = 'insert into data (title, revision, body, namespace) values (?,?
1422function SQLiteStore ( options ) {
1523 this . options = options ;
1624 this . db = new sqlite3 . Database ( options . dataBase ) ;
17- this . db . exec ( createTableQuery ) ;
18- this . queries = {
19- check : this . db . prepare ( checkQuery ) ,
20- purgeTitle : this . db . prepare ( purgeTitleQuery ) ,
21- save : this . db . prepare ( saveQuery ) ,
22- } ;
2325}
2426
27+ SQLiteStore . prototype . setup = function ( ) {
28+ var self = this ;
29+ return this . db . execAsync ( createTableQuery )
30+ . then ( function ( ) {
31+ return P . all ( pragmas . map ( function ( pragma ) {
32+ return self . db . execAsync ( pragma ) ;
33+ } ) ) ;
34+ } )
35+ . then ( function ( ) {
36+ self . queries = {
37+ check : self . db . prepare ( checkQuery ) ,
38+ purgeTitle : self . db . prepare ( purgeTitleQuery ) ,
39+ save : self . db . prepare ( saveQuery ) ,
40+ } ;
41+ return self ;
42+ } ) ;
43+ } ;
44+
2545SQLiteStore . prototype . checkArticle = function checkArticle ( title , oldid ) {
26- return this . queries . check . getAsync ( title , oldid )
46+ return this . queries . check . getAsync ( title , oldid ) ;
2747} ;
2848
2949SQLiteStore . prototype . saveArticle = function saveArticle ( body , title , oldid ) {
@@ -34,4 +54,6 @@ SQLiteStore.prototype.saveArticle = function saveArticle (body, title, oldid) {
3454 } ) ;
3555} ;
3656
37- module . exports = SQLiteStore ;
57+ module . exports = function makeSQLiteStore ( options ) {
58+ return new SQLiteStore ( options ) . setup ( ) ;
59+ } ;
0 commit comments