refactor: add db service to work with indexed db

This commit is contained in:
4gray committed 2022-01-03 21:01:48 +01:00
1 parent 0335867f2d
commit db0df76e53
2 files changed
+90 -4

No files matched your search

+21 -4
View File
@@ -17,20 +17,32 @@ import { AppComponent } from './app.component';
import { ElectronService } from './services/electron.service';
import { PwaService } from './services/pwa.service';
import { DataService } from './services/data.service';
import { NgxIndexedDBModule, NgxIndexedDBService } from 'ngx-indexed-db';
import { AppConfig } from '../environments/environment';
import { dbConfig } from './indexed-db.config';
// AoT requires an exported function for factories
export function HttpLoaderFactory(http: HttpClient): TranslateHttpLoader {
return new TranslateHttpLoader(http, './assets/i18n/', '.json');
}
function isElectron(): boolean {
return !!(window && window.process && window.process.type);
/**
* Returns true if the application runs in the electron based environment.
*/
function isElectron() {
return !!(window && window.process && (window.process as any).type);
}
export function DataFactory() {
/**
* Conditionally imports the necessary service based on the current environment
* @param dbService indexed db service
* @returns
*/
export function DataFactory(dbService: NgxIndexedDBService, http: HttpClient) {
if (isElectron()) {
return new ElectronService();
}
return new PwaService();
return new PwaService(dbService, http);
}
@NgModule({
@@ -42,6 +54,10 @@ export function DataFactory() {
HttpClientModule,
NgxWhatsNewModule,
SharedModule,
AppConfig.environment === 'WEB'
? NgxIndexedDBModule.forRoot(dbConfig)
: [],
NgxIndexedDBModule.forRoot(dbConfig),
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
@@ -54,6 +70,7 @@ export function DataFactory() {
{
provide: DataService,
useFactory: DataFactory,
deps: [NgxIndexedDBService, HttpClient],
},
],
bootstrap: [AppComponent],
+69
View File
@@ -0,0 +1,69 @@
import { DBConfig } from 'ngx-indexed-db';
/**
* Contains names of the database stores
*/
export enum DbStores {
Playlists = 'playlists',
}
/** Defines db tables and schema */
export const dbConfig: DBConfig = {
name: 'iptvnator',
version: 1,
objectStoresMeta: [
{
store: DbStores.Playlists,
storeConfig: { keyPath: '_id', autoIncrement: false },
storeSchema: [
{
name: '_id',
keypath: '_id',
options: { unique: false },
},
{
name: 'filename',
keypath: 'filename',
options: { unique: false },
},
{ name: 'title', keypath: 'title', options: { unique: false } },
{ name: 'count', keypath: 'count', options: { unique: false } },
{
name: 'playlist',
keypath: 'playlist',
options: { unique: false },
},
{
name: 'importDate',
keypath: 'importDate',
options: { unique: false },
},
{
name: 'lastUsage',
keypath: 'lastUsage',
options: { unique: false },
},
{
name: 'favorites',
keypath: 'favorites',
options: { unique: false },
},
{
name: 'autoRefresh',
keypath: 'autoRefresh',
options: { unique: false },
},
{
name: 'url',
keypath: 'url',
options: { unique: false },
},
{
name: 'filePath',
keypath: 'filePath',
options: { unique: false },
},
],
},
],
};