0.3.7-2 • Published 2 years ago

@irony0901/typeorm-chain-repository v0.3.7-2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

TypeORM's chain-repository

TypeORM's dynamic repository.
Gets the desired data as a path-type string.
Support deep path.

Notice

  • Only Typescript
  • CommonJS
  • TypeORM's version is based on 3.7
  • Only mysql was completed. But MariaDB and OracleDB are expected to be possible

Install

To install chain-repository, use yarn

$ yarn add typeorm mysql2 @irony0901/typeorm-chain-repository

Example (Get Example)

import { Bbs } from './entity/bbs.entity';
import { BbsChainRepository } from './repository/bbs.repository';
import { getChainRepositories } from '@irony0901/typeorm-chain-repository';

const repos = getChainRepositories({
  bbs: Bbs,
  bbsChain: BbsChainRepository
}, AppDataSource.manager);

const article: Bbs = await repos.bbs.findOne({where: {id: '1'}})
console.log('[article result]', article);
/**
 *  [article result] Bbs { 
 *    id: '1', 
 *    writer: 'irony', 
 *    reg_date: 2022-08-05T12:00:00.000Z 
 *  }
 **/

// 'langs.keywords' is deep path from Bbs Object.
await repos.bbsChain.setProperty(['langs.keywords', 'user'], [article])
console.log('[after setProperty]', article);
/**
 *  [after setProperty] Bbs {
 *    id: '1',
 *    writer: 'irony',
 *    reg_date: 2022-08-05T12:00:00.000Z,
 *    user: User { 
 *      id: '1', 
 *      nickname: 'irony' 
 *    },
 *    langs: [
 *      Lang {
 *        bbsId: '1',
 *        type: 'FR',
 *        title: 'Ceci est le premier article',
 *        keywords: [Array]
 *      },
 *      Lang { 
 *        bbsId: '1', 
 *        type: 'KO', 
 *        title: '첫글입니다', 
 *        keywords: [Array] 
 *      },
 *      Lang {
 *        bbsId: '1',
 *        type: 'US',
 *        title: 'first article',
 *        keywords: [Array]
 *      }
 *    ]
 *  }
 *  
 **/

Use in the source code

  1. Write TypeORM entity.
  2. Write(example_repo_bbs, example_repo_lang) ChainRepository through the method of createChainRepository.
    write a repository of the entity to bring the relation field dynamic relation fields
  3. Brings up the repositories through method of getChainRepositories

Document

createChainRepository

createChainRepository(
  Entity: ObjectType, chainOption: ChainOption
) => new (
    Entity: ObjectType, entityManager: EntityManager, queryRunner?: QueryRunner
  ): ChainRepository 

Entity: ObjectType

TypeORM's entity object

chainOption: ChainOption

Settings object that constitutes the relationship of Entity

  • alias: string
    Table alias name when creation.
  • primaryKeys: string[]
    Field name corresponding to primaryKey within Entity.
  • relationChain: ChainRelation
    Field information required when loading related objects
  • setPropertySubscriber?: SetPropertyEvent
  • saveSubscribe?: SaveSubscriber
    (CommingSoon)

relationChain: ChainRelation

type ChainRelation<SelfType, InverseType> = { 
  [P in keyof SelfType]: {
    Entity: ObjectType<InverseType>;
    fieldIsMany?: boolean
    getBridges: (props: BridgesProps<SelfType>) => Promise<Array<Bridge<SelfType, InverseType>>>;
    Repository?: ChainRepository<InverseType>;
    getDatas?: (props: DatasProps<SelfType, InverseType>) => Promise<Array<InverseType>>;
  }
}
  • Entity
    class in SelfType.
  • fieldIsMany
    Is the InverseType an array.
    default: false
  • getBridges
    Bridge relationship with SelfType and InverseType.
  • Repository
    ChainRepository of InverseType.
  • getDatas
    data(entity) of InverseType.
    If there is a Repository, getData is not needed.