mirror of
				https://github.com/cerc-io/watcher-ts
				synced 2025-10-30 20:14:17 +00:00 
			
		
		
		
	Add unique entities query without checking pruned block (#218)
This commit is contained in:
		
							parent
							
								
									7a4b459b1c
								
							
						
					
					
						commit
						772fdbec25
					
				| @ -45,6 +45,7 @@ export enum ENTITY_QUERY_TYPE { | |||||||
|   GROUP_BY, |   GROUP_BY, | ||||||
|   GROUP_BY_WITHOUT_PRUNED, |   GROUP_BY_WITHOUT_PRUNED, | ||||||
|   UNIQUE, |   UNIQUE, | ||||||
|  |   UNIQUE_WITHOUT_PRUNED | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| interface CachedEntities { | interface CachedEntities { | ||||||
| @ -381,6 +382,10 @@ export class Database { | |||||||
|         entities = await this.getEntitiesUnique(queryRunner, entity, block, where, queryOptions); |         entities = await this.getEntitiesUnique(queryRunner, entity, block, where, queryOptions); | ||||||
|         break; |         break; | ||||||
| 
 | 
 | ||||||
|  |       case ENTITY_QUERY_TYPE.UNIQUE_WITHOUT_PRUNED: | ||||||
|  |         entities = await this.getEntitiesUniqueWithoutPruned(queryRunner, entity, block, where, queryOptions); | ||||||
|  |         break; | ||||||
|  | 
 | ||||||
|       case ENTITY_QUERY_TYPE.DISTINCT_ON: |       case ENTITY_QUERY_TYPE.DISTINCT_ON: | ||||||
|         entities = await this.getEntitiesDistinctOn(queryRunner, entity, block, where, queryOptions); |         entities = await this.getEntitiesDistinctOn(queryRunner, entity, block, where, queryOptions); | ||||||
|         break; |         break; | ||||||
| @ -757,6 +762,52 @@ export class Database { | |||||||
|     return entities as Entity[]; |     return entities as Entity[]; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  |   async getEntitiesUniqueWithoutPruned<Entity> ( | ||||||
|  |     queryRunner: QueryRunner, | ||||||
|  |     entity: new () => Entity, | ||||||
|  |     block: BlockHeight, | ||||||
|  |     where: Where = {}, | ||||||
|  |     queryOptions: QueryOptions = {} | ||||||
|  |   ): Promise<Entity[]> { | ||||||
|  |     const repo = queryRunner.manager.getRepository(entity); | ||||||
|  |     const { tableName } = repo.metadata; | ||||||
|  |     let selectQueryBuilder = repo.createQueryBuilder(tableName); | ||||||
|  | 
 | ||||||
|  |     if (block.hash) { | ||||||
|  |       const { canonicalBlockNumber, blockHashes } = await this._baseDatabase.getFrothyRegion(queryRunner, block.hash); | ||||||
|  | 
 | ||||||
|  |       selectQueryBuilder = selectQueryBuilder | ||||||
|  |         .andWhere(new Brackets(qb => { | ||||||
|  |           qb.where(`${tableName}.block_hash IN (:...blockHashes)`, { blockHashes }) | ||||||
|  |             .orWhere(`${tableName}.block_number <= :canonicalBlockNumber`, { canonicalBlockNumber }); | ||||||
|  |         })); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     if (block.number) { | ||||||
|  |       selectQueryBuilder = selectQueryBuilder.andWhere(`${tableName}.block_number <= :blockNumber`, { blockNumber: block.number }); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     selectQueryBuilder = this._baseDatabase.buildQuery(repo, selectQueryBuilder, where); | ||||||
|  | 
 | ||||||
|  |     if (queryOptions.orderBy) { | ||||||
|  |       selectQueryBuilder = this._baseDatabase.orderQuery(repo, selectQueryBuilder, queryOptions); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     selectQueryBuilder = this._baseDatabase.orderQuery(repo, selectQueryBuilder, { ...queryOptions, orderBy: 'id' }); | ||||||
|  | 
 | ||||||
|  |     if (queryOptions.skip) { | ||||||
|  |       selectQueryBuilder = selectQueryBuilder.offset(queryOptions.skip); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     if (queryOptions.limit) { | ||||||
|  |       selectQueryBuilder = selectQueryBuilder.limit(queryOptions.limit); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     const entities = await selectQueryBuilder.getMany(); | ||||||
|  | 
 | ||||||
|  |     return entities as Entity[]; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|   async loadEntitiesRelations<Entity> ( |   async loadEntitiesRelations<Entity> ( | ||||||
|     queryRunner: QueryRunner, |     queryRunner: QueryRunner, | ||||||
|     block: BlockHeight, |     block: BlockHeight, | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user