-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_db.js
More file actions
131 lines (122 loc) · 3.54 KB
/
Copy pathsetup_db.js
File metadata and controls
131 lines (122 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
require('dotenv').config();
const readline = require('readline');
const bcrypt = require('bcrypt');
const { Pool } = require('pg');
const pool = new Pool({
user: process.env.PG_USER,
host: process.env.PG_HOST,
database: process.env.PG_DATABASE,
password: process.env.PG_PASSWORD,
port: parseInt(process.env.PG_PORT, 10),
});
const users = [
{
username: 'admin_user',
password: 's0aring42',
role: 'admin',
email: 'admin@example.com',
first_name: 'Admin',
last_name: 'User',
},
{
username: 'dev_user',
password: 'soardev',
role: 'developer',
email: 'dev@example.com',
first_name: 'Dev',
last_name: 'User',
},
{
username: 'read_user',
password: 'soaruser',
// role intentionally left out to test default
email: 'reader@example.com',
first_name: 'Read',
last_name: 'User',
},
];
// Function to ask for user input in console
function askQuestion(query) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
return new Promise((resolve) =>
rl.question(query, (ans) => {
rl.close();
resolve(ans.trim());
})
);
}
async function dropTablesIfExist() {
const tables = ['users', 'forms', 'configurations'];
for (const table of tables) {
const answer = await askQuestion(`Table "${table}" exists. Delete? (y/n): `);
if (answer.toLowerCase() === 'y') {
await pool.query(`DROP TABLE IF EXISTS ${table} CASCADE;`);
console.log(`Dropped table: ${table}`);
} else {
console.log(`Skipped dropping table: ${table}`);
}
}
}
async function createTables() {
await pool.query(`
CREATE TABLE IF NOT EXISTS configurations (
id SERIAL PRIMARY KEY,
ph_auth_token VARCHAR,
server VARCHAR,
ssl_verification BOOLEAN,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS forms (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
label VARCHAR(255) NOT NULL,
tags TEXT,
elements JSONB,
xml_data TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
first_name VARCHAR(255),
last_name VARCHAR(255),
email VARCHAR(255) UNIQUE NOT NULL,
username VARCHAR(255) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL,
role VARCHAR(50) NOT NULL DEFAULT 'read-only',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
`);
console.log('Tables created or verified successfully.');
}
async function upsertUsers() {
for (const user of users) {
const hash = await bcrypt.hash(user.password, 10);
const role = user.role && user.role.trim() ? user.role : 'read-only';
await pool.query(
`INSERT INTO users (username, password, role, email, first_name, last_name)
VALUES ($1, $2, $3, $4, $5, $6)
ON CONFLICT (username) DO UPDATE SET password = EXCLUDED.password, role = EXCLUDED.role;`,
[user.username, hash, role, user.email, user.first_name, user.last_name]
);
console.log(`Inserted or updated user: ${user.username} with role: ${role}`);
}
}
(async () => {
try {
// Check if tables exist, then ask to drop
await dropTablesIfExist();
// Create tables fresh
await createTables();
// Insert or update default users
await upsertUsers();
} catch (err) {
console.error('Error during setup:', err);
} finally {
await pool.end();
}
})();