Skip to content

Commit

Permalink
Merge pull request #13 from KoteiIto/1.x
Browse files Browse the repository at this point in the history
1.x
  • Loading branch information
KoteiIto committed Jan 1, 2018
2 parents 346713c + 9f2a2e8 commit 28e58b8
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 44 deletions.
55 changes: 21 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,15 @@ Install with:
## Usage Example

```js
var credentials = {
accessKeyId: 'xxxx',
secretAccessKey: 'xxxx',
var awsConfig = {
region: 'xxxx',
}
var config = {
var clientConfig = {
bucketUri: 's3://xxxx'
}

var Athena = require("athena-client")
var client = Athena.Client(credentials, config)
var athena = require("athena-client")
var client = athena.createClient(clientConfig, awsConfig)

client.execute('SELECT 1', function(err, data) {
if (err) {
Expand All @@ -30,7 +28,8 @@ client.execute('SELECT 1', function(err, data) {
})

// You can also execute query with promises
client.execute('SELECT 1').then(function(data) {
client.execute('SELECT 1').toPromise()
.then(function(data) {
console.log(data)
}).catch(function(err) {
console.error(err)
Expand All @@ -39,40 +38,34 @@ client.execute('SELECT 1').then(function(data) {

# API
### athena = require("athena-client")
This module exposes the `Client` method, which execute query to AWS Athena
This module exposes the `createClient` method, which execute query to AWS Athena

### client = athena.Client([_credentials_], [_config_])
Returns a client instance attached to the account specified by the given credentials and config.
### client = athena.createClient([_clientConfig_], [_awsConfig_])
Returns a client instance attached to the account specified by the given clientConfig and awsConfig .

The credentials can be specified as an object with `accessKeyId` and `secretAccessKey` and `region` members such as the following:

```javascript
var credentials = {
accessKeyId: 'xxxx',
secretAccessKey: 'xxxx',
region: 'xxxx',
}
```

#### `config` object properties
#### `clientConfig` object properties
| Property | Default | Description |
|-----------|-----------|-------------|
| bucketUri | __Required__ | URI of S3 bucket for saving a query results file(*.csv) and a metadata file (*.csv.metadata) |
| pollingInterval | 1000 | Interval of polling sql results (ms) |
| queryTimeout | 0 | Timeout of query execution. `0` is no timeout |
| format | 'array' | If `'array'`, the result of the query is as the following `[ { _col0: '1' } , { _col0: '2' }]` . If `'raw'`, the result of query is same with `aws-sdk` |
| concurrentExecMax | 5 | The number of cuncurrent execution of query max. it should be set `smaller than AWS Service limit`(default is 5) |

### client.execute([_query_], [_options_], [_callback_])
Returns query result. The _options_ can be specified as an object with `timeout` and `format` members such as the following:

The awsConfig can be specified as an object with `region` and `accessKeyId` and `secretAccessKey` members such as the following:

```javascript
var options = {
timeout: 3000,
format: 'raw',
var awsConfig = {
region: 'xxxx',
accessKeyId: 'xxxx', // Optional
secretAccessKey: 'xxxx', // Optional
}
```

### client.execute([_query_], [_callback_])
Returns query result.

```javascript
client.execute('SELECT 1', function(err, data) {
if (err) {
Expand All @@ -81,14 +74,8 @@ client.execute('SELECT 1', function(err, data) {
console.log(data)
})

client.execute('SELECT 1', {timeout: 3000}, function(err, data) {
if (err) {
return console.error(err)
}
console.log(data)
})

client.execute('SELECT 1').then(function(data) {
client.execute('SELECT 1').toPromise()
.then(function(data) {
console.log(data)
}).catch(function(err) {
console.error(err)
Expand Down
21 changes: 16 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,21 @@ const AWS = require('aws-sdk')
const Request = require('./lib/request')
const Client = require('./lib/client')

exports.Client = (credentials, config) => {
config = config || {}
AWS.config.update(credentials)
const createClient = (clientConfig, awsConfig) => {
clientConfig = clientConfig || {}
if (clientConfig.format) {
console.log('[WARN] The config "format" will be unusable in the next major version update')
}
AWS.config.update(awsConfig)
let athena = new AWS.Athena({ apiVersion: '2017-05-18' })
let request = Request.create(athena)
return Client.create(request, config)
}
return Client.create(request, clientConfig)
}

const client = (awsConfig, clientConfig) => {
console.log('[WARN] The function "Client([awsConfig], [clientConfig])" will be unusable in the next major version update. Please use "createClient([clientConfig], [awsConfig])".')
return createClient(clientConfig, awsConfig)
}

exports.createClient = createClient
exports.Client = client
29 changes: 24 additions & 5 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,21 @@ class Client {
}

execute(query, options, callback) {
return new Promise((resolve, reject) => {
options = options || {}
let toPromiseTimer
const promise = new Promise((resolve, reject) => {
if (typeof (options) == 'function') {
callback = options
options = {}
options = undefined
}
if (options) {
if (callback) {
console.log('[WARN] The function "execute(query, options, callback)" will be unusable in the next major version update. Please use "execute(query, callback)".')
} else {
console.log('[WARN] The function "execute(query, options)" will be unusable in the next major version update. Please use "execute(query).toPromise()".')
}
}

options = options || {}
let nowConfig = Object.assign({}, this.config)
let self = this
let right = self.getExecRight()
Expand Down Expand Up @@ -103,6 +112,16 @@ class Client {
return handleError(err, resolve, reject, callback)
})
})
if (!callback) {
toPromiseTimer = setTimeout(() => {
console.log('[WARN] The function "execute(query)" will be unusable in the next major version update. Please use "execute(query).toPromise()".')
}, 100)
}
promise.toPromise = () => {
clearTimeout(toPromiseTimer)
return promise
}
return promise
}
}

Expand All @@ -111,11 +130,11 @@ function checkConfig(config_) {
throw new Error('buket uri required')
}

if(!config_.pollingInterval || config_.pollingInterval < 0) {
if (!config_.pollingInterval || config_.pollingInterval < 0) {
config_.pollingInterval = 0;
}

if(!config_.queryTimeout || config_.queryTimeout < 0) {
if (!config_.queryTimeout || config_.queryTimeout < 0) {
config_.queryTimeout = 0;
}
}
Expand Down

0 comments on commit 28e58b8

Please sign in to comment.