Skip to content

Commit 1ba0b69

Browse files
authored
Create location directory recursively (#6)
1 parent 4110908 commit 1ba0b69

3 files changed

Lines changed: 45 additions & 2 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ An `abstract-level` and thus `classic-level` database is at its core a [key-valu
173173

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

176-
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:
176+
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:
177177

178178
- `keyEncoding` (string or object, default `'utf8'`): encoding to use for keys
179179
- `valueEncoding` (string or object, default `'utf8'`): encoding to use for values.

index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const { AbstractLevel } = require('abstract-level')
44
const ModuleError = require('module-error')
55
const { fromCallback } = require('catering')
6+
const fs = require('fs')
67
const binding = require('./binding')
78
const { ChainedBatch } = require('./chained-batch')
89
const { Iterator } = require('./iterator')
@@ -47,7 +48,14 @@ class ClassicLevel extends AbstractLevel {
4748
}
4849

4950
_open (options, callback) {
50-
binding.db_open(this[kContext], this[kLocation], options, callback)
51+
if (options.createIfMissing) {
52+
fs.mkdir(this[kLocation], { recursive: true }, (err) => {
53+
if (err) return callback(err)
54+
binding.db_open(this[kContext], this[kLocation], options, callback)
55+
})
56+
} else {
57+
binding.db_open(this[kContext], this[kLocation], options, callback)
58+
}
5159
}
5260

5361
_close (callback) {

test/mkdir-test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict'
2+
3+
const test = require('tape')
4+
const tempy = require('tempy')
5+
const path = require('path')
6+
const fs = require('fs')
7+
const { ClassicLevel } = require('..')
8+
9+
test('creates location directory recursively', async function (t) {
10+
const location = path.join(tempy.directory(), 'beep', 'boop')
11+
const db = new ClassicLevel(location)
12+
13+
t.is(fs.existsSync(location), false)
14+
await db.open()
15+
t.is(fs.existsSync(location), true)
16+
})
17+
18+
test('does not create location directory recursively if createIfMissing is false', async function (t) {
19+
t.plan(3)
20+
21+
const location = path.join(tempy.directory(), 'beep', 'boop')
22+
const db = new ClassicLevel(location, { createIfMissing: false })
23+
24+
try {
25+
await db.open()
26+
} catch (err) {
27+
t.is(err.code, 'LEVEL_DATABASE_NOT_OPEN')
28+
29+
// Error message is inconsistent between platforms so not checked
30+
t.ok(err.cause)
31+
32+
// On Windows, LevelDB itself creates the directory (technically a bug)
33+
t.is(fs.existsSync(location), process.platform === 'win32')
34+
}
35+
})

0 commit comments

Comments
 (0)