Skip to content

Instantly share code, notes, and snippets.

@timoxley
Created October 15, 2011 02:08
Show Gist options
  • Select an option

  • Save timoxley/1288897 to your computer and use it in GitHub Desktop.

Select an option

Save timoxley/1288897 to your computer and use it in GitHub Desktop.
spine_riak.js
var Config = require('../config')
var db = require('riak-js').getClient({port: Config.riakPort, debug: Config.debug})
var Spine = require('./spine')
var Model = Spine.Model
var Singleton = Spine.Class.create()
Singleton.include({
model: function() {
this.record.constructor
},
reload: function() {
//TODO
},
create: function () {
var data = JSON.stringify(this.record)
var self = this
self = this
db.save(this.model.className, data.id, data, function(err, buffer, data) {self.response(err, buffer, data)})
},
update: function(params) {
var data = JSON.stringify(this.record)
var self = this
self = this
db.update(this.model.className, data.id, data, function(err, buffer, data) {self.response(err, buffer, data)})
},
destroy: function(params) {
var data = JSON.stringify(this.record)
self = this
db.remove(this.model.className, data.id, function(err, buffer, data) {self.response(err, buffer, data)})
},
response: function(err, buffer, data) {
this.record.bind('riakFailure', function() {console.log("riakFailure triggered")})
this.record.bind('riakSuccess', function() {console.log("riakSuccess triggered" + arguments)})
this.record.trigger('riakSuccess', {some: 'data'})
if (err != null) {
this.record.trigger('riakFailure', err, this.record)
}
this.record.trigger('riakSuccess', data)
}
})
Model.Riak = {
db: db,
extended: function() {
this.change(this.doThing)
this.extend(Extend)
this.include({
riak: function() {
var a = Singleton.init()
a.record = this
a.model = this.constructor
return a
}
});
},
doThing: function(record, type) {
record.riak()[type]()
}
}
var Spine = require('../../lib/spine')
require('../../lib/spine-riak')
require('sugar')
TestServer = require('riak-js').TestServer
var path = require('path')
var Config = require('../../config')
var testCase = require('nodeunit').testCase
var tempDir = path.normalize(process.cwd() + '/.riaktest')
var ts = new TestServer({binDir: Config.riakBinDir, tempDir: tempDir})
var tests = undefined
var db = Spine.Model.Riak.db
tests = {
init: {
'setup test server': function (test) {
ts.prepare(function() {
console.log("Riak test server prepared...")
ts.start(function() {
console.log('Riak test server started...')
test.ok(db)
db.ping(function(err, data, meta) {
console.log('Riak test server working!')
test.done()
})
})
})
}
},
run: testCase({
setUp: function(callback) {
callback()
},
tearDown: function(callback) {
ts.clear(function() {
callback()
})
},
'is sane': function(test) {
test.expect(1)
test.ok(Spine.Model.Riak)
var Test = Spine.Model.sub()
Test.configure('Test', 'a')
Test.extend(Spine.Model.Riak)
test.ok(Test.saveRiak)
test.ok(Test.loadRiak)
test.done()
},
'saves models to bucket': function(test) {
var TestModel = Spine.Model.setup('TestModel', ['thing'])
TestModel.extend(Spine.Model.Riak)
var model = TestModel.init({thing: 'stuff'})
console.log('model')
console.log(model)
model.bind('riakSuccess', function(data) {
test.ok(data)
db.get(model.className, model.id, function(err) {
test.ok(!err)
test.done()
})
})
model.bind('riakFailure', function(err, object) {
console.log(err)
console.log(object)
test.fail()
})
console.log(JSON.stringify(model.bind))
test.ok(!Object.isArray(model.save()))
},
// 'removes item from bucket': function(test) {
// var TestModel = Spine.Model.setup('TestModel', ['thing'])
// TestModel.extend(Spine.Model.Riak)
// var model = TestModel.init({thing: 'stuff'})
// model.save()
// model.bind('riakSuccess', function(data) {
// console.log('success')
// test.fail()
// })
// model.bind('riakFailure', function(err, object) {
// test.ok(err)
// test.ok(object)
// console.log(err)
// console.log(object)
// test.done()
// })
// model.destroy()
// }
}),
shutdown: {
'shutdown test server': function(test) {
ts.stop(function(){
test.done()
})
},
'delete test server files': function(test) {
// Paranoid checking to make sure we don't accidentally rm -Rf /
test.notEqual(tempDir, '/')
test.notEqual(tempDir, '.')
test.notEqual(tempDir, './')
require('child_process').exec('rm -Rf ' + tempDir + ' ', function(error, stdout, stderr) {
if (error == null) {
test.done()
} else {
test.fail(stderr)
}
})
}
}
}
module.exports = tests
@timoxley

Copy link
Copy Markdown
Author

Output of those console logs in saveRiak

...
this.id
undefined
this.attributes
[ 'thing' ]
this.model
undefined
this.isNew()
✖ run - saves new items to bucket

TypeError: Object Test(thing) has no method 'isNew'
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment