@things-factory/aws-base 8.0.0-beta.9 → 8.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist-server/tsconfig.tsbuildinfo +1 -1
- package/package.json +4 -4
- package/server/controllers/athena-controller.ts +105 -0
- package/server/controllers/index.ts +2 -0
- package/server/index.ts +7 -0
- package/server/middlewares/index.ts +3 -0
- package/server/migrations/index.ts +9 -0
- package/server/routes.ts +26 -0
- package/server/service/athena/athena-query.ts +24 -0
- package/server/service/athena/index.ts +4 -0
- package/server/service/index.ts +14 -0
- package/tsconfig.json +9 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@things-factory/aws-base",
|
|
3
|
-
"version": "8.0.
|
|
3
|
+
"version": "8.0.2",
|
|
4
4
|
"main": "dist-server/index.js",
|
|
5
5
|
"things-factory": true,
|
|
6
6
|
"author": "heartyoh <heartyoh@hatiolab.com>",
|
|
@@ -23,10 +23,10 @@
|
|
|
23
23
|
"migration:create": "node ../../node_modules/typeorm/cli.js migration:create ./server/migrations/migration"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@things-factory/auth-base": "^8.0.
|
|
27
|
-
"@things-factory/shell": "^8.0.
|
|
26
|
+
"@things-factory/auth-base": "^8.0.2",
|
|
27
|
+
"@things-factory/shell": "^8.0.2",
|
|
28
28
|
"athena-express": "^7.1.4",
|
|
29
29
|
"aws-sdk": "^2.1096.0"
|
|
30
30
|
},
|
|
31
|
-
"gitHead": "
|
|
31
|
+
"gitHead": "39d60f56e142561233ddf6d47b539c637971357c"
|
|
32
32
|
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { AthenaExpress } from 'athena-express'
|
|
2
|
+
import AWS from 'aws-sdk'
|
|
3
|
+
|
|
4
|
+
import { config, logger } from '@things-factory/env'
|
|
5
|
+
|
|
6
|
+
const athenaConfig = config.get('athena', {})
|
|
7
|
+
const awsCredentials = {
|
|
8
|
+
region: athenaConfig.region,
|
|
9
|
+
accessKeyId: athenaConfig.accessKeyId,
|
|
10
|
+
secretAccessKey: athenaConfig.secretAccessKey
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
if (!athenaConfig.region) {
|
|
14
|
+
console.log(`\
|
|
15
|
+
application config for 'athena' required like following
|
|
16
|
+
/*
|
|
17
|
+
athena: {
|
|
18
|
+
accessKeyId: 'YOUR ACCESS KEY ID',
|
|
19
|
+
secretAccessKey: 'YOUR SECRET ACCESS KEY',
|
|
20
|
+
region: 'YOUR S3 REGION'
|
|
21
|
+
options: { athenaExpressConfig
|
|
22
|
+
retry: 200,
|
|
23
|
+
getStats: true,
|
|
24
|
+
formatJson: true
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
*/
|
|
28
|
+
please, refer to https://github.com/ghdna/athena-express#advance-config-parameters for about more detail configuration parameters
|
|
29
|
+
`)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export class AthenaController {
|
|
33
|
+
private athenaExpressClient
|
|
34
|
+
private queryPaginationInfo = {
|
|
35
|
+
NextToken: '',
|
|
36
|
+
QueryExecutionId: ''
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
constructor() {
|
|
40
|
+
if (!athenaConfig.region) {
|
|
41
|
+
return
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
try {
|
|
45
|
+
AWS.config.update(awsCredentials)
|
|
46
|
+
} catch (err) {
|
|
47
|
+
// missing config
|
|
48
|
+
logger.error(err)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// refer to https://github.com/ghdna/athena-express#advance-config-parameters
|
|
52
|
+
// about more detail configuration parameters
|
|
53
|
+
let athenaExpressConfig = {
|
|
54
|
+
aws: AWS,
|
|
55
|
+
...athenaConfig.options
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
this.athenaExpressClient = new AthenaExpress(athenaExpressConfig)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// query parameters: https://github.com/ghdna/athena-express#advance-query-parameters
|
|
62
|
+
/* SAMPLE QUERY PARAMTERS
|
|
63
|
+
{
|
|
64
|
+
sql: "SELECT * FROM elb_logs LIMIT 3" // required,
|
|
65
|
+
db: "sampledb", // optional.
|
|
66
|
+
pagination: 5, //optional
|
|
67
|
+
NextToken: "ARfCDXRjMk...", //optional
|
|
68
|
+
QueryExecutionId: "c274843b-4c5c-4ccf-ac8b-e33d595b927d", //optional
|
|
69
|
+
catalog: "hive" //optional
|
|
70
|
+
}; */
|
|
71
|
+
|
|
72
|
+
// pagination: https://github.com/ghdna/athena-express#pagination
|
|
73
|
+
async query(reqQuery) {
|
|
74
|
+
var queryResult
|
|
75
|
+
try {
|
|
76
|
+
// check if sql key is available in the input parameter
|
|
77
|
+
if (reqQuery.sql === undefined || reqQuery.sql === '') return {}
|
|
78
|
+
|
|
79
|
+
let queryInput = {
|
|
80
|
+
...reqQuery
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// check if pagination would be available.
|
|
84
|
+
if (
|
|
85
|
+
reqQuery.pagination > 0 &&
|
|
86
|
+
this.queryPaginationInfo.NextToken !== '' &&
|
|
87
|
+
this.queryPaginationInfo.QueryExecutionId !== ''
|
|
88
|
+
) {
|
|
89
|
+
queryInput.NextToken = this.queryPaginationInfo.NextToken
|
|
90
|
+
queryInput.QueryExecutionId = this.queryPaginationInfo.QueryExecutionId
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// run the query
|
|
94
|
+
queryResult = await this.athenaExpressClient.query(queryInput)
|
|
95
|
+
|
|
96
|
+
// keep pagination parameters if they are available in the result
|
|
97
|
+
this.queryPaginationInfo.NextToken = queryResult?.NextToken || ''
|
|
98
|
+
this.queryPaginationInfo.QueryExecutionId = queryResult?.QueryExecutionId || ''
|
|
99
|
+
} catch (error) {
|
|
100
|
+
logger.error(error)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return queryResult
|
|
104
|
+
}
|
|
105
|
+
}
|
package/server/index.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
const glob = require('glob')
|
|
2
|
+
const path = require('path')
|
|
3
|
+
|
|
4
|
+
export var migrations = []
|
|
5
|
+
|
|
6
|
+
glob.sync(path.resolve(__dirname, '.', '**', '*.js')).forEach(function(file) {
|
|
7
|
+
if (file.indexOf('index.js') !== -1) return
|
|
8
|
+
migrations = migrations.concat(Object.values(require(path.resolve(file))) || [])
|
|
9
|
+
})
|
package/server/routes.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
process.on('bootstrap-module-global-public-route' as any, (app, globalPublicRouter) => {
|
|
2
|
+
/*
|
|
3
|
+
* can add global public routes to application (auth not required, tenancy not required)
|
|
4
|
+
*
|
|
5
|
+
* ex) routes.get('/path', async(context, next) => {})
|
|
6
|
+
* ex) routes.post('/path', async(context, next) => {})
|
|
7
|
+
*/
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
process.on('bootstrap-module-global-private-route' as any, (app, globalPrivateRouter) => {
|
|
11
|
+
/*
|
|
12
|
+
* can add global private routes to application (auth required, tenancy not required)
|
|
13
|
+
*/
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
process.on('bootstrap-module-domain-public-route' as any, (app, domainPublicRouter) => {
|
|
17
|
+
/*
|
|
18
|
+
* can add domain public routes to application (auth not required, tenancy required)
|
|
19
|
+
*/
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
process.on('bootstrap-module-domain-private-route' as any, (app, domainPrivateRouter) => {
|
|
23
|
+
/*
|
|
24
|
+
* can add domain private routes to application (auth required, tenancy required)
|
|
25
|
+
*/
|
|
26
|
+
})
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import '@things-factory/auth-base' // for ResolverContext
|
|
2
|
+
|
|
3
|
+
import { Arg, Ctx, Query, Root } from 'type-graphql'
|
|
4
|
+
|
|
5
|
+
import { ScalarAny, ScalarObject } from '@things-factory/shell'
|
|
6
|
+
|
|
7
|
+
import { AthenaController } from '../../controllers'
|
|
8
|
+
|
|
9
|
+
const athenaClient = new AthenaController()
|
|
10
|
+
|
|
11
|
+
export class AthenaQuery {
|
|
12
|
+
@Query(returns => ScalarAny, { description: 'To query data in S3 using Amazon Athena SDK' })
|
|
13
|
+
async queryFromAthena(
|
|
14
|
+
@Root() _,
|
|
15
|
+
@Arg('queryData', type => ScalarObject) queryData: any,
|
|
16
|
+
@Ctx() context: ResolverContext
|
|
17
|
+
): Promise<any> {
|
|
18
|
+
const { domain, user } = context.state
|
|
19
|
+
|
|
20
|
+
var queryResult = await athenaClient.query(queryData)
|
|
21
|
+
|
|
22
|
+
return queryResult
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/* IMPORT ENTITIES AND RESOLVERS */
|
|
2
|
+
import { resolvers as AthenaResolvers } from './athena'
|
|
3
|
+
|
|
4
|
+
export const entities = [
|
|
5
|
+
/* ENTITIES */
|
|
6
|
+
]
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
export const schema = {
|
|
10
|
+
resolverClasses: [
|
|
11
|
+
/* RESOLVER CLASSES */
|
|
12
|
+
...AthenaResolvers,
|
|
13
|
+
]
|
|
14
|
+
}
|