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

koa-msc

v1.2.29

Published

一个使用装饰器开发后端API的框架

Downloads

14

Readme

koa-msc

样例

node engine dm

使用示例

  • 完整样例参考example目录代码

1.初始化项目

  1. 点击这里 根据ts-dev-template 模板仓库创建一个你自己的ts项目(需要你有github账号并能正常访问github官网)
  2. 使用git拉取刚刚创建好的项目
git clone https://github.com/用户名/仓库名.git # 此处仅为示范,请勿直接复制

2. 安装依赖

进入刚刚拉下来的项目文件夹,打开终端并执行一下命令

npm install koa-msc

3. 根据你的数据库类型安装对应的依赖

npm i pg pg-hstore # PostgreSQL
npm i mysql2 # MySQL
npm i mariadb # MariaDB
npm i sqlite3 # SQLite
npm i tedious # Microsoft SQL Server
npm i ibm_db # DB2

4. 新建入口文件src/index.ts

import {App} from 'koa-msc'
const app=new App({
    controller_path:'src/controllers',// 存储控制器的路径,相对于启动目录
    service_path:'src/services',// 存储服务的路径,相对于启动目录
    model_path:'src/models',// 存储表模型的路径,相对于启动目录
    transaction: true, // 是否启用事务
    log_level:'info', // 日志等级
    router:{ // KoaRouter初始化配置 参见 https://github.com/koajs/router/blob/master/API.md#new-routeropts
        prefix:'/api'
    },
    sequelzie:{ // Sequelize初始化配置 参见 https://www.sequelize.cn/core-concepts/getting-started
        dialect:'sqlite', // 数据库驱动名称
        storage:path.resolve(process.cwd(),'database.sqlite'),
        database:'test',
        logging(msg){
            app.logger.info(msg)
        }
    },
    // ... Koa初始化配置 参见 https://koajs.com/#application
})
app.start(7777)

5.在src目录新建三个文件夹,分别是controllers,services,models

mkdir controllers
mkdir services
mkdir models

6.在功能对应的文件夹新建User.ts,并编写相关代码

  1. src/models/User.ts
import {Column, Model,BaseModel} from 'koa-msc'
import {DataTypes} from "sequelize";

@Model
class UserModel extends BaseModel{
    @Column(DataTypes.STRING)
    user_id: string
    @Column(DataTypes.STRING)
    name: string
    @Column(DataTypes.INTEGER)
    age: number
}
  1. src/services/User.ts
import {Service,BaseService} from 'koa-msc'
import {ModelStatic,Model} from "sequelize";
import {UserModel} from '../models/User'

@Service
export class UserService extends BaseService<UserModel>{
}
  1. src/controllers/User.ts
import {Controller,BaseController, RequestMapping, Request} from 'koa-msc'
import {UserService} from '../services/User'

@Controller('/hello')
export class UserController extends BaseController<UserService> {
    @RequestMapping('list', Request.get)
    getUserList(ctx) {
        return this.service.list()
    }

    @RequestMapping('add', Request.post)
    addUser(ctx) {
        return this.service.add(ctx.req.body)
    }
}

7. 配置启动命令(若为模板仓库创建并未做修改,可跳过)

package.json的scripts添加启动命令

{
  // ... 其他配置
  scripts: {
    // ... 其他命令
    start: "node ./lib/index.js",// 执行前需先npm run build
    build: "tsc --project tsconfig.json && tsc-alias -p tsconfig.json",// 依赖typescript和tsc-alias,需自行安装依赖
    dev: "ts-node-dev -r tsconfig-paths/register ./src/index.ts", // 依赖 ts-node-dev和tsconfig-paths 需自行安装该依赖
    // ... 其他命令
  },
  // ... 其他配置
}

8. 启动项目

npm run dev # 开发环境
npm start # 生产环境(此处仅做演示,真实情况可根据个人需求处理,如tsc)