🎨 Configure standardJS
This commit is contained in:
@ -1,9 +1,9 @@
|
||||
const Sequelize = require('sequelize');
|
||||
const { DATABASE } = require('../config/config');
|
||||
const Sequelize = require('sequelize')
|
||||
const { DATABASE } = require('../config/config')
|
||||
|
||||
const sequelize = new Sequelize(DATABASE.name, DATABASE.user, DATABASE.password, {
|
||||
dialect: 'mysql',
|
||||
host: DATABASE.host
|
||||
});
|
||||
dialect: 'mysql',
|
||||
host: DATABASE.host
|
||||
})
|
||||
|
||||
module.exports = sequelize;
|
||||
module.exports = sequelize
|
||||
|
@ -1,19 +1,19 @@
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
|
||||
function deleteFilesNameStartWith(pattern, dirPath, callback) {
|
||||
fs.readdir(path.resolve(dirPath), (_error, fileNames) => {
|
||||
for (const name of fileNames) {
|
||||
const splitedName = name.split('.');
|
||||
if (splitedName.length === 2) {
|
||||
const fileName = splitedName[0];
|
||||
if (fileName === pattern && name !== 'default.png') {
|
||||
return fs.unlink(path.join(dirPath, name), callback);
|
||||
}
|
||||
}
|
||||
function deleteFilesNameStartWith (pattern, dirPath, callback) {
|
||||
fs.readdir(path.resolve(dirPath), (_error, fileNames) => {
|
||||
for (const name of fileNames) {
|
||||
const splitedName = name.split('.')
|
||||
if (splitedName.length === 2) {
|
||||
const fileName = splitedName[0]
|
||||
if (fileName === pattern && name !== 'default.png') {
|
||||
return fs.unlink(path.join(dirPath, name), callback)
|
||||
}
|
||||
return callback();
|
||||
});
|
||||
}
|
||||
}
|
||||
return callback()
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = deleteFilesNameStartWith;
|
||||
module.exports = deleteFilesNameStartWith
|
||||
|
@ -1,7 +1,7 @@
|
||||
function errorHandling(next, { statusCode, message }) {
|
||||
const error = new Error(message);
|
||||
error.statusCode = statusCode;
|
||||
next(error);
|
||||
function errorHandling (next, { statusCode, message }) {
|
||||
const error = new Error(message)
|
||||
error.statusCode = statusCode
|
||||
next(error)
|
||||
}
|
||||
|
||||
module.exports = errorHandling;
|
||||
module.exports = errorHandling
|
||||
|
@ -1,10 +1,10 @@
|
||||
const errorHandling = require('../utils/errorHandling');
|
||||
const { serverError } = require('../config/errors');
|
||||
const helperQueryNumber = require('../utils/helperQueryNumber');
|
||||
const errorHandling = require('../utils/errorHandling')
|
||||
const { serverError } = require('../config/errors')
|
||||
const helperQueryNumber = require('../utils/helperQueryNumber')
|
||||
|
||||
const DEFAULT_OPTIONS = {
|
||||
order: [['createdAt', 'DESC']]
|
||||
};
|
||||
order: [['createdAt', 'DESC']]
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Permet de faire un système de pagination sur un model Sequelize
|
||||
@ -12,23 +12,23 @@ const DEFAULT_OPTIONS = {
|
||||
* @param {*} Model Model Sequelize
|
||||
* @param {Object} options Options avec clause where etc.
|
||||
*/
|
||||
async function getPagesHelper({ req, res, next }, Model, options = DEFAULT_OPTIONS) {
|
||||
const page = helperQueryNumber(req.query.page, 1);
|
||||
const limit = helperQueryNumber(req.query.limit, 10);
|
||||
const offset = (page - 1) * limit;
|
||||
try {
|
||||
const result = await Model.findAndCountAll({
|
||||
limit,
|
||||
offset,
|
||||
...options
|
||||
});
|
||||
const { count, rows } = result;
|
||||
const hasMore = (page * limit) < count;
|
||||
return res.status(200).json({ totalItems: count, hasMore, rows });
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return errorHandling(next, serverError);
|
||||
}
|
||||
async function getPagesHelper ({ req, res, next }, Model, options = DEFAULT_OPTIONS) {
|
||||
const page = helperQueryNumber(req.query.page, 1)
|
||||
const limit = helperQueryNumber(req.query.limit, 10)
|
||||
const offset = (page - 1) * limit
|
||||
try {
|
||||
const result = await Model.findAndCountAll({
|
||||
limit,
|
||||
offset,
|
||||
...options
|
||||
})
|
||||
const { count, rows } = result
|
||||
const hasMore = (page * limit) < count
|
||||
return res.status(200).json({ totalItems: count, hasMore, rows })
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
return errorHandling(next, serverError)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = getPagesHelper;
|
||||
module.exports = getPagesHelper
|
||||
|
@ -1,6 +1,6 @@
|
||||
function helperQueryNumber(value, defaultValue) {
|
||||
if (value && !isNaN(value)) return parseInt(value);
|
||||
return defaultValue;
|
||||
function helperQueryNumber (value, defaultValue) {
|
||||
if (value && !isNaN(value)) return parseInt(value)
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
module.exports = helperQueryNumber;
|
||||
module.exports = helperQueryNumber
|
||||
|
Reference in New Issue
Block a user