Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Database component #36

Closed
Keith-CY opened this issue Nov 23, 2022 · 2 comments
Closed

Implement Database component #36

Keith-CY opened this issue Nov 23, 2022 · 2 comments
Assignees
Labels
enhancement New feature or request

Comments

@Keith-CY
Copy link
Member

Please add more info on how to implement the storage component in this issue, e.g. tech stack, schedule, and the PR of adding the database component could be referenced here.

Ref:

@Keith-CY Keith-CY added the enhancement New feature or request label Nov 23, 2022
@felicityin
Copy link
Contributor

felicityin commented Nov 23, 2022

  • tech stack: inversify, typeorm

  • schedule:

    • research & design: 11.21 - 11.24
    • implement: 11.24 - 12.01
    • test: 12.01 - 12.05
  • usage

    • inject typeorm
      user.service.ts:
      import { DataSource, EntityManager, Repository } from 'typeorm';
      import { InjectDataSource, InjectRepository, InjectEntityManager, Service } from 'my-lib';
      import { User } from './user.entity';
      
      @Service()
      export class UserService {
        @InjectDataSource()
        private dataSource!: DataSource;
      
        @InjectEntityManager()
        private entityManager!: EntityManager;
      
        constructor(@InjectRepository(User) private repository: Repository<User>) {}
      
        savebyRepository(post: User) {
          return this.repository.save(post);
        }
      
        saveByManager(post: User) {
          return this.entityManager.save(post);
        }
      
        async saveManyByTransaction(users: User[]) {
          const queryRunner = this.dataSource.createQueryRunner();
        
          await queryRunner.connect();
          await queryRunner.startTransaction();
          try {
            for (const user of users) {
              await queryRunner.manager.save(user);
            }
        
            await queryRunner.commitTransaction();
          } catch (err) {
            await queryRunner.rollbackTransaction();
          } finally {
            await queryRunner.release();
          }
        }
      
        findAll() {
          return this.repository.find();
        }
      }
      
    • use case
      app.ts:
      import { DataSourceOptions } from 'typeorm';
      import { container, TypeOrmManager } from 'my-lib';
      import { UserService } from './user.service';
      import { User } from './user.entity';
      
      export async function startApp() {
        const options: DataSourceOptions = {
          type: "postgres",
          host: "localhost",
          port: 5432,
          username: "test",
          password: "test",
          database: "test",
          synchronize: true,
          logging: false,
          entities: [User],
          migrations: [],
          subscribers: [],
        };
        await TypeOrmManager.importRoot(options);
        await TypeOrmManager.importRepository(options, [User]);
      
        const userOne = new User();
        userOne.id = 1;
        userOne.name = 'Alice';
      
        const userTwo = new User();
        userTwo.id = 2;
        userTwo.name = `Bob`;
      
        const service = container.get<UserService>(UserService.name);
      
        await service.savebyRepository(userOne);
        await service.saveByManager(userTwo);
      
        const userThree = new User();
        userThree.id = 3;
        userThree.name = `Olivia`;
      
        const userFour = new User();
        userFour.id = 4;
        userFour.name = `Ada`;
        await service.saveManyByTransaction([userThree, userFour]);
      
        const userList = await service.findAll();
        console.log({ userList });
      
        const conn = await TypeOrmManager.getDataSource(options);
        conn.destroy();
      }
      

@felicityin
Copy link
Contributor

felicityin commented Nov 30, 2022

PR: #56

@Keith-CY Keith-CY added this to the 2022/11/24 - 2022/12/01 milestone Nov 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Archived in project
Development

No branches or pull requests

2 participants