{"version":3,"file":"ngx-odm-rxdb.mjs","sources":["../../../../packages/rxdb/src/lib/rxdb.providers.ts","../../../../packages/rxdb/src/lib/rxdb.module.ts"],"sourcesContent":["import { Location } from '@angular/common';\nimport { APP_INITIALIZER, InjectionToken, NgZone, Provider, inject } from '@angular/core';\nimport { Router } from '@angular/router';\nimport { RxDBCollectionService } from '@ngx-odm/rxdb/collection';\nimport type { RxCollectionCreatorExtended } from '@ngx-odm/rxdb/config';\nimport { MangoQueryParams } from '@ngx-odm/rxdb/config';\nimport { RxDBService } from '@ngx-odm/rxdb/core';\nimport { RxDatabaseCreator } from 'rxdb';\nimport { BehaviorSubject, Observable, defer, distinctUntilChanged } from 'rxjs';\n\n/**\n * Instance of RxDB service\n */\nexport const RXDB = new InjectionToken<RxDBService>('RxDBService Instance', {\n  providedIn: 'root',\n  factory: () => new RxDBService(),\n});\n\n/**\n * Instance of RxDatabaseCreator\n */\nexport const RXDB_CONFIG = new InjectionToken<RxDatabaseCreator>('RxDatabaseCreator');\n\n/**\n * Instance of RxCollectionCreator\n */\n/* prettier-ignore */\nexport const RXDB_CONFIG_COLLECTION = new InjectionToken<RxCollectionCreatorExtended>('RxCollectionCreator');\n\n/**\n * Injection token for Service for interacting with a RxCollection.\n * This token is used to inject an instance of RxDBCollectionService into a component or service.\n */\nexport const RXDB_COLLECTION = new InjectionToken<RxDBCollectionService>(\n  'RxDBCollectionService'\n);\n\n/**\n * Initializes DB at `APP_INITIALIZER` cycle\n * @param dbService\n * @param dbConfig\n */\nfunction dbInitializerFactory(\n  dbService: RxDBService,\n  dbConfig: RxDatabaseCreator\n): () => Promise<void> {\n  return async () => {\n    await dbService.initDb(dbConfig);\n  };\n}\n\n/**\n * Returns the set of [dependency-injection providers](guide/glossary#provider)\n * to use RxDb in an application\n * @param config\n * @description\n *\n * The function is useful when you want to use RxDb in an application\n * bootstrapped using the `bootstrapApplication` function. In this scenario there\n * is no need to import the `NgxRxdbModule` NgModule at all, just add\n * providers returned by this function to the `providers` list as show below.\n *\n * ```typescript\n * bootstrapApplication(RootComponent, {\n *   providers: [\n *     provideRxDatabase(config)\n *   ]\n * });\n * ```\n */\nexport function provideRxDatabase(config: RxDatabaseCreator): Provider[] {\n  return [\n    { provide: RXDB_CONFIG, useValue: config },\n    {\n      provide: APP_INITIALIZER,\n      useFactory: dbInitializerFactory,\n      deps: [RXDB, RXDB_CONFIG],\n      multi: true,\n    },\n  ];\n}\n\n/**\n * Returns the set of [dependency-injection providers](guide/glossary#provider)\n * to use RxCollection in a standalone component\n * @param collectionConfig\n * @description\n *\n * The function initializes providers for a specific RxDB collection for a standalone component. In this scenario there is no need to import the `NgxRxdbFeatureModule` NgModule at all, just add\n * providers returned by this function to the `providers` list as show below.\n *\n * ```typescript\n * @Component({\n *   selector: 'demo-todo',\n *   templateUrl: 'todo.component.html',\n *   standalone: true,\n *   imports: [MatCheckboxModule, MatIconModule, MatTableModule],\n *   providers: [provideRxCollection(config)]\n * })\n * export class TodoComponent {\n * ```\n */\nexport function provideRxCollection(\n  collectionConfig: RxCollectionCreatorExtended\n): Provider[] {\n  return [\n    { provide: RXDB_CONFIG_COLLECTION, useValue: collectionConfig },\n    {\n      provide: RXDB_COLLECTION,\n      // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n      // @ts-ignore // INFO: no need for typings here, nothing's exposed, but ts complains // NOSONAR\n      useFactory: (config, dbService, ngZone, currentUrl, updateQueryParamsFn) =>\n        new RxDBCollectionService(\n          config,\n          dbService,\n          ngZone,\n          currentUrl,\n          updateQueryParamsFn\n        ),\n      deps: [\n        //\n        RXDB_CONFIG_COLLECTION,\n        RXDB,\n        NgZone,\n        CURRENT_URL,\n        updateQueryParams,\n      ],\n    },\n  ];\n}\n\n/**\n * The current URL as an observable\n */\nexport const CURRENT_URL = new InjectionToken<Observable<string>>('CURRENT_URL', {\n  providedIn: 'root',\n  factory: () => {\n    const location = inject(Location);\n    const subject = new BehaviorSubject(window.location.href);\n\n    location.onUrlChange(url => {\n      subject.next(url);\n    });\n    return defer(() => subject).pipe(distinctUntilChanged());\n  },\n});\n\n/**\n * Updates query params in the URL via {@link Router.navigate }\n */\nexport const updateQueryParams = new InjectionToken<\n  (queryParams: MangoQueryParams) => Promise<boolean>\n>('UPDATE_QUERY_PARAMS', {\n  providedIn: 'root',\n  factory: () => {\n    const router = inject(Router);\n    return (queryParams: MangoQueryParams) => {\n      return router.navigate([], {\n        queryParams,\n        queryParamsHandling: 'merge',\n      });\n    };\n  },\n});\n","import {\n  ApplicationInitStatus,\n  Inject,\n  Injector,\n  ModuleWithProviders,\n  NgModule,\n  Optional,\n  Self,\n  SkipSelf,\n} from '@angular/core';\nimport { RxDBCollectionService } from '@ngx-odm/rxdb/collection';\nimport type { RxCollectionCreatorExtended } from '@ngx-odm/rxdb/config';\nimport { RxDBService } from '@ngx-odm/rxdb/core';\nimport { NgxRxdbUtils } from '@ngx-odm/rxdb/utils';\nimport type { RxDatabaseCreator } from 'rxdb';\nimport {\n  RXDB,\n  RXDB_COLLECTION,\n  RXDB_CONFIG,\n  provideRxCollection,\n  provideRxDatabase,\n} from './rxdb.providers';\n\n/**\n * Main module which should be imported once in app module, will init RxDbDatabase with given configuration\n *\n * ### Installation\n *\n * 1) Import `NgxRxdbModule` to your root module.\n *\n ```typescript\n    @NgModule({\n      imports: [\n        // ...\n        NgxRxdbModule.forRoot({\n          name: 'demo',          // <- name (required, 'ngx')\n          adapter: 'idb',        // <- storage-adapter (required, default: 'idb')\n          multiInstance: true,   // <-(optional)\n          options: {\n            dumpPath: 'assets/data/db.dump.json', // <- remote url (optional)\n          },\n        })\n      ],\n    })\n    export class AppModule { }\n ```\n *\n * 2) Import `NgxRxdbModule` to your feature module.\n *\n ```typescript\n  @NgModule({\n    imports: [\n      // ...\n      NgxRxdbModule.forFeature({\n        name: 'todo',\n        // schema: todoSchema, <-(kind of optional)\n        statics: {}, // <-(optional)\n        options: { // <-(optional)\n          schemaUrl: 'assets/data/todo.schema.json', <- remote url (optional)\n          initialDocs: initialState.items, // <-(optional)\n        },\n      })\n    ]\n  }),\n  export class TodosModule { }\n ```\n * ### Usage\n *\n * Example of usage:\n * <example-url>http://localhost/demo/mysample.component.html</example-url>\n * <example-url>../index.html</example-url>\n */\n@NgModule()\nexport class NgxRxdbModule {\n  /**\n   * Creates a feature module with providers for a specific RxDB collection.\n   * @param collectionConfig The configuration for the RxDB collection.\n   */\n  static forFeature(\n    collectionConfig: RxCollectionCreatorExtended\n  ): ModuleWithProviders<NgxRxdbFeatureModule> {\n    return {\n      ngModule: NgxRxdbFeatureModule,\n      providers: provideRxCollection(collectionConfig),\n    };\n  }\n\n  /**\n   * Configures and initializes RxDB with the given configuration, during the `APP_INITIALIZER` cycle.\n   * @param config The configuration options for NgxRxdbModule.\n   */\n  static forRoot(config: RxDatabaseCreator): ModuleWithProviders<NgxRxdbModule> {\n    return {\n      ngModule: NgxRxdbModule,\n      providers: provideRxDatabase(config),\n    };\n  }\n\n  /**\n   * Prevents this module from being incorrectly imported\n   * @param appInitStatus - A class that reflects the state of\n   * running {@link https://v7.angular.io/api/core/APP_INITIALIZER|APP_INITIALIZER}s.\n   * @param ngxRxdbConfig - The configuration of the `NgxRxdbModule`\n   * @param trueNgxRxdbConfig\n   * @param injector\n   */\n  public constructor(\n    appInitStatus: ApplicationInitStatus,\n    @Optional() @SkipSelf() @Inject(RXDB_CONFIG) ngxRxdbConfig: RxDatabaseCreator,\n    @Optional() @Self() @Inject(RXDB_CONFIG) trueNgxRxdbConfig: RxDatabaseCreator,\n    injector: Injector\n  ) {\n    if (!trueNgxRxdbConfig && !ngxRxdbConfig) {\n      throw new Error(\n        `${RXDB_CONFIG.toString()} is not provided. Make sure you call the 'forRoot' method of the NgxRxdbModule in the AppModule only.`\n      );\n    }\n\n    if (trueNgxRxdbConfig && ngxRxdbConfig) {\n      throw new Error(\n        `${RXDB_CONFIG.toString()} is already provided. Make sure you call the 'forRoot' method of the NgxRxdbModule in the AppModule only.`\n      );\n    }\n\n    if (trueNgxRxdbConfig && !ngxRxdbConfig) {\n      appInitStatus.donePromise.then(() => {\n        const ngxRxdbService = injector.get(RXDB);\n        if (ngxRxdbService.db.startupErrors.length) {\n          NgxRxdbUtils.logger.log(ngxRxdbService.db.startupErrors);\n        }\n        NgxRxdbUtils.logger.log(\n          `database \"${ngxRxdbService.db.name}\" ready, rxdb version is \"${ngxRxdbService.db['rxdbVersion']}\", storage is \"${ngxRxdbService.db.storage.name}\"`\n        );\n      });\n    }\n  }\n}\n\n/**\n * (Fake) Feature module for NgxRxdbModule\n *\n * By being provided with `forChild` method of *root* NgxRxdbModule,\n * and by injecting `RXDB_COLLECTION` in its constructor,\n * this module actually creates a collection with collectionService and provided config\n */\n@NgModule()\nexport class NgxRxdbFeatureModule {\n  constructor(\n    @Inject(RXDB) private dbService: RxDBService,\n    @Inject(RXDB_COLLECTION) private collectionService: RxDBCollectionService\n  ) {}\n}\n"],"names":[],"mappings":";;;;;;;;;;;MAaa,IAAI,GAAG,IAAI,cAAc,CAAc,sBAAsB,EAAE;AAC1E,IAAA,UAAU,EAAE,MAAM;AAClB,IAAA,OAAO,EAAE,MAAM,IAAI,WAAW,EAAE;AACjC,CAAA,EAAE;MAKU,WAAW,GAAG,IAAI,cAAc,CAAoB,mBAAmB,EAAE;MAMzE,sBAAsB,GAAG,IAAI,cAAc,CAA8B,qBAAqB,EAAE;MAMhG,eAAe,GAAG,IAAI,cAAc,CAC/C,uBAAuB,EACvB;AAOF,SAAS,oBAAoB,CAC3B,SAAsB,EACtB,QAA2B,EAAA;IAE3B,OAAO,YAAW;AAChB,QAAA,MAAM,SAAS,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;AACnC,KAAC,CAAC;AACJ,CAAC;AAqBK,SAAU,iBAAiB,CAAC,MAAyB,EAAA;IACzD,OAAO;AACL,QAAA,EAAE,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,EAAE;AAC1C,QAAA;AACE,YAAA,OAAO,EAAE,eAAe;AACxB,YAAA,UAAU,EAA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