import { Injectable } from "@angular/core" ; import { HistoricalEntityData } from "./historical-entity-data" ; import { DatabaseIndexingService } from "../../core/entity/database-indexing/database-indexing.service" ; @ Injectable ( { providedIn : "root" , } ) export class HistoricalDataService { constructor ( private dbIndexingService ) { this . createHistoricalDataIndex ( ) ; } private createHistoricalDataIndex ( ) { const designDoc = { _id : "_design/historicalData_index" , views : { by_entity : { map : `(doc) => {
            if (doc._id.startsWith("${ HistoricalEntityData . ENTITY_TYPE }")) {
              emit([doc.relatedEntity, new Date(doc.date).getTime()]);
            }
          }` , } , } , } ; return this . dbIndexingService . createIndex ( designDoc ) ; } getHistoricalDataFor ( entityId ) { return this . dbIndexingService . queryIndexDocs ( HistoricalEntityData , "historicalData_index/by_entity" , { startkey : [ entityId + "\u0000" ] , endkey : [ entityId ] , descending : true , } ) ; } }