0

I'm learning Node and I used Spring Boot before where I used Hibernate ORM which enabled me to map objects like

{ 
 name:"John", 
 salary:50000, 
 address:{ 
  city: "Houston",
  state: "Texas"
 } 
}

into 2 separate tables for example Employee and Address and insert it. I wonder how it can be done in NodeJS and whats the best practice to map such data.

1 Answer 1

1

If you want to keep the Hibernate ORM habit on NodeJS, you can use bookshelfjs or sequelize. You have to make an insert process yourself regarding the json you shared.

var data = { 
 name:"John", 
 salary:50000, 
 address:{ 
  city: "Houston",
  state: "Texas"
 } 
};

connection.query('INSERT INTO Employee (NAME, SALARY) VALUES(?,?)', [data.name, data.salary], function (err, result, fields) {
    if (err) throw err;

    var lastInsertID = result.insertId;
    connection.query('INSERT INTO ADDRESS (CUSTOMER_ID, CITY, STATE) VALUES(?,?,?)', [lastInsertID, data.address.name, data.address.state], function (err, result, fields) {
        if (err) throw err;

    });
});

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.