web-backset.cn/apps/server/src/controller/home.controller.ts
2023-02-26 16:45:43 +08:00

34 lines
850 B
TypeScript

import { Controller, Get, Inject } from '@midwayjs/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Context } from '@midwayjs/koa';
import { Photo } from '../entity/photo.entity';
import { Repository } from 'typeorm';
@Controller('/')
export class HomeController {
@Inject()
ctx: Context;
@InjectEntityModel(Photo)
photoModel: Repository<Photo>;
@Get('/testMysql')
async testMysql() {
// create a entity object
let photo = new Photo();
photo.name = 'Me and Bears';
photo.description = 'I am near polar bears';
photo.filename = 'photo-with-bears.jpg';
photo.views = 1;
photo.isPublished = true;
// save entity
const photoResult = await this.photoModel.save(photo);
// save success
console.log('photo id = ', photoResult.id);
this.ctx.body = photoResult.id;
}
}