import { Injectable } from "@angular/core" ; import { Database } from "../../database/database" ; import { User } from "../../user/user" ; import { Papa } from "ngx-papaparse" ; import { ExportService } from "../../export/export-service/export.service" ; /**
 * Create and load backups of the database.
 */ @ Injectable ( { providedIn : "root" , } ) export class BackupService { constructor ( private db , private papa , private exportService ) { } returns async getJsonExport ( ) { const results = await this . db . getAll ( ) ; return this . exportService . createJson ( results ) ; } returns async getCsvExport ( ) { const results = await this . db . getAll ( ) ; return this . exportService . createCsv ( results ) ; } returns async clearDatabase ( ) { const allDocs = await this . db . getAll ( ) ; for ( const row of allDocs ) { if ( ! row . _id . startsWith ( User . ENTITY_TYPE + ":" ) ) { await this . db . remove ( row ) ; } } } param json param forceUpdate async importJson ( json , forceUpdate = false ) { const documents = JSON . parse ( json ) ; for ( const record of documents ) { delete record . _rev ; await this . db . put ( record , forceUpdate ) ; } } param csv param forceUpdate returns async importCsv ( csv , forceUpdate = false ) { const parsedCsv = this . papa . parse ( csv , { header : true , dynamicTyping : true , skipEmptyLines : true , } ) ; for ( const record of parsedCsv . data ) { for ( const propertyName in record ) { if ( record [ propertyName ] === null || propertyName === "_rev" ) { delete record [ propertyName ] ; } } await this . db . put ( record , forceUpdate ) ; } } }