Skip to content

Commit 428ac6f

Browse files
feat: typeorm many-to-many
1 parent e793a43 commit 428ac6f

File tree

10 files changed

+221
-0
lines changed

10 files changed

+221
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,4 @@
4848

4949
[typeorm 一对多映射](./typeorm-relation-mapping2)
5050

51+
[typeorm 多对多映射](./typeorm-relation-mapping3)
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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "typeorm-relation-mapping3",
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+
"mysql": "^2.14.1",
13+
"mysql2": "^3.3.3",
14+
"reflect-metadata": "^0.1.13",
15+
"typeorm": "0.3.16"
16+
},
17+
"scripts": {
18+
"start": "ts-node src/index.ts",
19+
"typeorm": "typeorm-ts-node-commonjs"
20+
}
21+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import "reflect-metadata"
2+
import { DataSource } from "typeorm"
3+
import { Article } from "./entity/Article"
4+
import { Tag } from "./entity/Tag"
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: [Tag, Article],
16+
migrations: [],
17+
subscribers: [],
18+
poolSize: 10,
19+
connectorPackage: 'mysql2',
20+
extra: {
21+
authPlugin: 'sha256_password',
22+
}
23+
})
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { Column, Entity, JoinTable, ManyToMany, PrimaryGeneratedColumn } from "typeorm"
2+
import { Tag } from "./Tag";
3+
4+
@Entity()
5+
export class Article {
6+
7+
@PrimaryGeneratedColumn()
8+
id: number;
9+
10+
@Column({
11+
length: 100,
12+
comment: '文章标题'
13+
})
14+
title: string;
15+
16+
@Column({
17+
type: 'text',
18+
comment: '文章内容'
19+
})
20+
content: string;
21+
22+
@ManyToMany(() => Tag, (tag)=> tag.articles)
23+
@JoinTable()
24+
tags: Tag[]
25+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Column, Entity, ManyToMany, PrimaryGeneratedColumn } from "typeorm"
2+
import { Article } from "./Article";
3+
4+
@Entity()
5+
export class Tag {
6+
7+
@PrimaryGeneratedColumn()
8+
id: number;
9+
10+
@Column({
11+
length: 100
12+
})
13+
name: string;
14+
15+
@ManyToMany(() => Article, (article)=> article.tags )
16+
articles: Article[]
17+
}
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: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import { AppDataSource } from "./data-source"
2+
import { Article } from "./entity/Article"
3+
import { Tag } from "./entity/Tag";
4+
5+
AppDataSource.initialize().then(async () => {
6+
7+
const a1 = new Article();
8+
a1.title = 'aaaa';
9+
a1.content = 'aaaaaaaaaa';
10+
11+
const a2 = new Article();
12+
a2.title = 'bbbbbb';
13+
a2.content = 'bbbbbbbbbb';
14+
15+
const t1 = new Tag();
16+
t1.name = 'ttt1111';
17+
18+
const t2 = new Tag();
19+
t2.name = 'ttt2222';
20+
21+
const t3 = new Tag();
22+
t3.name = 'ttt33333';
23+
24+
a1.tags = [t1,t2];
25+
a2.tags = [t1,t2,t3];
26+
27+
const entityManager = AppDataSource.manager;
28+
29+
// await entityManager.save(t1);
30+
// await entityManager.save(t2);
31+
// await entityManager.save(t3);
32+
33+
// await entityManager.save(a1);
34+
// await entityManager.save(a2);
35+
36+
37+
// const article = await entityManager.find(Article, {
38+
// relations: {
39+
// tags: true
40+
// }
41+
// });
42+
43+
// console.log(article);
44+
// console.log(article.map(item=> item.tags))
45+
46+
47+
48+
// const article = await entityManager
49+
// .createQueryBuilder(Article, "a")
50+
// .leftJoinAndSelect("a.tags", "t")
51+
// .getMany()
52+
// console.log(article);
53+
// console.log(article.map(item=> item.tags))
54+
55+
// const article = await entityManager
56+
// .getRepository(Article)
57+
// .createQueryBuilder( "a")
58+
// .leftJoinAndSelect("a.tags", "t")
59+
// .getMany()
60+
// console.log(article);
61+
// console.log(article.map(item=> item.tags))
62+
63+
64+
65+
// const article = await entityManager.findOne(Article, {
66+
// where: {
67+
// id: 2
68+
// },
69+
// relations: {
70+
// tags: true
71+
// }
72+
// });
73+
74+
// article.title = "ccccc";
75+
// article.tags = article.tags.filter(item => item.name.includes('ttt111'));
76+
77+
// await entityManager.save(article);
78+
79+
80+
const tags = await entityManager.find(Tag, {
81+
relations: {
82+
articles: true
83+
}
84+
});
85+
86+
console.log(tags);
87+
88+
}).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)