Created
July 30, 2014 02:20
-
-
Save maxbeatty/4ca969e05c5a7bc0494f to your computer and use it in GitHub Desktop.
Be careful how you update and delete with Sequelize
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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