Skip to content

Commit 1b46c70

Browse files
feat: typeorm-all-feature
1 parent b5d3f2b commit 1b46c70

File tree

10 files changed

+296
-1
lines changed

10 files changed

+296
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,6 @@
4040

4141
[通过 mysql2 操作 mysql](./mysql2-test)
4242

43-
[通过 typeorm 操作 mysql](./typeorm-test)
43+
[通过 typeorm 操作 mysql](./typeorm-mysql-test)
4444

45+
[typeorm 全部特性](./typeorm-all-feature)

typeorm-all-feature/.gitignore

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-all-feature/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# typeorm all feature
2+
3+
npm install
4+
5+
修改连接配置
6+
7+
npm run start

typeorm-all-feature/package.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "typeorm-all-feature",
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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { EntitySubscriberInterface, EventSubscriber, InsertEvent } from 'typeorm';
2+
import { User } from './entity/User';
3+
4+
@EventSubscriber()
5+
export class UserSubscriber implements EntitySubscriberInterface<User> {
6+
7+
listenTo() {
8+
return User;
9+
}
10+
11+
beforeInsert(event: InsertEvent<User>) {
12+
console.log(`Before user inserted: `, event.entity);
13+
}
14+
15+
afterInsert(event: InsertEvent<User>) {
16+
console.log(`After user inserted: `, event.entity);
17+
}
18+
}
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 { Aaa } from "./entity/Aaa"
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: "practice",
13+
synchronize: true,
14+
logging: true,
15+
entities: [User, Aaa],
16+
migrations: [],
17+
subscribers: [],
18+
poolSize: 10,
19+
connectorPackage: 'mysql2',
20+
extra: {
21+
authPlugin: 'sha256_password',
22+
}
23+
})
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { Entity, PrimaryGeneratedColumn, Column } from "typeorm"
2+
3+
@Entity({
4+
name: 't_aaa'
5+
})
6+
export class Aaa {
7+
8+
@PrimaryGeneratedColumn({
9+
comment: '这是 id'
10+
})
11+
id: number
12+
13+
@Column({
14+
name: 'a_aa',
15+
type: 'text',
16+
comment: '这是 aaa'
17+
})
18+
aaa: string
19+
20+
@Column({
21+
unique: true,
22+
nullable: false,
23+
length: 10,
24+
width: 5,
25+
type: 'varchar',
26+
default: 'bbb'
27+
})
28+
bbb: string
29+
30+
@Column({
31+
type: 'double',
32+
})
33+
ccc: number
34+
}
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+
}

typeorm-all-feature/src/index.ts

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
import { In } from "typeorm";
2+
import { AppDataSource } from "./data-source"
3+
import { User } from "./entity/User"
4+
5+
AppDataSource.initialize().then(async () => {
6+
7+
const user = new User()
8+
user.id = 1;
9+
user.firstName = "aaa111"
10+
user.lastName = "bbb"
11+
user.age = 25
12+
await AppDataSource.manager.save(user);
13+
14+
15+
// await AppDataSource.manager.save(User, [
16+
// { firstName: 'ccc', lastName: 'ccc', age: 21},
17+
// { firstName: 'ddd', lastName: 'ddd', age: 22},
18+
// { firstName: 'eee', lastName: 'eee', age: 23}
19+
// ]);
20+
// await AppDataSource.manager.save(User, [
21+
// { id: 2 ,firstName: 'ccc111', lastName: 'ccc', age: 21},
22+
// { id: 3 ,firstName: 'ddd222', lastName: 'ddd', age: 22},
23+
// { id: 4, firstName: 'eee333', lastName: 'eee', age: 23}
24+
// ]);
25+
26+
27+
28+
// await AppDataSource.manager.delete(User, 1);
29+
// await AppDataSource.manager.delete(User, [2,3]);
30+
31+
32+
33+
// const users = await AppDataSource.manager.find(User);
34+
// console.log(users);
35+
36+
37+
// const users = await AppDataSource.manager.findBy(User, {
38+
// age: 23
39+
// });
40+
// console.log(users);
41+
42+
43+
// const [users, count] = await AppDataSource.manager.findAndCount(User);
44+
// console.log(users, count);
45+
46+
47+
48+
49+
// const [users, count] = await AppDataSource.manager.findAndCountBy(User, {
50+
// age: 23
51+
// })
52+
// console.log(users, count);
53+
54+
55+
56+
// const user = await await AppDataSource.manager.findOne(User, {
57+
// select: {
58+
// firstName: true,
59+
// age: true
60+
// },
61+
// where: {
62+
// id: 4
63+
// },
64+
// order: {
65+
// age: 'ASC'
66+
// }
67+
// });
68+
// console.log(user);
69+
70+
71+
72+
73+
// const users = await await AppDataSource.manager.find(User, {
74+
// select: {
75+
// firstName: true,
76+
// age: true
77+
// },
78+
// where: {
79+
// id: In([4, 8])
80+
// },
81+
// order: {
82+
// age: 'ASC'
83+
// }
84+
// });
85+
// console.log(users);
86+
87+
88+
89+
90+
// const user = await AppDataSource.manager.findOneBy(User, {
91+
// age: 23
92+
// });
93+
// console.log(user);
94+
95+
96+
97+
98+
// try {
99+
// const user = await AppDataSource.manager.findOneOrFail(User, {
100+
// where: {
101+
// id: 666
102+
// }
103+
// });
104+
// console.log(user);
105+
// }catch(e) {
106+
// console.log(e);
107+
// console.log('没找到该用户');
108+
// }
109+
110+
111+
112+
113+
// const users = await AppDataSource.manager.query('select * from user where age in(?, ?)', [21, 22]);
114+
// console.log(users);
115+
116+
117+
118+
119+
// const queryBuilder = await AppDataSource.manager.createQueryBuilder();
120+
// const user = await queryBuilder.select("user")
121+
// .from(User, "user")
122+
// .where("user.age = :age", { age: 21 })
123+
// .getOne();
124+
125+
// console.log(user);
126+
127+
128+
// const queryBuilder = await AppDataSource.manager.createQueryBuilder();
129+
// const query = queryBuilder.select('user.name', 'name')
130+
// .addSelect('COUNT(post.id)', 'count')
131+
// .from(User, 'user')
132+
// .leftJoin(Post, 'post', 'post.userId = user.id')
133+
// .where('user.id = :id')
134+
// .andWhere('post.isActive = :isActive')
135+
// .setParameters({ id: 1, isActive: true })
136+
// .groupBy('user.name')
137+
// .having('COUNT(post.id) > :postCount', { postCount: 2 });
138+
139+
// const results = await query.getRawMany();
140+
141+
142+
// await AppDataSource.manager.transaction(async manager => {
143+
// await manager.save(User, {
144+
// id: 4,
145+
// firstName: 'eee',
146+
// lastName: 'eee',
147+
// age: 20
148+
// });
149+
// });
150+
151+
152+
}).catch(error => console.log(error))

typeorm-all-feature/tsconfig.json

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)