Permalink
Browse files

support .put(obj, cb)

  • Loading branch information...
maxogden committed May 29, 2014
1 parent 06320f9 commit 66e208037e40ffe40b7863c9cae0403ee5b2a024
View
@@ -45,18 +45,18 @@ Gets a key, calls callback with `(error, value)`. `value` is a JS object
## put
```js
db.put([json], [buffer], [opts], [cb])
db.put([key], value, [opts], cb)
```
Puts JSON into the database by key. Specify the key you want by setting it as `json.id`, e.g. `db.put({id: 'bob'} ... )`.
Puts value into the database by key. Specify the key you want by setting it as either `key` or `value.id`, e.g. `db.put({id: 'bob'} ... )`. `key` is optional to be compatible with the levelup API
`cb` will be called with `(error, newVersion)` where `newVersion` will be be a JS object with `id` and `version` properties.
If something already exists in the database with the key you specified you may receive a conflict error. To ensure you do not overwrite data accidentally you must pass in the current version of the key you wish to update, e.g. if `bob` is in the database at version 1 and you want to update it to add a `foo` key: `db.put({id: 'bob', version: 1, 'foo': 'bar'})`, which will update the row to version 2.
All versions of all rows are persisted and replicated.
If `buffer` is specified (and `Buffer.isBuffer(buffer)` is truthy) then instead of storing `json` as the value it will store whatever data is in `buffer` (only use this if you know what you are doing)
If `Buffer.isBuffer(value)` is truthy then it will store whatever binary data is in `value` (only use this if you know what you are doing)
### Options
View
@@ -446,7 +446,7 @@ dat.createBlobWriteStream = function(options, doc, cb) {
if (err) return cb(err)
if (!doc.attachments) doc.attachments = {}
doc.attachments[options.filename] = blob
self.put(doc.id, doc, function(err, stored) {
self.put(doc, function(err, stored) {
cb(err, stored)
})
})
View
@@ -212,7 +212,7 @@ RestHandler.prototype.post = function(req, res) {
self.bufferJSON(req, function(err, json) {
if (err) return self.error(res, 500, err)
if (!json) json = {}
self.dat.put(json.id, json, function(err, stored) {
self.dat.put(json, function(err, stored) {
if (err) {
// if (err.conflict) return self.error(res, 409, {error: "Document update conflict. Invalid version"})
return self.error(res, 500, err)
View
@@ -136,13 +136,27 @@ Database.prototype.put = function (key, val, opts, cb) {
var updated, doc
var isNew = false
// opts is optional
if (typeof opts === 'function') {
cb = opts
opts = {}
// argument overloading section! TODO abstractify this
if (!opts && !cb) { // put(obj, cb)
cb = val
val = key
key = undefined
} else if (!cb) {
// we only support string keys right now
if (typeof key === 'string') { // put(key, val, cb)
cb = opts
opts = {}
} else { // put(doc, opts, cb)
cb = opts
opts = val
val = key
key = undefined
}
}
// argument validation section! TODO abstractify this
if (!opts) opts = {}
if (Buffer.isBuffer(val)) { // assume val is a protobuf
doc = {id: key}
if (opts.version) doc.version = opts.version
@@ -157,8 +171,9 @@ Database.prototype.put = function (key, val, opts, cb) {
}
// at this point doc should be an object with id === key (or falsy to get an auto-gen'd key)
debug([key, val, opts, cb])
debug('args', [key, val, opts, typeof cb])
if (!opts.skipSchemaCheck) {
if (opts.columns) {
self.schema.merge(self.schema.normalize(opts.columns), check)
@@ -169,7 +184,6 @@ Database.prototype.put = function (key, val, opts, cb) {
check()
}
function check(err) {
if (err) return cb(err)
@@ -245,7 +259,7 @@ Database.prototype.delete = function (key, opts, cb) {
self.get(key, function(err, row) {
if (err) return cb(err)
row._deleted = true
self.put(null, row, cb)
self.put(row, cb)
})
}
@@ -23,7 +23,7 @@ module.exports.clone = function(test, common) {
function clone(cb) {
var dat = new Dat(common.dat1tmp, function ready(err) {
t.notOk(err, 'no open err on leveldown-hyper db')
dat.put(null, {foo: 'bar'}, function(err) {
dat.put({foo: 'bar'}, function(err) {
t.notOk(err, 'no put err')
var dat2 = new Dat(common.dat2tmp, function ready(err) {
t.notOk(err, 'no open err on leveldown db')
View
@@ -39,7 +39,7 @@ module.exports.decodeKey = function(test, common) {
module.exports.putJson = function(test, common) {
test('.put json', function(t) {
common.getDat(t, function(dat, done) {
dat.put(null, {"foo": "bar"}, function(err, doc) {
dat.put({"foo": "bar"}, function(err, doc) {
if (err) throw err
var cat = dat.createReadStream()
@@ -58,7 +58,6 @@ module.exports.putWeirdKeys = function(test, common) {
common.getDat(t, function(dat, done) {
dat.put(".error.", {"foo": "bar"}, function(err, doc) {
if (err) throw err
console.log(doc)
var cat = dat.createReadStream()
cat.pipe(concat(function(data) {
t.equal(data.length, 1)
@@ -73,7 +72,7 @@ module.exports.putWeirdKeys = function(test, common) {
module.exports.putJsonSetVersion = function(test, common) {
test('.put json at specific version', function(t) {
common.getDat(t, function(dat, done) {
dat.put(null, {"foo": "bar", version: 5}, function(err, doc) {
dat.put({"foo": "bar", version: 5}, function(err, doc) {
if (err) throw err
var cat = dat.createReadStream()
@@ -90,7 +89,7 @@ module.exports.putJsonSetVersion = function(test, common) {
module.exports.putJsonPrimary = function(test, common) {
test('.put json w/ primary key option', function(t) {
common.getDat(t, function(dat, done) {
dat.put(null, {"foo": "bar"}, {primary: 'foo'}, function(err, doc) {
dat.put({"foo": "bar"}, {primary: 'foo'}, function(err, doc) {
if (err) throw err
dat.get('bar', function(err, data) {
t.notOk(err, 'no err')
@@ -111,7 +110,7 @@ module.exports.updateJson = function(test, common) {
dat.put("foo", {bar: 'baz'}, function(err, doc2) {
t.ok(err, 'should err')
t.notOk(doc2, "should not return data")
dat.put(null, doc, function(err, doc3) {
dat.put(doc, function(err, doc3) {
t.notOk(err, 'no err')
t.equals(doc3.version, 2, 'should be version 2')
setImmediate(done)
@@ -140,9 +139,9 @@ module.exports.forceOption = function(test, common) {
module.exports.multiplePutJson = function(test, common) {
test('.put same json multiple times (random id generation)', function(t) {
common.getDat(t, function(dat, done) {
dat.put(null, {"foo": "bar"}, function(err) {
dat.put({"foo": "bar"}, function(err) {
if (err) throw err
dat.put(null, {"foo": "bar"}, function(err) {
dat.put({"foo": "bar"}, function(err) {
if (err) throw err
var cat = dat.createReadStream()
@@ -165,7 +164,7 @@ module.exports.putBuff = function(test, common) {
var schema = protobuf([{name:'foo', type:'string'}]);
var row = schema.encode({foo:'bar'});
dat.put(null, row, {columns: schema.toJSON()}, function(err) {
dat.put(row, {columns: schema.toJSON()}, function(err) {
if (err) throw err
var cat = dat.createReadStream()
@@ -182,7 +181,7 @@ module.exports.putBuff = function(test, common) {
module.exports.deleteRow = function(test, common) {
test('delete row', function(t) {
common.getDat(t, function(dat, done) {
dat.put(null, {"foo": "bar"}, function(err, doc) {
dat.put({"foo": "bar"}, function(err, doc) {
if (err) throw err
dat.delete(doc.id, function(err) {
t.false(err, 'should delete okay')
@@ -205,11 +204,11 @@ module.exports.deleteRow = function(test, common) {
module.exports.getAtVersion = function(test, common) {
test('get row at specific version', function(t) {
common.getDat(t, function(dat, done) {
dat.put(null, {"foo": "bar"}, function(err, doc) {
dat.put({"foo": "bar"}, function(err, doc) {
if (err) throw err
var ver1 = doc.version
doc.pizza = 'taco'
dat.put(null, doc, function(err, doc) {
dat.put(doc, function(err, doc) {
t.false(err)
if (!doc) doc = {}
dat.get(doc.id, { version: ver1 }, function(err, docAtVer) {
@@ -241,7 +240,7 @@ module.exports.keepTotalRowCount = function(test, common) {
test('inc row count on put', function(t) {
common.getDat(t, function(dat, done) {
dat.put(null, {"foo": "bar"}, function(err, doc) {
dat.put({"foo": "bar"}, function(err, doc) {
if (err) throw err
t.equal(dat.getRowCount(), 1)
setImmediate(done)
@@ -251,7 +250,7 @@ module.exports.keepTotalRowCount = function(test, common) {
test('dec row count on del', function(t) {
common.getDat(t, function(dat, done) {
dat.put(null, {"foo": "bar"}, function(err, doc) {
dat.put({"foo": "bar"}, function(err, doc) {
if (err) throw err
t.equal(dat.getRowCount(), 1)
dat.delete(doc.id, function(err) {
@@ -268,7 +267,7 @@ module.exports.keepTotalRowCount = function(test, common) {
dat.put("foo", {bar: 'baz'}, function(err, doc) {
if (err) throw err
t.equal(dat.getRowCount(), 1)
dat.put(null, doc, function(err, doc2) {
dat.put(doc, function(err, doc2) {
t.notOk(err, 'should not err')
t.equal(dat.getRowCount(), 1)
setImmediate(done)
@@ -279,8 +278,8 @@ module.exports.keepTotalRowCount = function(test, common) {
test('persist the row count', function(t) {
common.getDat(t, function(dat, done) {
dat.put(null, {"foo": "bar"}, function(err, doc) {
dat.put(null, {"bar": "foo"}, function(err, doc) {
dat.put({"foo": "bar"}, function(err, doc) {
dat.put({"bar": "foo"}, function(err, doc) {
dat.storage.getRowCount(function(err, val) {
if (err) throw err
t.equal(val, 2)
View
@@ -156,7 +156,7 @@ module.exports.customBackend = function(test, common) {
test('instantiate + pass in custom leveldown instance', function(t) {
var memdown = require('memdown')
var dat = new Dat(common.dat1tmp, { backend: memdown }, function ready() {
dat.put(null, {'foo': 'bar'}, function(err) {
dat.put({'foo': 'bar'}, function(err) {
t.notOk(err, 'no put err')
var onDiskDat = fs.existsSync(path.join(common.dat1tmp, '.dat', 'store.dat'))
t.notOk(onDiskDat, 'no dat folder was created')
@@ -146,7 +146,7 @@ module.exports.changesStream = function(test, common) {
setImmediate(done)
}))
dat.put(null, {"foo": "bar"}, function(err, doc) {
dat.put({"foo": "bar"}, function(err, doc) {
if (err) throw err
})
})
@@ -157,7 +157,7 @@ module.exports.changesStreamTail = function(test, common) {
test('createChangesStream tail:true', function(t) {
common.getDat(t, function(dat, done) {
dat.put(null, {"foo": "old"}, function(err) {
dat.put({"foo": "old"}, function(err) {
t.notOk(err, 'should not err')
var changes = dat.createChangesStream({ live: true, tail: true, data: true })
@@ -175,7 +175,7 @@ module.exports.changesStreamTail = function(test, common) {
setImmediate(done)
}))
dat.put(null, {"foo": "new"}, function(err) {
dat.put({"foo": "new"}, function(err) {
t.notOk(err, 'should not err')
})
})
@@ -320,7 +320,7 @@ module.exports.createVersionStream = function(test, common) {
t.false(err)
var ver1 = doc.version
doc.pizza = 'taco'
dat.put(null, doc, function(err, doc) {
dat.put(doc, function(err, doc) {
t.false(err)
// put some data before and after to make sure they dont get returned too
dat.put("abc", {'bar': 'baz'}, function(err) {
View
@@ -132,7 +132,7 @@ module.exports.pullReplicationMultiple = function(test, common) {
})
function putPullCompare(doc, cb) {
dat.put(null, doc, function(err, doc) {
dat.put(doc, function(err, doc) {
if (err) throw err
dat2.pull(function(err) {
if (err) throw err
@@ -163,7 +163,7 @@ module.exports.pullReplicationLive = function(test, common) {
var dat2 = new Dat(common.dat2tmp, function ready() {
common.getDat(t, function(dat, cleanup) {
var pull = dat2.pull({ live: true })
dat.put(null, {foo: 'bar'}, function(err) {
dat.put({foo: 'bar'}, function(err) {
if (err) throw err
setTimeout(function() {
dat2.createReadStream().pipe(concat(function(data) {
@@ -208,7 +208,7 @@ module.exports.pushReplication = function(test, common) {
})
function putPushCompare(doc, cb) {
dat.put(null, doc, function(err, doc) {
dat.put(doc, function(err, doc) {
if (err) throw err
dat.push('http://localhost:' + dat2port, function(err) {
if (err) throw err
@@ -258,7 +258,7 @@ module.exports.pushReplicationURLNormalize = function(test, common) {
})
function putPushCompare(doc, cb) {
dat.put(null, doc, function(err, doc) {
dat.put(doc, function(err, doc) {
if (err) throw err
dat.push('localhost:' + dat2port, function(err) {
if (err) throw err
@@ -288,7 +288,7 @@ module.exports.remoteClone = function(test, common) {
test('clone from remote', function(t) {
common.getDat(t, function(dat, cleanup) {
dat.put(null, {foo: 'bar'}, function(err) {
dat.put({foo: 'bar'}, function(err) {
if (err) throw err
var dat2 = new Dat(common.dat2tmp, { init: false }, function ready() {
var remote = 'http://localhost:' + dat.defaultPort
View
@@ -5,7 +5,7 @@ module.exports.restHello = function(test, common) {
test('rest get /api returns stats', function(t) {
if (common.rpc) return t.end()
common.getDat(t, function(dat, cleanup) {
dat.put(null, {foo: 'bar'}, function(err, stored) {
dat.put({foo: 'bar'}, function(err, stored) {
if (err) throw err
request('http://localhost:' + dat.defaultPort + '/api', function(err, res, json) {
t.false(err, 'no error')
@@ -29,7 +29,7 @@ module.exports.restGet = function(test, common) {
test('rest get', function(t) {
if (common.rpc) return t.end()
common.getDat(t, function(dat, cleanup) {
dat.put(null, {foo: 'bar'}, function(err, stored) {
dat.put({foo: 'bar'}, function(err, stored) {
if (err) throw err
request('http://localhost:' + dat.defaultPort + '/api/' + stored.id, function(err, res, json) {
t.false(err, 'no error')

0 comments on commit 66e2080

Please sign in to comment.