typescript egg mysql_egg.js typescript 开发采坑记录

常年使用sails 开发node 后端 苦不堪言,文档缺失,迭代较慢,并且周边也不是那么完善,虽说基本能满足需求 但是还是少了灵活性。 待到egg 推出之时就在观察,最近新开项目遂采用egg - ts

egg-mysql

这玩意没有index.d.ts .所以 你想使用app.mysql 是编译不过的,所以要用 ts 的merge 来给Application上挂载一个mysql 我们可以这么做

./typings/index.d.ts 写入

declare module 'egg' {

interface mysql {

get(tableName: String, find: {}): Promise

query(sql: String, values: Any[]): Promise

}

interface Application {

mysql: mysql;

}

}

可以在上面给mysql 加点方法这样就有提示了。

egg-sequelize

为node中较为常用的orm 框架。

这个玩意我们可以细说。

首先你要做一个model 来定义这个表中的字段,但是为了方便开发,我们得加点料。

/app/model/model.ts 这里写一个基类的basemodel 这样以后所有的model都可以用到基类

import { Application } from 'egg';

import { snakeCase } from 'lodash';

import * as moment from 'moment';

import { DefineAttributes, SequelizeStatic } from 'sequelize';

export default function BaseModel(

app: Application,

table: string,

attributes: DefineAttributes,

options: object = {},

) {

const { Op, UUID, UUIDV4 } = app.Sequelize;

const modelSchema = app.model.define(table, {

id: {

type: UUID,

unique: true,

primaryKey: true,

allowNull: false,

defaultValue: UUIDV4,

},

...attributes,

...getDefaultAttributes(opt

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Egg.js 是一个基于 Node.js 和 Koa 的 Web 框架,它提供了很多便捷的功能和插件,让开发者可以快速地构建 Web 应用程序。TypeScript 则是一种静态类型检查的编程语言,可以帮助我们在编写代码时更早地捕获一些错误,提高代码的可维护性和可读性。MySQL 则是一种常用的关系型数据库,用于存储和管理数据。 在 Egg.js 中使用 TypeScriptMySQL,我们可以使用 egg-mysql 插件来连接和操作 MySQL 数据库,并使用 TypeScript 的类型检查来保证代码的正确性。具体步骤如下: 1. 创建 Egg.js 项目 ```bash $ npm i egg-init -g $ egg-init egg-typescript-mysql --type=ts $ cd egg-typescript-mysql $ npm i ``` 2. 安装 egg-mysql 插件 ```bash $ npm i egg-mysql ``` 3. 配置 MySQL 数据库连接 在 `config/config.default.ts` 中添加以下配置: ```typescript config.mysql = { client: { host: 'localhost', port: '3306', user: 'root', password: 'password', database: 'test', }, app: true, agent: false, }; ``` 其中,`host`、`port`、`user`、`password`、`database` 分别为你的 MySQL 数据库连接信息。 4. 创建 Model 在 `app/model` 目录下创建一个新的文件 `user.ts`,用于定义操作用户数据的 Model: ```typescript import { Application } from 'egg'; export default (app: Application) => { const { INTEGER, STRING } = app.Sequelize; const User = app.model.define('user', { id: { type: INTEGER, primaryKey: true, autoIncrement: true, }, name: { type: STRING(50), allowNull: false, }, age: { type: INTEGER, allowNull: false, }, }); return User; }; ``` 其中,`app.Sequelize` 是通过 egg-sequelize 插件提供的 Sequelize 实例。 5. 创建 Controller 和 Service 在 `app/controller` 目录下创建一个新的文件 `user.ts`,用于处理用户相关的请求: ```typescript import { Controller } from 'egg'; export default class UserController extends Controller { async index() { const { ctx } = this; const users = await ctx.service.user.list(); ctx.body = { users }; } async show() { const { ctx } = this; const user = await ctx.service.user.get(ctx.params.id); ctx.body = { user }; } async create() { const { ctx } = this; const { name, age } = ctx.request.body; const user = await ctx.service.user.create(name, age); ctx.body = { user }; } async update() { const { ctx } = this; const { id, name, age } = ctx.request.body; const user = await ctx.service.user.update(id, name, age); ctx.body = { user }; } async destroy() { const { ctx } = this; const user = await ctx.service.user.delete(ctx.params.id); ctx.body = { user }; } } ``` 在 `app/service` 目录下创建一个新的文件 `user.ts`,用于实现对用户数据的操作: ```typescript import { Service } from 'egg'; export default class UserService extends Service { async list() { const { ctx } = this; const users = await ctx.model.User.findAll(); return users; } async get(id: number) { const { ctx } = this; const user = await ctx.model.User.findByPk(id); return user; } async create(name: string, age: number) { const { ctx } = this; const user = await ctx.model.User.create({ name, age }); return user; } async update(id: number, name: string, age: number) { const { ctx } = this; const user = await ctx.model.User.findByPk(id); if (!user) { ctx.throw(404, 'user not found'); } await user.update({ name, age }); return user; } async delete(id: number) { const { ctx } = this; const user = await ctx.model.User.findByPk(id); if (!user) { ctx.throw(404, 'user not found'); } await user.destroy(); return user; } } ``` 6. 路由配置 在 `app/router.ts` 中配置路由: ```typescript import { Application } from 'egg'; export default (app: Application) => { const { controller, router } = app; router.get('/users', controller.user.index); router.get('/users/:id', controller.user.show); router.post('/users', controller.user.create); router.put('/users', controller.user.update); router.delete('/users/:id', controller.user.destroy); }; ``` 7. 启动应用 ```bash $ npm run dev ``` 以上就是使用 Egg.jsTypeScriptMySQL 开发 Web 应用程序的基本步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值