Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Commit

Permalink
fix: use uuidv4 instead of timestamps for autogenerated ids
Browse files Browse the repository at this point in the history
fix #1877
  • Loading branch information
jackcmeyer committed Mar 4, 2020
1 parent 1d6047d commit 258cab8
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -30,7 +30,8 @@
"redux": "~4.0.5",
"redux-thunk": "~2.3.0",
"shortid": "^2.2.15",
"typescript": "~3.8.2"
"typescript": "~3.8.2",
"uuid": "^7.0.1"
},
"repository": {
"type": "git",
Expand Down
7 changes: 4 additions & 3 deletions src/__tests__/clients/db/AppointmentRepository.test.ts
@@ -1,7 +1,8 @@
import AppointmentRepository from 'clients/db/AppointmentRepository'
import { appointments } from 'config/pouchdb'
import Appointment from 'model/Appointment'
import { fromUnixTime } from 'date-fns'

const uuidV4Regex = /^[A-F\d]{8}-[A-F\d]{4}-4[A-F\d]{3}-[89AB][A-F\d]{3}-[A-F\d]{12}$/i

describe('Appointment Repository', () => {
it('should create a repository with the database set to the appointments database', () => {
Expand All @@ -24,12 +25,12 @@ describe('Appointment Repository', () => {
})

describe('save', () => {
it('should create an id that is a timestamp', async () => {
it('should create an id that is a uuid', async () => {
const newAppointment = await AppointmentRepository.save({
patientId: 'id',
} as Appointment)

expect(fromUnixTime(parseInt(newAppointment.id, 10)).getTime() > 0).toBeTruthy()
expect(uuidV4Regex.test(newAppointment.id)).toBeTruthy()

await appointments.remove(await appointments.get(newAppointment.id))
})
Expand Down
7 changes: 4 additions & 3 deletions src/__tests__/clients/db/PatientRepository.test.ts
@@ -1,9 +1,10 @@
import { patients } from 'config/pouchdb'
import PatientRepository from 'clients/db/PatientRepository'
import Patient from 'model/Patient'
import { fromUnixTime } from 'date-fns'
import * as shortid from 'shortid'

const uuidV4Regex = /^[A-F\d]{8}-[A-F\d]{4}-4[A-F\d]{3}-[89AB][A-F\d]{3}-[A-F\d]{12}$/i

async function removeAllDocs() {
// eslint-disable-next-line
const allDocs = await patients.allDocs({ include_docs: true })
Expand Down Expand Up @@ -93,12 +94,12 @@ describe('patient repository', () => {
await removeAllDocs()
})

it('should generate an id that is a timestamp for the patient', async () => {
it('should generate an id that is a uuid for the patient', async () => {
const newPatient = await PatientRepository.save({
fullName: 'test test',
} as Patient)

expect(fromUnixTime(parseInt(newPatient.id, 10)).getTime() > 0).toBeTruthy()
expect(uuidV4Regex.test(newPatient.id)).toBeTruthy()
})

it('should generate a patient code', async () => {
Expand Down
3 changes: 2 additions & 1 deletion src/clients/db/Repository.ts
@@ -1,5 +1,6 @@
/* eslint "@typescript-eslint/camelcase": "off" */
import { getTime } from 'date-fns'
import { v4 as uuidv4 } from 'uuid'
import AbstractDBModel from '../../model/AbstractDBModel'

function mapRow(row: any): any {
Expand Down Expand Up @@ -48,7 +49,7 @@ export default class Repository<T extends AbstractDBModel> {

async save(entity: T): Promise<T> {
const { id, rev, ...valuesToSave } = entity
const savedEntity = await this.db.put({ _id: getTime(new Date()).toString(), ...valuesToSave })
const savedEntity = await this.db.put({ _id: uuidv4(), ...valuesToSave })
return this.find(savedEntity.id)
}

Expand Down

0 comments on commit 258cab8

Please sign in to comment.