On Github ryanramage / js-on-the-level
I will talk tonight on 'JS on the LEVEL'. A high level preso on streams, why they are good, and some helpful tools
Writes are written to a level, when its full, a new one is created above it, pushing down older data.
0 ↠ 4 SST, 1 ↠ 10M, 2 ↠ 100M, 3 ↠ 1G, 4 ↠ 10G, 5 ↠ 100G, 6 ↠ 1T, 7 ↠ 10T
Node Jedis have returned to their trusty Node.js in an attempt to rescue the company from the clutches of the vile Mongo the DB.
Using a technology from the Google Galactic Empire they create on a powerful weapon...
npm install level --save
var level = require('level') // 1) Create our database var db = level('./mydb') // 2) put a key & value db.put('ryan', 'happy', function (err) { if (err) return console.log('Ooops!', err) // some kind of I/O error // 3) get by key db.get('name', function (err, value) { if (err) return console.log('Ooops!', err) // likely the key was not found }) })
All operations are asynchronous although they don't necessarily require a callback
db.del(key[, callback])
var ops = [ { type: 'del', key: 'father' } , { type: 'put', key: 'name', value: 'Yuri Irsenovich Kim' } , { type: 'put', key: 'dob', value: '16 February 1941' } , { type: 'put', key: 'spouse', value: 'Kim Young-sook' } , { type: 'put', key: 'occupation', value: 'Clown' } ] db.batch(ops, function (err) { if (err) return console.log('Ooops!', err) console.log('Great success dear leader!') })
Provide a list of operations to be executed sequentially, although as a whole they are performed as an atomic operation inside LevelDB.
db.batch() .del('father') .put('name', 'Yuri Irsenovich Kim') .put('dob', '16 February 1941') .put('spouse', 'Kim Young-sook') .put('occupation', 'Clown') .write(function () { console.log('Done!') })
function copy (srcdb, dstdb, callback) { srcdb.createReadStream() .pipe(dstdb.createWriteStream()) .on('close', callback) }
db.createReadStream() .on('data', function (data) { console.log(data.key, '=', data.value) }) .on('error', function (err) {}) .on('close', function () {}) .on('end', function () {})
db.createReadStream({start: 'r', end: 'r' + '\xff'}) .on('data', function (data) { only keys that start with r })
Node Jedis split the LevelDB force into two factions LevelUP and LevelDOWN.
This unleashes a wave of change and possibility across the universe...
LevelUP becomes the API. No backing store
Pure C++ Node.js LevelDB binding serving as the back-end to LevelUP
level.js an implementation of the leveldown API on top of IndexedDB (which is in turn implemented on top of LevelDB, which brings this whole shebang full circle)
This library is best used with browserify
This week the last browser holdout of IndexedDB was announced!
var localstorage = require('localstorage-down'); var levelup = require('levelup'); var db = levelup('/does/not/matter', { db: localstorage });
Things that you can use with any LevelDown compatable storage.
Adds the ability to create subsections that present the same API as LevelUP, but only write/read to a prefixed section, or bucket, of the key-space.
var SubLevel = require('level-sublevel') , db = level('/somewhere') , sub = SubLevel(db) , people = sub.sublevel('people') , places = sub.sublevel('places') , things = sub.sublevel('things')
A full MongoDB query language implementation with INDEXES for querying your levelup/leveldb database.
db.query({ $and: [ { tags: 'tag1' }, { num: { $lt: 100 } } ] }) .on('data', console.log) .on('stats', function (stats) { // stats contains the query statistics in the format // { indexHits: 1, dataHits: 1, matchHits: 1 }); });
A limitation of LevelDB is that only one process is allowed access to the underlying data. multilevel-http exports a LevelDB instance over http.
var multilevel = require('multilevel-http') // db = levelup instance or path to db var server = multilevel.server(db, options) server.listen(5000)
var multilevel = require('multilevel-http') var db = multilevel.client('http://localhost:5000/') // now you have the complete levelUP api!
A simple eventually consistent master-master replication module for leveldb.
The simple api surface area, combined with a powerful module system lets you build a DB as you want it.
You can use levelup everywhere. Client, server, etc...
Tight API design and the small module philosophy that has been a success for LevelDB
By Ryan Ramage / @ryan_ramage_