mirror of
https://github.com/pieartsy/PluralFlux.git
synced 2026-04-16 17:45:28 +10:00
* adding to git ignore * making imports named not default to not break all my tests * adjusted setup for memberhelper test --------- Co-authored-by: Aster Fialla <asterfialla@gmail.com>
86 lines
1.8 KiB
JavaScript
86 lines
1.8 KiB
JavaScript
const env = require('dotenv')
|
|
const {Sequelize, DataTypes} = require('sequelize');
|
|
|
|
env.config();
|
|
|
|
const password = process.env.POSTGRES_PASSWORD;
|
|
|
|
if (!password) {
|
|
console.error("Missing POSTGRES_PASSWORD environment variable.");
|
|
process.exit(1);
|
|
}
|
|
|
|
const database = {};
|
|
|
|
const sequelize = new Sequelize('postgres', 'postgres', password, {
|
|
host: 'localhost',
|
|
logging: false,
|
|
dialect: 'postgres'
|
|
});
|
|
|
|
database.sequelize = sequelize;
|
|
database.Sequelize = Sequelize;
|
|
|
|
database.members = sequelize.define('Member', {
|
|
userid: {
|
|
type: DataTypes.STRING,
|
|
allowNull: false,
|
|
},
|
|
name: {
|
|
type: DataTypes.STRING,
|
|
allowNull: false,
|
|
},
|
|
displayname: {
|
|
type: DataTypes.STRING,
|
|
},
|
|
propic: {
|
|
type: DataTypes.STRING,
|
|
},
|
|
proxy: {
|
|
type: DataTypes.STRING,
|
|
}
|
|
});
|
|
|
|
database.systems = sequelize.define('System', {
|
|
userid: {
|
|
type: DataTypes.STRING,
|
|
},
|
|
fronter: {
|
|
type: DataTypes.STRING
|
|
},
|
|
grouptag: {
|
|
type: DataTypes.STRING
|
|
},
|
|
autoproxy: {
|
|
type: DataTypes.BOOLEAN,
|
|
}
|
|
})
|
|
|
|
/**
|
|
* Checks Sequelize database connection.
|
|
*/
|
|
database.check_connection = async function () {
|
|
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);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Syncs Sequelize models.
|
|
*/
|
|
async function syncModels() {
|
|
try {
|
|
await sequelize.sync()
|
|
console.log('Models synced successfully.');
|
|
} catch(err) {
|
|
console.error('Syncing models did not work', err);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
module.exports.database = database; |