This repository has been archived by the owner on Jan 9, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(init): creates a generic repository class and refactors patient db
- Loading branch information
1 parent
d8e7a55
commit 24940b4
Showing
8 changed files
with
104 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import Patient from 'model/Patient' | ||
import Repository from './Repository' | ||
import { patients } from '../../config/pouchdb' | ||
|
||
export class PatientRepsitory extends Repository<Patient> { | ||
constructor() { | ||
super(patients) | ||
} | ||
} | ||
|
||
export default new PatientRepsitory() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* eslint "@typescript-eslint/camelcase": "off" */ | ||
|
||
import AbstractDBModel from 'model/AbstractDBModel' | ||
|
||
function mapRow(row: any): any { | ||
const { value, doc } = row | ||
const { id, _rev, _id, rev, ...restOfDoc } = doc | ||
return { | ||
id: _id, | ||
rev: value.rev, | ||
...restOfDoc, | ||
} | ||
} | ||
|
||
function mapDocument(document: any): any { | ||
const { _id, _rev, ...values } = document | ||
return { | ||
id: _id, | ||
rev: _rev, | ||
...values, | ||
} | ||
} | ||
|
||
export default class Repository<T extends AbstractDBModel> { | ||
db: PouchDB.Database | ||
|
||
constructor(db: PouchDB.Database) { | ||
this.db = db | ||
} | ||
|
||
async find(id: string): Promise<T> { | ||
const document = await this.db.get(id) | ||
return mapDocument(document) | ||
} | ||
|
||
async findAll(): Promise<T[]> { | ||
const allPatients = await this.db.allDocs({ | ||
include_docs: true, | ||
}) | ||
|
||
return allPatients.rows.map(mapRow) | ||
} | ||
|
||
async save(entity: T): Promise<T> { | ||
const { id, rev, ...valuesToSave } = entity | ||
const savedEntity = await this.db.post({ ...valuesToSave }) | ||
return this.find(savedEntity.id) | ||
} | ||
|
||
async saveOrUpdate(entity: T): Promise<T> { | ||
const e = entity as any | ||
try { | ||
// try and get a patient, if the patient is missing it will throw an error | ||
// and have a status of 404. | ||
await this.db.get(e.id) | ||
const { id, rev, ...restOfDocument } = e | ||
const updatedDcoument = { | ||
_id: id, | ||
_rev: rev, | ||
...restOfDocument, | ||
} | ||
|
||
await this.db.put(updatedDcoument) | ||
return this.find(e.id) | ||
} catch (error) { | ||
if (error.status !== 404) { | ||
throw Error(error) | ||
} | ||
|
||
return this.save(e) | ||
} | ||
} | ||
|
||
async delete(entity: T): Promise<T> { | ||
const e = entity as any | ||
return mapDocument(this.db.remove(e.id, e.rev)) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters