Permalink
Browse files

Done.

  • Loading branch information...
ricardobeat committed Nov 16, 2012
1 parent 2a61f6e commit 917dfb79edf460c9890b364282b17ce1537ce43c
Showing with 361 additions and 2 deletions.
  1. +3 −2 .gitignore
  2. +26 −0 Cakefile
  3. +23 −0 indexedlevel.sublime-project
  4. +140 −0 lib/ildb.js
  5. +25 −0 package.json
  6. +85 −0 src/ildb.coffee
  7. +16 −0 test/browser/index.html
  8. +4 −0 test/mocha.opts
  9. +39 −0 test/spec.coffee
View
@@ -19,5 +19,6 @@ thumbs.db
*.sublime-workspace
-test/*.js
-test/*.css
+test/browser/*.js
+test/browser/*.css
+test/test.db
View
@@ -0,0 +1,26 @@
+flour = require 'flour'
+cp = require 'child_process'
+
+task 'build', ->
+ compile 'src/ildb.coffee', 'lib/ildb.js'
+
+task 'build:tests', ->
+
+ flour.minifiers.js = null
+
+ compile 'main.coffee' , 'test/browser/ildb.js'
+ compile 'node_modules/chai/chai.js' , 'test/browser/chai.js'
+ compile 'node_modules/mocha/mocha.css', 'test/browser/mocha.css'
+ compile 'node_modules/mocha/mocha.js' , 'test/browser/mocha.js'
+ compile 'test/spec.coffee' , 'test/browser/spec.js'
+
+task 'watch:tests', ->
+ invoke 'build:tests'
+ watch [
+ 'test/spec.coffee'
+ 'main.coffee'
+ ], -> invoke 'build:tests'
+
+task 'test:browser', ->
+ invoke 'build:tests'
+ cp.exec 'open test/browser/index.html'
@@ -0,0 +1,23 @@
+{
+ "folders":
+ [
+ {
+ "file_exclude_patterns":
+ [
+ ".gitkeep",
+ "*.sublime-project",
+ "*.sublime-workspace"
+ ],
+ "folder_exclude_patterns":
+ [
+ "node_modules"
+ ],
+ "path": "./"
+ }
+ ],
+ "settings":
+ {
+ "tab_size": 4,
+ "translate_tabs_to_spaces": true
+ }
+}
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -0,0 +1,25 @@
+{
+ "name": "ildb",
+ "description": "LevelDB + IndexedDB wrapper",
+ "version": "0.1.0",
+ "main": "lib/ildb.js",
+ "dependencies": {
+ "leveldb": "~0.7.1"
+ },
+ "devDependencies": {
+ "flour": "",
+ "mocha": "",
+ "chai": "~1.3.0",
+ "coffee-script": "~1.4.0"
+ },
+ "scripts": {
+ "prepublish": "cake build",
+ "test": "mocha"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/ricardobeat/project.git"
+ },
+ "author": "Ricardo Tomasi",
+ "license": "MIT (ricardo.mit-license.org)"
+}
View
@@ -0,0 +1,85 @@
+class DB
+ constructor: (@path) ->
+ @setup()
+
+ ready: (fn) =>
+ return fn() if @isReady
+ @onReady = fn
+
+ loaded: =>
+ @isReady = true
+ @onReady?()
+ @onReady = null
+
+if process?.versions?.node
+
+ leveldb = require 'leveldb'
+
+ DB::setup = (cb) ->
+ leveldb.open @path, { create_if_missing: true }, (err, db) =>
+ throw err if err
+ @store = db
+ @loaded()
+ cb?()
+
+ DB::get = (key, fn) ->
+ @store.get key, fn
+
+ DB::put = (key, val, fn) ->
+ @store.put key, val.toString(), fn
+
+ DB::del = (key, fn) ->
+ @store.del key, fn
+
+ DB::getAll = (fn) ->
+ @store.iterator (err, iterator) ->
+ return fn err if err
+ data = {}
+ iterator.forRange (err, key, val) ->
+ return fn err if err
+ data[key] = val
+ , -> fn null, data
+
+ DB::clear = (fn) ->
+ leveldb.destroy @path, {}, =>
+ @setup fn
+
+else
+
+ DB::setup = ->
+ @store = new IDBStore
+ keyPath: 'id'
+ autoIncrement: false
+ onStoreReady: @loaded
+
+ okay = (fn) -> (res) -> fn null, res
+ nope = (fn) -> (err) -> fn err, null
+
+ DB::get = (key, fn) ->
+ @store.get key, (data) ->
+ fn null, data?.val
+ , nope(fn)
+
+ DB::put = (key, val, fn) ->
+ data = { id: key, val: val }
+ @store.put data, okay(fn), nope(fn)
+
+ DB::del = (key, fn) ->
+ @store.remove key, okay(fn), nope(fn)
+
+ DB::getAll = (fn) ->
+ @store.getAll (res) ->
+ data = {}
+ for obj in res
+ data[obj.id] = obj.val
+ fn null, data
+ , nope(fn)
+
+ DB::clear = (fn) ->
+ @store.clear (res) -> fn null, res
+
+if module?.exports?
+ module.exports = DB
+else
+ window.ILDB = DB
+
View
@@ -0,0 +1,16 @@
+<!DOCTYPE html>
+
+<title>Indexed Level test suite</title>
+
+<link rel="stylesheet" href="mocha.css" />
+
+<body>
+ <div id="mocha"></div>
+ <script src="https://raw.github.com/jensarps/IDBWrapper/master/IDBStore.js"></script>
+ <script src="ildb.js"></script>
+ <script src="chai.js"></script>
+ <script src="mocha.js"></script>
+ <script>mocha.setup('bdd')</script>
+ <script src="spec.js"></script>
+ <script>mocha.run()</script>
+</body>
View
@@ -0,0 +1,4 @@
+--require should
+--ui bdd
+--compilers coffee:coffee-script
+--reporter spec
View
@@ -0,0 +1,39 @@
+if module?.exports
+ chai = require('chai')
+ ILDB = require '../main'
+else
+ chai = window.chai
+ ILDB = window.ILDB
+
+should = chai.Should()
+
+db = null
+
+describe 'Wrapper', ->
+
+ before (done) ->
+ db = new ILDB 'test/test.db'
+ db.ready ->
+ db.clear? done
+
+ it 'should save and recover a value', (done) ->
+ db.put 'one', 'hello', (err) ->
+ should.not.exist err
+ db.get 'one', (err, val) ->
+ val.should.equal 'hello'
+ done()
+
+ it 'should remove a value', (done) ->
+ db.del 'one', (err) ->
+ should.not.exist err
+ db.get 'one', (err, val) ->
+ should.not.exist err
+ should.not.exist val
+ done()
+
+ it 'should get all values', (done) ->
+ db.put 'one', '1', ->
+ db.put 'two', '2', ->
+ db.getAll (err, all) ->
+ all.should.eql { one: '1', two: '2' }
+ done()

0 comments on commit 917dfb7

Please sign in to comment.