Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create location directory recursively #6

Merged
merged 2 commits into from
Mar 6, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 it will be created recursively. The optional `options` object may contain:
vweevers marked this conversation as resolved.
Show resolved Hide resolved

- `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')
}
})