-
Notifications
You must be signed in to change notification settings - Fork 2
/
seed.js
44 lines (38 loc) · 1.14 KB
/
seed.js
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
require('dotenv').config(); // Load environment variables from .env file
const { Pool } = require('pg');
// Create a new pool using environment variables
const pool = new Pool({
user: process.env.POSTGRES_USER,
host: process.env.POSTGRES_DB_HOST,
database: process.env.POSTGRES_DB,
password: process.env.POSTGRES_PASSWORD,
port: process.env.POSTGRES_PORT,
});
const seedData = async () => {
try {
// Drop the table if it already exists (optional)
await pool.query(`DROP TABLE IF EXISTS todos;`);
// Create the table with the correct structure
await pool.query(`
CREATE TABLE todos (
id SERIAL PRIMARY KEY,
task VARCHAR(255) NOT NULL,
completed BOOLEAN DEFAULT false
);
`);
// Insert seed data
await pool.query(`
INSERT INTO todos (task, completed) VALUES
('Buy groceries', false),
('Finish blog post', false),
('Clean the house', false);
`);
console.log('Database seeded successfully!');
} catch (err) {
console.error('Error seeding the database', err);
} finally {
pool.end();
}
};
// Call the seedData function to run the script
seedData();