/
rayray360180
/
indicators-pivot-learning
Обзор
Документация
Войти
/
rayray360180
/
indicators-pivot-learning
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
scripts/setup_db.js
46 строк
1 KB
rayra
Initial commit: indicators pivot dashboard with PostgreSQL and DevExtreme
15 июл 2026, 09:22
15 июл 2026, 09:22
ce382c6
Код
Авторство
О чём код?
/** * Create indicators_db (if needed) and apply 01_schema, 02_seed, 03_views. * Uses .env credentials. */ require('dotenv').config({ path: require('path').join(__dirname, '..', '.env') }); const fs = require('fs'); const path = require('path'); const { Client } = require('pg'); const base = { host: process.env.PGHOST || 'localhost', port: Number(process.env.PGPORT || 5432), user: process.env.PGUSER || 'postgres', password: process.env.PGPASSWORD, }; async function main() { const admin = new Client({ ...base, database: 'postgres' }); await admin.connect(); const exists = await admin.query( "SELECT 1 FROM pg_database WHERE datname = 'indicators_db'" ); if (exists.rowCount === 0) { await admin.query('CREATE DATABASE indicators_db'); console.log('created indicators_db'); } else { console.log('indicators_db already exists'); } await admin.end(); const db = new Client({ ...base, database: process.env.PGDATABASE || 'indicators_db' }); await db.connect(); for (const f of ['01_schema.sql', '02_seed.sql', '03_views.sql']) { const sql = fs.readFileSync(path.join(__dirname, '..', 'db', f), 'utf8'); await db.query(sql); console.log('applied', f); } const c = await db.query('SELECT COUNT(*)::int AS n FROM fact_values'); console.log('fact_values', c.rows[0].n); await db.end(); } main().catch((err) => { console.error(err); process.exit(1); });