Skip to content

Commit

Permalink
events and protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
apb2006 committed Dec 30, 2012
1 parent 7b9d178 commit 9afc7ab
Show file tree
Hide file tree
Showing 23 changed files with 512 additions and 377 deletions.
71 changes: 42 additions & 29 deletions readme.md → README.md
@@ -1,16 +1,14 @@
[![build status](https://secure.travis-ci.org/apb2006/basex-node.png)](http://travis-ci.org/apb2006/basex-node)
# basex - A BaseX client for node.js
===========================

This is BaseX client for node.js. It is work in progress.
This is BaseX client for Node.js. It is work in progress.

[BaseX](http://basex.org/) is a very light-weight, high-performance and scalable
XML Database engine and XPath/XQuery 3.0 Processor,
including full support for the W3C Update and Full Text extensions.
Built as a lightweight Java server, BaseX also supports XSLT, Webdav and RestXQ.



## Installing the BaseX Node client

To install with npm:
Expand All @@ -21,7 +19,7 @@ To install with npm:
$ mkdir myproject
cd myproject
$ npm install basex
basex@0.5.0 ./node_modules/basex
basex@0.6.0 ./node_modules/basex
```

Once BaseX is installed and the BaseX server is running, test it.
Expand All @@ -39,7 +37,7 @@ Once BaseX is installed and the BaseX server is running, test it.
## Installing BaseX
1. Java is required
1. [Download](http://basex.org/products/download/all-downloads/) and install BaseX
(tested against version 7.3)
(tested against version 7.5)
1. Run `basexserver -S`
## API specification
Expand All @@ -48,32 +46,47 @@ See [commands.md](https://github.com/apb2006/basex-node/blob/master/docs/command
in the docs folder for details of the API.
## Tests
There is a test suite, using [vows](http://vowsjs.org/) .
There is a test suite using [mocha](http://visionmedia.github.com/mocha/)
, [should](https://github.com/visionmedia/should.js) and
[sinon](http://sinonjs.org/).
```bash
vows tests/* --spec

♢ BaseX interface test

Request info
✓ we get no error
✓ we get a reply
Send an valid xquery statement: 2+2
✓ we get no error
✓ and the answer is 4
Send an invalid command: 2+
✓ we get an error
Create a database
✓ we get no error
Add a document
✓ we get no error
Drop the database
✓ we get no error
Send a xquery and iterate over the result items
✓ we get no error
✓ and the result is an array

✓ OK » 10 honored (0.253s)
mocha -R spec test/test-commands.js


Execute info command
✓ should not error
✓ should have reply

Send an valid xquery statement: 2+2
✓ It should not error
✓ It should equal 4

Send an invalid command: 2+
✓ It should error

Create a database
✓ It should not error

Add a document
✓ It should not error

drop db database
✓ It should not error

drop db database
✓ It should not error

Send a xquery and iterate over the result items
✓ It should not error
✓ It should return an array

create query and bind
✓ It should not error
✓ It should return a string

13 tests complete (408 ms)

```
Expand Down
4 changes: 4 additions & 0 deletions changelog.md
@@ -1,3 +1,7 @@
## v0.6.0 - 2012-12-29
- implement BaseX escape protocol
- switch from Vows to Mocha for tests

## v0.5.1 - 2012-07-19
- readline fixed

Expand Down
10 changes: 8 additions & 2 deletions docs/commands.md
Expand Up @@ -74,18 +74,24 @@ Closes the session.
# The query object

##bind
query.bind(name,value,callback);
query.bind(name,value,type,callback);
Binds a `name` to a `value`. Currently `type` is ignored.

##close
query.close();
##results
query.results(callback);
Returns results as an array.

##execute
query.execute(callback);

##info
query.info(callback);

##options
query.options(callback);

# Debugging
The basex module variable `debug_mode` can be set to true to
The `index.js` module variable `debug_mode` can be set to true to
print diagnostic info to the console.
46 changes: 27 additions & 19 deletions examples/EventExample.js
Expand Up @@ -10,31 +10,39 @@ var session1 = new basex.Session("localhost", 1984, "admin", "admin");
var session2 = new basex.Session("localhost", 1984, "admin", "admin");
basex.debug_mode = false;

// create the event
session1.execute("create event test_evt", afterCreate);

// watch for it in second session
function afterCreate(err, reply) {
console.log("running afterCreate...");
if (err)
console.log("Error: " + err);
session2.watch("test_evt", watchCallback, afterWatch);
};

session1.execute("create event messenger",afterEvent);
// run long query in sessions, and fire event from session1
function afterWatch(err, reply) {
console.log("running afterWatch...");
if (err)
console.log("Error: " + err, reply);

function afterEvent(err, reply){
if (err) console.log("Error: " + err);
console.log("afterevent");
session2.watch("messenger",watchCallback,afterWatch);
var xq = "for $i in 1 to 10000000 where $i=7 return $i"
session2.query(xq).execute(d.printMsg("S2:execute complete"));
session1.query("db:event('test_evt', 'Hello World!')").execute(
d.printMsg("S1:event sent"));
};

function afterWatch(err, reply){
if (err)console.log("Error: " + err);
console.log("afterwatch");
var xq="for $i in 1 to 10000000 where $i=0 return $i"
session2.query(xq).execute(d.printMsg("S2:execute"));
session1.query("db:event('messenger', 'Hello World!')").execute(d.printMsg("S1:event"));
// on event received show and unwatch
function watchCallback(name, msg) {
console.log("watch event received-----> ", msg)
session2.unwatch("test_evt", teardown);
};

function watchCallback(name,msg){
console.log("watch update-----> ",msg)
session2.unwatch("messenger",function(){
session1.execute("drop event messenger",d.printMsg("S1:drop event"));
// close all
function teardown(err, reply) {
console.log("unwatch:", err, reply)
session1.execute("drop event test_evt", d.printMsg("S1:drop event"));
// close session
session1.close(d.printMsg("S1:close"));
session2.close(d.printMsg("S2:close"));
});

session2.close(d.printMsg("S2:close"));
};
16 changes: 13 additions & 3 deletions examples/RawExample.js
Expand Up @@ -9,10 +9,20 @@ function print(err, reply) {
if (err) {
console.log("Error: " + err);
} else {
console.log(reply);
console.log("Reply: ",reply);
var str = reply.result;
var buf = new Buffer(str.length);

for (var i = 0; i < str.length ; i++) {
buf[i] = str.charCodeAt(i);
}

console.log(buf);
}
};
var xq="let $av:= (0 to 127,-128 to -1)!xs:byte(.)" +
" return convert:bytes-to-hex($av)";
//var xq="let $av:= (0 to 255)!xs:unsignedByte(.)" +
var xq="let $av:=(0 to 127,-127 to -1)!xs:byte(.)" +
" return convert:bytes-to-base64($av)";

client.execute("xquery declare option output:method 'raw';"+xq,print);
client.close();
28 changes: 28 additions & 0 deletions examples/bxstream.js
@@ -0,0 +1,28 @@
// test basexstream class
var bxs=require("../lib/basexstream");
var fs = require('fs');
var rs=new bxs.ReceiveStream();


rs.on("data",function(data){
console.log(data)
});
rs.on("marker",function(data){
console.log("marker")
});

var writeStream = fs.createWriteStream('someFile.txt', { flags : 'w' });
writeStream.on('close', function () {
console.log('All done!');
});
rs.pipe(writeStream);
var b=new Buffer("aaaaaaaaaaaaaaaaaaab\x00c\xFF\x00fg\x0099","binary");
rs.write(b);
console.log("-------------------------")

var ss=new bxs.SendStream();
ss.on("data",function(data){
console.log(data)
});
var b=new Buffer("aaaaa\x00bbb\xFFcccc ","binary");
ss.write(b);
1 change: 1 addition & 0 deletions examples/manysessions.js
@@ -1,4 +1,5 @@
// start multiple sessions
// fails with around 200 sessions
var basex =require("../index");
var log = require("../debug");
basex.debug_mode = false;
Expand Down
2 changes: 1 addition & 1 deletion examples/raw.js
@@ -1,5 +1,5 @@
/*
* This example shows the use of raw data. (NOT working)
* This example shows the use of raw data.
*/
var basex = require("../index");
var client = new basex.Session();
Expand Down

0 comments on commit 9afc7ab

Please sign in to comment.