added support for pouchencryption

disabled in GetDB() function.
This commit is contained in:
Jason Kulatunga 2022-10-09 08:54:23 -07:00
parent 009a85f93e
commit 0a36a179d5
6 changed files with 47 additions and 23 deletions

View File

@ -3,4 +3,5 @@ import {Source} from '../../../lib/models/database/source';
export class SourceSyncMessage {
source: Source
userIdentifier: string
encryptionKey?: string
}

View File

@ -54,7 +54,7 @@ export class DashboardComponent implements OnInit {
let metadataSource = results[1] as { [name: string]: MetadataSource }
//process
console.log(summary);
console.log("SUMMARY RESPONSE",summary);
this.sources = summary.sources
this.metadataSource = metadataSource
this.metadataSource["manual"] = {

View File

@ -30,7 +30,7 @@ export class FastenDbService extends PouchdbRepository {
remotePouchEndpoint = "http://localhost:5984"
constructor(private _httpClient: HttpClient) {
const userIdentifier = localStorage.getItem("current_user")
super(userIdentifier);
super(userIdentifier, "my-secret-encryption-key");
if(userIdentifier){
this.enableSync(userIdentifier)
}
@ -110,12 +110,16 @@ export class FastenDbService extends PouchdbRepository {
.then((paginatedResp) => paginatedResp.rows)
// summary.patients = []
summary.patients = await this.GetDB().find({
selector: {
doc_type: DocType.ResourceFhir,
source_resource_type: "Patient",
}
}).then((results) => results.docs)
summary.patients = await this.GetDB()
.then((db) => {
return db.find({
selector: {
doc_type: DocType.ResourceFhir,
source_resource_type: "Patient",
}
})
})
.then((results) => results.docs)
summary.resource_type_counts = await this.findDocumentByPrefix(`${DocType.ResourceFhir}`, false)
.then((paginatedResp) => {

View File

@ -16,6 +16,7 @@ export class QueueService {
const sourceSync = new SourceSyncMessage()
sourceSync.source = source
sourceSync.userIdentifier = localStorage.getItem("current_user")
sourceSync.encryptionKey = "my-secret-encryption-key"
const input$: Observable<string> = of(JSON.stringify(sourceSync));
return fromWorker<string, string>(() => new Worker(new URL('./source-sync.worker', import.meta.url), {type: 'module'}), input$)
// .subscribe(message => {

View File

@ -17,7 +17,7 @@ export class SourceSyncWorker implements DoWork<string, string> {
console.log(msg); // outputs 'Hello from main thread'
const sourceSyncMessage = JSON.parse(msg) as SourceSyncMessage
const db = NewRepositiory(sourceSyncMessage.userIdentifier)
const db = NewRepositiory(sourceSyncMessage.userIdentifier, sourceSyncMessage.encryptionKey)
const client = NewClient(sourceSyncMessage.source.source_type, sourceSyncMessage.source)
console.log("!!!!!!!!!!!!!!STARTING WORKER SYNC!!!!!!!!!", sourceSyncMessage)
return client.SyncAll(db)

View File

@ -27,21 +27,22 @@ PouchDB.plugin(PouchCrypto);
* Eventually this method should dyanmically dtermine the version of the repo to return from the env.
* @constructor
*/
export function NewRepositiory(userIdentifier?: string): IDatabaseRepository {
return new PouchdbRepository(userIdentifier)
export function NewRepositiory(userIdentifier?: string, encryptionKey?: string): IDatabaseRepository {
return new PouchdbRepository(userIdentifier, encryptionKey)
}
export class PouchdbRepository implements IDatabaseRepository {
encryptionKey: string
localPouchDb: PouchDB.Database
constructor(userIdentifier?: string) {
constructor(userIdentifier?: string, encryptionKey?: string) {
//setup PouchDB Plugins
//https://pouchdb.com/guides/mango-queries.html
this.localPouchDb = null
if(userIdentifier){
this.localPouchDb = new PouchDB(userIdentifier);
this.encryptionKey = encryptionKey
}
}
@ -175,11 +176,22 @@ export class PouchdbRepository implements IDatabaseRepository {
// Get the active PouchDB instance. Throws an error if no PouchDB instance is
// available (ie, user has not yet been configured with call to .configureForUser()).
public GetDB(): any {
public async GetDB(): Promise<PouchDB.Database> {
if(!this.localPouchDb) {
throw(new Error( "Database is not available - please configure an instance." ));
}
return this.localPouchDb;
// if(this.encryptionKey){
// return this.localPouchDb.crypto(this.encryptionKey, {ignore:[
// 'doc_type',
// 'source_id',
// 'source_resource_type',
// 'source_resource_id',
// ]}).then(() => {
// return this.localPouchDb
// })
// } else {
return this.localPouchDb;
// }
}
// create a new document. Returns a promise of the generated id.
@ -189,8 +201,9 @@ export class PouchdbRepository implements IDatabaseRepository {
// NOTE: All friends are given the key-prefix of "friend:". This way, when we go
// to query for friends, we can limit the scope to keys with in this key-space.
return this.GetDB()
.put(doc)
.then((db) => db.put(doc))
.then(( result ): string => {
return( result.id );
}
@ -200,7 +213,9 @@ export class PouchdbRepository implements IDatabaseRepository {
// create multiple documents, returns a list of generated ids
protected createBulk(docs: IDatabaseDocument[]): Promise<string[]> {
return this.GetDB()
.bulkDocs(docs.map((doc) => { doc.populateId(); return doc }))
.then((db) => {
return db.bulkDocs(docs.map((doc) => { doc.populateId(); return doc }))
})
.then((results): string[] => {
return results.map((result) => result.id)
})
@ -208,7 +223,7 @@ export class PouchdbRepository implements IDatabaseRepository {
protected getDocument(id: string): Promise<any> {
return this.GetDB()
.get(id)
.then((db) => db.get(id))
}
@ -217,17 +232,20 @@ export class PouchdbRepository implements IDatabaseRepository {
}
protected findDocumentByPrefix(prefix: string, includeDocs: boolean = true): Promise<IDatabasePaginatedResponse> {
return this.GetDB()
.allDocs({
include_docs: includeDocs,
startkey: `${prefix}:`,
endkey: `${prefix}:\uffff`
.then((db) => {
return db.allDocs({
include_docs: includeDocs,
startkey: `${prefix}:`,
endkey: `${prefix}:\uffff`
})
})
}
protected async deleteDocument(id: string): Promise<boolean> {
const docToDelete = await this.getDocument(id)
return this.GetDB()
.remove(docToDelete)
.then((db) => db.remove(docToDelete))
.then((result) => {
return result.ok
})