Skip to content

Commit 1d6b865

Browse files
committed
fixes #26 #28
1 parent e05c113 commit 1d6b865

19 files changed

Lines changed: 1134 additions & 157 deletions

README.md

Lines changed: 65 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# basex - A BaseX client for node.js
22
===========================
33

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

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

1313
## Installing the BaseX Node client
14-
[![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/)
14+
[![Npm package monthly downloads](https://badgen.net/npm/dm/basex)](https://npmjs.ccom/package/basex)
1515

1616
To install with npm:
1717

@@ -21,7 +21,7 @@ To install with npm:
2121
$ mkdir myproject
2222
cd myproject
2323
$ npm install basex
24-
basex@0.9.0 ./node_modules/basex
24+
basex@1.0.0 ./node_modules/basex
2525
```
2626

2727
Once BaseX is installed and the BaseX server is running, test it.
@@ -39,7 +39,7 @@ Once BaseX is installed and the BaseX server is running, test it.
3939
## Installing BaseX
4040
1. Java is required
4141
1. [Download](http://basex.org/products/download/all-downloads/) and install BaseX
42-
(tested against version 8.6)
42+
(tested against versions 9.6,8.6)
4343
1. Run `basexserver -S`
4444
4545
## API specification
@@ -55,40 +55,76 @@ There is a test suite using [mocha](http://mochajs.org/)
5555
```bash
5656
mocha -R spec test/
5757

58+
[auth] password good
59+
√ should not error
5860

59-
Execute info command
60-
✓ should not error
61-
✓ should have reply
61+
[auth] password bad
62+
√ should throw error
6263

63-
Send an valid xquery statement: 2+2
64-
✓ It should not error
65-
✓ It should equal 4
64+
[commands] Execute info command
65+
should not error
66+
should have reply
6667

67-
Send an invalid command: 2+
68-
✓ It should error
68+
[commands] Send valid xquery statement: 2+2
69+
√ It should not error
70+
√ It should equal 4
6971

70-
Create a database
71-
It should not error
72+
[commands] Send an invalid command: 2+
73+
It should error
7274

73-
Add a document
74-
It should not error
75+
[commands] Create a database testdb using execute
76+
It should not error
7577

76-
drop db database
77-
It should not error
78+
[commands] Add a document
79+
It should not error
7880

79-
drop db database
80-
It should not error
81+
[commands] Add an invalid document
82+
It should error
8183

82-
Send a xquery and iterate over the result items
83-
✓ It should not error
84-
✓ It should return an array
84+
[commands] drop db testdb
85+
√ It should not error
8586

86-
create query and bind
87-
✓ It should not error
88-
✓ It should return a string
87+
[commands] create database
88+
√ It should not error
8989

90-
13 tests complete (408 ms)
90+
[commands] drop db database
91+
√ It should not error
9192

93+
[parser] Parser test?
94+
√ should pop abc
95+
96+
[query] create query and bind
97+
√ It should not error
98+
√ It should return a string
99+
100+
[query] create query and bind with type
101+
√ It should not error
102+
√ It should return a string
103+
104+
[query] Send a xquery and iterate over the result items
105+
√ It should not error
106+
√ It should return an array
107+
108+
[stream] Create a database testdb from stream
109+
√ It should not error
110+
111+
[stream] Add doc from stream
112+
√ It should not error
113+
114+
[stream] drop db testdb
115+
√ It should not error
116+
117+
[stress] Send a xquery and iterate over the 1000000 result items
118+
√ should not error
119+
120+
[stress] return megabyte result from execute
121+
√ should not error
122+
123+
[stress] return megabyte result from query
124+
√ should not error
125+
126+
127+
26 passing (2s)
92128
```
93129
# Tools
94130
@@ -104,8 +140,8 @@ Thanks to:
104140
105141
- [jesseclark](https://github.com/jesseclark)
106142
- [Zearin](https://github.com/Zearin)
107-
108-
143+
- [Zearin](https://github.com/Zearin)
144+
- [salim-dev](https://github.com/salim-dev)
109145
110146
# Todo
111147
* stream i/o
@@ -123,7 +159,3 @@ Parts inspired by [node_redis](https://github.com/mranney/node_redis),
123159
# License
124160
125161
BSD license
126-
127-
128-
[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/apb2006/basex-node/trend.png)](https://bitdeli.com/free "Bitdeli Badge")
129-

changelog.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
# Changelog
2+
## v1.0.0 - 2021-09-29
3+
- removed use of Buffer() see #28
4+
- query bind pass type to server #26
5+
- update `combined-stream` to "1.0.8"
6+
- Node version ">=8.0"
7+
- reworked tests
8+
- reworked sample `issue22.js`
9+
110
## v0.9.0 - 2017-03-23
211
- session object now emits socketError events - see #9 #21
312
- min node version set to 4.0

debug.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ function printMsg(msg) {
3030
}
3131
};
3232
exports.print = print;
33-
exports.printMsg = printMsg;
33+
exports.printMsg = printMsg;

docs/commands.md

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ This allows an application to react to events such as the server doing down.(See
3636

3737
# Session commands
3838

39-
##execute
39+
## execute
4040
````
4141
session.execute(command,callback)
4242
````
@@ -45,7 +45,7 @@ Example
4545
````
4646
client.execute("create db test_db", log.print);
4747
````
48-
##query
48+
## query
4949
````
5050
var query=session.query(query)
5151
````
@@ -55,7 +55,7 @@ Example
5555
var input = 'for $i in 1 to 100 return <xml>Text { $i }</xml>';
5656
var query = session.query(input);
5757
````
58-
##create
58+
## create
5959
````
6060
session.create(name,in,callback)
6161
````
@@ -65,7 +65,7 @@ Creates a database from an input stream.
6565
client.create("test_db", "<x>Hello World!</x>", log.print);
6666
````
6767

68-
##add
68+
## add
6969
````
7070
session.add(name,target,in,callback)
7171
````
@@ -76,57 +76,59 @@ Example
7676
var s=fs.createReadStream(__dirname+ "/books.xml");
7777
client.add("/world/World.xml", s, log.print);
7878
````
79-
##replace
79+
## replace
8080
````
8181
session.replace(path,in,callback)
8282
````
8383
Replaces a document with the specified input stream.
8484

85-
##store
85+
## store
8686
````
8787
session.store(path,in,callback)
8888
````
8989
Stores raw data at the specified path.
9090

9191

9292

93-
##info
93+
## info
9494
session.info(callback)
9595
Returns process information.
9696

97-
##close
97+
## close
9898
session.close(callback)
9999
Closes the session.
100100

101101
# The query object
102102
Create a query object `var q=session.query(query)`, then `bind` any external variables,
103103
finally call `results or `execute`
104-
##bind
104+
See [Query](https://docs.basex.org/wiki/Server_Protocol#Query)
105+
106+
## bind
105107
````
106108
query.bind(name,value,type,callback);
107109
````
108-
Binds a `name` to a `value`. Currently `type` is ignored.
110+
Binds a `name` to a `value`.
109111
````
110112
query.bind("name", "nodex","",log.print);
111113
````
112-
##close
114+
## close
113115
query.close();
114-
##results
116+
## results
115117
query.results(callback);
116118
Returns results as an array.
117119
````
118120
query.results(log.print);
119121
````
120-
##execute
122+
## execute
121123
````
122124
query.execute(callback);
123125
````
124126
Executes the query and returns all results as a single string.
125127

126-
##info
128+
## info
127129
query.info(callback);
128130

129-
##options
131+
## options
130132
query.options(callback);
131133

132134
# Debugging

examples/QueryBindExample.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,38 @@
66
var basex = require("../index");
77
var log = require("../debug");
88
//basex.debug_mode = true;
9-
var session = new basex.Session("localhost", 1984, "admin", "admin");
9+
var session = new basex.Session();
1010

1111
// create query instance
12-
var input = "declare variable $name external; for $i in 1 to 1000 return element { $name } { $i }";
12+
var input = `
13+
declare variable $item external;
14+
declare variable $str as xs:string external;
15+
declare variable $int as xs:integer external;
16+
declare variable $int2 as xs:integer external;
17+
declare variable $bool as xs:boolean external;
18+
declare variable $bool2 as xs:boolean external;
19+
declare variable $what as item() external :="a default value";
20+
21+
declare function local:about($name,$value){($name || " :" || inspect:type($value) ,$value)};
22+
23+
local:about('$item',$item)
24+
,local:about('$str',$str)
25+
,local:about('$int',$int)
26+
,local:about('$int2',$int2)
27+
,local:about('$bool',$bool)
28+
,local:about('$bool2',$bool2)
29+
,local:about('$what',$what)
30+
`
1331
var query = session.query(input);
1432

15-
// bind variable
16-
query.bind("name", "nodex","",log.print);
33+
// bind variables
34+
query.bind("item", "item","",log.print);
35+
query.bind("str", "a string","",log.print);
36+
query.bind("int", -1,"xs:integer",log.print);
37+
query.bind("int2", -2,"",log.print);
38+
query.bind("bool", true,"",log.print);
39+
query.bind("bool2", true,"xs:boolean",log.print);
40+
//query.bind("what", true,"badtype",log.print);
1741
query.info(log.print);
1842
// print results
1943
query.execute(log.print);

examples/RawExample.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function print(err, reply) {
1818
} else {
1919
console.log("Reply: ",reply);
2020
var str = reply.result;
21-
var buf = new Buffer(str.length);
21+
var buf = Buffer.alloc(str.length);
2222

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

examples/auth.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* This example has all session options explicitly set.
3+
*/
4+
var basex = require("../index");
5+
basex.debug_mode = false;
6+
var client = new basex.Session("localhost", 8900,"admin","admin");
7+
8+
/**
9+
* Description
10+
* @method print
11+
* @param {} err
12+
* @param {} reply
13+
* @return
14+
*/
15+
function print(err, reply) {
16+
if (err) {
17+
console.log("Error: " + err);
18+
} else {
19+
var t2=new Date();
20+
console.log("Execution completed in ",t2-t0," milliseconds.");
21+
console.dir(reply);
22+
}
23+
};
24+
var t0=new Date();
25+
client.execute("xquery 1 to 10",print);
26+
client.close(function(){
27+
var t2=new Date();
28+
console.log("Closed in ",t2-t0," milliseconds.");
29+
});
30+
var t1=new Date();
31+
// time to send commands to server.
32+
console.log("Commands send in ",t1-t0," milliseconds.");

examples/bxstream.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ writeStream.on('close', function () {
1616
console.log('All done!');
1717
});
1818
rs.pipe(writeStream);
19-
var b=new Buffer("aaaaaaaaaaaaaaaaaaab\x00c\xFF\x00fg\x0099","binary");
19+
var b= Buffer.from("aaaaaaaaaaaaaaaaaaab\x00c\xFF\x00fg\x0099","binary");
2020
rs.write(b);
2121
console.log("-------------------------")
2222

2323
var ss=new bxs.SendStream();
2424
ss.on("data",function(data){
2525
console.log(data)
2626
});
27-
var b=new Buffer("aaaaa\x00bbb\xFFcccc ","binary");
27+
var b= Buffer.from("aaaaa\x00bbb\xFFcccc ","binary");
2828
ss.write(b);

0 commit comments

Comments
 (0)