Skip to content

Commit 02df5be

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

File tree

9 files changed

+211
-0
lines changed

9 files changed

+211
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,7 @@
4343
[通过 typeorm 操作 mysql](./typeorm-mysql-test)
4444

4545
[typeorm 全部特性](./typeorm-all-feature)
46+
47+
[typeorm 一对一映射](./typeorm-relation-mapping)
48+
49+
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/

typeorm-relation-mapping/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#typeorm relation mapping
2+
3+
npm install
4+
5+
修改数据库连接信息
6+
7+
npm run start
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "typeorm-relation-mapping",
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 { IdCard } from "./entity/IdCard"
4+
import { User } from "./entity/User"
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: [User, IdCard],
16+
migrations: [],
17+
subscribers: [],
18+
poolSize: 10,
19+
connectorPackage: 'mysql2',
20+
extra: {
21+
authPlugin: 'sha256_password',
22+
}
23+
})
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Column, Entity, JoinColumn, OneToOne, PrimaryGeneratedColumn } from "typeorm"
2+
import { User } from "./User"
3+
4+
@Entity({
5+
name: 'id_card'
6+
})
7+
export class IdCard {
8+
@PrimaryGeneratedColumn()
9+
id: number
10+
11+
@Column({
12+
length: 50,
13+
comment: '身份证号'
14+
})
15+
cardName: string
16+
17+
@JoinColumn()
18+
@OneToOne(() => User, {
19+
cascade: true,
20+
onDelete: 'CASCADE',
21+
onUpdate: 'CASCADE'
22+
})
23+
user: User
24+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Entity, PrimaryGeneratedColumn, Column, OneToOne, JoinColumn, BaseEntity } from "typeorm"
2+
import { IdCard } from "./IdCard"
3+
4+
@Entity()
5+
export class User{
6+
7+
@PrimaryGeneratedColumn()
8+
id: number
9+
10+
@Column()
11+
firstName: string
12+
13+
@Column()
14+
lastName: string
15+
16+
@Column()
17+
age: number
18+
19+
@OneToOne(() => IdCard, (idCard) => idCard.user)
20+
idCard: IdCard
21+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { AppDataSource } from "./data-source"
2+
import { IdCard } from "./entity/IdCard"
3+
import { User } from "./entity/User"
4+
5+
AppDataSource.initialize().then(async () => {
6+
7+
// const user = new User();
8+
// user.firstName = 'guang';
9+
// user.lastName = 'guang';
10+
// user.age = 20;
11+
12+
// const idCard = new IdCard();
13+
// idCard.cardName = '1111111';
14+
// idCard.user = user;
15+
16+
// await AppDataSource.manager.save(user);
17+
// await AppDataSource.manager.save(idCard);
18+
19+
20+
21+
// const ics = await AppDataSource.manager.find(IdCard, {});
22+
// console.log(ics);
23+
24+
25+
26+
27+
// const ics = await AppDataSource.manager.find(IdCard, {
28+
// relations: {
29+
// user: true
30+
// }
31+
// });
32+
// console.log(ics);
33+
34+
35+
36+
37+
// const ics = await AppDataSource.manager.getRepository(IdCard)
38+
// .createQueryBuilder("ic")
39+
// .leftJoinAndSelect("ic.user", "u")
40+
// .getMany();
41+
42+
// console.log(ics);
43+
44+
45+
46+
// const ics = await AppDataSource.manager.createQueryBuilder(IdCard, "ic")
47+
// .leftJoinAndSelect("ic.user", "u")
48+
// .getMany();
49+
// console.log(ics);
50+
51+
52+
53+
// const user = new User();
54+
// user.id = 1;
55+
// user.firstName = 'guang1111';
56+
// user.lastName = 'guang1111';
57+
// user.age = 20;
58+
59+
// const idCard = new IdCard();
60+
// idCard.id = 1;
61+
// idCard.cardName = '22222';
62+
// idCard.user = user;
63+
64+
// await AppDataSource.manager.save(idCard);
65+
66+
67+
68+
// await AppDataSource.manager.delete(User, 1)
69+
70+
71+
// const idCard = await AppDataSource.manager.findOne(IdCard, {
72+
// where: {
73+
// id: 1
74+
// },
75+
// relations: {
76+
// user: true
77+
// }
78+
// })
79+
// await AppDataSource.manager.delete(User, idCard.user.id)
80+
// await AppDataSource.manager.delete(IdCard, idCard.id)
81+
82+
83+
const user = await AppDataSource.manager.find(User, {
84+
relations: {
85+
idCard: true
86+
}
87+
});
88+
console.log(user);
89+
90+
}).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)