File tree Expand file tree Collapse file tree
packages/openneuro-server/src Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11import config from "../config"
22import { Client } from "@elastic/elasticsearch"
33
4- const elasticConfig = {
5- node : config . elasticsearch . connection || "http://mock-client" ,
6- maxRetries : 3 ,
7- }
4+ let _client : Client | null = null
85
9- export const elasticClient = new Client ( elasticConfig )
6+ export function getElasticClient ( ) : Client {
7+ if ( ! _client ) {
8+ _client = new Client ( {
9+ node : config . elasticsearch . connection || "http://mock-client" ,
10+ maxRetries : 3 ,
11+ } )
12+ }
13+ return _client
14+ }
1015
11- export default elasticClient
16+ export default getElasticClient
Original file line number Diff line number Diff line change 11import config from "../config"
22import { indexDataset , indexingToken , queryForIndex } from "@openneuro/search"
3- import { elasticClient } from "./elastic-client"
3+ import { getElasticClient } from "./elastic-client"
44import { ApolloClient , from , InMemoryCache } from "@apollo/client"
55import type { NormalizedCacheObject } from "@apollo/client"
66import { setContext } from "@apollo/client/link/context"
@@ -28,9 +28,13 @@ export const schemaLinkClient = (): ApolloClient<NormalizedCacheObject> => {
2828 } )
2929}
3030
31- const client = schemaLinkClient ( )
31+ let _client : ApolloClient < NormalizedCacheObject > | null = null
32+ function getClient ( ) : ApolloClient < NormalizedCacheObject > {
33+ if ( ! _client ) _client = schemaLinkClient ( )
34+ return _client
35+ }
3236
3337export const reindexDataset = async ( datasetId : string ) : Promise < void > => {
34- const datasetIndexQueryResult = await queryForIndex ( client , datasetId )
35- await indexDataset ( elasticClient , datasetIndexQueryResult . data . dataset )
38+ const datasetIndexQueryResult = await queryForIndex ( getClient ( ) , datasetId )
39+ await indexDataset ( getElasticClient ( ) , datasetIndexQueryResult . data . dataset )
3640}
Original file line number Diff line number Diff line change 11import * as Sentry from "@sentry/node"
2- import { elasticClient } from "../../elasticsearch/elastic-client"
2+ import { getElasticClient } from "../../elasticsearch/elastic-client"
33import { dataset } from "./dataset"
44import Star from "../../models/stars"
55import Subscription from "../../models/subscription"
@@ -17,7 +17,7 @@ const elasticIndex = "datasets"
1717 * @returns {Promise }
1818 */
1919export const removeDatasetSearchDocument = ( id ) =>
20- elasticClient . delete ( { id, index : elasticIndex } )
20+ getElasticClient ( ) . delete ( { id, index : elasticIndex } )
2121
2222/**
2323 * Accepts an array of fields representing the sort order for the search
@@ -101,7 +101,7 @@ export const datasetSearchConnection = async (
101101 // Don't include search_after if parsing fails
102102 }
103103 }
104- await elasticClient . search ( {
104+ await getElasticClient ( ) . search ( {
105105 index : elasticIndex ,
106106 size : first ,
107107 q : `${ q } AND public:true` ,
@@ -270,7 +270,7 @@ export const advancedDatasetSearchConnection = async (
270270 search_after,
271271 }
272272 // Run the query
273- const result = await elasticClient . search ( requestBody )
273+ const result = await getElasticClient ( ) . search ( requestBody )
274274 // Extend with relay connection pagination
275275 return elasticRelayConnection (
276276 result ,
You can’t perform that action at this time.
0 commit comments