Skip to content

update #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 49 commits into from
Apr 16, 2019
Merged
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
576bf6b
Merge pull request #2 from mhlm/patch-1
TimonKK Mar 7, 2017
6282c05
Fix some code style
Mar 7, 2017
711791c
Support for NULL values
baryshev Jun 19, 2017
3a0b623
Merge pull request #4 from baryshev/master
TimonKK Jun 21, 2017
12904ea
Upgrade version
Jun 21, 2017
1b14fdf
Support for ALTER query
baryshev Jul 1, 2017
0aa8492
Merge pull request #5 from baryshev/master
TimonKK Aug 18, 2017
303e955
Fix for last row
Aug 18, 2017
3704461
Fix package.json vergion
Aug 18, 2017
5b150f3
Update README.md
TimonKK Aug 18, 2017
e7a6b59
Changed package vergion
Aug 18, 2017
ba4bd82
Refactor all code
Aug 21, 2018
d330635
Update READ.ME and examples of code
TimonKK Aug 21, 2018
bd4e842
Refactor test
Aug 21, 2018
434ab89
Merge branch 'master' of https://github.com/TimonKK/clickhouse
Aug 21, 2018
a025be1
Update README.md
Aug 21, 2018
6afb796
Update README.md
Aug 21, 2018
1d183b7
Update package.json
TimonKK Aug 23, 2018
842b1de
Add insert field as array
Sep 3, 2018
b82659c
clean & update package.json
Sep 13, 2018
b4443fe
fix test 'should return not null object' & variable types
Sep 13, 2018
6fd53f0
Merge pull request #9 from xboston/fix-test
TimonKK Sep 13, 2018
8becac8
Update readme & version
Sep 13, 2018
7b29788
Merge pull request #10 from xboston/update-version
TimonKK Sep 13, 2018
b01ac91
remove unused encodeRow
xboston Sep 30, 2018
f86b931
add withTotals
xboston Sep 30, 2018
0e6ab7b
Merge pull request #14 from xboston/feature/withTotals
TimonKK Oct 29, 2018
4cff2be
Update package.json version
Oct 29, 2018
ec41c01
boolean and int values problem fix
Tgarifullin Nov 2, 2018
0758a42
Merge pull request #1 from Tgarifullin/Tgarifullin-patch-1
Tgarifullin Nov 2, 2018
115393f
zero int values inserting problem fix
Tgarifullin Nov 2, 2018
a0bedd9
Update test.js
Tgarifullin Nov 3, 2018
9b504c3
Merge pull request #16 from Tgarifullin/master
TimonKK Nov 4, 2018
b9df4fb
Add new way for callback and fix test
Jan 12, 2019
49b5b61
Add new option and test
Jan 21, 2019
352f1c9
Add TLS/SSL Protocol support and async iteration over SELECT
Feb 7, 2019
10a4951
Add hack for compatibility
Feb 12, 2019
ef204c0
Update README.md
vdemtcev Feb 12, 2019
564288d
Merge pull request #18 from vdemtcev/patch-1
TimonKK Feb 13, 2019
9bccb1d
Add some fearchers
Feb 13, 2019
a78786f
Fix username & password use
Feb 14, 2019
baf0209
TypeScript types definition
lexasa Feb 20, 2019
cf5c0e5
Fixed package name
lexasa Feb 20, 2019
643175f
Merge pull request #19 from lexasa/master
TimonKK Feb 20, 2019
8a7f3ba
Change version for publishing
Feb 20, 2019
5f51bfa
Fix EXISTS query
Feb 25, 2019
075766c
Add method "desttroy"
Mar 11, 2019
4de1b8d
Fix stuped mistake
Mar 19, 2019
89650dd
Fix #2
Mar 19, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
245 changes: 182 additions & 63 deletions README.md
Original file line number Diff line number Diff line change
@@ -2,77 +2,196 @@
NodeJS client for [ClickHouse](https://clickhouse.yandex/).
Send query over HTTP interface.

Install:

```bash
npm i clickhouse
```

Example:

```javascript
var async = require('async');
const { ClickHouse } = require('clickhouse');

const clickhouse = new ClickHouse();
```
or with all options:

```javascript
const clickhouse = new ClickHouse({
url: 'http://localhost',
port: 8123,
debug: false,
basicAuth: null,
isUseGzip: false,
config: {
session_timeout : 60,
output_format_json_quote_64bit_integers : 0,
enable_http_compression : 0
},

// This object merge with request params (see request lib docs)
reqParams: {
...
}
});
```

or change

basicAuth: null
to

basicAuth: {
username: 'default',
password: '',
},


***

var query = 'SELECT FlightDate, DestCityName, AirlineID, DestStateFips FROM ontime LIMIT 10';
var ch = new ClickHouse({
url : 'http://localhost',
port : 8123,
debug : false
Exec query:
```javascript
const queries = [
'DROP TABLE IF EXISTS session_temp',

`CREATE TABLE session_temp (
date Date,
time DateTime,
mark String,
ips Array(UInt32),
queries Nested (
act String,
id UInt32
)
)
ENGINE=MergeTree(date, (mark, time), 8192)`,

'OPTIMIZE TABLE ukit.loadstat PARTITION 201807 FINAL'
];

for(const query of queries) {
const r = await clickhouse.query(query).toPromise();

console.log(query, r);
}
````

***

Exec by callback way:
```javascript
clickhouse.query(query).exec(function (err, rows) {
...
});
````

async.parallel(
[
function (cb) {
***

// single query
ch.query(query, function (err, rows) {
console.log('single query result', err, rows);

cb(err);
});
},

function (cb) {
var error = null;

// query with data streaming
ch.query(query)
.on('data', function (data) {
console.log('data', data);
})
.on('error', function (err) {
error = err;
})
.on('end', function () {
cb(error);
});
Stream:
```javascript
clickhouse.query(`SELECT number FROM system.numbers LIMIT 10`).stream()
.on('data', function() {
const stream = this;

stream.pause();

setTimeout(() => {
stream.resume();
}, 1000);
})
.on('error', err => {
...
})
.on('end', () => {
...
});
```

or **async** stream:
```javascript
// async iteration
for await (const row of clickhouse.query(sql).stream()) {
console.log(row);
}
```

***

As promise:
```javascript
const rows = await clickhouse.query(query).toPromise();

// use query with external data
const rows = await clickhouse.query('SELECT * AS count FROM temp_table', {
external: [
{
name: 'temp_table',
data: e._.range(0, rowCount).map(i => `str${i}`)
},
]
}).toPromise();
```

***

Set session:
```javascript
clickhouse.sessionId = '...';
const r = await clickhouse.query(
`CREATE TEMPORARY TABLE test_table
(_id String, str String)
ENGINE=Memory`
).toPromise();
````

function(cb) {

// insert rows
ch.insertMany(
'sometable',
[
// row 1
[
'2016-07-02',
'1',
12
],

// row 2
[
'2016-07-03',
'2',
30
]
],
function(err, result) {
if (err) return cb(err);

console.log('insert result', result);
}
);
}
],
function () {
process.exit();

Insert stream:
```javascript
const ws = clickhouse.insert('INSERT INTO session_temp').stream();
for(let i = 0; i <= 1000; i++) {
await ws.writeRow(
[
e._.range(0, 50).map(
j => `${i}:${i * 2}:${j}`
).join('-')
]
);
}

//wait stream finish
const result = await ws.exec();
```

***

Pipe readable stream to writable stream (across transform):
```javascript
const rs = clickhouse.query(query).stream();

const tf = new stream.Transform({
objectMode : true,
transform : function (chunk, enc, cb) {
cb(null, JSON.stringify(chunk) + '\n');
}
);
});

clickhouse.sessionId = Date.now();
const ws = clickhouse.insert('INSERT INTO session_temp2').stream();

const result = await rs.pipe(tf).pipe(ws).exec();
```

***

```
**Changelogs**:
* 2019-02-13
- Add compatibility with user and username options
* 2019-02-07
- Add TLS/SSL Protocol support
- Add async iteration over SELECT



**Links**
* request lib doc https://github.com/request/request#requestoptions-callback
25 changes: 25 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/// <reference types="node" />

declare module 'clickhouse' {
import {Stream} from 'stream';

type callbackExec = (error: Error, rows?: Object[]) => void;

export class ClickHouse {
constructor(opts: Object);
query(query: String, reqParams?: object): QueryCursor;
insert(query: String, data?: object): QueryCursor;
sessionId: string;
}

export class WriteStream extends stream.Transform {
writeRow(data: Array<any>);
exec();
}

class QueryCursor {
async toPromise(): Promise<Object[]>;
exec(callback: callbackExec);
stream(): Stream | WriteStream;
}
}
Loading