Some sample code for JSDBI

I’ve decided to temporarily name this Class::DBI meets ajax idea JSDBI, for lack of a better name (subject to change when I find something better). I’ve also sat down and translated the synposis example for Class::DBI into an equivalent JSDBI version. Code is after the jump.

Also, crw pointed me at json and json-rpc. These seem to be in the same ballpark, but json is just a data format (like XML), albeit, an easier format to parse than XML, and json-rpc looks purely procedural, rather than object based like Class::DBI, so it looks like there’s still room for improvement in this area. However, both could possibly be implemented as the transport portion of this overall solution, instead of using xml/rest.


// ***** Setup ******
var Music.DBI = Class.create();
Music.DBI.prototype = (new JSDBI()).extend( {
});

var Music.Artist = Class.create();
Music.Artist.prototype = (new Music.DBI()).extend( {
});// basic class setup
Music.Artist.table(’artist’);
Music.Artist.columns([’artistid’, ‘name’]);
Music.Artist.has_many(’cds’, ‘Music::CD’});

var Music.CD = Class.create();
Music.CD.prototype = (new Music.DBI()).extend( {
});

// basic class setup
Music.CD.table(’cd’);
Music.CD.columns([’cdid’, ‘artist’, ‘title’]);

// relationships ?
Music.CD.has_many(’tracks’, ‘Music::Track’});
Music.CD.has_a(’artist’, ‘Music::Artist’});

var Music.Track = Class.create();
Music.Track.prototype = (new Music.DBI()).extend( {
});
// basic class setup
Music.Track.table(’track’);
Music.Track.columns([’trackid’, ‘cd’, ‘position’, ‘title’]);

// ***** Usage ******

// create
var artist = Music.Artist.insert({artistid: 1, name: ‘U2′});
var cd = artist.add_to_cds({cdid: 1, title: ‘October’, year: 1980});

// update
cd.year(1981);
cd.update();

for (var i=0; i
var track = (cd.tracks)[i];
document.write(track.position+’ ‘+track.title);
}

cd = Music::CD.retrieve(1);

cd.delete();

Leave a reply