-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 70d4a50
Showing
36 changed files
with
691 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# 🎨 editorconfig.org | ||
|
||
root = true | ||
|
||
[*] | ||
charset = utf-8 | ||
end_of_line = lf | ||
indent_style = space | ||
indent_size = 2 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"extends": "./node_modules/mwts/", | ||
"ignorePatterns": ["node_modules", "dist", "test", "jest.config.js", "typings"], | ||
"env": { | ||
"jest": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
logs/ | ||
npm-debug.log | ||
yarn-error.log | ||
node_modules/ | ||
package-lock.json | ||
yarn.lock | ||
coverage/ | ||
dist/ | ||
.idea/ | ||
run/ | ||
.DS_Store | ||
*.sw* | ||
*.un~ | ||
.tsbuildinfo | ||
.tsbuildinfo.* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
...require('mwts/.prettierrc.json') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# midway quick guide sample | ||
|
||
## Usage | ||
|
||
```bash | ||
$ npm i | ||
$ npm run dev | ||
$ npm run test | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Midway 快速入门示例 | ||
|
||
## 快速入门 | ||
|
||
```bash | ||
$ npm i | ||
$ npm run dev | ||
$ npm run test | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
const { Bootstrap } = require('@midwayjs/bootstrap'); | ||
Bootstrap.run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
testPathIgnorePatterns: ['<rootDir>/test/fixtures'], | ||
coveragePathIgnorePatterns: ['<rootDir>/test/'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
{ | ||
"name": "midway-quick-start", | ||
"version": "1.0.0", | ||
"description": "", | ||
"private": true, | ||
"dependencies": { | ||
"@midwayjs/bootstrap": "^3.0.0", | ||
"@midwayjs/core": "^3.0.0", | ||
"@midwayjs/decorator": "^3.0.0", | ||
"@midwayjs/info": "^3.0.0", | ||
"@midwayjs/koa": "^3.0.0", | ||
"@midwayjs/logger": "^2.14.0", | ||
"@midwayjs/static-file": "^3.4.12", | ||
"@midwayjs/typeorm": "^3.0.0", | ||
"@midwayjs/validate": "^3.0.0", | ||
"@midwayjs/view-nunjucks": "^3.4.12", | ||
"mysql2": "^3.2.0", | ||
"sqlite3": "^5.1.6", | ||
"typeorm": "~0.3.0" | ||
}, | ||
"devDependencies": { | ||
"@midwayjs/cli": "^2.0.0", | ||
"@midwayjs/mock": "^3.0.0", | ||
"@types/jest": "^29.2.0", | ||
"@types/koa": "^2.13.4", | ||
"@types/node": "14", | ||
"cross-env": "^6.0.0", | ||
"jest": "^29.2.2", | ||
"mwts": "^1.0.5", | ||
"nock": "^13.2.9", | ||
"ts-jest": "^29.0.3", | ||
"typescript": "~4.8.0" | ||
}, | ||
"engines": { | ||
"node": ">=12.0.0" | ||
}, | ||
"scripts": { | ||
"start": "NODE_ENV=production node ./bootstrap.js", | ||
"dev": "cross-env NODE_ENV=local midway-bin dev --ts", | ||
"test": "midway-bin test --ts", | ||
"cov": "midway-bin cov --ts", | ||
"lint": "mwts check", | ||
"lint:fix": "mwts fix", | ||
"ci": "npm run cov", | ||
"build": "midway-bin build -c", | ||
"typeorm": "typeorm-ts-node-commonjs" | ||
}, | ||
"midway-bin-clean": [ | ||
".vscode/.tsbuildinfo", | ||
"dist" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "" | ||
}, | ||
"author": "anonymous", | ||
"license": "MIT" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { MidwayConfig } from '@midwayjs/core'; | ||
|
||
export default { | ||
// use for cookie sign key, should change to your own and keep security | ||
keys: '1680822695278_3091', | ||
koa: { | ||
port: 7001, | ||
}, | ||
view: { | ||
defaultViewEngine: 'nunjucks', | ||
}, | ||
typeorm: { | ||
dataSource: { | ||
default: { | ||
type: 'mysql', | ||
host: '127.0.01', | ||
port: 3306, | ||
username: 'root', | ||
password: '', | ||
database: '1stblue', | ||
synchronize: true, // 如果第一次使用,不存在表,有同步的需求可以写 true,注意会丢数据 | ||
logging: true, | ||
|
||
// 或者扫描形式 | ||
entities: ['**/entity/*.entity{.ts,.js}'], | ||
}, | ||
}, | ||
}, | ||
} as MidwayConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { MidwayConfig } from '@midwayjs/core'; | ||
|
||
export default { | ||
koa: { | ||
port: null, | ||
}, | ||
} as MidwayConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Configuration, App } from '@midwayjs/core'; | ||
import * as koa from '@midwayjs/koa'; | ||
import * as validate from '@midwayjs/validate'; | ||
import * as info from '@midwayjs/info'; | ||
import { join } from 'path'; | ||
import { ReportMiddleware } from './middleware/report.middleware'; | ||
import * as view from '@midwayjs/view-nunjucks'; | ||
import { WeatherErrorFilter } from './filter/weather.filter'; | ||
import * as orm from '@midwayjs/typeorm'; | ||
|
||
@Configuration({ | ||
imports: [ | ||
orm, | ||
koa, | ||
validate, | ||
{ | ||
component: info, | ||
enabledEnvironment: ['local'], | ||
}, | ||
view, | ||
], | ||
importConfigs: [join(__dirname, './config')], | ||
}) | ||
export class ContainerLifeCycle { | ||
@App() | ||
app: koa.Application; | ||
|
||
async onReady() { | ||
// add middleware | ||
this.app.useMiddleware([ReportMiddleware]); | ||
// add filter | ||
this.app.useFilter([WeatherErrorFilter]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Inject, Controller, Get, Query } from '@midwayjs/core'; | ||
import { Context } from '@midwayjs/koa'; | ||
import { UserService } from '../service/user.service'; | ||
|
||
@Controller('/api') | ||
export class APIController { | ||
@Inject() | ||
ctx: Context; | ||
|
||
@Inject() | ||
userService: UserService; | ||
|
||
@Get('/get_user') | ||
async getUser(@Query('uid') uid) { | ||
const user = await this.userService.getUser({ uid }); | ||
return { success: true, message: 'OK', data: user }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Controller, Get } from '@midwayjs/core'; | ||
|
||
@Controller('/') | ||
export class HomeController { | ||
@Get('/') | ||
async home(): Promise<string> { | ||
return 'Hello Midwayjs!'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Controller, Inject, Get } from '@midwayjs/core'; | ||
import { PhotoService } from '../service/photo.service'; | ||
|
||
@Controller('/users') | ||
export class UserController { | ||
@Inject() | ||
userService: PhotoService; | ||
|
||
@Get('/') | ||
async home() { | ||
console.log(' all'); | ||
const a = await this.userService.findPhotos(); | ||
return a; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Controller, Get, Inject, Query } from '@midwayjs/core'; | ||
import { WeatherService } from '../service/weather.service'; | ||
import { Context } from '@midwayjs/koa'; | ||
|
||
@Controller('/') | ||
export class WeatherController { | ||
@Inject() | ||
weatherService: WeatherService; | ||
|
||
@Inject() | ||
ctx: Context; | ||
|
||
@Get('/weather') | ||
async getWeatherInfo(@Query('cityId') cityId: string): Promise<void> { | ||
const result = await this.weatherService.getWeather(cityId); | ||
if (result) { | ||
await this.ctx.render('info', result.weatherinfo); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Repository } from 'typeorm'; | ||
import { BaseEntity } from './entities/base.entity'; | ||
|
||
export abstract class CrudService<T extends BaseEntity> { | ||
constructor(private readonly repository: Repository<T>) {} | ||
|
||
async findAll(): Promise<T[]> { | ||
return await this.repository.find(); | ||
} | ||
|
||
async findById(id: any): Promise<T> { | ||
return await this.repository.findOne(id); | ||
} | ||
|
||
async create(data: any): Promise<T[]> { | ||
const entity = this.repository.create(data); | ||
return await this.repository.save(entity); | ||
} | ||
|
||
async update(id: string, data: any): Promise<T> { | ||
await this.repository.update(id, data); | ||
return this.findById(id); | ||
} | ||
|
||
async delete(id: string): Promise<void> { | ||
await this.repository.delete(id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { | ||
PrimaryGeneratedColumn, | ||
CreateDateColumn, | ||
UpdateDateColumn, | ||
} from 'typeorm'; | ||
|
||
export abstract class BaseEntity { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@CreateDateColumn() | ||
createdAt: Date; | ||
|
||
@UpdateDateColumn() | ||
updatedAt: Date; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export class Crud {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { BaseEntity } from '../entities/base.entity'; | ||
|
||
export interface IRepository<T extends BaseEntity> { | ||
findAll(): Promise<any>; | ||
findById(id: number): Promise<any>; | ||
create(data: Partial<T>): Promise<any>; | ||
update(id: number, data: Partial<any>): Promise<any>; | ||
delete(id: number): Promise<void>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Repository } from 'typeorm'; | ||
import { BaseEntity } from '../entities/base.entity'; | ||
import { IRepository } from '../interfaces/repository.interface'; | ||
|
||
export abstract class BaseRepository<T extends BaseEntity> | ||
implements IRepository<T> | ||
{ | ||
constructor(private readonly repository: Repository<T>) {} | ||
|
||
async findAll(): Promise<T[]> { | ||
return this.repository.find(); | ||
} | ||
|
||
async findById(id: any): Promise<T> { | ||
return await this.repository.findOne(id); | ||
} | ||
|
||
async create(data: any): Promise<unknown> { | ||
const entity = this.repository.create(data); | ||
return await this.repository.save(entity); | ||
} | ||
|
||
async update(id: number, data: Partial<unknown>): Promise<T> { | ||
await this.repository.update(id, data); | ||
return await this.findById(id); | ||
} | ||
|
||
async delete(id: number): Promise<void> { | ||
await this.repository.delete(id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm'; | ||
|
||
@Entity() | ||
export class Photo { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column() | ||
name: string; | ||
|
||
@Column() | ||
description: string; | ||
|
||
@Column() | ||
filename: string; | ||
|
||
@Column() | ||
views: number; | ||
|
||
@Column() | ||
isPublished: boolean; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { MidwayError } from '@midwayjs/core'; | ||
|
||
export class WeatherEmptyDataError extends MidwayError { | ||
constructor(err?: Error) { | ||
super('weather data is empty', { | ||
cause: err, | ||
}); | ||
if (err?.stack) { | ||
this.stack = err.stack; | ||
} | ||
} | ||
} |
Oops, something went wrong.