Skip to content

Commit

Permalink
Create Thunder database if not exists
Browse files Browse the repository at this point in the history
  • Loading branch information
micahmo committed May 10, 2024
1 parent c41afe9 commit 758bf39
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
2 changes: 1 addition & 1 deletion example.env
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ POSTGRES_USER=user
POSTGRES_PASSWORD=password
POSTGRES_HOSTNAME=postgres
POSTGRES_PORT=5432
POSTGRES_DATABASE=thunder-database
POSTGRES_DATABASE=thunder_database

APNS_KEY_ID=key_id
APNS_TEAM_ID=team_id
Expand Down
27 changes: 23 additions & 4 deletions src/database/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,28 @@ import { Sequelize } from "sequelize";
import dotenv from "dotenv";
dotenv.config();

// Configure database
const sequelize = new Sequelize(
`postgres://${process.env.POSTGRES_USER ?? "user"}:${process.env.POSTGRES_PASSWORD ?? "password"}@${process.env.POSTGRES_HOSTNAME ?? "postgres"}:${process.env.POSTGRES_PORT ?? "5432"}/${process.env.POSTGRES_DATABASE ?? "thunder-database"}`
// Connect to the default Postgres db
var sequelize = new Sequelize(
`postgres://${process.env.POSTGRES_USER ?? "user"}:${process.env.POSTGRES_PASSWORD ?? "password"}@${process.env.POSTGRES_HOSTNAME ?? "postgres"}:${process.env.POSTGRES_PORT ?? "5432"}/postgres`
);

export default sequelize;
// Connect to a default database
async function ensureThunderDatabaseExists() {
const thunderDatabase = process.env.POSTGRES_DATABASE ?? "thunder_database";

// Check if the Thunder database exists
const result = await sequelize.query(`SELECT FROM pg_database WHERE datname = '${thunderDatabase}'`);
const exists = result[0].length > 0;

// Create if not
if (!exists) {
await sequelize.query(`CREATE DATABASE ${thunderDatabase}`);
}

// Connect to the Thunder database
sequelize = new Sequelize(
`postgres://${process.env.POSTGRES_USER ?? "user"}:${process.env.POSTGRES_PASSWORD ?? "password"}@${process.env.POSTGRES_HOSTNAME ?? "postgres"}:${process.env.POSTGRES_PORT ?? "5432"}/${thunderDatabase}`
);
}

export { ensureThunderDatabaseExists, sequelize };
2 changes: 1 addition & 1 deletion src/database/models/account_notification.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DataTypes, Model } from "sequelize";

import sequelize from "../database";
import { sequelize } from "../database";

class AccountNotification extends Model {}

Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from "./types/notification_request";

// Database
import sequelize from "./database/database";
import { ensureThunderDatabaseExists, sequelize } from "./database/database";

// Helper functions
import { addAccountNotification } from "./notifications/notifications";
Expand Down Expand Up @@ -70,6 +70,7 @@ app.delete("/notifications", (req, res) => {
app.listen(port, async () => {
console.log(`Server is running on http://localhost:${port}`);

await ensureThunderDatabaseExists();
await sequelize.sync({ force: DEBUG_MODE });
console.log("All models were synchronized successfully.");
});

0 comments on commit 758bf39

Please sign in to comment.