datahub-restful-core
export mysql restful interface for koa v1
Last updated a month ago by jifengwang .
ISC · Original npm · Tarball · package.json
$ cnpm install datahub-restful-core 
SYNC missed versions from official npm registry.

datahub-restful-core NPM version NPM monthly downloads NPM total downloads Linux Build Status Windows Build Status

export mysql as restful service on koa v1

中文版

Usage


const koa = require('koa');

const {DatahubRestfulCore} = require('../index');
let port = process.env.PORT || 3003;
let host = process.env.IP || '127.0.0.1';

const core = new DatahubRestfulCore({
    port, host,
    name: 'demo',
    endpoint: "/",
    options:
        {
            client: 'mysql',
            connection: {
                host: '127.0.0.1',
                user: 'root',
                password: '',
                database: 'datahub',
                supportBigNumbers: true,
                bigNumberStrings: true,
                multipleStatements: true
            },
            acquireConnectionTimeout: 60000,
            pool: {min: 0, max: 2}
        }
});

app = core.mount(koa());

app.listen(port, host, function () {
    console.log('Datahub started http://%s:%s', host, port);
});

//curl -v http://127.0.0.1:3003/demo/

features:-)

  • router rules
  • restful interfaces
  • POST/GET/PUT/DELETE
  • pagination
  • order by
  • composite primary keys
  • custom SQL
  • search
  • or condition
  • hooks
  • not
  • in,not in
  • between,not between
  • null,empty string
  • charset

misc

  • execute vm
  • export to xls file
  • import

URL schema

API

  • all databases: [your ip:port/api/v1/databases](your ip:port/api/v1/databases)

  • all schemas of some database: [your ip:port/api/v1/{database}](your ip:port/api/v1/{db})

  • declare charset: ?_charset=utf8

  • all tables of some schema : [your ip:port/api/v1/{db}/{schema}](your ip:port/api/v1/{db}/{schema})

  • table of schema : [your ip:port/api/v1/{db}/{schema}/{table}](your ip:port/api/v1/{db}/{schema}/{table}) RESTFUL support on above url

  • query: GET to [your ip:port/api/v1/{db}/{schema}/{table}?{k}={v}](your ip:port/api/v1/{db}/{schema}/{table}?{k}={v})

    • support operators e.g. contains: [your ip:port/api/v1/{db}/{schema}/{table}?{k}:contains={v}](your ip:port/api/v1/{db}/{schema}/{table}?{k}:contains={v}),list all records contains keyword v
    • contains : like %keyword% contains keyword
    • startswith: like keyword% starts with keyword
    • endswith : like %keyword endswithkeyword
    • gt or >: greater than
    • lt or < : less than
    • gte : greater than or equals
    • lte :less than or equals
    • pagination: page=0 (0 represents first page)pageSize=3(records per page)
    • order by: _sort=-policy_name,id order by policy_name desc and order by id asc,
    • Or: [your ip:port/api/v1/{db}/{schema}/{table}?k=v&{or:k2}={v2}](your ip:port/api/v1/{db}/{schema}/{table}?k=v&{or:k2}={v2})
    • not:
    • isnull: IS NULL
    • isnotnull: IS NOT NULL
    • isempty: empty string
    • isnotempty: not empoty string
    • in / notin: parameters are array somecolumn:in=a,b,c orsomecolumn:in=a&somecolumn:in=b&somecolumn:in=c
    • between / notbetween: parameters are array somecolumn:between=1,100
    • nested or more complex query,use the complex query
    • return result example
    {"content":[{"id":83,"wifi_id":85,"policy_name":"wifi83_-78486031","status":-1,
    "create_time":"2016-05-12T12:43:19.000Z"},{"id":70,"wifi_id":82,"policy_name":"wifi",
    "status":-1,"create_time":"2016-05-11T02:57:22.000Z"},
    {"id":75,"wifi_id":82,"policy_name":"wifi",
    "status":1,"create_time":"2016-05-11T03:56:19.000Z"}],
    "page":{"size":30,"totalElements":3,"totalPages":1,"number":0}}
    
  • complex query:

  • get: GET [your ip:port/api/v1/{db}/{schema}/{table}/{id}](your ip:port/api/v1/{db}/{schema}/{table}/{id})

  • get(composite primary keys): GET [your ip:port/api/v1/{db}/{schema}/{table}/{key1}={a};{key2}={b}](your ip:port/api/v1/{db}/{schema}/{table}/{key1}={a};{key2}={b})

  • create: JSON POST to [your ip:port/api/v1/{db}/{schema}/{table}](your ip:port/api/v1/{db}/{schema}/{table})

  • update: JSON PUT to [your ip:port/api/v1/{db}/{schema}/{table}/{id}](your ip:port/api/v1/{db}/{schema}/{table}/{id})

  • delete: DELETE [your ip:port/api/v1/{db}/{schema}/{table}/{id}](your ip:port/api/v1/{db}/{schema}/{table}/{id})

  • patch(Partial update): PATCH [your ip:port/api/v1/{db}/{schema}/{table}/{id}](your ip:port/api/v1/{db}/{schema}/{table}/{id})

  • custom SQL: GET [your ip:port/api/v1/{db}/{schema}/_q/{key}](your ip:port/api/v1/{db}/{schema}/_q/{key})

  • custom SQL with pagination: GET [your ip:port/api/v1/{db}/{schema}/_q/{key}/pagination](your ip:port/api/v1/{db}/{schema}/_q/{key}/pagination)

  • hooks

    • pre hook * [your ip:port/api/v1/?_hook:before=someHook1](your ip:port/api/v1/?_hook:before=someHook1)
    • post hook * [your ip:port/api/v1/?_hook:after=someHook1](your ip:port/api/v1/?_hook:after=someHook1)
  • execute SQL: GET/POST sql to [your ip:port/api/v1/{db}/sqlExecutor](your ip:port/api/v1/{db}/sqlExecutor)

  • execute SQL with pagination: GET/POST sql to [your ip:port/api/v1/{db}/sqlExecutor/pagination](your ip:port/api/v1/{db}/sqlExecutor/pagination)

  • support queryString as ?sql=select * from t_x_table where f_some_field = :value&value=value

