Querying

A few simple queries are shown below:

  1. // Find all users
  2. User.findAll().then(users => {
  3. console.log("All users:", JSON.stringify(users, null, 4));
  4. });
  5. // Create a new user
  6. User.create({ firstName: "Jane", lastName: "Doe" }).then(jane => {
  7. console.log("Jane's auto-generated ID:", jane.id);
  8. });
  9. // Delete everyone named "Jane"
  10. User.destroy({
  11. where: {
  12. firstName: "Jane"
  13. }
  14. }).then(() => {
  15. console.log("Done");
  16. });
  17. // Change everyone without a last name to "Doe"
  18. User.update({ lastName: "Doe" }, {
  19. where: {
  20. lastName: null
  21. }
  22. }).then(() => {
  23. console.log("Done");
  24. });

Sequelize has a lot of options for querying. You will learn more about those in the next tutorials. It is also possible to make raw SQL queries, if you really need them.