Permalink
Comparing changes
Open a pull request
- 2 commits
- 14 files changed
- 0 commit comments
- 1 contributor
Unified
Split
Showing
with
215 additions
and 179 deletions.
- +2 −1 cli.js
- +3 −3 lib/commands.js
- +1 −1 lib/rest-handler.js
- +97 −97 lib/schema.js
- +34 −37 lib/storage.js
- +3 −2 lib/write-stream.js
- +1 −1 test/experimental_tests/hyperlevel-clone.js
- +1 −0 test/run.js
- +21 −21 test/tests/crud.js
- +2 −2 test/tests/init.js
- +36 −0 test/tests/levelup.js
- +7 −7 test/tests/read-streams.js
- +5 −5 test/tests/replication.js
- +2 −2 test/tests/rest.js
| @@ -64,7 +64,8 @@ var dat = Dat(datPath, datOpts, function ready(err) { | ||
| } | ||
| if (typeof message === 'object') message = JSON.stringify(message) | ||
| if (!opts.argv.quiet && message) stdout.write(message.toString() + EOL) | ||
| if (datCommand.command !== 'serve' || datCommand.command !== 'listen') close() | ||
| var persist = ['serve', 'listen'] | ||
| if (persist.indexOf(datCommand.command) === -1) close() | ||
| }) | ||
| }) | ||
| @@ -383,8 +383,8 @@ dat.get = function(key, opts, cb) { | ||
| return this.storage.get(key, opts, cb) | ||
| } | ||
| dat.put = function(rawDoc, buffer, opts, cb) { | ||
| return this.storage.put(rawDoc, buffer, opts, cb) | ||
| dat.put = function(key, val, opts, cb) { | ||
| return this.storage.put(key, val, opts, cb) | ||
| } | ||
| dat.delete = function(key, opts, cb) { | ||
| @@ -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, function(err, stored) { | ||
| self.put(doc.id, doc, function(err, stored) { | ||
| cb(err, stored) | ||
| }) | ||
| }) | ||
| @@ -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, function(err, stored) { | ||
| self.dat.put(json.id, 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) | ||
| @@ -1,138 +1,138 @@ | ||
| var protobuf = require('protocol-buffers'); | ||
| var Schema = function(storage, onReady) { | ||
| if (!(this instanceof Schema)) return new Schema(storage, onReady) | ||
| this._storage = storage | ||
| this.protobuf = null | ||
| this.columns = [] | ||
| this.index = {} | ||
| this.reserved = ['id', 'change', 'version', 'deleted'] | ||
| this.update(onReady) | ||
| if (!(this instanceof Schema)) return new Schema(storage, onReady) | ||
| this._storage = storage | ||
| this.protobuf = null | ||
| this.columns = [] | ||
| this.index = {} | ||
| this.reserved = ['id', 'change', 'version', 'deleted'] | ||
| this.update(onReady) | ||
| } | ||
| Schema.prototype.update = function(cb) { | ||
| var self = this | ||
| this._storage.getMeta('schema', {valueEncoding:'json'}, function(err, schema) { | ||
| if (err && err.notFound) err = null // grrrrr | ||
| if (err) return cb(err) | ||
| self.columns = schema || [] | ||
| self.compile() | ||
| cb() | ||
| }); | ||
| var self = this | ||
| this._storage.getMeta('schema', {valueEncoding:'json'}, function(err, schema) { | ||
| if (err && err.notFound) err = null // grrrrr | ||
| if (err) return cb(err) | ||
| self.columns = schema || [] | ||
| self.compile() | ||
| cb() | ||
| }); | ||
| } | ||
| Schema.prototype.compile = function() { | ||
| var self = this | ||
| var self = this | ||
| this.protobuf = protobuf(this.columns) | ||
| this.names = [] | ||
| this.protobuf = protobuf(this.columns) | ||
| this.names = [] | ||
| this.columns.forEach(function(col) { | ||
| self.names.push(col.name) | ||
| }) | ||
| this.columns.forEach(function(col) { | ||
| self.names.push(col.name) | ||
| }) | ||
| } | ||
| Schema.prototype.save = function(cb) { | ||
| this._storage.setMeta('schema', this.columns, {valueEncoding:'json'}, cb) | ||
| this._storage.setMeta('schema', this.columns, {valueEncoding:'json'}, cb) | ||
| } | ||
| Schema.prototype.normalize = function(cols) { | ||
| for (var i = 0; i < cols.length; i++) { | ||
| if (typeof cols[i] === 'string') cols[i] = {name:cols[i], type:'json'} | ||
| if (!cols[i]) cols[i].type = 'json' | ||
| } | ||
| return cols | ||
| for (var i = 0; i < cols.length; i++) { | ||
| if (typeof cols[i] === 'string') cols[i] = {name:cols[i], type:'json'} | ||
| if (!cols[i]) cols[i].type = 'json' | ||
| } | ||
| return cols | ||
| } | ||
| Schema.prototype.encode = function(doc) { | ||
| return this.protobuf.encode(doc) | ||
| return this.protobuf.encode(doc) | ||
| } | ||
| Schema.prototype.decode = function(buf) { | ||
| return this.protobuf.decode(buf) | ||
| return this.protobuf.decode(buf) | ||
| } | ||
| Schema.prototype.validate = function(doc) { | ||
| return this.protobuf.validate(doc) | ||
| return this.protobuf.validate(doc) | ||
| } | ||
| var mismatch = function() { | ||
| var err = new Error('Column mismatch') | ||
| err.type = 'columnMismatch' | ||
| return err | ||
| var err = new Error('Column mismatch') | ||
| err.type = 'columnMismatch' | ||
| return err | ||
| } | ||
| Schema.prototype.mergeFromObject = function(obj, cb) { | ||
| var keys = Object.keys(obj) | ||
| var updated = 0 | ||
| for (var i = 0; i < keys.length; i++) { | ||
| var key = keys[i] | ||
| var val = obj[key] | ||
| if (val === null || val === undefined) continue | ||
| if (this.reserved.indexOf(key) > -1) continue | ||
| var idx = this.names.indexOf(key) | ||
| if (idx === -1) { | ||
| this.names.push(key) | ||
| this.columns.push({name:key, type:'json'}) | ||
| updated++ | ||
| } else { | ||
| // TODO: if type !== json type check | ||
| } | ||
| } | ||
| if (!updated) return cb() | ||
| this.compile() | ||
| this.save(cb) | ||
| var keys = Object.keys(obj) | ||
| var updated = 0 | ||
| for (var i = 0; i < keys.length; i++) { | ||
| var key = keys[i] | ||
| var val = obj[key] | ||
| if (val === null || val === undefined) continue | ||
| if (this.reserved.indexOf(key) > -1) continue | ||
| var idx = this.names.indexOf(key) | ||
| if (idx === -1) { | ||
| this.names.push(key) | ||
| this.columns.push({name:key, type:'json'}) | ||
| updated++ | ||
| } else { | ||
| // TODO: if type !== json type check | ||
| } | ||
| } | ||
| if (!updated) return cb() | ||
| this.compile() | ||
| this.save(cb) | ||
| } | ||
| Schema.prototype.merge = function(cols, opts, cb) { | ||
| if (typeof opts === 'function') { | ||
| cb = opts | ||
| opts = null | ||
| } | ||
| var updated = 0 | ||
| if (opts && opts.strict) { | ||
| for (var i = 0; i < cols.length; i++) { | ||
| var col = cols[i] | ||
| if (i >= this.columns.length) { | ||
| if (!col.type) col.type = 'json' | ||
| this.names.push(col.name) | ||
| this.columns.push(col) | ||
| updated++ | ||
| } else { | ||
| if (col.type !== this.columns[i].type) return cb(mismatch()) | ||
| if (col.name !== this.columns[i].name) return cb(mismatch()) | ||
| } | ||
| } | ||
| } else { | ||
| for (var i = 0; i < cols.length; i++) { | ||
| var col = cols[i] | ||
| var idx = this.names.indexOf(col.name) | ||
| if (idx === -1) { | ||
| if (!col.type) col.type = 'json' | ||
| this.names.push(col.name) | ||
| this.columns.push(col) | ||
| updated++ | ||
| } else { | ||
| if (col.type !== this.columns[i].type) return cb(mismatch()) | ||
| } | ||
| } | ||
| } | ||
| if (!updated) return cb() | ||
| this.compile() | ||
| this.save(cb) | ||
| if (typeof opts === 'function') { | ||
| cb = opts | ||
| opts = null | ||
| } | ||
| var updated = 0 | ||
| if (opts && opts.strict) { | ||
| for (var i = 0; i < cols.length; i++) { | ||
| var col = cols[i] | ||
| if (i >= this.columns.length) { | ||
| if (!col.type) col.type = 'json' | ||
| this.names.push(col.name) | ||
| this.columns.push(col) | ||
| updated++ | ||
| } else { | ||
| if (col.type !== this.columns[i].type) return cb(mismatch()) | ||
| if (col.name !== this.columns[i].name) return cb(mismatch()) | ||
| } | ||
| } | ||
| } else { | ||
| for (var i = 0; i < cols.length; i++) { | ||
| var col = cols[i] | ||
| var idx = this.names.indexOf(col.name) | ||
| if (idx === -1) { | ||
| if (!col.type) col.type = 'json' | ||
| this.names.push(col.name) | ||
| this.columns.push(col) | ||
| updated++ | ||
| } else { | ||
| if (col.type !== this.columns[i].type) return cb(mismatch()) | ||
| } | ||
| } | ||
| } | ||
| if (!updated) return cb() | ||
| this.compile() | ||
| this.save(cb) | ||
| } | ||
| Schema.prototype.toJSON = function() { | ||
| return this.columns | ||
| return this.columns | ||
| } | ||
| module.exports = Schema |
Oops, something went wrong.