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 ) )
0 commit comments