Ajout de la base de données MySQL
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
const DB = require('../database');
|
||||
|
||||
const { Projects } = DB;
|
||||
|
||||
module.exports = class ProjectService {
|
||||
static async createProject(data) {
|
||||
return Projects.create(data);
|
||||
}
|
||||
|
||||
static async getProjectsByUserId(userId) {
|
||||
return Projects.findAll({
|
||||
where: { userId },
|
||||
include: [
|
||||
{
|
||||
model: DB.Users,
|
||||
as: 'user',
|
||||
attributes: ['name', 'email'],
|
||||
},
|
||||
],
|
||||
order: [['createdAt', 'DESC']],
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
const DB = require('../database');
|
||||
const sequelize = require('../database/config/sequelize');
|
||||
const associate = require('../database/relationships');
|
||||
|
||||
// Connect to the database and log a message to the console
|
||||
const connectDB = async () => {
|
||||
try {
|
||||
await sequelize.authenticate();
|
||||
console.log('Connection has been established successfully🔥');
|
||||
|
||||
// Synchronize the database with the models without need of dropping the tables
|
||||
await DB.sequelize.sync({
|
||||
force: false,
|
||||
});
|
||||
|
||||
associate(); // Call the associate function to create the relationships between the models
|
||||
} catch (error) {
|
||||
console.error('Unable to connect to the database:', error);
|
||||
process.exit(1); // Exit the process if the connection is not successful
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = connectDB;
|
||||
@@ -0,0 +1,21 @@
|
||||
const DB = require('../database');
|
||||
|
||||
const { Users } = DB;
|
||||
|
||||
module.exports = class UserService {
|
||||
static async createUser(data) {
|
||||
return Users.create(data);
|
||||
}
|
||||
|
||||
static async getAllUsers() {
|
||||
return Users.findAll({
|
||||
order: [['createdAt', 'DESC']],
|
||||
});
|
||||
}
|
||||
|
||||
static async getUserById(id) {
|
||||
return Users.findByPk(id);
|
||||
}
|
||||
}
|
||||
|
||||
//module.exports = userService;
|
||||
Reference in New Issue
Block a user