@@ -4,29 +4,31 @@ import { NodeProofRate } from "../model/staking/NodeProofRate";
44import { EstimatedReward } from "../model/staking/EstimatedRewards" ;
55
66
7- let AppDataSource : DataSource ;
7+ let AppDataSource : DataSource = null ;
88
9- export async function initDatabase ( ) {
9+ export async function initOrGetDatabase ( ) {
1010 const schema = process . env . DB_SCHEMA || "computed" ;
1111
12- AppDataSource = new DataSource ( {
13- type : "postgres" ,
14- host : "postgres" ,
15- port : parseInt ( process . env . DB_PORT ) || 5432 ,
16- username : process . env . DB_USER ,
17- password : process . env . DB_PASS ,
18- database : process . env . DB_NAME ,
19- entities : [ CollectedReward , EstimatedReward , NodeProofRate ] ,
20- // This means we always update the database schema to match the entities
21- synchronize : true ,
22- schema,
23- } ) ;
12+ if ( ! AppDataSource ) {
13+ AppDataSource = new DataSource ( {
14+ type : "postgres" ,
15+ host : "postgres" ,
16+ port : parseInt ( process . env . DB_PORT ) || 5432 ,
17+ username : process . env . DB_USER ,
18+ password : process . env . DB_PASS ,
19+ database : process . env . DB_NAME ,
20+ entities : [ CollectedReward , EstimatedReward , NodeProofRate ] ,
21+ // This means we always update the database schema to match the entities
22+ synchronize : true ,
23+ schema,
24+ } ) ;
25+
26+ await AppDataSource . initialize ( )
27+ . then ( ( ) => {
28+ console . log ( "[✔] Type ORM Data Source initialized. Using schema:" , schema ) ;
29+ } )
30+ . catch ( ( error ) => console . log ( error ) ) ;
31+ }
2432
25- await AppDataSource . initialize ( )
26- . then ( ( ) => {
27- console . log ( "[✔] Type ORM Data Source initialized. Using schema:" , schema ) ;
28- } )
29- . catch ( ( error ) => console . log ( error ) ) ;
30-
3133 return AppDataSource ;
3234}
0 commit comments