-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
188 lines (163 loc) Β· 5.38 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
var JlSqlApi = require( 'jl-sql-api' );
var fs = require( 'fs' );
/**
* Database-js driver for JSON files.
*
* @see https://github.com/mlaanderson/database-js
*/
class JsonDriver {
/**
* Constructor
*
* @param {string} filename File name
* @param {object} options Options. Defaults to { charset: 'utf-8', checkOnConnect: true }.
*
* @see https://github.com/avz/node-jl-sql-api#options-object for more options.
*/
constructor( filename, options ) {
this._api = new JlSqlApi();
this._filename = filename;
this._options = Object.assign( {
charset: 'utf-8',
checkOnConnect: true
}, options || {} );
this._data = null;
if ( this._options.checkOnConnect && ! fs.existsSync( this._filename ) ) {
throw new Error( 'File "' + this._filename + '" does not exist.' );
}
}
/**
* Loads the file and returns a promise to its content.
*
* @returns {Promise}
*/
_loadFile() {
var _this = this;
return new Promise( function ( resolve, reject ) {
fs.readFile( _this._filename, _this._options.charset, function ( err, data ) {
if ( err ) {
return reject( err );
}
try {
return resolve( JSON.parse( data ) );
} catch ( e ) {
return reject( e );
}
} );
} );
}
/**
* Saves the file and returns a promise with no data.
*
* @returns {Promise}
*/
_saveFile() {
var _this = this;
return new Promise( function ( resolve, reject ) {
fs.writeFile(
_this._filename,
JSON.stringify( _this._data || [] ),
_this._options.charset,
function ( err ) {
if ( err ) {
return reject( err );
}
return resolve();
}
);
} );
}
/**
* Returns a promise to the file content.
*
* @param {boolean} reloadFile Whether is desired to reload the file. Defaults to false.
* @returns {Promise}
*/
data( reloadFile ) {
if ( this._data && ! reloadFile ) {
return Promise.resolve( this._data );
}
var _this = this;
return this._loadFile().then( function ( data ) {
_this._data = data;
return data;
} );
}
/**
* Executes the given query and returns a promise to the resulting data.
*
* @param {string} sql Query to execute.
* @returns {Promise}
*/
query( sql ) {
var _this = this;
return new Promise( function( resolve, reject ) {
var queryData = function( data ) {
_this._api.query( sql )
.fromArrayOfObjects( Array.isArray( data ) ? data : [ data ] )
.toArrayOfObjects( function( rows ) {
resolve( rows );
} );
};
_this.data()
.then( queryData )
.catch( function ( err ) { reject( err ); } );
} );
}
/**
* Executes the given command and returns a promise with the resulting data.
* The file is saved after the command execution, whether successful.
*
* @param {string} sql Command to execute.
* @returns {Promise}
*/
execute( sql ) {
var _this = this;
return new Promise( function( resolve, reject ) {
_this.query( sql )
.then( function keepResults( rows ) {
_this._data = rows; // Keep the last content
return rows;
} )
.then( function saveToFile( rows ) {
return _this._saveFile()
.then( function ( data ) { resolve( data ); } )
.catch( function ( err ) { reject( err ); } );
} )
.catch( function ( err ) { reject( err ); } );
} );
}
}
module.exports = {
open: function( connection ) {
var options = {};
if ( connection.Parameters ) {
connection.Parameters.split( '&' ).map( function ( s ) {
var separated = s.split( '=' );
var key = separated[ 0 ];
var val = separated[ 1 ];
var subKey = null;
// e.g. "sortOptions.bufferSize"
if ( key.indexOf( '.' ) >= 0 ) {
var sub = key.split( '.' );
key = sub[ 0 ];
subKey = sub[ 1 ];
}
if ( ! subKey ) {
options[ key ] = val;
} else {
if ( ! options[ key ] ) {
options[ key ] = {};
}
options[ key ][ subKey ] = val;
}
// Adjust 'checkOnConnect'
if ( options[ 'checkOnConnect' ] ) {
var isFalse = function ( v ) { return 'false' === v || 'no' === v || '0' == v; };
options[ 'checkOnConnect' ] = ! isFalse( val );
}
});
}
return new JsonDriver( connection.Database, options );
}
};