@prisma/client#Transaction TypeScript Examples

The following examples show how to use @prisma/client#Transaction. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: blocks-transactions.service.ts    From ironfish-api with Mozilla Public License 2.0 6 votes vote down vote up
async upsert(
    prisma: BasePrismaClient,
    block: Block,
    transaction: Transaction,
  ): Promise<BlockTransaction> {
    return prisma.blockTransaction.upsert({
      create: {
        block_id: block.id,
        transaction_id: transaction.id,
      },
      update: {
        block_id: block.id,
        transaction_id: transaction.id,
      },
      where: {
        block_id_transaction_id: {
          block_id: block.id,
          transaction_id: transaction.id,
        },
      },
    });
  }
Example #2
Source File: blocks-transactions.service.ts    From ironfish-api with Mozilla Public License 2.0 6 votes vote down vote up
async findBlocksByTransaction(transaction: Transaction): Promise<Block[]> {
    const blocksTransactions = await this.prisma.blockTransaction.findMany({
      where: {
        transaction_id: transaction.id,
      },
      include: {
        block: true,
      },
    });
    return blocksTransactions.map((blockTransaction) => blockTransaction.block);
  }
Example #3
Source File: blocks-transactions.service.ts    From ironfish-api with Mozilla Public License 2.0 6 votes vote down vote up
async findTransactionsByBlock(block: Block): Promise<Transaction[]> {
    const blocksTransactions = await this.prisma.blockTransaction.findMany({
      where: {
        block_id: block.id,
      },
      include: {
        transaction: true,
      },
    });
    return blocksTransactions.map(
      (blockTransaction) => blockTransaction.transaction,
    );
  }
Example #4
Source File: blocks-transactions-loader.ts    From ironfish-api with Mozilla Public License 2.0 4 votes vote down vote up
async bulkUpsert({
    blocks,
  }: UpsertBlocksDto): Promise<(Block & { transactions: Transaction[] })[]> {
    const deleteBlockMinedPayloads: DeleteBlockMinedEventOptions[] = [];
    const upsertBlockMinedPayloads: UpsertBlockMinedEventOptions[] = [];

    const previousHashes = new Map<string, BlockDto>();
    for (const block of blocks) {
      previousHashes.set(block.hash, block);
    }

    const records: (Block & { transactions: Transaction[] })[] = [];

    for (const block of blocks) {
      let timeSinceLastBlockMs: number | undefined = undefined;
      if (block.previous_block_hash !== undefined) {
        const seenPreviousBlock = previousHashes.get(block.previous_block_hash);
        if (seenPreviousBlock) {
          const prevTimestamp = seenPreviousBlock.timestamp;
          timeSinceLastBlockMs =
            block.timestamp.getTime() - prevTimestamp.getTime();
        } else {
          const unseenPreviousBlock = await this.prisma.block.findFirst({
            where: {
              hash: block.previous_block_hash,
            },
          });
          if (unseenPreviousBlock) {
            timeSinceLastBlockMs =
              block.timestamp.getTime() -
              unseenPreviousBlock.timestamp.getTime();
          }
        }
      }

      await this.prisma.$transaction(
        async (prisma) => {
          const {
            block: createdBlock,
            deleteBlockMinedOptions,
            upsertBlockMinedOptions,
          } = await this.blocksService.upsert(prisma, {
            ...block,
            timeSinceLastBlockMs,
            previousBlockHash: block.previous_block_hash,
            transactionsCount: block.transactions.length,
          });

          if (deleteBlockMinedOptions) {
            deleteBlockMinedPayloads.push(deleteBlockMinedOptions);
          }

          if (upsertBlockMinedOptions) {
            upsertBlockMinedPayloads.push(upsertBlockMinedOptions);
          }

          const transactions = await this.transactionsService.bulkUpsert(
            prisma,
            block.transactions,
          );

          for (const transaction of transactions) {
            await this.blocksTransactionsService.upsert(
              prisma,
              createdBlock,
              transaction,
            );
          }
          records.push({ ...createdBlock, transactions });
        },
        {
          // We increased this from the default of 5000 because the transactions were
          // timing out and failing to upsert blocks
          timeout: 20000,
        },
      );
    }

    for (const payload of deleteBlockMinedPayloads) {
      await this.graphileWorkerService.addJob(
        GraphileWorkerPattern.DELETE_BLOCK_MINED_EVENT,
        payload,
        {
          queueName: 'delete_block_mined_event',
        },
      );
    }

    for (const payload of upsertBlockMinedPayloads) {
      await this.graphileWorkerService.addJob(
        GraphileWorkerPattern.UPSERT_BLOCK_MINED_EVENT,
        payload,
        {
          queueName: 'upsert_block_mined_event',
        },
      );
    }

    await this.graphileWorkerService.addJob(
      GraphileWorkerPattern.SYNC_BLOCKS_DAILY,
      {
        date: await this.blocksDailyService.getNextDateToSync(),
      },
    );

    return records;
  }