Connection between Node and Postgresql

Asked

Viewed 92 times

0

I am creating a Node API for a personal project with Postgresql connection. Is there any way to migrate (so that records are automatically created in the database) from created models? I know how this can be done using Mongoose (Connection to Mongodb) with:

const mongoose = require('mongoose');
const Product = mongoose.model('Product');

module.exports = {  

    async store(req, res) {

        const products = await Product.create(req.body);

        return res.json(products);
    },
}

But in this project the entities of my bank have relationships. Any ideas? Thanks for the help!

  • Sequelize should solve your problem, just map the tables to the models, then just create the Eds and migrates, for I am one of the best ORM, I use for a long time.

2 answers

0

To connect to the Postgres database, you can use Sequelize.

Documentation of the sequelize

To create a new Migration you must use the command:

yarn sequelize migration:create --name=nome-migration

And to create it in the bank just run:

yarn sequelize db:migrate

Remember that in order to use the commands to create the Migrations, you must first configure Sequelize in your project.

Article for you to configure Sequelize in your project

0


You can use sequelize for relational databases like Postgres or Mysql.

sudo yarn add sequelize

in this link has the documentation for the settings

then just give a sudo yarn sequelize db:create

I hope I helped :D

Browser other questions tagged

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