@@ -18,23 +18,27 @@ The first step we need to do is to establish the connection with our database us
18
18
19
19
``` typescript
20
20
@@filename (database .providers )
21
- import { createConnection } from ' typeorm' ;
21
+ import { DataSource } from ' typeorm' ;
22
22
23
23
export const databaseProviders = [
24
24
{
25
- provide: ' DATABASE_CONNECTION' ,
26
- useFactory : async () => await createConnection ({
27
- type: ' mysql' ,
28
- host: ' localhost' ,
29
- port: 3306 ,
30
- username: ' root' ,
31
- password: ' root' ,
32
- database: ' test' ,
33
- entities: [
34
- __dirname + ' /../**/*.entity{.ts,.js}' ,
35
- ],
36
- synchronize: true ,
37
- }),
25
+ provide: ' DATA_SOURCE' ,
26
+ useFactory : async () => {
27
+ const dataSource = new DataSource ({
28
+ type: ' mysql' ,
29
+ host: ' localhost' ,
30
+ port: 3306 ,
31
+ username: ' root' ,
32
+ password: ' root' ,
33
+ database: ' test' ,
34
+ entities: [
35
+ __dirname + ' /../**/*.entity{.ts,.js}' ,
36
+ ],
37
+ synchronize: true ,
38
+ });
39
+
40
+ return dataSource .initialize ();
41
+ },
38
42
},
39
43
];
40
44
```
@@ -95,14 +99,14 @@ The `Photo` entity belongs to the `photo` directory. This directory represents t
95
99
96
100
``` typescript
97
101
@@filename (photo .providers )
98
- import { Connection } from ' typeorm' ;
102
+ import { DataSource } from ' typeorm' ;
99
103
import { Photo } from ' ./photo.entity' ;
100
104
101
105
export const photoProviders = [
102
106
{
103
107
provide: ' PHOTO_REPOSITORY' ,
104
- useFactory : (connection : Connection ) => connection .getRepository (Photo ),
105
- inject: [' DATABASE_CONNECTION ' ],
108
+ useFactory : (dataSource : DataSource ) => dataSource .getRepository (Photo ),
109
+ inject: [' DATA_SOURCE ' ],
106
110
},
107
111
];
108
112
```
0 commit comments