Skip to content

Commit

Permalink
document, please ignore
Browse files Browse the repository at this point in the history
  • Loading branch information
i5ting committed Apr 17, 2023
0 parents commit 70d4a50
Show file tree
Hide file tree
Showing 36 changed files with 691 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
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
7 changes: 7 additions & 0 deletions .eslintrc.json
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
}
}
15 changes: 15 additions & 0 deletions .gitignore
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.*
3 changes: 3 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
...require('mwts/.prettierrc.json')
}
10 changes: 10 additions & 0 deletions README.md
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
```

9 changes: 9 additions & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Midway 快速入门示例

## 快速入门

```bash
$ npm i
$ npm run dev
$ npm run test
```
2 changes: 2 additions & 0 deletions bootstrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const { Bootstrap } = require('@midwayjs/bootstrap');
Bootstrap.run();
6 changes: 6 additions & 0 deletions jest.config.js
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/'],
};
58 changes: 58 additions & 0 deletions package.json
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"
}
29 changes: 29 additions & 0 deletions src/config/config.default.ts
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;
7 changes: 7 additions & 0 deletions src/config/config.unittest.ts
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;
34 changes: 34 additions & 0 deletions src/configuration.ts
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]);
}
}
18 changes: 18 additions & 0 deletions src/controller/api.controller.ts
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 };
}
}
9 changes: 9 additions & 0 deletions src/controller/home.controller.ts
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!';
}
}
15 changes: 15 additions & 0 deletions src/controller/user.controller.ts
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;
}
}
20 changes: 20 additions & 0 deletions src/controller/weather.controller.ts
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);
}
}
}
28 changes: 28 additions & 0 deletions src/core/crud.service.ts
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);
}
}
16 changes: 16 additions & 0 deletions src/core/entities/base.entity.ts
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;
}
1 change: 1 addition & 0 deletions src/core/entities/crud.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export class Crud {}
9 changes: 9 additions & 0 deletions src/core/interfaces/repository.interface.ts
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>;
}
31 changes: 31 additions & 0 deletions src/core/repository/base.repository.ts
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);
}
}
22 changes: 22 additions & 0 deletions src/entity/photo.entity.ts
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;
}
12 changes: 12 additions & 0 deletions src/error/weather.error.ts
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;
}
}
}
Loading

0 comments on commit 70d4a50

Please sign in to comment.