@@ -329,6 +329,149 @@ MATCH ()-[]->() RETURN { name:'relationships', data: count(*)} AS result
329329export const serverInfoQuery =
330330 'CALL dbms.components() YIELD name, versions, edition'
331331
332+ const databaseList = store =>
333+ Rx . Observable . fromPromise (
334+ new Promise ( async ( resolve , reject ) => {
335+ const supportsMultiDb = await bolt . hasMultiDbSupport ( )
336+ if ( ! supportsMultiDb ) {
337+ return resolve ( null )
338+ }
339+ bolt
340+ . directTransaction (
341+ 'SHOW DATABASES' ,
342+ { } ,
343+ {
344+ useCypherThread : shouldUseCypherThread ( store . getState ( ) ) ,
345+ ...getBackgroundTxMetadata ( {
346+ hasServerSupport : canSendTxMetadata ( store . getState ( ) )
347+ } ) ,
348+ useDb : SYSTEM_DB
349+ }
350+ )
351+ . then ( resolve )
352+ . catch ( reject )
353+ } )
354+ )
355+ . catch ( e => {
356+ return Rx . Observable . of ( null )
357+ } )
358+ . do ( res => {
359+ if ( ! res ) return Rx . Observable . of ( null )
360+ const databases = res . records . map ( record => ( {
361+ ...reduce (
362+ record . keys ,
363+ ( agg , key ) => assign ( agg , { [ key ] : record . get ( key ) } ) ,
364+ { }
365+ ) ,
366+ status : record . get ( 'currentStatus' )
367+ } ) )
368+
369+ store . dispatch ( update ( { databases } ) )
370+
371+ return Rx . Observable . of ( null )
372+ } )
373+
374+ const getLabelsAndTypes = store =>
375+ Rx . Observable . of ( null ) . mergeMap ( ( ) => {
376+ const db = getUseDb ( store . getState ( ) )
377+
378+ // System db, do nothing
379+ if ( db === SYSTEM_DB ) {
380+ store . dispatch ( updateMeta ( [ ] ) )
381+ return Rx . Observable . of ( null )
382+ }
383+ // Not system db, try and fetch meta data
384+ return Rx . Observable . fromPromise (
385+ bolt . routedReadTransaction (
386+ metaQuery ,
387+ { } ,
388+ {
389+ useCypherThread : shouldUseCypherThread ( store . getState ( ) ) ,
390+ onLostConnection : onLostConnection ( store . dispatch ) ,
391+ ...getBackgroundTxMetadata ( {
392+ hasServerSupport : canSendTxMetadata ( store . getState ( ) )
393+ } )
394+ }
395+ )
396+ )
397+ . do ( res => {
398+ if ( res ) {
399+ store . dispatch ( updateMeta ( res ) )
400+ }
401+ } )
402+ . catch ( e => {
403+ store . dispatch ( updateMeta ( [ ] ) )
404+ return Rx . Observable . of ( null )
405+ } )
406+ } )
407+
408+ const clusterRole = store =>
409+ Rx . Observable . fromPromise (
410+ new Promise ( ( resolve , reject ) => {
411+ if ( ! isACausalCluster ( store . getState ( ) ) ) {
412+ return resolve ( null )
413+ }
414+ bolt
415+ . directTransaction (
416+ getDbClusterRole ( store . getState ( ) ) ,
417+ { } ,
418+ {
419+ useCypherThread : shouldUseCypherThread ( store . getState ( ) ) ,
420+ ...getBackgroundTxMetadata ( {
421+ hasServerSupport : canSendTxMetadata ( store . getState ( ) )
422+ } )
423+ }
424+ )
425+ . then ( resolve )
426+ . catch ( reject )
427+ } )
428+ )
429+ . catch ( e => {
430+ return Rx . Observable . of ( null )
431+ } )
432+ . do ( res => {
433+ if ( ! res ) return Rx . Observable . of ( null )
434+ const role = res . records [ 0 ] . get ( 0 )
435+ store . dispatch ( update ( { role } ) )
436+ return Rx . Observable . of ( null )
437+ } )
438+
439+ const switchToRequestedDb = store => {
440+ if ( getUseDb ( store . getState ( ) ) ) return Rx . Observable . of ( null )
441+
442+ const databases = getDatabases ( store . getState ( ) )
443+ const activeConnection = getActiveConnectionData ( store . getState ( ) )
444+ const requestedUseDb = activeConnection ?. requestedUseDb
445+
446+ const defaultDb = databases . find ( db => db . default )
447+
448+ if ( requestedUseDb ) {
449+ const wantedDb =
450+ requestedUseDb &&
451+ databases . find (
452+ ( { name } ) => name . toLowerCase ( ) === requestedUseDb . toLowerCase ( )
453+ )
454+ if ( wantedDb ) {
455+ store . dispatch ( useDb ( wantedDb . name ) )
456+ // update labels and such for new db
457+ return getLabelsAndTypes ( store )
458+ } else {
459+ // this will show the db not found frame
460+ store . dispatch ( executeSingleCommand ( `:use ${ requestedUseDb } ` ) )
461+ store . dispatch (
462+ updateConnection ( {
463+ id : activeConnection . id ,
464+ requestedUseDb : ''
465+ } )
466+ )
467+ store . dispatch ( useDb ( defaultDb . name ) )
468+ }
469+ } else {
470+ store . dispatch ( useDb ( defaultDb . name ) )
471+ }
472+ return Rx . Observable . of ( null )
473+ }
474+
332475export const dbMetaEpic = ( some$ , store ) =>
333476 some$
334477 . ofType ( UPDATE_CONNECTION_STATE )
@@ -342,157 +485,20 @@ export const dbMetaEpic = (some$, store) =>
342485 . throttle ( ( ) => some$ . ofType ( DB_META_DONE ) )
343486 // Server version and edition
344487 . do ( store . dispatch ( { type : FETCH_SERVER_INFO } ) )
345- . mergeMap ( ( ) =>
346- Rx . Observable . forkJoin ( [
347- // Labels, types and propertyKeys, and server version
348- Rx . Observable . of ( null ) . mergeMap ( ( ) => {
349- const db = getUseDb ( store . getState ( ) )
350-
351- // System db, do nothing
352- if ( db === SYSTEM_DB ) {
353- store . dispatch ( updateMeta ( [ ] ) )
354- return Rx . Observable . of ( null )
355- }
356-
357- // Not system db, try and fetch meta data
358- return Rx . Observable . fromPromise (
359- bolt . routedReadTransaction (
360- metaQuery ,
361- { } ,
362- {
363- useCypherThread : shouldUseCypherThread ( store . getState ( ) ) ,
364- onLostConnection : onLostConnection ( store . dispatch ) ,
365- ...getBackgroundTxMetadata ( {
366- hasServerSupport : canSendTxMetadata ( store . getState ( ) )
367- } )
368- }
369- )
370- )
371- . do ( res => {
372- if ( res ) {
373- store . dispatch ( updateMeta ( res ) )
374- }
375- } )
376- . catch ( e => {
377- store . dispatch ( updateMeta ( [ ] ) )
378- return Rx . Observable . of ( null )
379- } )
380- } ) ,
381- // Cluster role
382- Rx . Observable . fromPromise (
383- new Promise ( ( resolve , reject ) => {
384- if ( ! isACausalCluster ( store . getState ( ) ) ) {
385- return resolve ( null )
386- }
387- bolt
388- . directTransaction (
389- getDbClusterRole ( store . getState ( ) ) ,
390- { } ,
391- {
392- useCypherThread : shouldUseCypherThread (
393- store . getState ( )
394- ) ,
395- ...getBackgroundTxMetadata ( {
396- hasServerSupport : canSendTxMetadata ( store . getState ( ) )
397- } )
398- }
399- )
400- . then ( resolve )
401- . catch ( reject )
402- } )
403- )
404- . catch ( e => {
405- return Rx . Observable . of ( null )
406- } )
407- . do ( res => {
408- if ( ! res ) return Rx . Observable . of ( null )
409- const role = res . records [ 0 ] . get ( 0 )
410- store . dispatch ( update ( { role } ) )
411- return Rx . Observable . of ( null )
412- } ) ,
413- // Database list
414- Rx . Observable . fromPromise (
415- new Promise ( async ( resolve , reject ) => {
416- const supportsMultiDb = await bolt . hasMultiDbSupport ( )
417- if ( ! supportsMultiDb ) {
418- return resolve ( null )
419- }
420- bolt
421- . directTransaction (
422- 'SHOW DATABASES' ,
423- { } ,
424- {
425- useCypherThread : shouldUseCypherThread (
426- store . getState ( )
427- ) ,
428- ...getBackgroundTxMetadata ( {
429- hasServerSupport : canSendTxMetadata ( store . getState ( ) )
430- } ) ,
431- useDb : SYSTEM_DB // System db
432- }
433- )
434- . then ( resolve )
435- . catch ( reject )
436- } )
437- )
438- . catch ( e => {
439- return Rx . Observable . of ( null )
440- } )
441- . do ( res => {
442- if ( ! res ) return Rx . Observable . of ( null )
443- const databases = res . records . map ( record => ( {
444- ...reduce (
445- record . keys ,
446- ( agg , key ) => assign ( agg , { [ key ] : record . get ( key ) } ) ,
447- { }
448- ) ,
449- status : record . get ( 'currentStatus' )
450- } ) )
451-
452- store . dispatch ( update ( { databases } ) )
453-
454- if ( getUseDb ( store . getState ( ) ) ) return Rx . Observable . of ( null )
455-
456- const activeConnection = getActiveConnectionData (
457- store . getState ( )
458- )
459- const requestedUseDb = activeConnection ?. requestedUseDb
460-
461- if ( requestedUseDb ) {
462- const wantedDb = databases . find (
463- ( { name } ) =>
464- name . toLowerCase ( ) === requestedUseDb . toLowerCase ( )
465- )
466- if ( wantedDb ) {
467- store . dispatch ( useDb ( wantedDb . name ) )
468- } else {
469- // this will show the db not found frame
470- store . dispatch (
471- executeSingleCommand ( `:use ${ requestedUseDb } ` )
472- )
473- store . dispatch (
474- updateConnection ( {
475- id : activeConnection . id ,
476- requestedUseDb : ''
477- } )
478- )
479- }
480- } else {
481- const defaultDb = databases . find ( db => db . default )
482- if ( defaultDb ) {
483- store . dispatch ( useDb ( defaultDb . name ) )
484- }
485- }
486- return Rx . Observable . of ( null )
487- } )
488+ . mergeMap ( ( ) => {
489+ return Rx . Observable . forkJoin ( [
490+ getLabelsAndTypes ( store ) ,
491+ clusterRole ( store ) ,
492+ databaseList ( store )
488493 ] )
489- )
494+ } )
490495 . takeUntil (
491496 some$
492497 . ofType ( LOST_CONNECTION )
493498 . filter ( connectionLossFilter )
494499 . merge ( some$ . ofType ( DISCONNECTION_SUCCESS ) )
495500 )
501+ . mergeMap ( ( ) => switchToRequestedDb ( store ) )
496502 . mapTo ( { type : DB_META_DONE } )
497503 )
498504 } )
0 commit comments