Skip to content

Commit 4dcad2a

Browse files
Merge pull request nestjs#2318 from pixtron/master
docs(typeorm): Adapt to new typorm syntax
2 parents b7b3279 + 4278fd9 commit 4dcad2a

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

content/recipes/sql-typeorm.md

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,27 @@ The first step we need to do is to establish the connection with our database us
1818

1919
```typescript
2020
@@filename(database.providers)
21-
import { createConnection } from 'typeorm';
21+
import { DataSource } from 'typeorm';
2222

2323
export const databaseProviders = [
2424
{
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+
},
3842
},
3943
];
4044
```
@@ -95,14 +99,14 @@ The `Photo` entity belongs to the `photo` directory. This directory represents t
9599

96100
```typescript
97101
@@filename(photo.providers)
98-
import { Connection } from 'typeorm';
102+
import { DataSource } from 'typeorm';
99103
import { Photo } from './photo.entity';
100104

101105
export const photoProviders = [
102106
{
103107
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'],
106110
},
107111
];
108112
```

0 commit comments

Comments
 (0)