Skip to content

Commit

Permalink
Merge pull request #408 from MRinaldi9/feat/integrate-modern-api-angular
Browse files Browse the repository at this point in the history
Integration modern api angular
  • Loading branch information
aparzi authored Nov 15, 2024
2 parents 2c6fbdd + 96adb76 commit 3c2b926
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 16 deletions.
4 changes: 2 additions & 2 deletions projects/ngx-indexed-db/src/lib/ngx-indexed-db.meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ export enum DBMode {

export type Key = string | number | Date | ArrayBufferView | ArrayBuffer | IDBValidKey | IDBKeyRange;

export type WithID = {id: number};
export type WithID = { id: number };

export const CONFIG_TOKEN = new InjectionToken<DBConfig>(null);
export const CONFIG_TOKEN = new InjectionToken<Record<string, DBConfig>>(null);
15 changes: 4 additions & 11 deletions projects/ngx-indexed-db/src/lib/ngxindexeddb.module.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
import { NgModule, ModuleWithProviders, InjectionToken } from '@angular/core';
import { CommonModule } from '@angular/common';
import { NgxIndexedDBService } from './ngx-indexed-db.service';
import { DBConfig, CONFIG_TOKEN } from './ngx-indexed-db.meta';
import { DBConfig } from './ngx-indexed-db.meta';
import { _provideIndexedDb } from './provide-indexed-db';

@NgModule({
declarations: [],
imports: [CommonModule]
})
@NgModule()
export class NgxIndexedDBModule {
static forRoot(...dbConfigs: DBConfig[]): ModuleWithProviders<NgxIndexedDBModule> {
const value = {};
for (const dbConfig of dbConfigs) {
Object.assign(value, {[dbConfig.name]: dbConfig});
}
return {
ngModule: NgxIndexedDBModule,
providers: [NgxIndexedDBService, { provide: CONFIG_TOKEN, useValue: value }]
providers: [..._provideIndexedDb(...dbConfigs)],
};
}
}
15 changes: 15 additions & 0 deletions projects/ngx-indexed-db/src/lib/provide-indexed-db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { makeEnvironmentProviders, Provider } from '@angular/core';
import { DBConfig, CONFIG_TOKEN } from './ngx-indexed-db.meta';
import { NgxIndexedDBService } from './ngx-indexed-db.service';

export const provideIndexedDb = (...dbConfigs: DBConfig[]) => {
return makeEnvironmentProviders([..._provideIndexedDb(...dbConfigs)]);
};

export const _provideIndexedDb = (...dbConfigs: DBConfig[]): Provider[] => {
const configs = dbConfigs.reduce<Record<string, DBConfig>>((acc, curr) => {
acc[curr.name] = curr;
return acc;
}, {});
return [NgxIndexedDBService, { provide: CONFIG_TOKEN, useValue: configs }];
};
1 change: 1 addition & 0 deletions projects/ngx-indexed-db/src/public-api.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './lib/ngxindexeddb.module';
export * from './lib/ngx-indexed-db.service';
export * from './lib/ngx-indexed-db.meta';
export { provideIndexedDb } from './lib/provide-indexed-db';
6 changes: 3 additions & 3 deletions projects/playground/src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NgxIndexedDBModule, DBConfig } from 'ngx-indexed-db';
import { DBConfig, provideIndexedDb } from 'ngx-indexed-db';
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

Expand Down Expand Up @@ -27,8 +27,8 @@ const dbConfig: DBConfig = {

@NgModule({
declarations: [AppComponent],
imports: [BrowserModule, NgxIndexedDBModule.forRoot(dbConfig), FormsModule],
providers: [],
imports: [BrowserModule, FormsModule],
providers: [provideIndexedDb(dbConfig)],
bootstrap: [AppComponent],
})
export class AppModule {}

0 comments on commit 3c2b926

Please sign in to comment.