Browse Source

fix(syncing-server): reduce the amount of select queries for items

Karol Sójko 1 year ago
parent
commit
1fa476d1f9

+ 0 - 1
packages/syncing-server/src/Domain/Item/ItemRepositoryInterface.ts

@@ -17,7 +17,6 @@ export interface ItemRepositoryInterface {
   findByUuid(uuid: Uuid): Promise<Item | null>
   remove(item: Item): Promise<void>
   removeByUuid(uuid: Uuid): Promise<void>
-  save(item: Item): Promise<void>
   insert(item: Item): Promise<void>
   update(item: Item): Promise<void>
   markItemsAsDeleted(itemUuids: Array<string>, updatedAtTimestamp: number): Promise<void>

+ 5 - 5
packages/syncing-server/src/Domain/UseCase/Syncing/SaveNewItem/SaveNewItem.spec.ts

@@ -62,7 +62,7 @@ describe('SaveNewItem', () => {
     }).getValue()
 
     itemRepository = {} as jest.Mocked<ItemRepositoryInterface>
-    itemRepository.save = jest.fn()
+    itemRepository.insert = jest.fn()
 
     itemRepositoryResolver = {} as jest.Mocked<ItemRepositoryResolverInterface>
     itemRepositoryResolver.resolve = jest.fn().mockReturnValue(itemRepository)
@@ -97,7 +97,7 @@ describe('SaveNewItem', () => {
     })
 
     expect(result.isFailed()).toBeFalsy()
-    expect(itemRepository.save).toHaveBeenCalled()
+    expect(itemRepository.insert).toHaveBeenCalled()
   })
 
   it('saves a new empty item', async () => {
@@ -120,7 +120,7 @@ describe('SaveNewItem', () => {
     })
 
     expect(result.isFailed()).toBeFalsy()
-    expect(itemRepository.save).toHaveBeenCalled()
+    expect(itemRepository.insert).toHaveBeenCalled()
   })
 
   it('saves a new item with given timestamps', async () => {
@@ -368,7 +368,7 @@ describe('SaveNewItem', () => {
       expect(result.getValue().props.sharedVaultAssociation?.props.lastEditedBy.value).toEqual(
         '00000000-0000-0000-0000-000000000000',
       )
-      expect(itemRepository.save).toHaveBeenCalled()
+      expect(itemRepository.insert).toHaveBeenCalled()
     })
 
     it('should return a failure if it fails to create a shared vault association', async () => {
@@ -416,7 +416,7 @@ describe('SaveNewItem', () => {
       })
 
       expect(result.isFailed()).toBeFalsy()
-      expect(itemRepository.save).toHaveBeenCalled()
+      expect(itemRepository.insert).toHaveBeenCalled()
     })
 
     it('should return a failure if the item hash has an invalid key system identifier', async () => {

+ 1 - 1
packages/syncing-server/src/Domain/UseCase/Syncing/SaveNewItem/SaveNewItem.ts

@@ -142,7 +142,7 @@ export class SaveNewItem implements UseCaseInterface<Item> {
 
     const itemRepository = this.itemRepositoryResolver.resolve(roleNames)
 
-    await itemRepository.save(newItem)
+    await itemRepository.insert(newItem)
 
     if (contentType.value !== null && [ContentType.TYPES.Note, ContentType.TYPES.File].includes(contentType.value)) {
       if (!dto.onGoingRevisionsTransition) {

+ 9 - 9
packages/syncing-server/src/Domain/UseCase/Syncing/UpdateExistingItem/UpdateExistingItem.spec.ts

@@ -89,7 +89,7 @@ describe('UpdateExistingItem', () => {
     }).getValue()
 
     itemRepository = {} as jest.Mocked<ItemRepositoryInterface>
-    itemRepository.save = jest.fn()
+    itemRepository.update = jest.fn()
 
     itemRepositoryResolver = {} as jest.Mocked<ItemRepositoryResolverInterface>
     itemRepositoryResolver.resolve = jest.fn().mockReturnValue(itemRepository)
@@ -148,7 +148,7 @@ describe('UpdateExistingItem', () => {
     })
 
     expect(result.isFailed()).toBeFalsy()
-    expect(itemRepository.save).toHaveBeenCalled()
+    expect(itemRepository.update).toHaveBeenCalled()
   })
 
   it('should not create a revision if user has an ongoin revisions transition', async () => {
@@ -164,7 +164,7 @@ describe('UpdateExistingItem', () => {
     })
 
     expect(result.isFailed()).toBeFalsy()
-    expect(itemRepository.save).toHaveBeenCalled()
+    expect(itemRepository.update).toHaveBeenCalled()
   })
 
   it('should return error if session uuid is invalid', async () => {
@@ -231,7 +231,7 @@ describe('UpdateExistingItem', () => {
     })
 
     expect(result.isFailed()).toBeFalsy()
-    expect(itemRepository.save).toHaveBeenCalled()
+    expect(itemRepository.update).toHaveBeenCalled()
     expect(item1.props.deleted).toBeTruthy()
     expect(item1.props.content).toBeNull()
     expect(item1.props.encItemKey).toBeNull()
@@ -256,7 +256,7 @@ describe('UpdateExistingItem', () => {
     })
 
     expect(result.isFailed()).toBeFalsy()
-    expect(itemRepository.save).toHaveBeenCalled()
+    expect(itemRepository.update).toHaveBeenCalled()
     expect(item1.props.duplicateOf?.value).toBe('00000000-0000-0000-0000-000000000001')
   })
 
@@ -295,7 +295,7 @@ describe('UpdateExistingItem', () => {
     })
 
     expect(result.isFailed()).toBeFalsy()
-    expect(itemRepository.save).toHaveBeenCalled()
+    expect(itemRepository.update).toHaveBeenCalled()
   })
 
   it('should fallback to updated at timestamp if created at time is not give in any form', async () => {
@@ -316,7 +316,7 @@ describe('UpdateExistingItem', () => {
     })
 
     expect(result.isFailed()).toBeFalsy()
-    expect(itemRepository.save).toHaveBeenCalled()
+    expect(itemRepository.update).toHaveBeenCalled()
   })
 
   it('should fallback to updated at date if created at time is not give in any form', async () => {
@@ -338,7 +338,7 @@ describe('UpdateExistingItem', () => {
     })
 
     expect(result.isFailed()).toBeFalsy()
-    expect(itemRepository.save).toHaveBeenCalled()
+    expect(itemRepository.update).toHaveBeenCalled()
   })
 
   it('should fallback to 0 if created at and update at time is not give in any form', async () => {
@@ -360,7 +360,7 @@ describe('UpdateExistingItem', () => {
     })
 
     expect(result.isFailed()).toBeFalsy()
-    expect(itemRepository.save).toHaveBeenCalled()
+    expect(itemRepository.update).toHaveBeenCalled()
   })
 
   it('should return error if dates could not be created from timestamps', async () => {

+ 1 - 1
packages/syncing-server/src/Domain/UseCase/Syncing/UpdateExistingItem/UpdateExistingItem.ts

@@ -176,7 +176,7 @@ export class UpdateExistingItem implements UseCaseInterface<Item> {
 
     const itemRepository = this.itemRepositoryResolver.resolve(roleNames)
 
-    await itemRepository.save(dto.existingItem)
+    await itemRepository.update(dto.existingItem)
 
     if (secondsFromLastUpdate >= this.revisionFrequency) {
       if (

+ 0 - 14
packages/syncing-server/src/Infra/TypeORM/MongoDBItemRepository.ts

@@ -170,20 +170,6 @@ export class MongoDBItemRepository implements ItemRepositoryInterface {
     await this.mongoRepository.deleteOne({ _id: { $eq: BSON.UUID.createFromHexString(item.uuid.value) } })
   }
 
-  async save(item: Item): Promise<void> {
-    const persistence = this.mapper.toProjection(item)
-
-    const { _id, ...rest } = persistence
-
-    await this.mongoRepository.updateOne(
-      { _id: { $eq: _id } },
-      {
-        $set: rest,
-      },
-      { upsert: true },
-    )
-  }
-
   async insert(item: Item): Promise<void> {
     const persistence = this.mapper.toProjection(item)
 

+ 0 - 6
packages/syncing-server/src/Infra/TypeORM/SQLLegacyItemRepository.ts

@@ -37,12 +37,6 @@ export class SQLLegacyItemRepository implements ItemRepositoryInterface {
       .execute()
   }
 
-  async save(item: Item): Promise<void> {
-    const persistence = this.mapper.toProjection(item)
-
-    await this.ormRepository.save(persistence)
-  }
-
   async insert(item: Item): Promise<void> {
     const projection = this.mapper.toProjection(item)