@@ -39,35 +39,35 @@ export function getInitializationCommands(config: SQLiteCloudConfig): string {
3939 // first user authentication, then all other commands
4040 let commands = ''
4141
42- if ( config . apiKey ) {
43- commands = `AUTH APIKEY ${ config . apiKey } ; `
42+ if ( config . apikey ) {
43+ commands = `AUTH APIKEY ${ config . apikey } ; `
4444 } 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 || '' } ; `
4646 }
4747
4848 if ( config . database ) {
49- if ( config . createDatabase && ! config . dbMemory ) {
49+ if ( config . create && ! config . memory ) {
5050 commands += `CREATE DATABASE ${ config . database } IF NOT EXISTS; `
5151 }
5252 commands += `USE DATABASE ${ config . database } ; `
5353 }
5454 if ( config . compression ) {
5555 commands += 'SET CLIENT KEY COMPRESSION TO 1; '
5656 }
57- if ( config . nonlinearizable ) {
57+ if ( config . non_linearizable ) {
5858 commands += 'SET CLIENT KEY NONLINEARIZABLE TO 1; '
5959 }
60- if ( config . noBlob ) {
60+ if ( config . noblob ) {
6161 commands += 'SET CLIENT KEY NOBLOB TO 1; '
6262 }
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 } ; `
6565 }
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 } ; `
6868 }
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 } ; `
7171 }
7272
7373 return commands
@@ -202,39 +202,39 @@ export function popCallback<T extends ErrorCallback = ErrorCallback>(
202202/** Validate configuration, apply defaults, throw if something is missing or misconfigured */
203203export function validateConfiguration ( config : SQLiteCloudConfig ) : SQLiteCloudConfig {
204204 console . assert ( config , 'SQLiteCloudConnection.validateConfiguration - missing config' )
205- if ( config . connectionString ) {
205+ if ( config . connectionstring ) {
206206 config = {
207207 ...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
210210 }
211211 }
212212
213213 // apply defaults where needed
214214 config . port ||= DEFAULT_PORT
215215 config . timeout = config . timeout && config . timeout > 0 ? config . timeout : DEFAULT_TIMEOUT
216- config . clientId ||= 'SQLiteCloud'
216+ config . clientid ||= 'SQLiteCloud'
217217
218218 config . verbose = parseBoolean ( config . verbose )
219- config . noBlob = parseBoolean ( config . noBlob )
219+ config . noblob = parseBoolean ( config . noblob )
220220 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 )
223223 config . insecure = parseBoolean ( config . insecure )
224224
225- const hasCredentials = ( config . username && config . password ) || config . apiKey
225+ const hasCredentials = ( config . username && config . password ) || config . apikey
226226 if ( ! config . host || ! hasCredentials ) {
227227 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' } )
229229 }
230230
231- if ( ! config . connectionString ) {
231+ if ( ! config . connectionstring ) {
232232 // build connection string from configuration, values are already validated
233233 // 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 } `
236236 } 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 } :${
238238 config . port
239239 } /${ config . database } `
240240 }
@@ -244,44 +244,24 @@ export function validateConfiguration(config: SQLiteCloudConfig): SQLiteCloudCon
244244}
245245
246246/**
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
249249 * into its basic components.
250250 */
251- export function parseConnectionString ( connectionString : string ) : SQLiteCloudConfig {
251+ export function parseconnectionstring ( connectionstring : string ) : SQLiteCloudConfig {
252252 try {
253253 // The URL constructor throws a TypeError if the URL is not valid.
254254 // in spite of having the same structure as a regular url
255255 // protocol://username:password@host :port/database?option1=xxx&option2=xxx)
256256 // the sqlitecloud: protocol is not recognized by the URL constructor in browsers
257257 // 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:' )
259259 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- ]
280260
261+ // all lowecase options
262+ const options : { [ key : string ] : string } = { }
281263 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
285265 } )
286266
287267 const config : SQLiteCloudConfig = {
@@ -292,10 +272,10 @@ export function parseConnectionString(connectionString: string): SQLiteCloudConf
292272 ...options
293273 }
294274
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 ) {
297277 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 ' )
299279 }
300280 delete config . username
301281 delete config . password
@@ -308,7 +288,7 @@ export function parseConnectionString(connectionString: string): SQLiteCloudConf
308288
309289 return config
310290 } catch ( error ) {
311- throw new SQLiteCloudError ( `Invalid connection string: ${ connectionString } ` )
291+ throw new SQLiteCloudError ( `Invalid connection string: ${ connectionstring } ` )
312292 }
313293}
314294
@@ -319,3 +299,11 @@ export function parseBoolean(value: string | boolean | null | undefined): boolea
319299 }
320300 return value ? true : false
321301}
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