2026-02-13 18:20:29 -05:00
|
|
|
import {DataTypes, Sequelize} from 'sequelize';
|
2026-02-16 11:07:43 -05:00
|
|
|
import * as env from 'dotenv';
|
|
|
|
|
|
|
|
|
|
env.config();
|
2026-02-13 18:20:29 -05:00
|
|
|
|
|
|
|
|
const password = process.env.POSTGRES_PASSWORD;
|
|
|
|
|
|
|
|
|
|
if (!password) {
|
|
|
|
|
console.error("Missing POSTGRES_PWD environment variable.");
|
|
|
|
|
process.exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-19 21:45:10 -05:00
|
|
|
const db = {};
|
2026-02-13 18:20:29 -05:00
|
|
|
|
|
|
|
|
const sequelize = new Sequelize('postgres', 'postgres', password, {
|
|
|
|
|
host: 'localhost',
|
2026-02-15 16:21:00 -05:00
|
|
|
logging: false,
|
2026-02-13 18:20:29 -05:00
|
|
|
dialect: 'postgres'
|
|
|
|
|
});
|
|
|
|
|
|
2026-02-19 21:45:10 -05:00
|
|
|
db.sequelize = sequelize;
|
|
|
|
|
db.Sequelize = Sequelize;
|
2026-02-13 18:20:29 -05:00
|
|
|
|
2026-02-19 21:45:10 -05:00
|
|
|
db.members = sequelize.define('Member', {
|
2026-02-13 18:20:29 -05:00
|
|
|
userid: {
|
|
|
|
|
type: DataTypes.STRING,
|
|
|
|
|
allowNull: false,
|
|
|
|
|
},
|
|
|
|
|
name: {
|
|
|
|
|
type: DataTypes.STRING,
|
|
|
|
|
allowNull: false,
|
|
|
|
|
},
|
|
|
|
|
displayname: {
|
|
|
|
|
type: DataTypes.STRING,
|
|
|
|
|
},
|
|
|
|
|
propic: {
|
|
|
|
|
type: DataTypes.STRING,
|
|
|
|
|
},
|
|
|
|
|
proxy: {
|
|
|
|
|
type: DataTypes.STRING,
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2026-02-19 21:45:10 -05:00
|
|
|
db.systems = sequelize.define('System', {
|
2026-02-16 11:09:03 -05:00
|
|
|
userid: {
|
|
|
|
|
type: DataTypes.STRING,
|
|
|
|
|
},
|
|
|
|
|
fronter: {
|
|
|
|
|
type: DataTypes.STRING
|
|
|
|
|
},
|
|
|
|
|
grouptag: {
|
|
|
|
|
type: DataTypes.STRING
|
|
|
|
|
},
|
|
|
|
|
autoproxy: {
|
|
|
|
|
type: DataTypes.BOOLEAN,
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
2026-02-14 18:16:10 -05:00
|
|
|
/**
|
|
|
|
|
* Checks Sequelize database connection.
|
|
|
|
|
*/
|
2026-02-19 21:45:10 -05:00
|
|
|
db.check_connection = async function() {
|
|
|
|
|
await sequelize.authenticate().then(async () => {
|
2026-02-13 18:20:29 -05:00
|
|
|
console.log('Connection has been established successfully.');
|
|
|
|
|
await syncModels();
|
|
|
|
|
}).catch(err => {
|
|
|
|
|
console.error('Unable to connect to the database:', err);
|
|
|
|
|
process.exit(1);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-14 18:16:10 -05:00
|
|
|
/**
|
|
|
|
|
* Syncs Sequelize models.
|
|
|
|
|
*/
|
2026-02-13 18:20:29 -05:00
|
|
|
async function syncModels() {
|
2026-02-14 12:55:36 -05:00
|
|
|
await sequelize.sync().then(() => {
|
2026-02-13 18:20:29 -05:00
|
|
|
console.log('Models synced successfully.');
|
|
|
|
|
}).catch((err) => {
|
|
|
|
|
console.error('Syncing models did not work', err);
|
|
|
|
|
process.exit(1);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-19 21:45:10 -05:00
|
|
|
export const database = db;
|