The BaseX Node client is asynchronous. Most functions take an optional
callback function as last argument. The callback function will be
called with two arguments when the results are available .
The first err
will contain any error information or null if there is no error
The second reply
will hold the reply. The reply is often an object with
result
and info
properties.
{ result: '1 2 3 4 5 6 7 8 9 10',
info: '\nQuery executed in 0.38 ms.\n' }
The utility function print
in the debug module shows the syntax:
function print(err, reply) {
if (err) {
console.log("Error: " + err);
} else {
console.dir(reply);
}
};
The API is modelled closely on the BaseX client protocol See the examples for more details
var basex=require("basex");
var session=basex.Session(host, port, username, password)
The default values are: hostname (default="localhost") port (default=1984) username (default="admin") password (default= "admin")
Since version 0.8.1 the session
object emits socketError
events on socket errors.
This allows an application to react to events such as the server doing down.(See examples/issue22.js
)
session.execute(command,callback)
Executes a command on the server calls callback
with the result.
Example
client.execute("create db test_db", log.print);
var query=session.query(query)
Returns a query object for the specified query, see below for more detail. Example
var input = 'for $i in 1 to 100 return <xml>Text { $i }</xml>';
var query = session.query(input);
session.create(name,in,callback)
Creates a database from an input stream.
// create new database
client.create("test_db", "<x>Hello World!</x>", log.print);
session.add(name,target,in,callback)
Adds a document to the current database from an input stream or a string. Example
// add document from stream
var s=fs.createReadStream(__dirname+ "/books.xml");
client.add("/world/World.xml", s, log.print);
session.replace(path,in,callback)
Replaces a document with the specified input stream.
session.store(path,in,callback)
Stores raw data at the specified path.
session.info(callback)
Returns process information.
session.close(callback)
Closes the session.
Create a query object var q=session.query(query)
, then bind
any external variables,
finally call results or
execute`
See Query
query.bind(name,value,type,callback);
Binds a name
to a value
.
query.bind("name", "nodex","",log.print);
query.close();
query.results(callback); Returns results as an array.
query.results(log.print);
query.execute(callback);
Executes the query and returns all results as a single string.
query.info(callback);
query.options(callback);
The index.js
module variable debug_mode
can be set to true to
print diagnostic info to the console.