Do not create table in API, use the one I have

Asked

Viewed 185 times

0

I made an api on nodejs that the models is:

    'use strict';
module.exports = (sequelize, DataTypes) => {

  const RESP_CC = sequelize.define('RESP_CC', {
    RCC_RESP: {
      type: DataTypes.STRING(6),
      allowNull: false, //se aceita nulo
      autoIncrement: false
    },
    RCC_CCT: {
      type: DataTypes.STRING(15),
      allowNull: false, //se aceita nulo
      autoIncrement: false
    },
    R_E_C_N_O_:{
      type: DataTypes.INTEGER,
      allowNull: false, //se aceita nulo
      autoIncrement: true,
      primaryKey: true
    },
    D_E_L_E_T_: {
      type: DataTypes.STRING(1),
      allowNull: false, //se aceita nulo
      autoIncrement: false
    }

  }, {});
  RESP_CC.associate = function (models) {
    // associations can be defined here
  };
  return RESP_CC;
};

CONTROLLER:

class RESP_CC {

    async list(db) {

       return await db.RESP_CC.findAll({
        where: {
            RCC_CCT: '130103'
           }
       })

    }
}


export default new RESP_CC();

ROUTER:

module.exports = (routes, db) => {

    routes.get('/users', async (req, res) => {
        const user = await User.list(db);
        if (user) {
            res.status(200).send(user);
        } else {
            res.status(404).json({ message: 'No Data Found' });
        }
    });
};

And she created a table, but I need to use the one that already exists in the bank, there is some way to do it without having to create a table in the comic?

  • Hi Maria, do you happen to have any calls for a Sync method? In my example I use like this: sequelize.Sync() If you are using, just remove. The Sync method causes the creation of tables to be performed automatically. http://docs.sequelizejs.com/class/lib/sequelize.js~Sequelize.html

  • i do not want to create a table, I just want to make a query in the table that already exists in my bank

  • As Guilherme said, you must have somewhere in your application the code snippet similar to this sequelize.sync(), just remove it it will not create the table and just map to what already exists.

  • @Justcase updated the question. sequelize.Sync() does not, but has sequelize.define

  • @Guilhermenass then in place in Sync() I leave what? is in my file www db.sequelize.Sync() . then(() => { server.Listen(port); server.on('error', onerror); server.on('Listening', onListening); });

  • @Maria I imagine that this server.Lieutenant() serves to increase the application. You can remove the code from the sequelize.Sync() and start the application this way: app.Listen(process.env.PORT || 4000, Function(){ console.log('server is up') }) The app variable can be set like this if you are using Express: var app = express()

  • @Guilhermenass I do this to connect in bd tbm, but before you answer I made the following change: db.sequelize.transaction() 
 .then(() => { 
 server.listen(port); 
 server.on('error', onError); 
 server.on('listening', onListening);
}) and it worked, it is no longer creating table. So it is also correct?

  • @Maria I never used the transaction, but from what I’ve seen in the documentation it seems to fit. "The transaction object is used to identify a running transaction. It is created by calling Sequelize.transaction(). To execute a query on a transaction, you must pass the transaction on the options object." Source: http://docs.sequelizejs.com/class/lib/transaction.js~Transaction.html

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.