-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit-db.js
More file actions
24 lines (22 loc) · 829 Bytes
/
init-db.js
File metadata and controls
24 lines (22 loc) · 829 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import { readFile } from 'fs/promises';
import { query } from './db.js';
import 'dotenv/config';
export const initDB = async () => {
try {
const schema = await readFile(new URL('./schema.sql', import.meta.url), 'utf-8');
await query(schema);
// Insert initial block value
const startBlock = parseInt(process.env.START_BLOCK ?? '0');
await query(`
INSERT INTO last_processed_block (id, block_num)
VALUES (1, $1)
ON CONFLICT (id) DO UPDATE
SET block_num = EXCLUDED.block_num,
processed_at = NOW();
`, [startBlock]);
console.log(`✅ Database initialized successfully at block ${startBlock}`);
} catch (err) {
console.error('❌ Error initializing DB:', err);
process.exit(1);
}
};