ganjiang-datasource
ganjiang datasource crud sdk.
:star: Features
- support ganjiang datasource crud - support datasource query:tada: Install
npm install ganjiang-datasource --save
:ghost: Test
npm test
:rocket: Usage
if you want print out the debug info, please setDEBUG='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
})