# How to insert into MySQL database and Elastic search same time

**URL:** <https://discuss.elastic.co/t/how-to-insert-into-mysql-database-and-elastic-search-same-time/167107>\
**Category:** Elasticsearch\
**Created:** [February 5, 2019, 11:29am UTC](https://discuss.elastic.co/t/how-to-insert-into-mysql-database-and-elastic-search-same-time/167107 "2019-02-05T11:29:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pathum\_Kalhan](https://avatars.discourse-cdn.com/v4/letter/p/a183cd/32.png) [@Pathum\_Kalhan](https://discuss.elastic.co/u/Pathum_Kalhan)\
**Post date:** [February 5, 2019, 11:29am UTC](https://discuss.elastic.co/t/how-to-insert-into-mysql-database-and-elastic-search-same-time/167107/1 "2019-02-05T11:29:29Z")

</div>

Hi this is my first question in this forum. I am new to elastic search. 🙄

I have an express.js project. ORM is sequlize.js

I have three tables.

1. Category

2. Item

3. City

I have search bar in mobile app where users can search category, item or a city.  
So what I did in my category.controller.js file

```
async function create(req, res, next) {
    const transaction = await db.sequelize.transaction();
    try {

        const data = await db[model_name].create(req.body, {
            transaction
        });
        await client.create({
            index: 'live_search_index',
            type: 'default',
            id: `cat ${data.dataValues.id}`,
            body: {
                title: `${req.body.name}`,
                my_type: `${req.body.my_type}`,
                my_id: data.dataValues.id,
                my_status: true
            }
        });
        await transaction.commit();
        res.sendStatus(200);

    } catch (error) {
        console.log("ERR", error);
        await transaction.rollback();
        res.sendStatus(500);

    }
}

```

But I have 3 controllers and each controller I need to alter 3 functions (create, update, enableDisable). So a lot of work and painful.

**There might be a smart way to do this.**  
You install a plugin and all the CRUD operations of a certain table will be automatically sync to elastic search so that no pain.

Is there a way to do so?  
or else do I need to continue in this way? 🥶

Any help!  
Thanks in advanced.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 5, 2019, 11:29am UTC](https://discuss.elastic.co/t/how-to-insert-into-mysql-database-and-elastic-search-same-time/167107/2 "2019-03-05T11:29:31Z")

</div>

Closed on March 5, 2019, 11:29am UTC

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
