proxy-oidcv2/scripts/seedDb.js
2025-12-03 21:36:55 +01:00

76 lines
2.0 KiB
JavaScript

import { getDatabase } from '../src/db.js';
import { initDatabase } from '../src/db.js';
import { v4 as uuidv4 } from 'uuid';
import config from '../src/config.js';
import path from 'path';
import fs from 'fs';
async function main() {
try {
console.log('🚀 Initializing and seeding database...');
// Ensure db directory exists
const dbDir = path.dirname(config.db.path);
if (!fs.existsSync(dbDir)) {
fs.mkdirSync(dbDir, { recursive: true });
}
await initDatabase(config.db.path);
const db = await getDatabase();
// Seed sample services
const services = [
{
id: uuidv4(),
name: 'API Service',
path: '/api',
target_url: 'http://localhost:3001',
require_auth: 1,
description: 'Internal API service',
},
{
id: uuidv4(),
name: 'Admin Dashboard',
path: '/admin-app',
target_url: 'http://localhost:4200',
require_auth: 1,
description: 'Admin dashboard application',
},
{
id: uuidv4(),
name: 'Public Service',
path: '/public',
target_url: 'http://localhost:5000',
require_auth: 0,
description: 'Public accessible service',
},
];
for (const service of services) {
const existing = await db.get(
'SELECT id FROM services WHERE name = ?',
[service.name]
);
if (!existing) {
await db.run(
`INSERT INTO services (id, name, path, target_url, require_auth, description)
VALUES (?, ?, ?, ?, ?, ?)`,
[service.id, service.name, service.path, service.target_url, service.require_auth, service.description]
);
console.log(`✓ Created service: ${service.name}`);
} else {
console.log(`⊙ Service already exists: ${service.name}`);
}
}
console.log('✓ Database seeded successfully!');
process.exit(0);
} catch (error) {
console.error('✗ Database seeding failed:', error);
process.exit(1);
}
}
main();