1
- const typeorm = require ( "typeorm" ) ; // import * as typeorm from "typeorm";
2
- const Post = require ( "./model/Post" ) . Post ; // import {Post} from "./model/Post";
3
- const Category = require ( "./model/Category" ) . Category ; // import {Category} from "./model/Category";
4
-
5
- typeorm . createConnection ( {
6
- type : "mysql" ,
7
- host : "localhost" ,
8
- port : 3306 ,
9
- username : "test" ,
10
- password : "test" ,
11
- database : "test" ,
12
- synchronize : true ,
13
- logging : false ,
14
- entities : [
15
- require ( "./entity/PostSchema" ) ,
16
- require ( "./entity/CategorySchema" )
17
- ]
18
- } ) . then ( function ( connection ) {
19
-
20
- const category1 = new Category ( 0 , "TypeScript" ) ;
21
- const category2 = new Category ( 0 , "Programming" ) ;
22
-
23
- return connection
24
- . manager
25
- . save ( [ category1 , category2 ] )
26
- . then ( ( ) => {
27
-
28
- let post = new Post ( ) ;
29
- post . title = "Control flow based type analysis" ;
30
- post . text = "TypeScript 2.0 implements a control flow-based type analysis for local variables and parameters." ;
31
- post . categories = [ category1 , category2 ] ;
32
-
33
- let postRepository = connection . getRepository ( Post ) ;
34
- postRepository . save ( post )
35
- . then ( function ( savedPost ) {
36
- console . log ( "Post has been saved: " , savedPost ) ;
37
- console . log ( "Now lets load all posts: " ) ;
38
-
39
- return postRepository . find ( ) ;
40
- } )
41
- . then ( function ( allPosts ) {
42
- console . log ( "All posts: " , allPosts ) ;
43
- } ) ;
44
- } ) ;
45
-
46
- } ) . catch ( function ( error ) {
47
- console . log ( "Error: " , error ) ;
48
- } ) ;
1
+ import { DataSource } from "typeorm" ;
2
+
3
+ import Post from "./model/Post.js" ;
4
+ import Category from "./model/Category.js" ;
5
+
6
+ const dataSource = new DataSource ( {
7
+ type : "better-sqlite3" ,
8
+ database : "app3-es6.db" ,
9
+ synchronize : true ,
10
+ logging : false ,
11
+ entities : [ Post . schema , Category . schema ] ,
12
+ } ) ;
13
+
14
+ await dataSource . initialize ( ) ;
15
+
16
+ const category1 = new Category ( 1 , "TypeScript" ) ;
17
+ const category2 = new Category ( 2 , "Programming" ) ;
18
+
19
+ await Category . save ( [ category1 , category2 ] ) ;
20
+
21
+ const post = new Post ( ) ;
22
+ post . title = "Control flow based type analysis" ;
23
+ post . text =
24
+ "TypeScript 2.0 implements a control flow-based type analysis for local variables and parameters." ;
25
+ post . categories = [ category1 , category2 ] ;
26
+
27
+ const savedPost = await post . save ( ) ;
28
+ console . log ( "Post has been saved: " , savedPost ) ;
29
+ console . log ( "Now lets load all posts: " ) ;
30
+
31
+ const allPosts = await Post . find ( { relations : { categories : true } } ) ;
32
+
33
+ console . log ( "All posts: " , allPosts ) ;
0 commit comments