Skip to content

Commit

Permalink
fixes #26 #28
Browse files Browse the repository at this point in the history
  • Loading branch information
apb2006 committed Sep 29, 2021
1 parent e05c113 commit 1d6b865
Show file tree
Hide file tree
Showing 19 changed files with 1,134 additions and 157 deletions.
98 changes: 65 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# basex - A BaseX client for node.js
===========================

This is a [BaseX](http://basex.org/) client for Node.js. It is work in progress.
This is a [BaseX](http://basex.org/) client for Node.js.
It uses the [client interface](http://docs.basex.org/wiki/Server_Protocol)
via a socket connection to the BaseX server.

Expand All @@ -11,7 +11,7 @@ BaseX is a very light-weight, high-performance and scalable
Built as a lightweight Java server, BaseX also supports XSLT, Webdav and RestXQ.

## Installing the BaseX Node client
[![NPM](https://nodei.co/npm/basex.png?stars&downloads)](https://nodei.co/npm/basex/) [![NPM](https://nodei.co/npm-dl/basex.png)](https://nodei.co/npm/basex/)
[![Npm package monthly downloads](https://badgen.net/npm/dm/basex)](https://npmjs.ccom/package/basex)

To install with npm:

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

Once BaseX is installed and the BaseX server is running, test it.
Expand All @@ -39,7 +39,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 8.6)
(tested against versions 9.6,8.6)
1. Run `basexserver -S`
## API specification
Expand All @@ -55,40 +55,76 @@ There is a test suite using [mocha](http://mochajs.org/)
```bash
mocha -R spec test/

[auth] password good
√ should not error

Execute info command
✓ should not error
✓ should have reply
[auth] password bad
√ should throw error

Send an valid xquery statement: 2+2
✓ It should not error
✓ It should equal 4
[commands] Execute info command
should not error
should have reply

Send an invalid command: 2+
✓ It should error
[commands] Send valid xquery statement: 2+2
√ It should not error
√ It should equal 4

Create a database
It should not error
[commands] Send an invalid command: 2+
It should error

Add a document
It should not error
[commands] Create a database testdb using execute
It should not error

drop db database
It should not error
[commands] Add a document
It should not error

drop db database
It should not error
[commands] Add an invalid document
It should error

Send a xquery and iterate over the result items
✓ It should not error
✓ It should return an array
[commands] drop db testdb
√ It should not error

create query and bind
✓ It should not error
✓ It should return a string
[commands] create database
√ It should not error

13 tests complete (408 ms)
[commands] drop db database
√ It should not error

[parser] Parser test?
√ should pop abc

[query] create query and bind
√ It should not error
√ It should return a string

[query] create query and bind with type
√ It should not error
√ It should return a string

[query] Send a xquery and iterate over the result items
√ It should not error
√ It should return an array

[stream] Create a database testdb from stream
√ It should not error

[stream] Add doc from stream
√ It should not error

[stream] drop db testdb
√ It should not error

[stress] Send a xquery and iterate over the 1000000 result items
√ should not error

[stress] return megabyte result from execute
√ should not error

[stress] return megabyte result from query
√ should not error


26 passing (2s)
```
# Tools
Expand All @@ -104,8 +140,8 @@ Thanks to:
- [jesseclark](https://github.com/jesseclark)
- [Zearin](https://github.com/Zearin)
- [Zearin](https://github.com/Zearin)
- [salim-dev](https://github.com/salim-dev)
# Todo
* stream i/o
Expand All @@ -123,7 +159,3 @@ Parts inspired by [node_redis](https://github.com/mranney/node_redis),
# License
BSD license
[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/apb2006/basex-node/trend.png)](https://bitdeli.com/free "Bitdeli Badge")
9 changes: 9 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# Changelog
## v1.0.0 - 2021-09-29
- removed use of Buffer() see #28
- query bind pass type to server #26
- update `combined-stream` to "1.0.8"
- Node version ">=8.0"
- reworked tests
- reworked sample `issue22.js`

## v0.9.0 - 2017-03-23
- session object now emits socketError events - see #9 #21
- min node version set to 4.0
Expand Down
2 changes: 1 addition & 1 deletion debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ function printMsg(msg) {
}
};
exports.print = print;
exports.printMsg = printMsg;
exports.printMsg = printMsg;
32 changes: 17 additions & 15 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ This allows an application to react to events such as the server doing down.(See

# Session commands

##execute
## execute
````
session.execute(command,callback)
````
Expand All @@ -45,7 +45,7 @@ Example
````
client.execute("create db test_db", log.print);
````
##query
## query
````
var query=session.query(query)
````
Expand All @@ -55,7 +55,7 @@ Example
var input = 'for $i in 1 to 100 return <xml>Text { $i }</xml>';
var query = session.query(input);
````
##create
## create
````
session.create(name,in,callback)
````
Expand All @@ -65,7 +65,7 @@ Creates a database from an input stream.
client.create("test_db", "<x>Hello World!</x>", log.print);
````

##add
## add
````
session.add(name,target,in,callback)
````
Expand All @@ -76,57 +76,59 @@ Example
var s=fs.createReadStream(__dirname+ "/books.xml");
client.add("/world/World.xml", s, log.print);
````
##replace
## replace
````
session.replace(path,in,callback)
````
Replaces a document with the specified input stream.

##store
## store
````
session.store(path,in,callback)
````
Stores raw data at the specified path.



##info
## info
session.info(callback)
Returns process information.

##close
## close
session.close(callback)
Closes the session.

# The query object
Create a query object `var q=session.query(query)`, then `bind` any external variables,
finally call `results or `execute`
##bind
See [Query](https://docs.basex.org/wiki/Server_Protocol#Query)

## bind
````
query.bind(name,value,type,callback);
````
Binds a `name` to a `value`. Currently `type` is ignored.
Binds a `name` to a `value`.
````
query.bind("name", "nodex","",log.print);
````
##close
## close
query.close();
##results
## results
query.results(callback);
Returns results as an array.
````
query.results(log.print);
````
##execute
## execute
````
query.execute(callback);
````
Executes the query and returns all results as a single string.

##info
## info
query.info(callback);

##options
## options
query.options(callback);

# Debugging
Expand Down
32 changes: 28 additions & 4 deletions examples/QueryBindExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,38 @@
var basex = require("../index");
var log = require("../debug");
//basex.debug_mode = true;
var session = new basex.Session("localhost", 1984, "admin", "admin");
var session = new basex.Session();

// create query instance
var input = "declare variable $name external; for $i in 1 to 1000 return element { $name } { $i }";
var input = `
declare variable $item external;
declare variable $str as xs:string external;
declare variable $int as xs:integer external;
declare variable $int2 as xs:integer external;
declare variable $bool as xs:boolean external;
declare variable $bool2 as xs:boolean external;
declare variable $what as item() external :="a default value";
declare function local:about($name,$value){($name || " :" || inspect:type($value) ,$value)};
local:about('$item',$item)
,local:about('$str',$str)
,local:about('$int',$int)
,local:about('$int2',$int2)
,local:about('$bool',$bool)
,local:about('$bool2',$bool2)
,local:about('$what',$what)
`
var query = session.query(input);

// bind variable
query.bind("name", "nodex","",log.print);
// bind variables
query.bind("item", "item","",log.print);
query.bind("str", "a string","",log.print);
query.bind("int", -1,"xs:integer",log.print);
query.bind("int2", -2,"",log.print);
query.bind("bool", true,"",log.print);
query.bind("bool2", true,"xs:boolean",log.print);
//query.bind("what", true,"badtype",log.print);
query.info(log.print);
// print results
query.execute(log.print);
Expand Down
2 changes: 1 addition & 1 deletion examples/RawExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function print(err, reply) {
} else {
console.log("Reply: ",reply);
var str = reply.result;
var buf = new Buffer(str.length);
var buf = Buffer.alloc(str.length);

for (var i = 0; i < str.length ; i++) {
buf[i] = str.charCodeAt(i);
Expand Down
32 changes: 32 additions & 0 deletions examples/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* This example has all session options explicitly set.
*/
var basex = require("../index");
basex.debug_mode = false;
var client = new basex.Session("localhost", 8900,"admin","admin");

/**
* Description
* @method print
* @param {} err
* @param {} reply
* @return
*/
function print(err, reply) {
if (err) {
console.log("Error: " + err);
} else {
var t2=new Date();
console.log("Execution completed in ",t2-t0," milliseconds.");
console.dir(reply);
}
};
var t0=new Date();
client.execute("xquery 1 to 10",print);
client.close(function(){
var t2=new Date();
console.log("Closed in ",t2-t0," milliseconds.");
});
var t1=new Date();
// time to send commands to server.
console.log("Commands send in ",t1-t0," milliseconds.");
4 changes: 2 additions & 2 deletions examples/bxstream.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ writeStream.on('close', function () {
console.log('All done!');
});
rs.pipe(writeStream);
var b=new Buffer("aaaaaaaaaaaaaaaaaaab\x00c\xFF\x00fg\x0099","binary");
var b= Buffer.from("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");
var b= Buffer.from("aaaaa\x00bbb\xFFcccc ","binary");
ss.write(b);
Loading

0 comments on commit 1d6b865

Please sign in to comment.