Skip to content

Commit

Permalink
Version 0.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
santoshrajan committed Apr 29, 2013
1 parent e13ba57 commit 884ba61
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 99 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@ Delete a record.
//
})

#### Considerations

You can only use one instance of `level-autotable` per process. ie you cannot have two databases with level-autotable` in the same process.



188 changes: 93 additions & 95 deletions index.js
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()
}
})
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
"author" : "Santosh Rajan <santrajan@gmail.com>",
"repository" : {"type": "git", "url": "git://github.com/santoshrajan/levelup-autotable.git"},
"main" : "index.js",
"version" : "0.0.3",
"version" : "0.0.4",
"license" : "MIT"
}

0 comments on commit 884ba61

Please sign in to comment.