搭建好了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/ node_modules/
build/ build/
tmp/ tmp/
temp/ temp/
./build

View File

@ -17,6 +17,7 @@
}, },
"scripts": { "scripts": {
"start": "ts-node src/index.ts", "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 { AppDataSource } from "../data-source"
import { NextFunction, Request, Response } from "express" import { NextFunction, Request, Response } from "express"
import { User } from "../entity/User"
export class UserController { export class UserController {
@ -50,4 +51,5 @@ export class UserController {
return "user has been removed" return "user has been removed"
} }
} }
*/

View File

@ -1,17 +1,35 @@
import "reflect-metadata" import "reflect-metadata"
import { DataSource } from "typeorm" 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({ export const AppDataSource = new DataSource({
type: "mysql", type: "mysql",
host: "localhost", host: "localhost",
port: 3306, port: 3306,
username: "test", username: "root",
password: "test", password: "ZengtudorRXR2008",
database: "test", // password: "Zengtudor",
database: "hot",
synchronize: true, synchronize: true,
logging: false, logging: false,
entities: [User], entities: [Search,Hot,Web,Times],
migrations: [], migrations: [],
subscribers: [], 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 { Request, Response } from "express"
import { AppDataSource } from "./data-source" import { AppDataSource } from "./data-source"
import { Routes } from "./routes" import { Routes } from "./routes"
import { User } from "./entity/User"
AppDataSource.initialize().then(async () => { AppDataSource.initialize().then(async () => {
@ -28,25 +27,25 @@ AppDataSource.initialize().then(async () => {
// ... // ...
// start express server // start express server
app.listen(3000) app.listen(3501)
// insert new users for test // insert new users for test
await AppDataSource.manager.save( // await AppDataSource.manager.save(
AppDataSource.manager.create(User, { // AppDataSource.manager.create(User, {
firstName: "Timber", // firstName: "Timber",
lastName: "Saw", // lastName: "Saw",
age: 27 // age: 27
}) // })
) // )
await AppDataSource.manager.save( // await AppDataSource.manager.save(
AppDataSource.manager.create(User, { // AppDataSource.manager.create(User, {
firstName: "Phantom", // firstName: "Phantom",
lastName: "Assassin", // lastName: "Assassin",
age: 24 // 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)) }).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", export const Routes = [
controller: UserController, {
action: "all" method: "get",
}, { route: "/hot/top/:num",
method: "get", controller: HotTopController,
route: "/users/:id", action: "all"
controller: UserController, },
action: "one" // {
}, { // method: "get",
method: "post", // route: "/users",
route: "/users", // controller: UserController,
controller: UserController, // action: "all"
action: "save" // }, {
}, { // method: "get",
method: "delete", // route: "/users/:id",
route: "/users/:id", // controller: UserController,
controller: UserController, // action: "one"
action: "remove" // }, {
}] // method: "post",
// route: "/users",
// controller: UserController,
// action: "save"
// }, {
// method: "delete",
// route: "/users/:id",
// controller: UserController,
// action: "remove"
// }
]