0.0.2 • Published 4 years ago

@yield-engine/yields-utils-yield-database v0.0.2

Weekly downloads
-
License
-
Repository
github
Last release
4 years ago

Awesome Project Build with TypeORM

Steps to run this project:

  1. Run npm i command
  2. Run docker-compose up command
  3. Run npm start command

Run migrations

We do this programmatically. See ./src/utils/migration.ts

Revert previous migration

We do this programmatically. See ./src/utils/migration.ts

Generate migration files based on current db schema

  1. Ensure database is up to date
  2. Apply entity change within the src/entity directory
  3. Run `npm run migrate:generate -- -n '<migration_description> -c 'yields-database'
  4. Test -> Run migrations section
  5. Test -> Revert previous migration section

Examples of a creating/fetching details from the HolderDetail table

import { Connection } from 'typeorm'
import { getOrCreateConnection } from './util/connection'
import { PublicKey } from './entity/PublicKey'
import { KbeAccount } from './entity/KbeAccount'
import { HolderDetail } from './entity/HolderDetail'
import { KbeAccountBalanceRecord } from './entity/KbeAccountBalanceRecord'
import { PublicKeyBalanceRecord } from './entity/PublicKeyBalanceRecord'
import { KbeAccountPayout } from './entity/KbeAccountPayout'
import { PublicKeyPayout } from './entity/PublicKeyPayout'

export async function bootstrap() {
  let connection: Connection

  try {
    connection = await getYieldDatabaseConnection()
  } catch (e) {
    console.error('Error - Getting a db connection has failed ')
    console.error(e.message)
    return
  }

  connection.transaction(async transactionalEntityManager => {
    // create public key
    const publicKey = await transactionalEntityManager.save(
      transactionalEntityManager.create(PublicKey, {
        publicKey: '0xKVT1512613213423472346256256456432',
        eligibleAt: new Date(Date.now()),
      }),
    )
    // add balance to pub key
    const publicKeyBalanceRecord = await transactionalEntityManager.save(
      transactionalEntityManager.create(PublicKeyBalanceRecord, {
        balance: 222,
        balanceAt: new Date(Date.now()),
        currencyId: 1,
        publicKey,
      }),
    )
    // add payout to pub key
    const publicKeyPayout = await transactionalEntityManager.save(
      transactionalEntityManager.create(PublicKeyPayout, {
        status: 'paid',
        type: 'KVT',
        amount: 11130,
        paidAt: new Date(Date.now()),
        currencyId: 1,
        publicKey,
      }),
    )
    const publicKeyCancelled = await transactionalEntityManager.save(
      transactionalEntityManager.create(PublicKeyPayout, {
        status: 'cancelled',
        type: 'KVT',
        metadata: 'This order was cancelled due to being blacklisted',
        amount: 1110,
        paidAt: new Date(Date.now()),
        currencyId: 1,
        publicKey,
      }),
    )
    // create account
    const kbeAccount = await transactionalEntityManager.save(
      transactionalEntityManager.create(KbeAccount, {
        accountId: 'f48e9cc6-3192-4c4c-b342-8877421e0cca',
        hin: 'KM12345678',
        eligibleAt: new Date(Date.now()),
        status: 'kycVerified',
        suspended: false,
      }),
    )
    // add balance to account
    const kbeBalanceRecord = await transactionalEntityManager.save(
      transactionalEntityManager.create(KbeAccountBalanceRecord, {
        balance: 10,
        balanceAt: new Date(Date.now()),
        currencyId: 1,
        kbeAccount,
      }),
    )
    // add payout to account
    const kbeAccountPayout = await transactionalEntityManager.save(
      transactionalEntityManager.create(KbeAccountPayout, {
        status: 'paid',
        type: 'KVT',
        amount: 10,
        paidAt: new Date(Date.now()),
        currencyId: 1,
        kbeAccount,
      }),
    )
    const kbeAccountPayoutCancelled = await transactionalEntityManager.save(
      transactionalEntityManager.create(KbeAccountPayout, {
        status: 'cancelled',
        type: 'KVT',
        metadata: 'This order was cancelled due to being blacklisted',
        amount: 10,
        paidAt: new Date(Date.now()),
        currencyId: 1,
        kbeAccount,
      }),
    )
    // link account with pub key in holder detail table
    const holderDetail = await transactionalEntityManager.save(
      transactionalEntityManager.create(HolderDetail, {
        publicKey,
        kbeAccount,
      }),
    )
    const holderDetailFind = await transactionalEntityManager.findOne(HolderDetail, {
      where: { id: 1 },
      relations: ['kbeAccount', 'publicKey'],
    })
    console.log(holderDetail)
  })
}