Skip to content

Commit

Permalink
Create location directory recursively (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
vweevers authored Mar 6, 2022
1 parent 4110908 commit 1ba0b69
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ An `abstract-level` and thus `classic-level` database is at its core a [key-valu

### `db = new ClassicLevel(location[, options])`

Create a database or open an existing database. The `location` argument must be a directory path (relative or absolute) where LevelDB will store its files. The optional `options` object may contain:
Create a database or open an existing database. The `location` argument must be a directory path (relative or absolute) where LevelDB will store its files. If the directory does not yet exist (and `options.createIfMissing` is true) it will be created recursively. The optional `options` object may contain:

- `keyEncoding` (string or object, default `'utf8'`): encoding to use for keys
- `valueEncoding` (string or object, default `'utf8'`): encoding to use for values.
Expand Down
10 changes: 9 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const { AbstractLevel } = require('abstract-level')
const ModuleError = require('module-error')
const { fromCallback } = require('catering')
const fs = require('fs')
const binding = require('./binding')
const { ChainedBatch } = require('./chained-batch')
const { Iterator } = require('./iterator')
Expand Down Expand Up @@ -47,7 +48,14 @@ class ClassicLevel extends AbstractLevel {
}

_open (options, callback) {
binding.db_open(this[kContext], this[kLocation], options, callback)
if (options.createIfMissing) {
fs.mkdir(this[kLocation], { recursive: true }, (err) => {
if (err) return callback(err)
binding.db_open(this[kContext], this[kLocation], options, callback)
})
} else {
binding.db_open(this[kContext], this[kLocation], options, callback)
}
}

_close (callback) {
Expand Down
35 changes: 35 additions & 0 deletions test/mkdir-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict'

const test = require('tape')
const tempy = require('tempy')
const path = require('path')
const fs = require('fs')
const { ClassicLevel } = require('..')

test('creates location directory recursively', async function (t) {
const location = path.join(tempy.directory(), 'beep', 'boop')
const db = new ClassicLevel(location)

t.is(fs.existsSync(location), false)
await db.open()
t.is(fs.existsSync(location), true)
})

test('does not create location directory recursively if createIfMissing is false', async function (t) {
t.plan(3)

const location = path.join(tempy.directory(), 'beep', 'boop')
const db = new ClassicLevel(location, { createIfMissing: false })

try {
await db.open()
} catch (err) {
t.is(err.code, 'LEVEL_DATABASE_NOT_OPEN')

// Error message is inconsistent between platforms so not checked
t.ok(err.cause)

// On Windows, LevelDB itself creates the directory (technically a bug)
t.is(fs.existsSync(location), process.platform === 'win32')
}
})

0 comments on commit 1ba0b69

Please sign in to comment.