Skip to content
This repository was archived by the owner on Mar 23, 2023. It is now read-only.

Commit 9d630a3

Browse files
committed
chore: addresses PR comments
1 parent f5a7ae1 commit 9d630a3

File tree

2 files changed

+49
-11
lines changed

2 files changed

+49
-11
lines changed

src/index.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ class FsDatastore {
7373
*/
7474
_open () {
7575
if (!fs.existsSync(this.path)) {
76-
throw new Error(`Datastore directory: ${this.path} does not exist`)
76+
throw Errors.notFoundError(new Error(`Datastore directory: ${this.path} does not exist`))
7777
}
7878

7979
if (this.opts.errorIfExists) {
80-
throw new Error(`Datastore directory: ${this.path} already exists`)
80+
throw Errors.dbOpenFailedError(new Error(`Datastore directory: ${this.path} already exists`))
8181
}
8282
}
8383

@@ -101,7 +101,7 @@ class FsDatastore {
101101
try {
102102
this._open()
103103
} catch (err) {
104-
if (err.message.match('does not exist')) {
104+
if (err.code === 'ERR_NOT_FOUND') {
105105
this._create()
106106
return
107107
}
@@ -243,6 +243,10 @@ class FsDatastore {
243243
try {
244244
await fsUnlink(parts.file)
245245
} catch (err) {
246+
if (err.code === 'ENOENT') {
247+
return
248+
}
249+
246250
throw Errors.dbDeleteFailedError(err)
247251
}
248252
}
@@ -262,9 +266,14 @@ class FsDatastore {
262266
delete (key /* : Key */) /* : void */ {
263267
deletes.push(key)
264268
},
265-
commit: async () /* : Promise<void> */ => {
266-
await Promise.all((puts.map((put) => this.put(put.key, put.value))))
267-
await Promise.all((deletes.map((del) => this.delete(del))))
269+
commit: () /* : Promise<void> */ => {
270+
return Promise.all(
271+
puts
272+
.map((put) => this.put(put.key, put.value))
273+
.concat(
274+
deletes.map((del) => this.delete(del))
275+
)
276+
)
268277
}
269278
}
270279
}
@@ -318,10 +327,8 @@ class FsDatastore {
318327

319328
/**
320329
* Close the store.
321-
*
322-
* @returns {Promise<void>}
323330
*/
324-
async close () /* : Promise<void> */ { }
331+
close () /* : Promise<void> */ { }
325332
}
326333

327334
module.exports = FsDatastore

test/index.spec.js

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ const sh = require('datastore-core').shard
1818

1919
const FsStore = require('../src')
2020

21-
describe('FsDatastore', async () => {
22-
describe('construction', async () => {
21+
describe('FsDatastore', () => {
22+
describe('construction', () => {
2323
it('defaults - folder missing', () => {
2424
const dir = utils.tmpdir()
2525
expect(
@@ -69,6 +69,37 @@ describe('FsDatastore', async () => {
6969
)
7070
})
7171

72+
it('deleting files', async () => {
73+
const dir = utils.tmpdir()
74+
const fs = new FsStore(dir)
75+
const key = new Key('1234')
76+
77+
await fs.put(key, Buffer.from([0, 1, 2, 3]))
78+
await fs.delete(key)
79+
80+
try {
81+
await fs.get(key)
82+
throw new Error('Should have errored')
83+
} catch (err) {
84+
expect(err.code).to.equal('ERR_NOT_FOUND')
85+
}
86+
})
87+
88+
it('deleting non-existent files', async () => {
89+
const dir = utils.tmpdir()
90+
const fs = new FsStore(dir)
91+
const key = new Key('5678')
92+
93+
await fs.delete(key)
94+
95+
try {
96+
await fs.get(key)
97+
throw new Error('Should have errored')
98+
} catch (err) {
99+
expect(err.code).to.equal('ERR_NOT_FOUND')
100+
}
101+
})
102+
72103
it('sharding files', async () => {
73104
const dir = utils.tmpdir()
74105
const fstore = new FsStore(dir)

0 commit comments

Comments
 (0)