Skip to content

Conversation

@jpsoaresXy
Copy link

No description provided.

@@ -0,0 +1,13 @@
module.exports = {
HOST: "localhost",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Poderia ter criado variáveis ambientes

app.use(express.urlencoded({ extended: true }));
initRoutes(app);
db.sequelize.sync();
// db.sequelize.sync({ force: true }).then(() => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pode apagar comentários desnecessários

const excelController = require("../controllers/pokemon/excel.controllers");
const upload = require("../middlewares/upload");
let routes = (app) => {
router.post("/upload", upload.single("file"), excelController.upload);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Poderia separar em duas rotas, uma para fazer o upload do excel de pokemon e outra para listar os pokemons do banco.

@@ -0,0 +1,96 @@
module.exports = (sequelize, Sequelize) => {
const Pokemon = sequelize.define("pokemon", {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nome da tabela é interessante colocar sempre no plural

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

O plural de pokemon é pokemon 🤣

type: Sequelize.STRING
},
evolved: {
type: Sequelize.STRING

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Poderia converter os dados numéricos e booleano e salvar com o tipo certo no banco para ocupar menos espaço

return res.status(400).send("Please upload an excel file!");
}
let path =
__basedir + "/resources/static/assets/uploads/" + req.file.filename;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Poderia usar a lib fs pra pegar o caminho do diretório e facilitar essa busca

// skip header
rows.shift();
let pokemons = [];
rows.forEach((row) => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aqui vc poderia usar o map, ele é mais rápido em termos de performance e já retorna um array pra você, não precisando criar um array de dar push.

let pokemons = [];
rows.forEach((row) => {
let pokemon = {
id: row[0],

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ao invés de pegar cada coluna da linha indicando o índice(row[0]) por exemplo, voce poderia desestruturar, assim:

const [id, name, pokedex_number, img_name, generation...] = row

};
pokemons.push(pokemon);
});
Pokemon.bulkCreate(pokemons)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Se a função é assíncrona, poderia então usar o await ao invés do then e catch

}
};
const getPokemons = (req, res) => {
Pokemon.findAll()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Poderia deixar assíncrono e fazer assim:

return await Pokemon.findAll()

});
Pokemon.bulkCreate(pokemons)
.then(() => {
res.status(200).send({

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

o status padrão pra criação de dados é o 201

.catch((error) => {
res.status(500).send({
message: "Fail to import data into database!",
error: error.message,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

o stacktrace do erro fica perdido aqui.

// db.sequelize.sync({ force: true }).then(() => {
// console.log("Drop and re-sync db.");
// });
let port = 8080;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seria interessante deixar isso em uma variável de ambiente

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants