Skip to content
This repository has been archived by the owner on Dec 1, 2024. It is now read-only.

Commit

Permalink
optional location & options arguments
Browse files Browse the repository at this point in the history
closes #206
  • Loading branch information
rvagg committed Oct 14, 2013
1 parent c313cf8 commit 524b041
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ db.put('name', 'LevelUP', function (err) {
--------------------------------------------------------
<a name="ctor"></a>
### levelup(location[, options[, callback]])
### levelup(options[, callback ])
### levelup(db[, callback ])
<code>levelup()</code> is the main entry point for creating a new LevelUP instance and opening the underlying store with LevelDB.

This function returns a new instance of LevelUP and will also initiate an <a href="#open"><code>open()</code></a> operation. Opening the database is an asynchronous operation which will trigger your callback if you provide one. The callback should take the form: `function (err, db) {}` where the `db` is the LevelUP instance. If you don't provide a callback, any read & write operations are simply queued internally until the database is fully opened.
Expand All @@ -158,6 +160,26 @@ db.get('foo', function (err, value) {

The `location` argument is available as a read-only property on the returned LevelUP instance.

The `levelup(options, callback)` form (with optional `callback`) is only available where you provide a valid `'db'` property on the options object (see below). Only for back-ends that don't require a `location` argument, such as [MemDOWN](https://github.com/rvagg/memdown).

For example:

```js
var levelup = require('levelup')
var memdown = require('memdown')
var db = levelup({ db: memdown })
```

The `levelup(db, callback)` form (with optional `callback`) is only available where `db` is a factory function, as would be provided as a `'db'` property on an `options` object (see below). Only for back-ends that don't require a `location` argument, such as [MemDOWN](https://github.com/rvagg/memdown).

For example:

```js
var levelup = require('levelup')
var memdown = require('memdown')
var db = levelup(memdown)
```

#### `options`

`levelup()` takes an optional options object as its second argument; the following properties are accepted:
Expand Down
11 changes: 10 additions & 1 deletion lib/levelup.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,21 @@ function LevelUP (location, options, callback) {
EventEmitter.call(this)
this.setMaxListeners(Infinity)

if (typeof location == 'function') {
options = typeof options == 'object' ? options : {}
options.db = location
location = null
} else if (typeof location == 'object' && typeof location.db == 'function') {
options = location
location = null
}

if (typeof options == 'function') {
callback = options
options = {}
}

if (typeof location != 'string') {
if ((!options || typeof options.db != 'function') && typeof location != 'string') {
error = new InitializationError(
'Must provide a location for the database')
if (callback) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
, "prr" : "~0.0.0"
, "semver" : "~1.1.4"
, "bops" : "~0.0.6"
, "deferred-leveldown" : "~0.0.1"
, "deferred-leveldown" : "~0.1.0"
}
, "devDependencies" : {
"leveldown" : "~0.9.0"
Expand Down
33 changes: 33 additions & 0 deletions test/init-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var levelup = require('../lib/levelup.js')
, assert = require('referee').assert
, refute = require('referee').refute
, buster = require('bustermove')
, MemDOWN = require('memdown')

buster.testCase('Init & open()', {
'setUp': common.commonSetUp
Expand Down Expand Up @@ -181,4 +182,36 @@ buster.testCase('Init & open()', {
}.bind(this))
}.bind(this))
}

, 'constructor with options argument uses factory': function (done) {
var db = levelup({ db: MemDOWN })
assert.isNull(db.location, 'location property is null')
db.on('open', function () {
assert(db.db instanceof MemDOWN, 'using a memdown backend')
assert.same(db.db.location, '', 'db location property is ""')
db.put('foo', 'bar', function (err) {
refute(err, 'no error')
db.get('foo', function (err, value) {
assert.equals(value, 'bar', 'correct value')
done()
})
})
})
}

, 'constructor with only function argument uses factory': function (done) {
var db = levelup(MemDOWN)
assert.isNull(db.location, 'location property is null')
db.on('open', function () {
assert(db.db instanceof MemDOWN, 'using a memdown backend')
assert.same(db.db.location, '', 'db location property is ""')
db.put('foo', 'bar', function (err) {
refute(err, 'no error')
db.get('foo', function (err, value) {
assert.equals(value, 'bar', 'correct value')
done()
})
})
})
}
})

0 comments on commit 524b041

Please sign in to comment.