src/app/core/database/database-resolver.service.ts
Manages access to individual databases, as data may be stored across multiple different instances.
Methods |
Accessors |
constructor(databaseFactory: DatabaseFactoryService)
|
||||||
Parameters :
|
Async destroyDatabases |
destroyDatabases()
|
Returns :
any
|
getDatabase | ||||||||
getDatabase(dbName: string)
|
||||||||
Parameters :
Returns :
Database
|
initDatabasesForAnonymous |
initDatabasesForAnonymous()
|
Returns :
void
|
Async initDatabasesForSession | ||||||
initDatabasesForSession(session: SessionInfo)
|
||||||
Connect the database(s) for the current user's "session", i.e. configuring the access for that account after login (especially for local and remote database modes)
Parameters :
Returns :
any
|
Public initializeNotificationsDatabaseForCurrentUser | ||||||
initializeNotificationsDatabaseForCurrentUser(userId: string)
|
||||||
Initialize db sync for current user's notifications-... DB. Only call this if the user has notifications enabled and the CouchDB actually exists, to avoid flooding to console with errors.
Parameters :
Returns :
void
|
Async resetDatabases |
resetDatabases()
|
Returns :
any
|
changesFeed |
getchangesFeed()
|
A stream of changes from all databases. Use pipe() where necessary to filter for specific changes.
Returns :
Observable<DatabaseDocChange>
|
import { Injectable } from "@angular/core";
import { Database, DatabaseDocChange } from "./database";
import { SessionInfo } from "../session/auth/session-info";
import { DatabaseFactoryService } from "./database-factory.service";
import { Entity } from "../entity/model/entity";
import { Observable, Subject } from "rxjs";
import { NotificationEvent } from "app/features/notification/model/notification-event";
import { SyncedPouchDatabase } from "./pouchdb/synced-pouch-database";
/**
* Manages access to individual databases,
* as data may be stored across multiple different instances.
*/
@Injectable({
providedIn: "root",
})
export class DatabaseResolverService {
private databases: Map<string, Database> = new Map();
/**
* A stream of changes from all databases.
* Use pipe() where necessary to filter for specific changes.
*/
get changesFeed(): Observable<DatabaseDocChange> {
return this._changesFeed.asObservable();
}
private _changesFeed: Subject<any>;
constructor(private databaseFactory: DatabaseFactoryService) {
this._changesFeed = new Subject();
}
private registerDatabase(dbName: string) {
const newDb = this.databaseFactory.createDatabase(dbName);
this.databases.set(dbName, newDb);
newDb.changes().subscribe((change) => this._changesFeed.next(change));
}
getDatabase(dbName: string = Entity.DATABASE): Database {
if (!this.databases.has(dbName)) {
this.registerDatabase(dbName);
}
let db = this.databases.get(dbName);
return db;
}
async resetDatabases() {
for (const db of this.databases.values()) {
await db.reset();
}
}
async destroyDatabases() {
for (const db of this.databases.values()) {
await db.destroy();
}
}
/**
* Connect the database(s) for the current user's "session",
* i.e. configuring the access for that account after login
* (especially for local and remote database modes)
*/
async initDatabasesForSession(session: SessionInfo) {
this.initializeAppDatabaseForCurrentUser(session);
}
private initializeAppDatabaseForCurrentUser(user: SessionInfo) {
const userDBName = `${user.name}-${Entity.DATABASE}`;
this.getDatabase(Entity.DATABASE).init(userDBName);
}
/**
* Initialize db sync for current user's notifications-... DB.
* Only call this if the user has notifications enabled and the CouchDB actually exists,
* to avoid flooding to console with errors.
* @param userId
*/
public initializeNotificationsDatabaseForCurrentUser(userId: string) {
const db = this.getDatabase(NotificationEvent.DATABASE);
if (db.isInitialized()) {
return;
}
const serverDbName = `${NotificationEvent.DATABASE}_${userId}`;
const browserDbName = serverDbName;
if (db instanceof SyncedPouchDatabase) {
db.init(browserDbName, serverDbName);
} else {
db.init(browserDbName);
}
}
initDatabasesForAnonymous() {
if (!this.getDatabase(Entity.DATABASE).isInitialized()) {
this.getDatabase(Entity.DATABASE).init(null);
}
}
}