Skip to content

Instantly share code, notes, and snippets.

@maxbeatty
Created July 30, 2014 02:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxbeatty/4ca969e05c5a7bc0494f to your computer and use it in GitHub Desktop.
Save maxbeatty/4ca969e05c5a7bc0494f to your computer and use it in GitHub Desktop.
Be careful how you update and delete with Sequelize
require('dotenv').load()
db = require './server/lib/db'
addressRepo = require './server/repositories/addressRepository'
db.sequelize.sync().complete (err) ->
throw err if err
addressRepo.findAll {}, (err, addresses) ->
throw err if err
console.log addresses.length + ' addresses to begin with'
# dangerous update all without search criteria
addressRepo.update { city: 'A' }, {}, (err) ->
throw err if err
addressRepo.findAll {}, (err, addresses) ->
throw err if err
for a in addresses when a.city isnt 'A'
throw new Error 'did not update all'
# dangerous update with undefined
addressRepo.update { city: 'B' }, undefined, (err) ->
throw err if err
addressRepo.findAll {}, (err, addresses) ->
throw err if err
for a in addresses when a.city isnt 'B'
throw new Error 'did not update all'
# dangerous update with null
addressRepo.update { city: 'C' }, null, (err) ->
throw err if err
addressRepo.findAll {}, (err, addresses) ->
throw err if err
for a in addresses when a.city isnt 'C'
throw new Error 'did not update all'
addressRepo.destroy {}, (err) ->
throw err if err
addressRepo.findAll {}, (err, addresses) ->
throw err if err
console.log addresses.length + ' addresses to end with'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment