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 🙏

© 2025 – Pkg Stats / Ryan Hefner

rest-router-model

v1.9.5

Published

rest-router-model

Downloads

50

Readme

rest-router-model

rest-router-model是一个根据资源描述JOSN配置文件(Resource Config),自动生成REST风格的HTTP接口,并且完成数据模型映射。 它具有功能扩展性,可以通过BaseBusiness类继承,实现业务功能扩展。它是完全业务内包,只需要提供资源config和数据库配置即可。 这样可以更好更快的构建一个微服务,你只需要关心如何设计好你的资源模型。

框架:目前只支持koa上使用。

数据库:目前只支持SQL系列数据库,如Postgres, MSSQL, MySQL, MariaDB, SQLite3, Oracle, Amazon Redshift。(以knex为准)

安装

需要node v8.9.0 版本,或者更高的版本,需支持async函数.

  $ npm install rest-router-model

例子

我们将以一个学校管理服务(SchoolServer)为例: 首先编写一个资源配置文件:resourceConfig.js,下一节会详细的我们如何配置resourceConfig。 以下resourceConfig描述了class资源、student资源、activityGroup资源、还有一个描述学生与活动小组关联关系的studentActivityGroupMembership资源。 在student中通过super字段描述了他所属于上级资源为class, 在studentActivityGroupMembership中,通过membership字段描述他所要关联的那两个资源。

// resourceConfig.js

module.exports = {
    // 班级
    "class": {
        rest_api: 'base',
        params: {
            name:{type:'string'},
            description:{type:'string'},
            createdAt: {type:'time'},
            modifiedAt:{type:'time'},
        },
    },
    // 学生
    "student":{
        rest_api: 'base',
        super: 'class',
        params: {
            name:{type:'string'},
            description:{type:'string'},
            createdAt: {type:'time'},
            modifiedAt:{type:'time'},
        },
    },
    // 活动组 (如:学生会、校蓝球队、...等)
    "activityGroup": {
        type: 'membershipContainer',
        rest_api: 'base',
        params: {
            name:{type:'string'},
            description:{type:'string'},
            createdAt: {type:'time'},
            modifiedAt:{type:'time'},
        },
    },
    // 学生与活动小组的关联关系统
    "studentActivityGroupMembership":{
        type: 'membership',
        memberships : ['student','activityGroup'],
        rest_api: 'base',
        params: {
            name:{type:'string'},
            description:{type:'string'},
            createdAt: {type:'time'},
            modifiedAt:{type:'time'},
        },
    }
};

然后server.js中使用rest-router-model

// server.js

const Koa = require('koa');
const restRouterModel = require('rest-router-model');
const resourceConfig = require('./resourceConfig');

let knexConfig = {
    client: 'mysql',
    connection: {
        host : 'localhost',
        user : 'db-user',
        password : 'xxxxxx',
        database : 'SchoolServerDB',
        port : 3306
    }
}
let extendBusinesses = {};        // 扩展业务接口支持
let options = {
    serverName: 'SchoolServer',  // 配置服务的名称
    ip: 'school.server.com',     // 配置服务域名或ip
    port: '3000',                // 配置服务监听端口
};
const app = new Koa();
restRouterModel.koaRestRouter(resourceConfig, extendBusinesses, knexConfig, options).then(koa_router=>{
    app.use(koa_router.routes());
    app.listen(options.port);
});

运行成功过后,插件会在router的中注册,以下REST HTTP接口:

| Resource | Method | URI | |:--------|:------| :---| |class | POST | /api/:version/classes | |class | GET | /api/:version/classes | |class | GET | /api/:version/classes/:uuid | |class | PUT,POST | /api/:version/classes/:uuid | |class | DELETE | /api/:version/classes/:uuid | |student | POST | /api/:version/students /api/:version/classes/:classUUID/students | |student | GET | /api/:version/students /api/:version/classes/:classUUID/students /api/:version/activityGroups/:activityGroupUUID/students | |student | GET | /api/:version/students/:uuid /api/:version/classes/:classUUID/students/:uuid| |student | PUT,POST | /api/:version/students/:uuid /api/:version/classes/:classUUID/students/:uuid| |student | DELETE | /api/:version/students/:uuid /api/:version/classes/:classUUID/students/:uuid| |activityGroup | POST | /api/:version/activityGroups | |activityGroup | GET | /api/:version/activityGroups /api/:version/students/:studentUUID/activityGroups| |activityGroup | GET | /api/:version/activityGroups/:uuid | |activityGroup | PUT,POST | /api/:version/activityGroups/:uuid | |activityGroup | DELETE | /api/:version/activityGroups/:uuid | |activityGroup | POST | /api/:version/activityGroups/:uuid/add | |activityGroup | POST | /api/:version/activityGroups/:uuid/remove | |studentActivityGroupMemberships | POST | /api/:version/studentActivityGroupMemberships | |studentActivityGroupMemberships | GET | /api/:version/studentActivityGroupMemberships /api/:version/activityGroups/:activityGroupUUID/studentActivityGroupMemberships /api/:version/students/:studentUUID/studentActivityGroupMemberships | |studentActivityGroupMemberships | GET | /api/:version/studentActivityGroupMemberships/:uuid| |studentActivityGroupMemberships | PUT,POST | /api/:version/studentActivityGroupMemberships/:uuid | |studentActivityGroupMemberships | DELETE | /api/:version/studentActivityGroupMemberships/:uuid |

待续... ...