搭建好了data ALL

This commit is contained in:
ZtRXR 2023-09-01 22:36:01 +08:00
parent fddddd68fd
commit e88c39c0f1
12 changed files with 177 additions and 66 deletions

3
.gitignore vendored
View File

@ -3,4 +3,5 @@
node_modules/
build/
tmp/
temp/
temp/
./build

View File

@ -17,6 +17,7 @@
},
"scripts": {
"start": "ts-node src/index.ts",
"typeorm": "typeorm-ts-node-commonjs"
"typeorm": "typeorm-ts-node-commonjs",
"serve": "npm i && npx tsc && node ./build/index.js"
}
}

View File

@ -0,0 +1,30 @@
import { AppDataSource } from "../data-source";
import { Times } from "../entity/Times";
import { NextFunction, Request, Response } from "express"
export class HotTopController{
private TimesRepository = AppDataSource.getRepository(Times)
async all(request: Request, response: Response, next: NextFunction){
const nowDate = new Date()
const yestDay = new Date()
yestDay.setDate(yestDay.getDate()-1)
// console.log(nowDate.toLocaleString())
let num:number
if(request.params.num) num = parseInt(request.params.num)
// console.log(id)
if(isNaN(num) || num>50) num = 50
// return "OK"
return this.TimesRepository
.createQueryBuilder("time")
.leftJoinAndSelect("time.fromWeb", "fromWeb")
.where("time.lastTime BETWEEN :beginTime AND :endTime",
{
beginTime:yestDay,
endTime:nowDate
}
)
.addOrderBy('time.times',"DESC")
.limit(num)
.getMany()
}
}

View File

@ -1,6 +1,7 @@
/*
import { AppDataSource } from "../data-source"
import { NextFunction, Request, Response } from "express"
import { User } from "../entity/User"
export class UserController {
@ -50,4 +51,5 @@ export class UserController {
return "user has been removed"
}
}
}
*/

View File

@ -1,17 +1,35 @@
import "reflect-metadata"
import { DataSource } from "typeorm"
import { User } from "./entity/User"
import { Search } from "./entity/Search"
import { Hot } from "./entity/Hot"
import { Web } from "./entity/Web"
import { Times } from "./entity/Times"
import { readFileSync } from "fs"
import path = require("path")
// import { main } from "."
export const AppDataSource = new DataSource({
type: "mysql",
host: "localhost",
port: 3306,
username: "test",
password: "test",
database: "test",
username: "root",
password: "ZengtudorRXR2008",
// password: "Zengtudor",
database: "hot",
synchronize: true,
logging: false,
entities: [User],
entities: [Search,Hot,Web,Times],
migrations: [],
subscribers: [],
})
// export const AppDataSource = new DataSource({
// type: "sqlite",
// database: "./hot.sqlite",
// synchronize: true,
// logging: false,
// entities: [Search,Hot,Web,Times],
// migrations: [],
// subscribers: [],
// })

15
src/entity/Hot.ts Normal file
View File

@ -0,0 +1,15 @@
import { Column, Entity, ManyToOne, PrimaryGeneratedColumn } from "typeorm";
import { Search } from "./Search";
import { Times } from "./Times";
@Entity()
export class Hot{
@PrimaryGeneratedColumn()
id:number
@Column()
word:string
@ManyToOne(()=>Search,(search)=>search.Hots)
fromSearch:Search
@ManyToOne((type)=>Times,(time)=>time.Hots)
fromTimes
}

15
src/entity/Search.ts Normal file
View File

@ -0,0 +1,15 @@
import { Column, Entity, ManyToOne, OneToMany, PrimaryGeneratedColumn } from "typeorm";
import { Hot } from "./Hot";
import { Web } from "./Web";
@Entity()
export class Search{
@PrimaryGeneratedColumn()
id:number
@OneToMany((type)=>Hot,(hot)=>hot.fromSearch)
Hots:Hot[]
@ManyToOne((type)=>Web,(web)=>web.Searches)
fromWeb
@Column()
date:Date
}

19
src/entity/Times.ts Normal file
View File

@ -0,0 +1,19 @@
import { Column, Entity, ManyToOne, OneToMany, OneToOne, PrimaryGeneratedColumn } from "typeorm";
import { Hot } from "./Hot";
import { Web } from "./Web";
@Entity()
export class Times{
@PrimaryGeneratedColumn()
id:number
@OneToMany((type)=>Hot,(hot)=>hot.fromTimes)
Hots:Hot[]
@Column()
times:number
@Column()
word:string
@ManyToOne(()=>Web,(web)=>web.times)
fromWeb:Web
@Column()
lastTime:Date
}

View File

@ -1,18 +0,0 @@
import { Entity, PrimaryGeneratedColumn, Column } from "typeorm"
@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number
@Column()
firstName: string
@Column()
lastName: string
@Column()
age: number
}

19
src/entity/Web.ts Normal file
View File

@ -0,0 +1,19 @@
import { Column, Entity, OneToMany, PrimaryGeneratedColumn } from "typeorm";
import { Search } from "./Search";
import { Times } from "./Times";
@Entity()
export class Web{
@PrimaryGeneratedColumn()
id:number
@OneToMany((type)=>Search,(search)=>search.fromWeb)
Searches:Search[]
@Column()
name:string
@Column()
fromUrl:string
@Column()
searchUrl:string
@OneToMany(()=>Times,(times)=>times.fromWeb)
times:Times[]
}

View File

@ -3,7 +3,6 @@ import * as bodyParser from "body-parser"
import { Request, Response } from "express"
import { AppDataSource } from "./data-source"
import { Routes } from "./routes"
import { User } from "./entity/User"
AppDataSource.initialize().then(async () => {
@ -28,25 +27,25 @@ AppDataSource.initialize().then(async () => {
// ...
// start express server
app.listen(3000)
app.listen(3501)
// insert new users for test
await AppDataSource.manager.save(
AppDataSource.manager.create(User, {
firstName: "Timber",
lastName: "Saw",
age: 27
})
)
// await AppDataSource.manager.save(
// AppDataSource.manager.create(User, {
// firstName: "Timber",
// lastName: "Saw",
// age: 27
// })
// )
await AppDataSource.manager.save(
AppDataSource.manager.create(User, {
firstName: "Phantom",
lastName: "Assassin",
age: 24
})
)
// await AppDataSource.manager.save(
// AppDataSource.manager.create(User, {
// firstName: "Phantom",
// lastName: "Assassin",
// age: 24
// })
// )
console.log("Express server has started on port 3000. Open http://localhost:3000/users to see results")
console.log("Express server has started on port 3501. Open http://localhost:3501/ to see results")
}).catch(error => console.log(error))

View File

@ -1,23 +1,33 @@
import { UserController } from "./controller/UserController"
import { HotTopController } from "./controller/HotTopController";
export const Routes = [{
method: "get",
route: "/users",
controller: UserController,
action: "all"
}, {
method: "get",
route: "/users/:id",
controller: UserController,
action: "one"
}, {
method: "post",
route: "/users",
controller: UserController,
action: "save"
}, {
method: "delete",
route: "/users/:id",
controller: UserController,
action: "remove"
}]
export const Routes = [
{
method: "get",
route: "/hot/top/:num",
controller: HotTopController,
action: "all"
},
// {
// method: "get",
// route: "/users",
// controller: UserController,
// action: "all"
// }, {
// method: "get",
// route: "/users/:id",
// controller: UserController,
// action: "one"
// }, {
// method: "post",
// route: "/users",
// controller: UserController,
// action: "save"
// }, {
// method: "delete",
// route: "/users/:id",
// controller: UserController,
// action: "remove"
// }
]