npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

datahub-restful-core

v2.1.2

Published

export mysql restful interface for koa v1

Downloads

183

Readme

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