KnexHelper object

- getInternalApiUrl() get internal api url`http://${host}:${port}${endpoint}`
- * findConnectionByDbkey(dbkey) get the db id as `cdb_x`
- getDatabaseConnection(cdbId) get datbase raw connection object

Test

tnpm i -g mocha
tnpm run test

const knex1 = require('knex')({ client: 'mysql', connection: { host: '172.21.16.20', user: 'writeuser', password: 's$%dSDdfdf3DYU23', port: 23306, insecureAuth: true, supportBigNumbers: true, bigNumberStrings: true }, acquireConnectionTimeout: 60000 }); knex1.raw('show databases').then(console.log)

Current Tags

  • 1.1.8-beta1                                ...           beta (7 months ago)
  • 2.0.3                                ...           latest (a month ago)
  • 2.0.0                                ...           next (a month ago)
  • 1.1.7-privilege7                                ...           privilege (7 months ago)

49 Versions

  • 2.0.3                                ...           a month ago
  • 2.0.2                                ...           a month ago
  • 2.0.1                                ...           a month ago
  • 2.0.0                                ...           a month ago
  • 2.0.0-test.4                                ...           a month ago
  • 2.0.0-test.3                                ...           a month ago
  • 2.0.0-test.2                                ...           a month ago
  • 1.2.4                                ...           a month ago
  • 2.0.0-test.1                                ...           a month ago
  • 1.2.3                                ...           a month ago
  • 1.2.2                                ...           a month ago
  • 1.3.1                                ...           4 months ago
  • 1.2.1                                ...           6 months ago
  • 1.2.0                                ...           6 months ago
  • 1.1.9                                ...           6 months ago
  • 1.1.7-privilege7                                ...           7 months ago
  • 1.1.8                                ...           7 months ago
  • 1.1.8-beta1                                ...           7 months ago
  • 1.1.7-beta1                                ...           7 months ago
  • 1.1.7-privilege6                                ...           7 months ago
  • 1.1.7-privilege5                                ...           7 months ago
  • 1.1.7-privilege4                                ...           7 months ago
  • 1.1.7-privilege3                                ...           7 months ago
  • 1.1.7-privilege2                                ...           7 months ago
  • 1.1.7-privilege1                                ...           7 months ago
  • 1.1.6                                ...           7 months ago
  • 1.1.5                                ...           7 months ago
  • 1.1.5-beta2                                ...           7 months ago
  • 1.1.5-beta                                ...           7 months ago
  • 1.1.4                                ...           7 months ago
  • 1.1.3                                ...           8 months ago
  • 1.1.2                                ...           8 months ago
  • 1.1.1                                ...           8 months ago
  • 1.1.1-beta5                                ...           8 months ago
  • 1.1.1-beta4                                ...           8 months ago
  • 1.1.1-beta3                                ...           8 months ago
  • 1.1.1-beta2                                ...           8 months ago
  • 1.1.1-beta                                ...           8 months ago
  • 1.1.0                                ...           9 months ago
  • 1.0.9                                ...           9 months ago
  • 1.0.8                                ...           9 months ago
  • 1.0.7                                ...           a year ago
  • 1.0.6                                ...           a year ago
  • 1.0.5                                ...           a year ago
  • 1.0.4                                ...           a year ago
  • 1.0.3                                ...           a year ago
  • 1.0.2                                ...           a year ago
  • 1.0.1                                ...           a year ago
  • 1.0.0                                ...           a year ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 1
Last Day 0
Last Week 0
Last Month 193
Dependencies (20)
Dev Dependencies (11)
Dependents (0)
None

Copyright 2014 - 2017 © taobao.org |