|
| 1 | +import _ from "lodash"; |
| 2 | +import { DataStoreMetadata } from "../../domain/data-store/DataStoreMetadata"; |
| 3 | +import { DataStoreMetadataRepository } from "../../domain/data-store/DataStoreMetadataRepository"; |
| 4 | +import { Instance } from "../../domain/instance/entities/Instance"; |
| 5 | +import { Stats } from "../../domain/reports/entities/Stats"; |
| 6 | +import { SynchronizationResult } from "../../domain/reports/entities/SynchronizationResult"; |
| 7 | +import { promiseMap } from "../../utils/common"; |
| 8 | +import { StorageDataStoreClient } from "../storage/StorageDataStoreClient"; |
| 9 | + |
| 10 | +export class DataStoreMetadataD2Repository implements DataStoreMetadataRepository { |
| 11 | + private instance: Instance; |
| 12 | + |
| 13 | + constructor(instance: Instance) { |
| 14 | + this.instance = instance; |
| 15 | + } |
| 16 | + |
| 17 | + async get(dataStores: DataStoreMetadata[]): Promise<DataStoreMetadata[]> { |
| 18 | + const result = await promiseMap(dataStores, async dataStore => { |
| 19 | + const dataStoreClient = new StorageDataStoreClient(this.instance, dataStore.namespace); |
| 20 | + const dataStoreWithValue = this.getValuesByDataStore(dataStoreClient, dataStore); |
| 21 | + return dataStoreWithValue; |
| 22 | + }); |
| 23 | + return result; |
| 24 | + } |
| 25 | + |
| 26 | + private async getValuesByDataStore( |
| 27 | + dataStoreClient: StorageDataStoreClient, |
| 28 | + dataStore: DataStoreMetadata |
| 29 | + ): Promise<DataStoreMetadata> { |
| 30 | + const keys = await this.getAllKeys(dataStoreClient, dataStore); |
| 31 | + const keyWithValue = await promiseMap(keys, async key => { |
| 32 | + const keyValue = await dataStoreClient.getObject(key.id); |
| 33 | + return { id: key.id, value: keyValue }; |
| 34 | + }); |
| 35 | + const keyInNamespace = _(dataStore.keys).first()?.id; |
| 36 | + const sharing = keyInNamespace ? await dataStoreClient.getObjectSharing(keyInNamespace) : undefined; |
| 37 | + return new DataStoreMetadata({ |
| 38 | + namespace: dataStore.namespace, |
| 39 | + keys: keyWithValue, |
| 40 | + sharing, |
| 41 | + }); |
| 42 | + } |
| 43 | + |
| 44 | + private async getAllKeys( |
| 45 | + dataStoreClient: StorageDataStoreClient, |
| 46 | + dataStore: DataStoreMetadata |
| 47 | + ): Promise<DataStoreMetadata["keys"]> { |
| 48 | + if (dataStore.keys.length > 0) return dataStore.keys; |
| 49 | + const keys = await dataStoreClient.listKeys(); |
| 50 | + return keys.map(key => ({ id: key, value: "" })); |
| 51 | + } |
| 52 | + |
| 53 | + async save(dataStores: DataStoreMetadata[]): Promise<SynchronizationResult> { |
| 54 | + const keysIdsToDelete = await this.getKeysToDelete(dataStores); |
| 55 | + |
| 56 | + const resultStats = await promiseMap(dataStores, async dataStore => { |
| 57 | + const dataStoreClient = new StorageDataStoreClient(this.instance, dataStore.namespace); |
| 58 | + const stats = await promiseMap(dataStore.keys, async key => { |
| 59 | + const exist = await dataStoreClient.getObject(key.id); |
| 60 | + await dataStoreClient.saveObject(key.id, key.value); |
| 61 | + if (dataStore.sharing) { |
| 62 | + await dataStoreClient.saveObjectSharing(key.id, dataStore.sharing); |
| 63 | + } |
| 64 | + return exist ? Stats.createOrEmpty({ updated: 1 }) : Stats.createOrEmpty({ imported: 1 }); |
| 65 | + }); |
| 66 | + return stats; |
| 67 | + }); |
| 68 | + |
| 69 | + const deleteStats = await promiseMap(keysIdsToDelete, async keyId => { |
| 70 | + const [namespace, key] = keyId.split(DataStoreMetadata.NS_SEPARATOR); |
| 71 | + const dataStoreClient = new StorageDataStoreClient(this.instance, namespace); |
| 72 | + await dataStoreClient.removeObject(key); |
| 73 | + return Stats.createOrEmpty({ deleted: 1 }); |
| 74 | + }); |
| 75 | + |
| 76 | + const allStats = resultStats.flatMap(result => result).concat(deleteStats); |
| 77 | + const dataStoreStats = { ...Stats.combine(allStats.map(stat => Stats.create(stat))), type: "DataStore Keys" }; |
| 78 | + |
| 79 | + const result: SynchronizationResult = { |
| 80 | + date: new Date(), |
| 81 | + instance: this.instance, |
| 82 | + status: "SUCCESS", |
| 83 | + type: "metadata", |
| 84 | + stats: dataStoreStats, |
| 85 | + typeStats: [dataStoreStats], |
| 86 | + }; |
| 87 | + |
| 88 | + return result; |
| 89 | + } |
| 90 | + |
| 91 | + private async getKeysToDelete(dataStores: DataStoreMetadata[]): Promise<string[]> { |
| 92 | + const allKeysToDelete = await promiseMap(dataStores, async dataStore => { |
| 93 | + if (dataStore.action === "MERGE") return []; |
| 94 | + |
| 95 | + const existingRecords = await this.get([{ ...dataStore, keys: [] }]); |
| 96 | + const existingKeysIds = existingRecords.flatMap(dataStore => { |
| 97 | + return dataStore.keys.map(key => DataStoreMetadata.generateKeyId(dataStore.namespace, key.id)); |
| 98 | + }); |
| 99 | + |
| 100 | + const keysIdsToSave = dataStores.flatMap(dataStore => { |
| 101 | + return dataStore.keys.map(key => DataStoreMetadata.generateKeyId(dataStore.namespace, key.id)); |
| 102 | + }); |
| 103 | + |
| 104 | + const keysIdsToDelete = existingKeysIds.filter(id => !keysIdsToSave.includes(id)); |
| 105 | + return keysIdsToDelete; |
| 106 | + }); |
| 107 | + |
| 108 | + return allKeysToDelete.flat(); |
| 109 | + } |
| 110 | +} |
0 commit comments