import { Inject , Injectable } from "@angular/core" ; import { DataStore } from "../data-store/data-store" ; import { StorageLocationModel } from "../data-store/storage-location.model" ; @ Injectable ( ) export abstract class BaseDao < T > { public defaultStorage : T [ ] | T ; public storageLocation : StorageLocationModel = null ; constructor ( @ Inject ( DataStore ) public dataStore ) { this . init ( ) ; } public abstract getStorageLocation ( ) ; public abstract getDefaultStorageValue ( ) ; public init ( ) { this . storageLocation = this . getStorageLocation ( ) ; this . defaultStorage = this . getDefaultStorageValue ( ) ; } /**
     * Check if StorageLocationModel is well set
     */ public checkCompliantDao ( ) { if ( ! this . storageLocation ) { return Promise . reject ( "StorageLocationModel not set in '" + this . constructor . name + "'. Please override init method to assign a StorageLocationModel." ) ; } return Promise . resolve ( ) ; } /**
     * Fetch all data
     */ public fetch ( ) { return this . checkCompliantDao ( ) . then ( ( ) => { return this . dataStore . fetch ( this . storageLocation , null , this . defaultStorage ) ; } ) ; } param value public save ( value ) { return this . checkCompliantDao ( ) . then ( ( ) => { return this . dataStore . save ( this . storageLocation , value , this . defaultStorage ) ; } ) ; } param path param value public upsertProperty < V > ( path , value ) { return this . checkCompliantDao ( ) . then ( ( ) => { return this . dataStore . upsertProperty < V > ( this . storageLocation , path , value , this . defaultStorage ) ; } ) ; } /**
     * Clear all data
     */ public clear ( ) { return this . checkCompliantDao ( ) . then ( ( ) => { return this . dataStore . clear ( this . storageLocation ) ; } ) ; } }