easy-exist
v0.5.0
Published
Node module providing promisified API to interact with eXist-db's REST API
Downloads
15
Readme
Easy-Exist
Node module providing promisified API to interact with eXist-db's REST API.
Code Example
var exist = require('easy-exist');
// connect
var db = new exist.DB('http://localhost', {
username: "test-user",
password: "password"
});
body = '<message><body>Hello World</body></message>'
// PUT a document
db.put('/my-collection/my-document', body)
// Get the body of a document
.then(function() {
return db.get('/my-collection/my-document');
})
.then(function(doc) {
console.log('Document Body:', doc);
})
// Execute xQuery
.then(function() {
return db.query('collection("my-collection")/message/body');
})
.then(function(result) {
console.log('xQuery result:', result);
})
// Delete document
.then(function() {
return db.delete('/my-collection/my-document');
})
.then(function() {
console.log('Document Deleted');
});
Installation
Install via NPM
npm install easy-exist --save
Then require
var exist = require('easy-exist');
API
new exist.DB(url, options)
Parameters
url
- stringoptions
- object - configuration options
Options
The options
object supports the following properties:
username
- string - Username for HTTP Basic Authpassword
- string - Password for HTTP Basic Authcollection
- string - Name of collection under which all subsequent requests will apply to
.put(uri, body)
Stores an XML document at the given URI.
Parameters
uri
- string - the URI at which to store the given bodybody
- string - the document body
Returns
A request-promise Promise. If a non-2xx response is returned, the promise is rejected.
.get(uri)
Retrieves the body of the document at the given URI.
Parameters
uri
- string - the URI of the document to fetch
Returns
A request-promise Promise. If a non-2xx response is returned, the promise is rejected, otherwise it is resolved with the body of the specified document.
.delete(uri)
Deletes the document at the given URI.
Parameters
uri
- string - the URI of the document to delete
Returns
A request-promise Promise. If a non-2xx response is returned, the promise is rejected.
.query(query, options)
Executes given query against store and returns result
Parameters
query
- string - xQuery to execute agains the storeoptions
- object - Query options
Options
The options
object supports the following properties:
start
- integer - Specifies the index position of the first item in the result sequence to be returned. The default value is 1max
- integer - The maximum number of items to be returnedwrap
- string - (yes
|no
) - Specifies whether the returned query results are to be wrapped into a surrounding exist:result element. The default value is yes
Returns
A request-promise Promise. If a non-2xx response is returned, the promise is rejected, otherwise it is resolved with the xQuery result.
.storeQuery(uri, query)
Stores the given xQuery at the specified URI
Parameters
uri
- string - the URI at which to store the given xQueryquery
- string - the xQuery body
Returns
A request-promise Promise. If a non-2xx response is returned, the promise is rejected.
.executeStoredQuery(uri)
Executes the xQuery stored at the given URI and returns the results.
Syntactic Sugar. This function simply calls through to .get(uri)
Parameters
uri
- string - the URI of the xQuery to execute
Returns
A request-promise Promise. If a non-2xx response is returned, the promise is rejected, otherwise it is resolved with the result of executing the specified specified xQuery.
.exists(uri)
Determines if a document exists at the given URI
Parameters
uri
- string - the URI of the document to check
Returns
A Promise. Resolves with true
if a document exists at the given uri, false
if it does not. If a non 2xx and non 404 status code is returned, the promise is rejected.
Contributing
- Fork the project.
- Create your branch
- Make your changes with tests
- Run tests:
grunt test
- Run hint:
grunt hint
- Run tests:
- Create a Pull Request
Test setup
The tests run against a local exist-db instance under a "test-user" account. If you want to run the tests yourself, ensure that this test-user account has been created. You can update the connection properties in spec/lib/db-spec.js
var DB_HOST = 'http://localhost';
// ...
var db = new DB(DB_HOST, { username: 'test-user', password: 'password'} );