1.2.29 • Published 6 months ago

koa-msc v1.2.29

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

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)
1.2.29

6 months ago

1.2.16

12 months ago

1.2.17

12 months ago

1.2.15

12 months ago

1.2.18

12 months ago

1.2.19

12 months ago

1.2.20

12 months ago

1.2.23

12 months ago

1.2.24

12 months ago

1.2.21

12 months ago

1.2.22

12 months ago

1.2.27

9 months ago

1.2.28

9 months ago

1.2.25

12 months ago

1.2.26

9 months ago

1.2.13

1 year ago

1.2.14

1 year ago

1.2.12

1 year ago

1.2.11

1 year ago

1.2.0

1 year ago

1.1.29

1 year ago

1.1.28

1 year ago

1.2.8

1 year ago

1.2.7

1 year ago

1.2.6

1 year ago

1.2.5

1 year ago

1.2.4

1 year ago

1.2.3

1 year ago

1.2.2

1 year ago

1.2.1

1 year ago

1.1.30

1 year ago

1.1.31

1 year ago

1.2.10

1 year ago

1.1.9

2 years ago

1.1.8

2 years ago

1.1.12

2 years ago

1.1.11

2 years ago

1.1.10

2 years ago

1.1.16

2 years ago

1.1.15

2 years ago

1.1.14

2 years ago

1.1.13

2 years ago

1.1.19

2 years ago

1.1.18

2 years ago

1.1.17

2 years ago

1.2.9

1 year ago

1.1.23

1 year ago

1.1.22

1 year ago

1.1.21

2 years ago

1.1.20

2 years ago

1.1.27

1 year ago

1.1.26

1 year ago

1.1.25

1 year ago

1.1.24

1 year ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.9

2 years ago

1.1.7

2 years ago

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.0.8-5

2 years ago

1.0.8-4

2 years ago

1.0.8-3

2 years ago

1.0.8-2

2 years ago

1.0.8-1

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago