npm.io
0.0.8 • Published 7 years ago

ganjiang-datasource

Licence
MIT
Version
0.0.8
Deps
3
Size
11 kB
Vulns
25
Weekly
0
Stars
1

ganjiang-datasource

Build Status Codacy Badge Coverage Status Dependencies Known Vulnerabilities

npm version NPM downloads

issues

license

ganjiang datasource crud sdk.

Features

  • support ganjiang datasource crud
  • support datasource query

Install

npm install ganjiang-datasource --save

Test

npm test

Usage

if you want print out the debug info, please set DEBUG='ds' in process environment by command: export DEBUG='ds'

Init ganjiang datasource manager

import GanJiangDataSource from 'ganjiang-datasource'

const ganjiang = new GanJiangDataSource({
  host: 'xx.xx.xx.xx', // datasource host required
  token: 'access_token' // datasource api access token optional
})
Create DataSource
Create MySQL DataSource
const DATASOURCE_INFO = {
  name: 'test',
  host: 'host',
  port: 3306,
  database: 'database',
  user: 'user',
  password: 'password',
  db_cfg: { // 可选
    limit: 100 // 限制select最多返回100, 默认是1000
  },
  dialect: 'mysql', // optional
  token: '0123456789', // read, update, delete or query datasource need this token
  whiteList: [
    'select::null::(.*)', // db white list
  ],
  type: 1 // 0 for api, 1 for mysql 2 for psql 3 for api gateway
}
const data = await ganjiang.create(DATASOURCE_INFO) // {id: 'xxxxx'}
Create Api DataSource
const API_INFO = {
  name: 'api_test',
  url: 'http://xxx.xxx.xx.xx/url/path',
  method: 'get',
  token: '0123456789',
  headers: {}, // default headers
  query: {}, // default query
  body: {}, // default body
  type: 0
}
const data = await ganjiang.create(API_INFO) // {id: 'xxxxx'}
Create Api GateWay DataSource
const API_INFO = {
  name: 'api_test',
  url: 'http://xxx.xxx.xx.xx/url/path',
  method: 'get',
  token: '0123456789',
  headers: {}, // default headers
  query: {}, // default query
  body: {}, // default body
  type: 3,
  appKey: 'your app key',
  appSecret: 'your app secret'
}
const data = await ganjiang.create(API_INFO) // {id: 'xxxxx'}
Get DataSource
const opt = { // optional
  attributes: 'database,token' // get datasource and token property only
}
const data = await ganjiang.read({
  name: 'datasource name',
  token: 'datasource token'
}, opt) // DATASOURCE_INFO
Update DataSource
const num = await ganjiang.update({
  name: 'datasource name',
  token: 'datasource token'
}, updateInfo) // updateInfo would be part of DATASOURCE_INFO

// num indicates the number updated successfully
Delete DataSource
const num = await ganjiang.delete({
  name: 'datasource name',
  token: 'datasource token'
})
// num indicates the number deleted successfully
Query DataSource with sql
const data = await ganjiang.query({
  name: 'datasource name',
  token: 'datasource token'
}, {
  sql: 'sql statement'
})
  • for dynamic values, using values to prevent from sql inject
const data = await ganjiang.query({
  name: 'datasource name',
  token: 'datasource token'
}, {
  sql: 'select * from app where id = ?',
  values: [1] // select * from app where id = 1
})
Request API (GateWay)
const data = await ganjiang.query({
  name: 'api name',
  token: 'api token'
}, {
  query: {}, // merge with default query
  body: {},// merge with default body
  headers: {},// merge with default headers
  opt: {}// override default options
})

WHILTE_LIST_AUTHORITY

you could ready more about white list in node-sql-parser module

License

MIT

Keywords