Skip to content

Commit

Permalink
feat: add facility to read binary resources
Browse files Browse the repository at this point in the history
`db.documents.readBinary(path)` allows to read XQuery, textfiles, images
and other resources existdb treats as binary.
  • Loading branch information
line-o authored and duncdrum committed Oct 26, 2022
1 parent d285864 commit 4acf5b2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
5 changes: 5 additions & 0 deletions components/documents.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ function read (client, documentName, options) {
return client.promisedMethodCall('getDocument', [documentName, options])
}

function readBinary (client, documentName) {
return client.promisedMethodCall('getBinaryResource', [documentName])
}

function remove (client, documentName) {
return client.promisedMethodCall('remove', [documentName])
}
Expand All @@ -24,5 +28,6 @@ module.exports = {
upload,
parseLocal,
read,
readBinary,
remove
}
31 changes: 22 additions & 9 deletions spec/tests/documents.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,31 @@ const { readFileSync } = require('fs')
const { connect } = require('../../index')
const { envOptions } = require('../connection')

test('upload document', function (t) {
test('binary document', function (t) {
const path = '/db/test.txt'
const content = Buffer.from('test')
const db = connect(envOptions)
const buffer = Buffer.from('test')

db.documents.upload(buffer, buffer.length)
.then(function (result) {
t.ok(result >= 0, 'returned filehandle')
t.end()
})
.catch(function (e) {
t.test('should upload', async function (st) {
try {
const db = connect(envOptions)
const fh = await db.documents.upload(content, content.length)
st.ok(fh >= 0, 'returned filehandle:' + fh)
const r = await db.documents.parseLocal(fh, path)
st.ok(r)
} catch (e) {
t.fail(e)
t.end()
}
})

t.test('can be read', async function (st) {
try {
const readContent = await db.documents.readBinary(path)
console.log(readContent.toString())
st.equal(content.toString(), readContent.toString(), 'returned contents equal')
} catch (e) {
st.fail(e)
}
})
})

Expand Down

0 comments on commit 4acf5b2

Please sign in to comment.