@@ -39,35 +39,35 @@ export function getInitializationCommands(config: SQLiteCloudConfig): string {
39
39
// first user authentication, then all other commands
40
40
let commands = ''
41
41
42
- if ( config . apiKey ) {
43
- commands = `AUTH APIKEY ${ config . apiKey } ; `
42
+ if ( config . apikey ) {
43
+ commands = `AUTH APIKEY ${ config . apikey } ; `
44
44
} else {
45
- commands = `AUTH USER ${ config . username || '' } ${ config . passwordHashed ? 'HASH' : 'PASSWORD' } ${ config . password || '' } ; `
45
+ commands = `AUTH USER ${ config . username || '' } ${ config . password_hashed ? 'HASH' : 'PASSWORD' } ${ config . password || '' } ; `
46
46
}
47
47
48
48
if ( config . database ) {
49
- if ( config . createDatabase && ! config . dbMemory ) {
49
+ if ( config . create && ! config . memory ) {
50
50
commands += `CREATE DATABASE ${ config . database } IF NOT EXISTS; `
51
51
}
52
52
commands += `USE DATABASE ${ config . database } ; `
53
53
}
54
54
if ( config . compression ) {
55
55
commands += 'SET CLIENT KEY COMPRESSION TO 1; '
56
56
}
57
- if ( config . nonlinearizable ) {
57
+ if ( config . non_linearizable ) {
58
58
commands += 'SET CLIENT KEY NONLINEARIZABLE TO 1; '
59
59
}
60
- if ( config . noBlob ) {
60
+ if ( config . noblob ) {
61
61
commands += 'SET CLIENT KEY NOBLOB TO 1; '
62
62
}
63
- if ( config . maxData ) {
64
- commands += `SET CLIENT KEY MAXDATA TO ${ config . maxData } ; `
63
+ if ( config . maxdata ) {
64
+ commands += `SET CLIENT KEY MAXDATA TO ${ config . maxdata } ; `
65
65
}
66
- if ( config . maxRows ) {
67
- commands += `SET CLIENT KEY MAXROWS TO ${ config . maxRows } ; `
66
+ if ( config . maxrows ) {
67
+ commands += `SET CLIENT KEY MAXROWS TO ${ config . maxrows } ; `
68
68
}
69
- if ( config . maxRowset ) {
70
- commands += `SET CLIENT KEY MAXROWSET TO ${ config . maxRowset } ; `
69
+ if ( config . maxrowset ) {
70
+ commands += `SET CLIENT KEY MAXROWSET TO ${ config . maxrowset } ; `
71
71
}
72
72
73
73
return commands
@@ -202,39 +202,39 @@ export function popCallback<T extends ErrorCallback = ErrorCallback>(
202
202
/** Validate configuration, apply defaults, throw if something is missing or misconfigured */
203
203
export function validateConfiguration ( config : SQLiteCloudConfig ) : SQLiteCloudConfig {
204
204
console . assert ( config , 'SQLiteCloudConnection.validateConfiguration - missing config' )
205
- if ( config . connectionString ) {
205
+ if ( config . connectionstring ) {
206
206
config = {
207
207
...config ,
208
- ...parseConnectionString ( config . connectionString ) ,
209
- connectionString : config . connectionString // keep original connection string
208
+ ...parseconnectionstring ( config . connectionstring ) ,
209
+ connectionstring : config . connectionstring // keep original connection string
210
210
}
211
211
}
212
212
213
213
// apply defaults where needed
214
214
config . port ||= DEFAULT_PORT
215
215
config . timeout = config . timeout && config . timeout > 0 ? config . timeout : DEFAULT_TIMEOUT
216
- config . clientId ||= 'SQLiteCloud'
216
+ config . clientid ||= 'SQLiteCloud'
217
217
218
218
config . verbose = parseBoolean ( config . verbose )
219
- config . noBlob = parseBoolean ( config . noBlob )
219
+ config . noblob = parseBoolean ( config . noblob )
220
220
config . compression = parseBoolean ( config . compression )
221
- config . createDatabase = parseBoolean ( config . createDatabase )
222
- config . nonlinearizable = parseBoolean ( config . nonlinearizable )
221
+ config . create = parseBoolean ( config . create )
222
+ config . non_linearizable = parseBoolean ( config . non_linearizable )
223
223
config . insecure = parseBoolean ( config . insecure )
224
224
225
- const hasCredentials = ( config . username && config . password ) || config . apiKey
225
+ const hasCredentials = ( config . username && config . password ) || config . apikey
226
226
if ( ! config . host || ! hasCredentials ) {
227
227
console . error ( 'SQLiteCloudConnection.validateConfiguration - missing arguments' , config )
228
- throw new SQLiteCloudError ( 'The user, password and host arguments or the ?apiKey = must be specified.' , { errorCode : 'ERR_MISSING_ARGS' } )
228
+ throw new SQLiteCloudError ( 'The user, password and host arguments or the ?apikey = must be specified.' , { errorCode : 'ERR_MISSING_ARGS' } )
229
229
}
230
230
231
- if ( ! config . connectionString ) {
231
+ if ( ! config . connectionstring ) {
232
232
// build connection string from configuration, values are already validated
233
233
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
234
- if ( config . apiKey ) {
235
- config . connectionString = `sqlitecloud://${ config . host } :${ config . port } /${ config . database || '' } ?apiKey =${ config . apiKey } `
234
+ if ( config . apikey ) {
235
+ config . connectionstring = `sqlitecloud://${ config . host } :${ config . port } /${ config . database || '' } ?apikey =${ config . apikey } `
236
236
} else {
237
- config . connectionString = `sqlitecloud://${ encodeURIComponent ( config . username || '' ) } :${ encodeURIComponent ( config . password || '' ) } @${ config . host } :${
237
+ config . connectionstring = `sqlitecloud://${ encodeURIComponent ( config . username || '' ) } :${ encodeURIComponent ( config . password || '' ) } @${ config . host } :${
238
238
config . port
239
239
} /${ config . database } `
240
240
}
@@ -244,44 +244,24 @@ export function validateConfiguration(config: SQLiteCloudConfig): SQLiteCloudCon
244
244
}
245
245
246
246
/**
247
- * Parse connectionString like sqlitecloud://username:password@host:port/database?option1=xxx&option2=xxx
248
- * or sqlitecloud://host.sqlite.cloud:8860/chinook.sqlite?apiKey =mIiLARzKm9XBVllbAzkB1wqrgijJ3Gx0X5z1Agm3xBo
247
+ * Parse connectionstring like sqlitecloud://username:password@host:port/database?option1=xxx&option2=xxx
248
+ * or sqlitecloud://host.sqlite.cloud:8860/chinook.sqlite?apikey =mIiLARzKm9XBVllbAzkB1wqrgijJ3Gx0X5z1Agm3xBo
249
249
* into its basic components.
250
250
*/
251
- export function parseConnectionString ( connectionString : string ) : SQLiteCloudConfig {
251
+ export function parseconnectionstring ( connectionstring : string ) : SQLiteCloudConfig {
252
252
try {
253
253
// The URL constructor throws a TypeError if the URL is not valid.
254
254
// in spite of having the same structure as a regular url
255
255
// protocol://username:password@host :port/database?option1=xxx&option2=xxx)
256
256
// the sqlitecloud: protocol is not recognized by the URL constructor in browsers
257
257
// so we need to replace it with https: to make it work
258
- const knownProtocolUrl = connectionString . replace ( 'sqlitecloud:' , 'https:' )
258
+ const knownProtocolUrl = connectionstring . replace ( 'sqlitecloud:' , 'https:' )
259
259
const url = new URL ( knownProtocolUrl )
260
- const options : { [ key : string ] : string } = { }
261
-
262
- // properties that are mixed case in the connection string should be accepted even if the
263
- // customer mistakenly write them in camelCase or kebab-case or whateverTheCase
264
- const mixedCaseProperties = [
265
- 'connectionString' ,
266
- 'passwordHashed' ,
267
- 'apiKey' ,
268
- 'createDatabase' ,
269
- 'dbMemory' ,
270
- 'compression' ,
271
- 'noBlob' ,
272
- 'maxData' ,
273
- 'maxRows' ,
274
- 'maxRowset' ,
275
- 'tlsOptions' ,
276
- 'useWebsocket' ,
277
- 'gatewayUrl' ,
278
- 'clientId'
279
- ]
280
260
261
+ // all lowecase options
262
+ const options : { [ key : string ] : string } = { }
281
263
url . searchParams . forEach ( ( value , key ) => {
282
- let normalizedKey = key . toLowerCase ( ) . replaceAll ( '-' , '' ) . replaceAll ( '_' , '' )
283
- const mixedCaseKey = mixedCaseProperties . find ( mixedCaseProperty => mixedCaseProperty . toLowerCase ( ) == normalizedKey )
284
- options [ mixedCaseKey || normalizedKey ] = value
264
+ options [ key . toLowerCase ( ) . replaceAll ( '-' , '_' ) ] = value
285
265
} )
286
266
287
267
const config : SQLiteCloudConfig = {
@@ -292,10 +272,10 @@ export function parseConnectionString(connectionString: string): SQLiteCloudConf
292
272
...options
293
273
}
294
274
295
- // either you use an apiKey or username and password
296
- if ( config . apiKey ) {
275
+ // either you use an apikey or username and password
276
+ if ( config . apikey ) {
297
277
if ( config . username || config . password ) {
298
- console . warn ( 'SQLiteCloudConnection.parseConnectionString - apiKey and username/password are both specified, using apiKey ' )
278
+ console . warn ( 'SQLiteCloudConnection.parseconnectionstring - apikey and username/password are both specified, using apikey ' )
299
279
}
300
280
delete config . username
301
281
delete config . password
@@ -308,7 +288,7 @@ export function parseConnectionString(connectionString: string): SQLiteCloudConf
308
288
309
289
return config
310
290
} catch ( error ) {
311
- throw new SQLiteCloudError ( `Invalid connection string: ${ connectionString } ` )
291
+ throw new SQLiteCloudError ( `Invalid connection string: ${ connectionstring } ` )
312
292
}
313
293
}
314
294
@@ -319,3 +299,11 @@ export function parseBoolean(value: string | boolean | null | undefined): boolea
319
299
}
320
300
return value ? true : false
321
301
}
302
+
303
+ /** Returns true if value is 1 or true */
304
+ export function parseBooleanToZeroOne ( value : string | boolean | null | undefined ) : 0 | 1 {
305
+ if ( typeof value === 'string' ) {
306
+ return value . toLowerCase ( ) === 'true' || value === '1' ? 1 : 0
307
+ }
308
+ return value ? 1 : 0
309
+ }
0 commit comments