Skip to content

Instantly share code, notes, and snippets.

@Fauntleroy
Created September 9, 2013 03:12
Show Gist options
  • Save Fauntleroy/6491087 to your computer and use it in GitHub Desktop.
Save Fauntleroy/6491087 to your computer and use it in GitHub Desktop.
Node orm "callback never fired" test case
var orm = require('orm');
var db = orm.connect({
database: 'test',
protocol: 'mysql',
host: '127.0.0.1',
user: 'root',
password: null
});
var pos_cursor = 0;
var Data = db.define( 'Data', {
title: String,
artist: String,
album: String
});
var Item = db.define( 'item', {
poster: String,
updated_at: Date
}, {
autoFetch: true,
hooks: {
beforeSave: function( next ){
this.updated_at = new Date;
next();
}
}
});
var Playlist = db.define( 'playlist', {
title: String
});
Playlist.hasMany( 'items', Item, {
position: Number
}, {
reverse: 'playlists'
});
Item.hasOne( 'data', Data );
db.drop( function(){
db.sync( function( err ){
if( err ) console.log( 'Error syncing db', err );
Playlist.create({
title: 'Tunes'
}, function( err, playlist ){
if( err ) console.log( 'Error creating playlist', err );
Data.create({
title: 'Gotta Knock a Little Harder',
artist: 'Mai Yamane',
album: 'Cowboy Bebop - Tank! THE! BEST!'
}, function( err, data ){
if( err ) console.log( 'Error creating item data', err );
Item.create({
poster: 'Joe'
}, function( err, item1 ){
if( err ) console.log( 'Error creating first item', err );
item1.setData( data, function( err ){
if( err ) console.log( 'Error creating first item association', err );
Item.create({
poster: 'Joe'
}, function( err, item2 ){
if( err ) console.log( 'Error creating second item', err );
item2.setData( data, function( err ){
if( err ) console.log( 'Error creating second item association', err );
Item.create({
poster: 'Joe'
}, function( err, item3 ){
if( err ) console.log( 'Error creating third item', err );
item3.setData( data, function( err ){
if( err ) console.log( 'Error creating third item association', err );
playlist.addItems( item1, { position: pos_cursor++ }, function( err ){
playlist.addItems( item2, { position: pos_cursor++ }, function( err ){
playlist.addItems( item3, { position: pos_cursor++ }, function( err ){
if( err ) console.log( 'Error associating items with playlist', err );
playlist.getItems().find({
position: 0
}).run( function( err, items ){
if( err ) console.log( 'Error getting associated item by id', err );
var item_to_remove = items[0];
playlist.removeItems( item_to_remove, function( err ){
if( err ) console.log( 'Error removing item', err );
playlist.getItems().find({
position: orm.gt( item_to_remove.extra.position )
}).each( function( item ){
item.extra.position = item.extra.position - 1;
}).save( function( err ){
if( err ) console.log( 'Error saving each item in the playlist', err );
else console.log('\\o/');
});
});
});
});
});
});
});
});
});
});
});
});
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment