Skip to content

Commit 8353d8e

Browse files
committed
Merge branch 'main' into release/testnet
2 parents 8c41491 + 41dd78c commit 8353d8e

2 files changed

Lines changed: 24 additions & 22 deletions

File tree

src/db/database.ts

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,31 @@ import { NodeProofRate } from "../model/staking/NodeProofRate";
44
import { 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
}

src/db/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { initDatabase } from "./database";
1+
import { initOrGetDatabase } from "./database";
22
import { processStakingData } from "./procedures/staking";
33

44
export async function processData(blocks: Number[]) {
5-
const db = await initDatabase();
5+
const db = await initOrGetDatabase();
66
await processStakingData(db, blocks);
77
}

0 commit comments

Comments
 (0)