-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from adripo/patch-1
Add a Postgres template
- Loading branch information
Showing
7 changed files
with
222 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Sequelize } from 'sequelize'; | ||
|
||
// Initialize Sequelize | ||
const sequelize = new Sequelize('database_name', 'username', 'password', { | ||
host: 'localhost', | ||
dialect: 'postgres', | ||
}); | ||
|
||
// Export the Sequelize instance | ||
export default sequelize; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import express from 'express'; | ||
import db from './db.js'; | ||
import dotenv from 'dotenv' | ||
|
||
dotenv.config() | ||
|
||
const app = express(); | ||
const PORT = process.env.PORT || 3000; | ||
|
||
app.use(express.json()); | ||
|
||
// Test PostgreSQL connection | ||
db.authenticate() | ||
.then(() => { | ||
console.log('Database connection established successfully.'); | ||
}) | ||
.catch((err) => { | ||
console.error('Unable to connect to the database:', err); | ||
}); | ||
|
||
app.get('/', (req, res) => { | ||
return res.send('Hello, Express with PostgreSQL!'); | ||
}); | ||
|
||
// Dummy route to test DB query | ||
app.get('/db-test', async (req, res) => { | ||
try { | ||
const [results, metadata] = await db.query('SELECT NOW()'); | ||
return res.status(200).json({ message: 'DB query successful!', data: results }); | ||
} catch (error) { | ||
return res.status(500).json({ message: 'Error querying the database', error }); | ||
} | ||
}); | ||
|
||
// Start the server | ||
app.listen(PORT, () => { | ||
console.log(`Server is running on http://localhost:${PORT}`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"name": "express_pg", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"scripts": { | ||
"start": "node index.js" | ||
}, | ||
"dependencies": { | ||
"express": "^4.17.1", | ||
"pg": "^8.6.0", | ||
"pg-hstore": "^2.3.4", | ||
"sequelize": "^6.6.5", | ||
"dotenv": "^16.4.1" | ||
} | ||
} | ||
|