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

Commit 69bfff7

Browse files
committed
feat: split .query into .query and .queryKeys
Applies changes from ipfs/interface-datastore/pull/87 Depends on: - [ ] ipfs/interface-datastore#87 - [ ] ipfs/js-datastore-core#59
1 parent 6273fa7 commit 69bfff7

File tree

3 files changed

+43
-26
lines changed

3 files changed

+43
-26
lines changed

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@
3838
},
3939
"homepage": "https://github.com/ipfs/js-datastore-fs#readme",
4040
"dependencies": {
41-
"datastore-core": "^3.0.0",
41+
"datastore-core": "ipfs/js-datastore-core#feat/split-query-into-query-and-query-keys",
4242
"fast-write-atomic": "^0.2.0",
43-
"interface-datastore": "^3.0.3",
43+
"interface-datastore": "ipfs/interface-datastore#feat/split-query-into-query-and-query-keys",
4444
"it-glob": "^0.0.11",
45+
"it-map": "^1.0.5",
4546
"mkdirp": "^1.0.4"
4647
},
4748
"devDependencies": {

src/index.js

+38-22
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ const promisify = require('util').promisify
99
const writeAtomic = promisify(require('fast-write-atomic'))
1010
const path = require('path')
1111
const {
12-
Adapter, Key, Errors, utils: {
13-
map
14-
}
12+
Adapter, Key, Errors
1513
} = require('interface-datastore')
14+
const map = require('it-map')
1615

1716
const noop = () => {}
1817
const fsAccess = promisify(fs.access || noop)
@@ -23,6 +22,7 @@ const fsUnlink = promisify(fs.unlink || noop)
2322
* @typedef {import('interface-datastore').Datastore} Datastore
2423
* @typedef {import('interface-datastore').Pair} Pair
2524
* @typedef {import('interface-datastore').Query} Query
25+
* @typedef {import('interface-datastore').KeyQuery} KeyQuery
2626
*/
2727

2828
/**
@@ -251,9 +251,7 @@ class FsDatastore extends Adapter {
251251
}
252252

253253
/**
254-
*
255254
* @param {Query} q
256-
* @returns {AsyncIterable<Pair>}
257255
*/
258256
async * _all (q) {
259257
let prefix = q.prefix || '**'
@@ -268,27 +266,45 @@ class FsDatastore extends Adapter {
268266
absolute: true
269267
})
270268

271-
if (!q.keysOnly) {
272-
for await (const file of files) {
273-
try {
274-
const buf = await fsReadFile(file)
275-
276-
yield {
277-
key: this._decode(file),
278-
value: buf
279-
}
280-
} catch (err) {
281-
// if keys are removed from the datastore while the query is
282-
// running, we may encounter missing files.
283-
if (err.code !== 'ENOENT') {
284-
throw err
285-
}
269+
for await (const file of files) {
270+
try {
271+
const buf = await fsReadFile(file)
272+
273+
/** @type {Pair} */
274+
const pair = {
275+
key: this._decode(file),
276+
value: buf
277+
}
278+
279+
yield pair
280+
} catch (err) {
281+
// if keys are removed from the datastore while the query is
282+
// running, we may encounter missing files.
283+
if (err.code !== 'ENOENT') {
284+
throw err
286285
}
287286
}
288-
} else {
289-
yield * map(files, f => /** @type {Pair} */({ key: this._decode(f) }))
290287
}
291288
}
289+
290+
/**
291+
* @param {KeyQuery} q
292+
*/
293+
async * _allKeys (q) {
294+
let prefix = q.prefix || '**'
295+
296+
// strip leading slashes
297+
prefix = prefix.replace(/^\/+/, '')
298+
299+
const pattern = `${prefix}/*${this.opts.extension}`
300+
.split(path.sep)
301+
.join('/')
302+
const files = glob(this.path, pattern, {
303+
absolute: true
304+
})
305+
306+
yield * map(files, f => this._decode(f))
307+
}
292308
}
293309

294310
module.exports = FsDatastore

tsconfig.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
2-
"extends": "./node_modules/aegir/src/config/tsconfig.aegir.json",
2+
"extends": "aegir/src/config/tsconfig.aegir.json",
33
"compilerOptions": {
44
"outDir": "dist"
55
},
66
"include": [
7-
"test", // remove this line if you don't want to type-check tests
7+
"test",
88
"src"
99
]
1010
}

0 commit comments

Comments
 (0)