-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
29 lines (25 loc) · 826 Bytes
/
cli.js
File metadata and controls
29 lines (25 loc) · 826 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
25
26
27
28
29
require('dotenv').config()
const { Sequelize, QueryTypes } = require('sequelize')
const { addBlogs, addTestUser } = require('./utils/initialcontent.js')
const sequelize = new Sequelize(process.env.DATABASE_URL, {
dialect: 'postgres',
dialectOptions: {
// Your pg options here
}
})
const main = async () => {
try {
await sequelize.authenticate()
await addTestUser()
await addBlogs()
const blogs = await sequelize.query("SELECT author, title, likes FROM blogs", { type: QueryTypes.SELECT })
for (var i = 0; i < blogs.length; i++) {
const blog = `${i + 1} - ${blogs[i].author}: ${blogs[i].title}, likes: ${blogs[i].likes}`
console.log(blog);
}
sequelize.close()
} catch (error) {
console.error('Unable to connect to the database:', error)
}
}
main()