src/app/core/database/pouchdb/memory-pouch-database.ts
An alternative implementation of PouchDatabase that uses the in-memory adapter not persisting any data after the page is closed.
No results matching.
Properties |
|
Methods |
|
constructor(dbName: string, globalSyncState: SyncStateSubject, ngZone?: NgZone)
|
||||||||||||
|
Parameters :
|
| adapter |
Type : string
|
Default value : "indexeddb"
|
|
Inherited from
PouchDatabase
|
|
The PouchDB adapter to use for local storage. Set by the factory/resolver before calling init(). Default: "indexeddb" (the newer adapter). Use "idb" for the legacy adapter. |
| Optional analytics |
Type : function
|
|
Inherited from
PouchDatabase
|
|
Optional accessor for usage analytics (see reportConflict). Assigned by |
| Protected changesFeed |
Type : Subject<any>
|
|
Inherited from
PouchDatabase
|
|
An observable that emits a value whenever the PouchDB receives a new change. This change can come from the current user or remotely from the (live) synchronization |
| Protected databaseInitialized |
Type : unknown
|
Default value : new Subject<void>()
|
|
Inherited from
PouchDatabase
|
| Protected Readonly destroy$ |
Type : unknown
|
Default value : new Subject<void>()
|
|
Inherited from
PouchDatabase
|
|
trigger to unsubscribe any internal subscriptions |
| Protected indexPromises |
Type : Promise<any>[]
|
Default value : []
|
|
Inherited from
PouchDatabase
|
|
A list of promises that resolve once all the (until now saved) indexes are created |
| Protected pouchDB |
Type : PouchDB.Database
|
|
Inherited from
PouchDatabase
|
|
The reference to the PouchDB instance |
| Protected Async findPage | |||||||||
findPage(findOptions: PouchDB.Find.FindRequest
|
|||||||||
|
Inherited from
PouchDatabase
|
|||||||||
|
Query a page through PouchDB's local Mango engine (see PouchDatabase.find). supportsFind stays false on purpose: the local engine has no bookmark cursor, so the opaque bookmark here is the positional offset of the next unseen document. That is enough for a caller which only passes it back unread, but it is not the cursor the remote contract promises, and production code choosing a data source should keep treating this database as one that cannot page.
Parameters :
Returns :
Promise<FindPage>
|
| init | ||||||||
init(dbName?: string)
|
||||||||
|
Inherited from
Database
|
||||||||
|
Initialize the PouchDB with the in-memory adapter. See https://github.com/pouchdb/pouchdb/tree/master/packages/node_modules/pouchdb-adapter-memory
Parameters :
Returns :
void
|
| Async allDocs | ||||||||
allDocs(options?: GetAllOptions)
|
||||||||
|
Inherited from
Database
|
||||||||
|
Load all documents (matching the given PouchDB options) from the database. (see Database) Normally you should rather use "getAll()" or another well typed method of this class instead of passing PouchDB specific options here because that will make your code tightly coupled with PouchDB rather than any other database provider.
Parameters :
Returns :
unknown
|
| changes |
changes()
|
|
Inherited from
Database
|
|
Listen to changes to documents in the database. Use rxjs operators to filter for specific prefixes etc. if needed.
Returns :
Observable<any>
observable which emits the filtered changes |
| Async destroy |
destroy()
|
|
Inherited from
Database
|
|
Destroy the database and all saved data
Returns :
Promise<any>
|
| Async find | ||||||||||||||||||||
find(prefix: string, query: PouchDB.Find.Selector, page?: { limit?: number; bookmark?: string }, sort?: { prop?: string; dir?: "asc" | "desc" })
|
||||||||||||||||||||
|
Inherited from
Database
|
||||||||||||||||||||
|
Query a page of one entity type's documents with a Mango selector, optionally sorted (see Database.find). Only available on databases that implement findPage, which differ
only in how they fetch one page of results: RemotePouchDatabase uses
CouchDB's real Mango When sorted, documents without a value for the sort property are included as well: last for "asc", first for "desc" (see sortedQueries).
Parameters :
Returns :
Promise<FindPage>
|
| Async get | ||||||||||||||||||||
get(id: string, options: GetOptions, returnUndefined?: boolean)
|
||||||||||||||||||||
|
Inherited from
Database
|
||||||||||||||||||||
|
Load a single document by id from the database. (see Database)
Parameters :
Returns :
Promise<any>
|
| getPouchDB |
getPouchDB()
|
|
Inherited from
PouchDatabase
|
|
Get the actual instance of the PouchDB
Returns :
PouchDB.Database
|
| Async getPouchDBOnceReady |
getPouchDBOnceReady()
|
|
Inherited from
PouchDatabase
|
|
Returns :
Promise<PouchDB.Database>
|
| isEmpty |
isEmpty()
|
|
Inherited from
Database
|
|
Check if a database is new/empty. Returns true if there are no documents in the database
Returns :
Promise<boolean>
|
| isInitialized |
isInitialized()
|
|
Inherited from
Database
|
|
Returns :
boolean
|
| Protected isNotificationsDatabase |
isNotificationsDatabase()
|
|
Inherited from
PouchDatabase
|
|
Check if this is a notifications database based on the database name. (may be used for special handling of notification DBs)
Returns :
boolean
|
| Async purge | ||||||||
purge(id: string)
|
||||||||
|
Inherited from
Database
|
||||||||
|
Permanently purge a document and all its revisions from the local database. Unlike remove, which creates a deletion tombstone that is synced, purge completely removes local data without affecting the remote database. This also emits a synthetic deletion event via the changes feed so that in-memory caches (entity stores) drop the purged entity. Adapter limitation: The underlying PouchDB
Parameters :
Returns :
Promise<boolean>
true if the document was purged, false if the document did not exist locally (benign — desired state already achieved) |
| Async put | |||||||||||||||
put(object: any, forceOverwrite: unknown)
|
|||||||||||||||
|
Inherited from
Database
|
|||||||||||||||
|
Save a document to the database. (see Database)
Parameters :
Returns :
Promise<any>
|
| Async putAll | |||||||||||||||
putAll(objects: any[], forceOverwrite: unknown)
|
|||||||||||||||
|
Inherited from
Database
|
|||||||||||||||
|
Save an array of documents to the database
The save can partially fail and return a mix of success and error states in the array (e.g.
Parameters :
Returns :
Promise<any>
array with the result for each object to be saved, if any item fails to be saved, this returns a rejected Promise.
The save can partially fail and return a mix of success and error states in the array (e.g. |
| query | ||||||||||||
query(fun: string | unknown, options: QueryOptions)
|
||||||||||||
|
Inherited from
Database
|
||||||||||||
|
Query data from the database based on a more complex, indexed request. (see Database) This is directly calling the PouchDB implementation of this function. Also see the documentation there: https://pouchdb.com/api.html#query_database
Parameters :
Returns :
Promise<any>
|
| remove | ||||||||
remove(object: any)
|
||||||||
|
Inherited from
Database
|
||||||||
|
Delete a document from the database (see Database)
Parameters :
Returns :
any
|
| Async reset |
reset()
|
|
Inherited from
Database
|
|
Reset the database state so a new one can be opened.
Returns :
any
|
| saveDatabaseIndex | ||||||||
saveDatabaseIndex(designDoc: any)
|
||||||||
|
Inherited from
Database
|
||||||||
|
Create a database index to Also see the PouchDB documentation regarding indices and queries: https://pouchdb.com/api.html#query_database
Parameters :
Returns :
Promise<void>
|
| Protected shouldSkipIndexUpdate | ||||||
shouldSkipIndexUpdate(_existingDesignDoc: any)
|
||||||
|
Inherited from
PouchDatabase
|
||||||
|
Whether to skip updating a design doc that differs from the local version. Overridden in RemotePouchDatabase to prevent older clients from overwriting indexes created by a newer app version on a shared server.
Parameters :
Returns :
boolean
|
| Protected Async subscribeChanges |
subscribeChanges()
|
|
Inherited from
PouchDatabase
|
|
Returns :
any
|
| Protected Async withReadRetry | ||||||
withReadRetry<T>(operation: () => void)
|
||||||
|
Inherited from
PouchDatabase
|
||||||
Type parameters :
|
||||||
|
Hook wrapping an idempotent read ( The base implementation runs the operation exactly once (no retry). RemotePouchDatabase overrides this to re-issue reads that fail with a transient network abort/timeout, which recovers e.g. a connection gone stale after the tab was suspended. Only reads use this hook — writes must run exactly once and are never routed through it, except for creating a query index, which changes nothing when repeated.
Parameters :
Returns :
Promise<T>
|
| getAll | ||||||||||
getAll(prefix: string)
|
||||||||||
|
Inherited from
Database
|
||||||||||
|
Load all documents (with the given prefix) from the database.
Parameters :
Returns :
Promise<Array<any>>
|
| Async removeAll | ||||||||
removeAll<T>(shouldDelete: (doc: T) => void)
|
||||||||
|
Inherited from
Database
|
||||||||
Type parameters :
|
||||||||
|
Delete all documents of the database matching the given filter. Note that
Parameters :
Returns :
Promise<number>
the number of deleted documents |
| supportsFind |
supportsFind()
|
|
Inherited from
Database
|
|
Whether find (bookmark-based pagination) is supported by this implementation. Callers should fall back to loading all docs otherwise.
Returns :
boolean
|
import { PouchDatabase } from "./pouch-database";
import PouchDB from "pouchdb-browser";
import memory from "pouchdb-adapter-memory";
import { SyncStateSubject } from "app/core/session/session-type";
import { SyncState } from "app/core/session/session-states/sync-state.enum";
import { NgZone } from "@angular/core";
import { FindPage } from "./find-queries";
/**
* An alternative implementation of PouchDatabase that uses the in-memory adapter
* not persisting any data after the page is closed.
*/
export class MemoryPouchDatabase extends PouchDatabase {
constructor(
dbName: string = "in-memory-db",
globalSyncState: SyncStateSubject,
ngZone?: NgZone,
) {
super(dbName, globalSyncState, ngZone);
}
/**
* Initialize the PouchDB with the in-memory adapter.
* See {@link https://github.com/pouchdb/pouchdb/tree/master/packages/node_modules/pouchdb-adapter-memory}
* @param dbName the name for the database
*/
override init(dbName?: string) {
PouchDB.plugin(memory);
this.pouchDB = new PouchDB(dbName ?? this.dbName, { adapter: "memory" });
this.databaseInitialized.complete();
this.globalSyncState.next(SyncState.COMPLETED);
}
/**
* Query a page through PouchDB's local Mango engine (see {@link PouchDatabase.find}).
*
* {@link supportsFind} stays false on purpose: the local engine has no
* bookmark cursor, so the opaque bookmark here is the positional offset of
* the next unseen document. That is enough for a caller which only passes it
* back unread, but it is not the cursor the remote contract promises, and
* production code choosing a data source should keep treating this database
* as one that cannot page.
*/
protected override async findPage(
findOptions: PouchDB.Find.FindRequest<any>,
page?: { limit?: number; bookmark?: string },
): Promise<FindPage> {
const skip = page?.bookmark ? Number(page.bookmark) : 0;
const request: PouchDB.Find.FindRequest<any> = { ...findOptions, skip };
if (Number.isInteger(page?.limit)) {
request.limit = page.limit;
}
const pouchDB = await this.getPouchDBOnceReady();
const res = await pouchDB.find(request);
return { docs: res.docs, bookmark: String(skip + res.docs.length) };
}
}