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

Commit

Permalink
feat: split .query into .query and .queryKeys (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain authored Apr 15, 2021
1 parent 6273fa7 commit 9f2a64b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 26 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@
},
"homepage": "https://github.com/ipfs/js-datastore-fs#readme",
"dependencies": {
"datastore-core": "^3.0.0",
"datastore-core": "^4.0.0",
"fast-write-atomic": "^0.2.0",
"interface-datastore": "^3.0.3",
"interface-datastore": "^4.0.0",
"it-glob": "^0.0.11",
"it-map": "^1.0.5",
"mkdirp": "^1.0.4"
},
"devDependencies": {
Expand Down
60 changes: 38 additions & 22 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ const promisify = require('util').promisify
const writeAtomic = promisify(require('fast-write-atomic'))
const path = require('path')
const {
Adapter, Key, Errors, utils: {
map
}
Adapter, Key, Errors
} = require('interface-datastore')
const map = require('it-map')

const noop = () => {}
const fsAccess = promisify(fs.access || noop)
Expand All @@ -23,6 +22,7 @@ const fsUnlink = promisify(fs.unlink || noop)
* @typedef {import('interface-datastore').Datastore} Datastore
* @typedef {import('interface-datastore').Pair} Pair
* @typedef {import('interface-datastore').Query} Query
* @typedef {import('interface-datastore').KeyQuery} KeyQuery
*/

/**
Expand Down Expand Up @@ -251,9 +251,7 @@ class FsDatastore extends Adapter {
}

/**
*
* @param {Query} q
* @returns {AsyncIterable<Pair>}
*/
async * _all (q) {
let prefix = q.prefix || '**'
Expand All @@ -268,27 +266,45 @@ class FsDatastore extends Adapter {
absolute: true
})

if (!q.keysOnly) {
for await (const file of files) {
try {
const buf = await fsReadFile(file)

yield {
key: this._decode(file),
value: buf
}
} catch (err) {
// if keys are removed from the datastore while the query is
// running, we may encounter missing files.
if (err.code !== 'ENOENT') {
throw err
}
for await (const file of files) {
try {
const buf = await fsReadFile(file)

/** @type {Pair} */
const pair = {
key: this._decode(file),
value: buf
}

yield pair
} catch (err) {
// if keys are removed from the datastore while the query is
// running, we may encounter missing files.
if (err.code !== 'ENOENT') {
throw err
}
}
} else {
yield * map(files, f => /** @type {Pair} */({ key: this._decode(f) }))
}
}

/**
* @param {KeyQuery} q
*/
async * _allKeys (q) {
let prefix = q.prefix || '**'

// strip leading slashes
prefix = prefix.replace(/^\/+/, '')

const pattern = `${prefix}/*${this.opts.extension}`
.split(path.sep)
.join('/')
const files = glob(this.path, pattern, {
absolute: true
})

yield * map(files, f => this._decode(f))
}
}

module.exports = FsDatastore
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"extends": "./node_modules/aegir/src/config/tsconfig.aegir.json",
"extends": "aegir/src/config/tsconfig.aegir.json",
"compilerOptions": {
"outDir": "dist"
},
"include": [
"test", // remove this line if you don't want to type-check tests
"test",
"src"
]
}

0 comments on commit 9f2a64b

Please sign in to comment.