Files
standardnotes-server/packages/auth/src/Mapping/CacheEntryPersistenceMapper.ts
T
Karol SójkoandGitHub c71f7ff8ad feat: extract cache entry model to domain-core (#581)
* feat: extract cache entry model to domain-core

* fix(auth): rename cache table to be auth specific
2023-05-02 11:43:50 +02:00

33 lines
1012 B
TypeScript

import { CacheEntry, MapperInterface, UniqueEntityId } from '@standardnotes/domain-core'
import { TypeORMCacheEntry } from '../Infra/TypeORM/TypeORMCacheEntry'
export class CacheEntryPersistenceMapper implements MapperInterface<CacheEntry, TypeORMCacheEntry> {
toDomain(projection: TypeORMCacheEntry): CacheEntry {
const cacheEntryOrError = CacheEntry.create(
{
key: projection.key,
value: projection.value,
expiresAt: projection.expiresAt,
},
new UniqueEntityId(projection.uuid),
)
if (cacheEntryOrError.isFailed()) {
throw new Error(`CacheEntryPersistenceMapper.toDomain: ${cacheEntryOrError.getError()}`)
}
return cacheEntryOrError.getValue()
}
toProjection(domain: CacheEntry): TypeORMCacheEntry {
const typeorm = new TypeORMCacheEntry()
typeorm.uuid = domain.id.toString()
typeorm.key = domain.props.key
typeorm.value = domain.props.value
typeorm.expiresAt = domain.props.expiresAt
return typeorm
}
}