Skip to content

Instantly share code, notes, and snippets.

@tobsn
Created May 5, 2012 10:19
Show Gist options
  • Save tobsn/2601361 to your computer and use it in GitHub Desktop.
Save tobsn/2601361 to your computer and use it in GitHub Desktop.
capping taffydb
TAFFY.extend( 'capped', function( i, runInsert, runRemove ) {
var c = this.getDBI().cap || false;
if( c && ( c < ( this.count() + 1 ) ) ) {
while( c < ( this.count() + 1 ) ) {
this.getDBI().remove( this.context().results[0].___id );
this.context({
run: null
});
this.getDBI().removeCommit( runRemove );
}
}
return this.getDBI().insert( i, runInsert );
});
var db = TAFFY();
db().getDBI().cap = 20;
for(var i = 0; i < 10; i++ ) {
db.insert({field:i});
}
for(var i = 10; i < 40; i++ ) {
db().capped({field:i});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment