1- var util = require ( 'util' ) ;
2- var sqlite3 = require ( 'sqlite3' ) . verbose ( ) ;
3- var Base = require ( 'db-migrate-base' ) ;
4- var Promise = require ( 'bluebird' ) ;
5- var util = require ( 'util' ) ;
6- var log ;
7- var type ;
1+ const util = require ( 'util' ) ;
2+ const sqlite3 = require ( 'sqlite3' ) . verbose ( ) ;
3+ const Base = require ( 'db-migrate-base' ) ;
4+ const Promise = require ( 'bluebird' ) ;
85
9-
10- var defaultMode = sqlite3 . OPEN_READWRITE | sqlite3 . OPEN_CREATE ,
11- internals = { } ;
6+ var defaultMode = sqlite3 . OPEN_READWRITE | sqlite3 . OPEN_CREATE ;
7+ var internals = { } ;
128
139var Sqlite3Driver = Base . extend ( {
14- init : function ( connection ) {
10+ init : function ( connection , intern ) {
11+ this . log = intern . mod . log ;
12+ this . type = intern . mod . type ;
1513 this . _escapeString = '"' ;
1614 this . _super ( internals ) ;
1715 this . connection = connection ;
1816 } ,
1917
20- createDatabase : function ( cb ) {
18+ createDatabase : function ( cb ) {
2119 // sqlite does this automatically if needed
2220 return Promise . resolve ( null ) . nodeify ( cb ) ;
2321 } ,
2422
25- startMigration : function ( cb ) {
26-
27- if ( ! internals . notransactions ) {
28-
29- return Promise . promisify ( this . runSql . bind ( this ) ) ( 'BEGIN TRANSACTION;' ) . nodeify ( cb ) ;
30- }
31- else
32- return Promise . resolve ( ) . nodeify ( cb ) ;
23+ startMigration : function ( cb ) {
24+ if ( ! internals . notransactions ) {
25+ return Promise . promisify ( this . runSql . bind ( this ) ) (
26+ 'BEGIN TRANSACTION;'
27+ ) . nodeify ( cb ) ;
28+ } else return Promise . resolve ( ) . nodeify ( cb ) ;
3329 } ,
3430
35- endMigration : function ( cb ) {
36-
37- if ( ! internals . notransactions ) {
38-
31+ endMigration : function ( cb ) {
32+ if ( ! internals . notransactions ) {
3933 return Promise . promisify ( this . runSql . bind ( this ) ) ( 'COMMIT;' ) . nodeify ( cb ) ;
40- }
41- else
42- return Promise . resolve ( null ) . nodeify ( cb ) ;
34+ } else return Promise . resolve ( null ) . nodeify ( cb ) ;
4335 } ,
4436
45- mapDataType : function ( str ) {
46- switch ( str ) {
47- case type . DATE_TIME :
37+ mapDataType : function ( str ) {
38+ switch ( str ) {
39+ case this . type . DATE_TIME :
4840 return 'datetime' ;
49- case type . TIME :
41+ case this . type . TIME :
5042 return 'time' ;
5143 }
5244 return this . _super ( str ) ;
5345 } ,
5446
55- switchDatabase : function ( options , callback ) {
47+ switchDatabase : function ( options , callback ) {
5648 callback ( null ) ;
5749 } ,
5850
59- createColumnDef : function ( name , spec , options ) {
51+ createColumnDef : function ( name , spec , options ) {
6052 name = '"' + name + '"' ;
61- var dType = this . mapDataType ( spec . type ) ;
62- var len = spec . length ? util . format ( '(%s)' , spec . length ) : '' ;
53+ var dType = this . mapDataType ( spec . type ) ;
54+ var len = spec . length ? util . format ( '(%s)' , spec . length ) : '' ;
6355 var constraint = this . createColumnConstraint ( spec , options ) ;
6456
65- if ( spec . type === type . INTEGER )
66- len = '' ;
57+ if ( spec . type === this . type . INTEGER ) len = '' ;
6758
68- return { foreignKey : null ,
69- constraints : [ name , dType , len , constraint ] . join ( ' ' ) } ;
59+ return {
60+ foreignKey : null ,
61+ constraints : [ name , dType , len , constraint ] . join ( ' ' )
62+ } ;
7063 } ,
7164
72- createColumnConstraint : function ( spec , options ) {
65+ createColumnConstraint : function ( spec , options ) {
7366 var constraint = [ ] ;
7467 if ( spec . primaryKey && options . emitPrimaryKey ) {
7568 constraint . push ( 'PRIMARY KEY' ) ;
@@ -89,96 +82,94 @@ var Sqlite3Driver = Base.extend({
8982 if ( spec . defaultValue !== undefined ) {
9083 constraint . push ( 'DEFAULT' ) ;
9184
92- if ( typeof ( spec . defaultValue ) === 'string' )
85+ if ( typeof spec . defaultValue === 'string' ) {
9386 constraint . push ( '"' + spec . defaultValue + '"' ) ;
94- else
95- constraint . push ( spec . defaultValue ) ;
87+ } else constraint . push ( spec . defaultValue ) ;
9688 }
9789
9890 return constraint . join ( ' ' ) ;
9991 } ,
10092
101- renameTable : function ( tableName , newTableName , callback ) {
102- var sql = util . format ( 'ALTER TABLE %s RENAME TO %s' , tableName , newTableName ) ;
93+ renameTable : function ( tableName , newTableName , callback ) {
94+ var sql = util . format (
95+ 'ALTER TABLE %s RENAME TO %s' ,
96+ tableName ,
97+ newTableName
98+ ) ;
10399
104100 return this . runSql ( sql ) . nodeify ( callback ) ;
105101 } ,
106102
107- //removeColumn: function(tableName, columnName, callback) {
108- //},
103+ // removeColumn: function(tableName, columnName, callback) {
104+ // },
109105
110- //renameColumn: function(tableName, oldColumnName, newColumnName, callback) {
111- //};
106+ // renameColumn: function(tableName, oldColumnName, newColumnName, callback) {
107+ // };
112108
113- //changeColumn: function(tableName, columnName, columnSpec, callback) {
114- //},
109+ // changeColumn: function(tableName, columnName, columnSpec, callback) {
110+ // },
115111
116- runSql : function ( ) {
112+ runSql : function ( ) {
117113 var callback = arguments [ arguments . length - 1 ] ;
118114 var params = arguments ;
119115
120- log . sql . apply ( null , arguments ) ;
121- if ( internals . dryRun ) {
116+ this . log . sql . apply ( null , arguments ) ;
117+ if ( internals . dryRun ) {
122118 return Promise . resolve ( ) . nodeify ( callback ) ;
123119 }
124120
125- return new Promise ( function ( resolve , reject ) {
126- var prCB = function ( err , data ) {
127- return ( err ? reject ( err ) : resolve ( data ) ) ;
128- } ;
129-
130- if ( typeof ( params [ params . length - 1 ] ) === 'function' )
131- params [ params . length - 1 ] = prCB ;
132- else
133- params [ params . length ++ ] = prCB ;
134-
135-
136- if ( params . length === 1 || ( callback && typeof ( params [ 1 ] ) === 'function' ) )
137- this . connection . exec . apply ( this . connection , params ) ;
138- else
139- this . connection . run . apply ( this . connection , params ) ;
140- } . bind ( this ) ) . nodeify ( callback ) ;
121+ return new Promise (
122+ function ( resolve , reject ) {
123+ var prCB = function ( err , data ) {
124+ return err ? reject ( err ) : resolve ( data ) ;
125+ } ;
126+
127+ if ( typeof params [ params . length - 1 ] === 'function' ) {
128+ params [ params . length - 1 ] = prCB ;
129+ } else params [ params . length ++ ] = prCB ;
130+
131+ if (
132+ params . length === 1 ||
133+ ( callback && typeof params [ 1 ] === 'function' )
134+ ) {
135+ this . connection . exec . apply ( this . connection , params ) ;
136+ } else this . connection . run . apply ( this . connection , params ) ;
137+ } . bind ( this )
138+ ) . nodeify ( callback ) ;
141139 } ,
142140
143- all : function ( ) {
144-
145- log . sql . apply ( null , arguments ) ;
141+ all : function ( ) {
142+ this . log . sql . apply ( null , arguments ) ;
146143
147144 this . connection . all . apply ( this . connection , arguments ) ;
148145 } ,
149146
150- close : function ( callback ) {
147+ close : function ( callback ) {
151148 this . connection . close ( ) ;
152149
153- if ( typeof ( callback ) === 'function' )
154- callback ( null ) ;
155- else
156- return Promise . resolve ( ) ;
150+ if ( typeof callback === 'function' ) callback ( null ) ;
151+ else return Promise . resolve ( ) ;
157152 }
158-
159153} ) ;
160154
161155Promise . promisifyAll ( Sqlite3Driver ) ;
162156
163- exports . connect = function ( config , intern , callback ) {
157+ exports . connect = function ( config , intern , callback ) {
164158 var mode = config . mode || defaultMode ;
165159
166160 internals = intern ;
167161
168- log = internals . mod . log ;
169- type = internals . mod . type ;
170-
171162 if ( config . db ) {
172163 callback ( null , new Sqlite3Driver ( config . db ) ) ;
173164 } else {
174- if ( typeof ( config . filename ) === 'undefined' ) {
165+ if ( typeof config . filename === 'undefined' ) {
175166 console . error ( 'filename is required in database.json' ) ;
176167 return ;
177168 }
178169 var db = new sqlite3 . Database ( config . filename , mode ) ;
179- db . on ( " error" , callback ) ;
180- db . on ( " open" , function ( ) {
181- callback ( null , new Sqlite3Driver ( db ) ) ;
170+ db . on ( ' error' , callback ) ;
171+ db . on ( ' open' , function ( ) {
172+ callback ( null , new Sqlite3Driver ( db , intern ) ) ;
182173 } ) ;
183174 }
184175} ;
0 commit comments