Skip to content

Commit 5d693ab

Browse files
refactor: updade example to modern format
1 parent 3f13dc7 commit 5d693ab

File tree

9 files changed

+663
-69
lines changed

9 files changed

+663
-69
lines changed

sample/package-lock.json

Lines changed: 535 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sample/package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "typeorm-typedi-extensions-example",
3+
"description": "Example project for testing the library.",
4+
"author": "TypeORM contributors",
5+
"dependencies": {
6+
"@types/node": "^14.14.21",
7+
"reflect-metadata": "0.1.13",
8+
"sql.js": "^1.4.0",
9+
"typedi": "^0.10.0",
10+
"typeorm": "^0.2.30",
11+
"typescript": "^4.1.3"
12+
},
13+
"scripts": {
14+
"test": "npm run build && node ./build/sample/src/main.js",
15+
"build": "rm -rf build && tsc"
16+
}
17+
}

sample/sample1-simple-usage/app.ts

Lines changed: 0 additions & 37 deletions
This file was deleted.

sample/sample1-simple-usage/repository/PostRepository.ts

Lines changed: 0 additions & 26 deletions
This file was deleted.

sample/src/app.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { ok, deepStrictEqual } from 'assert';
2+
import { Container } from 'typedi';
3+
import { createConnection, } from 'typeorm';
4+
import { UserRepository } from './user.respository';
5+
import { User } from './user.entity';
6+
7+
export async function startApp() {
8+
const connection = await createConnection({
9+
/** we use in memoty SQL JS to test the lib. */
10+
type: 'sqljs',
11+
logger: 'advanced-console',
12+
logging: true,
13+
/** We don't want to mess with the creation of table now, so we auto-create it. */
14+
synchronize: true,
15+
/** Entities must be registered in TypeORM before you can use them. */
16+
entities: [User],
17+
});
18+
19+
const userOne = new User();
20+
userOne.id = 1;
21+
userOne.name = 'Umed Khudoiberdiev';
22+
23+
const userTwo = new User();
24+
userTwo.id = 2;
25+
userTwo.name = `Quick John`;
26+
27+
const repository = Container.get(UserRepository);
28+
29+
/** Save through the various methods. */
30+
await repository.saveUsingRepository(userOne);
31+
await repository.saveUsingManager(userTwo);
32+
33+
/** Load the saved users so we can assert on them. */
34+
const userList = await repository.findAll();
35+
36+
console.log({ userList });
37+
38+
ok(userList.length === 2);
39+
ok(userList[0] instanceof User);
40+
ok(userList[1] instanceof User);
41+
42+
deepStrictEqual(userList, [userOne, userTwo]);
43+
44+
/** Close the connection so the app can exit. */
45+
await connection.close();
46+
}

sample/src/main.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import 'reflect-metadata';
2+
import { Container } from 'typedi';
3+
import { useContainer } from 'typeorm';
4+
import { startApp } from './app'
5+
6+
useContainer(Container);
7+
8+
startApp()
9+
.then(() => console.log(`App finished running successfully.`))
10+
.catch(error => {
11+
console.error(`Ohh noo! Error while running the app!`);
12+
console.error(error);
13+
});
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';
22

33
@Entity()
4-
export class Post {
4+
export class User {
55
@PrimaryGeneratedColumn()
6-
id: number;
6+
id!: number;
77

88
@Column()
9-
title: string;
10-
11-
@Column()
12-
text: string;
9+
name!: string;
1310
}

sample/src/user.respository.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { EntityManager, Repository } from 'typeorm';
2+
import { Service } from 'typedi';
3+
4+
/**
5+
* It's important that we import these from the index.ts file, because the
6+
* entry point file runs some setup logic for the default container.
7+
*/
8+
import { InjectRepository, InjectManager } from '../../src';
9+
import { User } from './user.entity';
10+
11+
@Service()
12+
export class UserRepository {
13+
14+
@InjectManager()
15+
private entityManager!: EntityManager;
16+
17+
constructor(@InjectRepository(User) private repository: Repository<User>) {}
18+
19+
saveUsingRepository(post: User) {
20+
return this.repository.save(post);
21+
}
22+
23+
saveUsingManager(post: User) {
24+
return this.entityManager.save(post);
25+
}
26+
27+
findAll() {
28+
return this.repository.find();
29+
}
30+
}

sample/tsconfig.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"moduleResolution": "node",
5+
"target": "es2018",
6+
"lib": ["es2018"],
7+
"outDir": "build",
8+
"rootDirs": ["./src"],
9+
"strict": true,
10+
"sourceMap": true,
11+
"inlineSources": true,
12+
"removeComments": false,
13+
"esModuleInterop": true,
14+
"experimentalDecorators": true,
15+
"emitDecoratorMetadata": true,
16+
"forceConsistentCasingInFileNames": true
17+
},
18+
"exclude": ["build", "node_modules"]
19+
}

0 commit comments

Comments
 (0)