-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e13ba57
commit 884ba61
Showing
3 changed files
with
94 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,113 +1,111 @@ | ||
var counter, | ||
validName = /^[a-zA-Z_$][0-9a-zA-Z_$]*$/, | ||
methods = {} | ||
|
||
methods.initAutoKeys = function(callback) { | ||
if (typeof counter != 'undefined') { | ||
return callback() | ||
module.exports = function(db) { | ||
|
||
var counter, | ||
validName = /^[a-zA-Z_$][0-9a-zA-Z_$]*$/, | ||
methods = {} | ||
|
||
db.initAutoKeys = function(callback) { | ||
if (typeof counter != 'undefined') { | ||
return callback() | ||
} | ||
this.get('~counter', function (err, value) { | ||
if (err) { | ||
if (err.name === 'NotFoundError') { | ||
counter = 0 | ||
} else { | ||
throw err | ||
} | ||
} else { | ||
counter = value | ||
} | ||
callback() | ||
}) | ||
} | ||
this.get('~counter', function (err, value) { | ||
if (err) { | ||
if (err.name === 'NotFoundError') { | ||
counter = 0 | ||
} else { | ||
throw err | ||
} | ||
} else { | ||
counter = value | ||
|
||
db.newAutoKey = function(callback) { | ||
if (typeof callback != 'function') { | ||
throw new Error("newKey requires a callback function") | ||
} | ||
callback() | ||
}) | ||
} | ||
this.put("~counter", ++counter, function(err) { | ||
callback(err, counter.toString()) | ||
}) | ||
} | ||
|
||
methods.newAutoKey = function(callback) { | ||
if (typeof callback != 'function') { | ||
throw new Error("newKey requires a callback function") | ||
db.putField = function(key, name, value, options, callback) { | ||
if (validName.test(name)) { | ||
this.put(key + ":" + name, value, options, callback) | ||
} else { | ||
throw new Error("Invalid char in field name") | ||
} | ||
} | ||
this.put("~counter", ++counter, function(err) { | ||
callback(err, counter.toString()) | ||
}) | ||
} | ||
|
||
methods.putField = function(key, name, value, options, callback) { | ||
if (validName.test(name)) { | ||
this.put(key + ":" + name, value, options, callback) | ||
} else { | ||
throw new Error("Invalid char in field name") | ||
db.getField = function(key, name, options, callback) { | ||
this.get(key + ":" + name, options, callback) | ||
} | ||
} | ||
|
||
methods.getField = function(key, name, options, callback) { | ||
this.get(key + ":" + name, options, callback) | ||
} | ||
db.delField = function(key, name, options, callback) { | ||
this.del(key + ":" + name, options, callback) | ||
} | ||
|
||
methods.delField = function(key, name, options, callback) { | ||
this.del(key + ":" + name, options, callback) | ||
} | ||
db.getRecord = function(key, callback) { | ||
var ret = {} | ||
this.createReadStream({start: key, end: parseInt(key) + 1}) | ||
.on('data', function (data) { | ||
ret[data.key.split(':')[1]] = data.value | ||
}) | ||
.on('error', function (err) { | ||
callback(err) | ||
}) | ||
.on('end', function (err) { | ||
callback(null, ret) | ||
}) | ||
} | ||
|
||
methods.getRecord = function(key, callback) { | ||
var ret = {} | ||
this.createReadStream({start: key, end: parseInt(key) + 1}) | ||
.on('data', function (data) { | ||
ret[data.key.split(':')[1]] = data.value | ||
db.putRecord = function(key, value, callback) { | ||
var ops = [] | ||
Object.keys(value).forEach(function(k) { | ||
ops.push({type: 'put', key: key + ":" + k, value: value[k]}) | ||
}) | ||
.on('error', function (err) { | ||
this.batch(ops, function (err) { | ||
callback(err) | ||
}) | ||
.on('end', function (err) { | ||
callback(null, ret) | ||
}) | ||
} | ||
|
||
methods.putRecord = function(key, value, callback) { | ||
var ops = [] | ||
Object.keys(value).forEach(function(k) { | ||
ops.push({type: 'put', key: key + ":" + k, value: value[k]}) | ||
}) | ||
this.batch(ops, function (err) { | ||
callback(err) | ||
}) | ||
} | ||
} | ||
|
||
methods.delRecord = function(key, callback) { | ||
var ops = [] | ||
var db = this | ||
this.createReadStream({start: key, end: parseInt(key) + 1}) | ||
.on('data', function (data) { | ||
ops.push({type: 'del', key: data.key}) | ||
}) | ||
.on('error', function (err) { | ||
callback(err) | ||
}) | ||
.on('end', function (err) { | ||
db.batch(ops, function (err) { | ||
if (err) return callback(err) | ||
callback() | ||
db.delRecord = function(key, callback) { | ||
var ops = [] | ||
var db = this | ||
this.createReadStream({start: key, end: parseInt(key) + 1}) | ||
.on('data', function (data) { | ||
ops.push({type: 'del', key: data.key}) | ||
}) | ||
}) | ||
} | ||
|
||
methods.dump = function(callback) { | ||
this.createReadStream() | ||
.on('data', function (data) { | ||
console.log(data.key, '=', data.value) | ||
}) | ||
.on('error', function (err) { | ||
if (callback) { | ||
.on('error', function (err) { | ||
callback(err) | ||
} else { | ||
throw err | ||
} | ||
}) | ||
.on('end', function (err) { | ||
if (callback) { | ||
callback() | ||
} | ||
}) | ||
} | ||
}) | ||
.on('end', function (err) { | ||
db.batch(ops, function (err) { | ||
if (err) return callback(err) | ||
callback() | ||
}) | ||
}) | ||
} | ||
|
||
module.exports = function(db) { | ||
Object.keys(methods).forEach(function(key) { | ||
db[key] = methods[key] | ||
}) | ||
db.dump = function(callback) { | ||
this.createReadStream() | ||
.on('data', function (data) { | ||
console.log(data.key, '=', data.value) | ||
}) | ||
.on('error', function (err) { | ||
if (callback) { | ||
callback(err) | ||
} else { | ||
throw err | ||
} | ||
}) | ||
.on('end', function (err) { | ||
if (callback) { | ||
callback() | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters