Skip to content

Commit

Permalink
feat: change primaykey type to number and update migration, fix some bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ruiming committed Mar 12, 2018
1 parent 62142b4 commit fcc62db
Show file tree
Hide file tree
Showing 19 changed files with 197 additions and 166 deletions.
3 changes: 3 additions & 0 deletions config/test.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
database:
logging: false

env: test
port: 0
14 changes: 8 additions & 6 deletions doc.json
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@
"type": "object",
"properties": {
"pageId": {
"type": "string"
"type": "number"
}
},
"required": ["pageId"]
Expand All @@ -240,10 +240,12 @@
"type": "string"
},
"startTime": {
"type": "number"
"description": "Enables basic storage and retrieval of dates and times.",
"type": "string",
"format": "date-time"
},
"prePageId": {
"type": "string"
"type": "number"
},
"referrer": {
"type": "string"
Expand Down Expand Up @@ -321,7 +323,7 @@
"type": "object",
"properties": {
"pageId": {
"type": "string"
"type": "number"
},
"assets": {
"type": "array",
Expand All @@ -336,10 +338,10 @@
"type": "object",
"properties": {
"pageId": {
"type": "string"
"type": "number"
},
"exitTime": {
"type": "number"
"type": "string"
}
},
"required": ["exitTime", "pageId"]
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"build": "tsc",
"test:run": "./node_modules/.bin/mocha --require espower-typescript/guess 'test/**/*.ts' --timeout=10000",
"test:coverage": "nyc npm run test:run",
"test:watch": "npm run test:run -- --watch",
"test:watch": "NODE_ENV=test npm run test:run -- --watch --watch-extensions ts",
"test:ci": "NODE_ENV=ci npm run test:coverage",
"test": "NODE_ENV=test npm run test:run",
"report-coverage": "NODE_ENV=test nyc report --reporter=text-lcov > coverage.lcov && codecov",
Expand Down Expand Up @@ -44,7 +44,7 @@
"routing-controllers": "^0.7.7",
"routing-controllers-openapi-v3": "^0.0.1-alpha5",
"typedi": "^0.7.1",
"typeorm": "^0.2.0-alpha.28"
"typeorm": "^0.1.16"
},
"devDependencies": {
"@types/faker": "^4.1.2",
Expand Down
10 changes: 5 additions & 5 deletions src/controllers/Log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class LogController {
async initialize(
@Ctx() ctx: Context,
@Body() body: IInitialize,
@State('hostId') hostId: string,
@State('hostId') hostId: number,
@HeaderParam('Authorization') token?: string
): Promise<IToken> {
return await this.logService.initialize(body, ctx.ip, hostId, token);
Expand All @@ -32,23 +32,23 @@ export class LogController {
@UseBefore(sessionInject())
async pageInfo(
@Body() body: IPageInfo,
@State('visiterId') visiterId: string,
@State('hostId') hostId: string
@State('visiterId') visiterId: number,
@State('hostId') hostId: number
): Promise<IPageId> {
return await this.logService.savePageInfo(body, visiterId, hostId);
}

@Description('页面资源数据')
@Post('/assets')
@UseBefore(sessionInject())
async assetsInfo(@Body() body: IAssetsInfo, @State('visiterId') visiterId: string, @State('hostId') hostId: string) {
async assetsInfo(@Body() body: IAssetsInfo, @State('visiterId') visiterId: number, @State('hostId') hostId: number) {
return await this.logService.saveAssetsInfo(body, visiterId, hostId);
}

@Description('网页退出')
@Post('/exit')
@UseBefore(sessionInject())
async exit(@Body() body: IExit, @State('visiterId') visiterId: string, @State('hostId') hostId: string) {
async exit(@Body() body: IExit, @State('visiterId') visiterId: number, @State('hostId') hostId: number) {
return await this.logService.exit(body, visiterId, hostId);
}
}
8 changes: 4 additions & 4 deletions src/entities/Asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Page from './Page';

@Entity()
export default class Asset {
@PrimaryGeneratedColumn('uuid') id: string;
@PrimaryGeneratedColumn() id: number;

/** 请求文件 */
@Column() name: string;
Expand All @@ -29,13 +29,13 @@ export default class Asset {
@Column() duration: number;

/** 访客 ID */
@Column() visiterId: string;
@Column() visiterId: number;

/** 访问的网站 */
@Column() hostId: string;
@Column() hostId: number;

/** 所属的页面 */
@Column() pageId: string;
@Column() pageId: number;

@ManyToOne(type => Visiter)
@JoinColumn({ name: 'visiterId' })
Expand Down
4 changes: 2 additions & 2 deletions src/entities/Host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import User from './User';

@Entity()
export default class Host {
@PrimaryGeneratedColumn('uuid') id: string;
@PrimaryGeneratedColumn() id: number;

/** 网站 */
@Column() website: string;

/** 所属用户 ID */
@Column() userId: string;
@Column() userId: number;

/** 所属用户 */
@OneToOne(type => User)
Expand Down
19 changes: 11 additions & 8 deletions src/entities/Page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Host from './Host';

@Entity()
export default class Page {
@PrimaryGeneratedColumn('uuid') id: string;
@PrimaryGeneratedColumn() id: number;

/** 当前页面 */
@Column() url: string;
Expand Down Expand Up @@ -38,16 +38,19 @@ export default class Page {
@Column() loadEvent: number;

/** 当前页面的访问开始时间 */
@Column() startTime: number;
@Column({ type: 'datetime' })
startTime: Date;

/** 当前页面的访问结束时间 */
@Column() endTime: number;
@Column({ type: 'datetime', nullable: true })
endTime: Date;

/** 退出时间,只有退出时才有 */
@Column({ nullable: true })
exitTime: number;
@Column({ type: 'datetime', nullable: true })
exitTime: Date;

@Column() prePageId: string;
@Column({ nullable: true })
prePageId: number;

/** 上一个页面,如果是入口页面则没有该值 */
@OneToOne(type => Page)
Expand All @@ -59,10 +62,10 @@ export default class Page {
assets?: Asset[];

/** 访问的网站 */
@Column() hostId: string;
@Column() hostId: number;

/** 访客 */
@Column() visiterId: string;
@Column() visiterId: number;

@ManyToOne(type => Visiter)
@JoinColumn({ name: 'visiterId' })
Expand Down
4 changes: 2 additions & 2 deletions src/entities/Session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import Host from './Host';

@Entity()
export default class Session {
@PrimaryColumn() visiterId: string;
@PrimaryColumn() visiterId: number;

@PrimaryColumn() hostId: string;
@PrimaryColumn() hostId: number;

/** referrer */
@Column() referrer: string;
Expand Down
2 changes: 1 addition & 1 deletion src/entities/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';

@Entity()
export default class User {
@PrimaryGeneratedColumn('uuid') id: string;
@PrimaryGeneratedColumn() id: number;

/** 用户邮箱 */
@Column() email: string;
Expand Down
2 changes: 1 addition & 1 deletion src/entities/Visiter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Column, Entity, PrimaryGeneratedColumn, CreateDateColumn } from 'typeor

@Entity()
export default class Visiter {
@PrimaryGeneratedColumn('uuid') id: string;
@PrimaryGeneratedColumn() id: number;

/** 语言 */
@Column() lang: string;
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/Helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export class IToken {
}

export class IPageId {
pageId: string;
pageId: number;
}

export class IDefaultSuccessResponse {}
12 changes: 6 additions & 6 deletions src/interfaces/Log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Session from '../entities/Session';
import Visiter from '../entities/Visiter';
import Page from '../entities/Page';
import Asset from '../entities/Asset';
import { IsString, IsNumber, IsOptional, IsArray } from 'class-validator';
import { IsString, IsNumber, IsOptional, IsArray, IsDateString } from 'class-validator';
import { Type } from 'class-transformer';

export class IInitialize implements Partial<Session>, Partial<Visiter> {
Expand All @@ -14,10 +14,10 @@ export class IInitialize implements Partial<Session>, Partial<Visiter> {

export class IPageInfo implements Partial<Page> {
@IsString() url: string;
@IsNumber() startTime: number;
@IsDateString() startTime: Date;
@IsOptional()
@IsNumber()
prePageId?: string;
prePageId?: number;
@IsString() referrer: string;
@IsNumber() loadPage: number;
@IsNumber() domReady: number;
Expand All @@ -30,8 +30,8 @@ export class IPageInfo implements Partial<Page> {
}

export class IExit {
@IsString() pageId: string;
@IsNumber() exitTime: number;
@IsNumber() pageId: number;
@IsDateString() exitTime: string;
}

export class IAsset implements Partial<Asset> {
Expand All @@ -45,7 +45,7 @@ export class IAsset implements Partial<Asset> {
}

export class IAssetsInfo {
@IsString() pageId: string;
@IsNumber() pageId: number;
@IsArray()
@Type(() => IAsset)
assets: IAsset[];
Expand Down
2 changes: 1 addition & 1 deletion src/middlewares/sessionInject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default function sessionInject() {
}

// TODO: hostId compare with http host
const [hostId, visiterId] = session.split(':');
const [hostId, visiterId] = session.split(':').map(i => +i);

ctx.state.hostId = hostId;
ctx.state.visiterId = visiterId;
Expand Down
88 changes: 0 additions & 88 deletions src/migrations/1520836070176-Update.ts

This file was deleted.

0 comments on commit fcc62db

Please sign in to comment.