2026-02-28 14:39:32 -05:00
|
|
|
const env = require('dotenv')
|
|
|
|
|
const {Sequelize, DataTypes} = require('sequelize');
|
2026-02-16 11:07:43 -05:00
|
|
|
|
|
|
|
|
env.config();
|
2026-02-13 18:20:29 -05:00
|
|
|
|
|
|
|
|
const password = process.env.POSTGRES_PASSWORD;
|
|
|
|
|
|
|
|
|
|
if (!password) {
|
2026-02-25 19:30:39 -05:00
|
|
|
console.error("Missing POSTGRES_PASSWORD environment variable.");
|
2026-02-13 18:20:29 -05:00
|
|
|
process.exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-28 14:39:32 -05:00
|
|
|
const database = {};
|
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-28 14:39:32 -05:00
|
|
|
database.sequelize = sequelize;
|
|
|
|
|
database.Sequelize = Sequelize;
|
2026-02-13 18:20:29 -05:00
|
|
|
|
2026-02-28 14:39:32 -05:00
|
|
|
database.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-28 14:39:32 -05:00
|
|
|
database.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-28 14:39:32 -05:00
|
|
|
database.check_connection = async function () {
|
2026-02-25 19:30:39 -05:00
|
|
|
try {
|
|
|
|
|
await sequelize.authenticate();
|
|
|
|
|
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-13 18:20:29 -05:00
|
|
|
}
|
|
|
|
|
|
2026-02-14 18:16:10 -05:00
|
|
|
/**
|
|
|
|
|
* Syncs Sequelize models.
|
|
|
|
|
*/
|
2026-02-13 18:20:29 -05:00
|
|
|
async function syncModels() {
|
2026-02-25 19:30:39 -05:00
|
|
|
try {
|
|
|
|
|
await sequelize.sync()
|
2026-02-13 18:20:29 -05:00
|
|
|
console.log('Models synced successfully.');
|
2026-02-25 19:30:39 -05:00
|
|
|
} catch(err) {
|
2026-02-13 18:20:29 -05:00
|
|
|
console.error('Syncing models did not work', err);
|
|
|
|
|
process.exit(1);
|
2026-02-25 19:30:39 -05:00
|
|
|
}
|
2026-02-13 18:20:29 -05:00
|
|
|
}
|
|
|
|
|
|
2026-02-28 15:28:27 -05:00
|
|
|
module.exports.database = database;
|