Skip to content

Commit e793a43

Browse files
feat: typeorm one-to-many
1 parent 02df5be commit e793a43

File tree

10 files changed

+208
-0
lines changed

10 files changed

+208
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,5 @@
4646

4747
[typeorm 一对一映射](./typeorm-relation-mapping)
4848

49+
[typeorm 一对多映射](./typeorm-relation-mapping2)
4950

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.idea/
2+
.vscode/
3+
node_modules/
4+
build/
5+
tmp/
6+
temp/
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Awesome Project Build with TypeORM
2+
3+
Steps to run this project:
4+
5+
1. Run `npm i` command
6+
2. Setup database settings inside `data-source.ts` file
7+
3. Run `npm start` command
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "typeorm-relation-mapping2",
3+
"version": "0.0.1",
4+
"description": "Awesome project developed with TypeORM.",
5+
"type": "commonjs",
6+
"devDependencies": {
7+
"@types/node": "^16.11.10",
8+
"ts-node": "10.7.0",
9+
"typescript": "4.5.2"
10+
},
11+
"dependencies": {
12+
"mysql2": "^3.3.3",
13+
"reflect-metadata": "^0.1.13",
14+
"typeorm": "0.3.16"
15+
},
16+
"scripts": {
17+
"start": "ts-node src/index.ts",
18+
"typeorm": "typeorm-ts-node-commonjs"
19+
}
20+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Employee } from './entity/Employee';
2+
import { Department } from './entity/Department';
3+
import "reflect-metadata"
4+
import { DataSource } from "typeorm"
5+
6+
export const AppDataSource = new DataSource({
7+
type: "mysql",
8+
host: "localhost",
9+
port: 3306,
10+
username: "root",
11+
password: "guang",
12+
database: "typeorm_test",
13+
synchronize: true,
14+
logging: true,
15+
entities: [Department, Employee],
16+
migrations: [],
17+
subscribers: [],
18+
poolSize: 10,
19+
connectorPackage: 'mysql2',
20+
extra: {
21+
authPlugin: 'sha256_password',
22+
}
23+
})
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Employee } from './Employee';
2+
import { Column, Entity, ManyToOne, OneToMany, PrimaryGeneratedColumn } from "typeorm"
3+
4+
@Entity()
5+
export class Department {
6+
7+
@PrimaryGeneratedColumn()
8+
id: number;
9+
10+
@Column({
11+
length: 50
12+
})
13+
name: string;
14+
15+
@OneToMany(() => Employee, (employee) => employee.department, {
16+
cascade: true
17+
})
18+
employees: Employee[];
19+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Department } from './Department';
2+
import { Column, Entity, JoinColumn, ManyToOne, PrimaryGeneratedColumn } from "typeorm"
3+
4+
@Entity()
5+
export class Employee {
6+
@PrimaryGeneratedColumn()
7+
id: number;
8+
9+
@Column({
10+
length: 50
11+
})
12+
name: string;
13+
14+
@ManyToOne(() => Department, {
15+
// cascade: true
16+
})
17+
department: Department;
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Entity, PrimaryGeneratedColumn, Column } from "typeorm"
2+
3+
@Entity()
4+
export class User {
5+
6+
@PrimaryGeneratedColumn()
7+
id: number
8+
9+
@Column()
10+
firstName: string
11+
12+
@Column()
13+
lastName: string
14+
15+
@Column()
16+
age: number
17+
18+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import { Department } from './entity/Department';
2+
import { Employee } from './entity/Employee';
3+
import { AppDataSource } from "./data-source"
4+
5+
AppDataSource.initialize().then(async () => {
6+
7+
// const d1 = new Department();
8+
// d1.name = '技术部';
9+
10+
// const e1 = new Employee();
11+
// e1.name = '张三';
12+
// e1.department = d1;
13+
14+
// const e2 = new Employee();
15+
// e2.name = '李四';
16+
// e2.department = d1;
17+
18+
// const e3 = new Employee();
19+
// e3.name = '王五';
20+
// e3.department = d1;
21+
22+
// AppDataSource.manager.save(Department, d1);
23+
// AppDataSource.manager.save(Employee,[e1, e2, e3]);
24+
25+
26+
27+
28+
// const e1 = new Employee();
29+
// e1.name = '张三';
30+
31+
// const e2 = new Employee();
32+
// e2.name = '李四';
33+
34+
// const e3 = new Employee();
35+
// e3.name = '王五';
36+
37+
// const d1 = new Department();
38+
// d1.name = '技术部';
39+
// d1.employees = [e1, e2, e3];
40+
41+
// AppDataSource.manager.save(Department, d1);
42+
43+
44+
// const deps = await AppDataSource.manager.find(Department, {
45+
// relations: {
46+
// employees: true
47+
// }
48+
// });
49+
// console.log(deps);
50+
// console.log(deps.map(item => item.employees))
51+
52+
53+
// const es = await AppDataSource.manager.getRepository(Department)
54+
// .createQueryBuilder('d')
55+
// .leftJoinAndSelect('d.employees', 'e')
56+
// .getMany();
57+
58+
// console.log(es);
59+
// console.log(es.map(item => item.employees))
60+
61+
62+
const es = await AppDataSource.manager
63+
.createQueryBuilder(Department, 'd')
64+
.leftJoinAndSelect('d.employees', 'e')
65+
.getMany();
66+
67+
console.log(es);
68+
console.log(es.map(item => item.employees))
69+
70+
71+
72+
73+
// const deps = await AppDataSource.manager.find(Department, {
74+
// relations: {
75+
// employees: true
76+
// }
77+
// });
78+
// await AppDataSource.manager.delete(Employee, deps[0].employees);
79+
// await AppDataSource.manager.delete(Department, deps[0].id);
80+
81+
}).catch(error => console.log(error))
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"compilerOptions": {
3+
"lib": [
4+
"es5",
5+
"es6"
6+
],
7+
"target": "es5",
8+
"module": "commonjs",
9+
"moduleResolution": "node",
10+
"outDir": "./build",
11+
"emitDecoratorMetadata": true,
12+
"experimentalDecorators": true,
13+
"sourceMap": true
14+
}
15+
}

0 commit comments

Comments
 (0)