/
azure-table.mapper.ts
56 lines (46 loc) · 1.54 KB
/
azure-table.mapper.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { randomUUID } from 'crypto';
import { Logger } from '@nestjs/common';
import { AZURE_TABLE_ENTITY } from './azure-table.decorators';
const logger = new Logger(`AzureEntityMapper`);
export interface PartitionRowKeyValues {
partitionKey: string;
rowKey: string;
}
export class AzureEntityMapper {
static serializeAll<E>(entriesDescriptor: any[]): E[] {
return entriesDescriptor.map<E>(entry => {
return AzureEntityMapper.serialize<E>(entry);
});
}
static serialize<E>(entryDescriptor: any) {
const result = {} as E;
for (const key in entryDescriptor) {
if (key !== 'odata.metadata') {
result[key] = entryDescriptor[key];
}
}
return result;
}
static createEntity<D>(partialDto: Partial<AzureEntityMapper>) {
// Note: make sure we are getting the metadata from the DTO constructor
// See: src/table-storage/azure-table.repository.ts
const entityDescriptor = Reflect.getMetadata(AZURE_TABLE_ENTITY, partialDto.constructor) as PartitionRowKeyValues;
if (typeof entityDescriptor === 'undefined') {
throw new Error(
`Could not extract metadata from ${partialDto.constructor.name}. Make sure you are passing a valid Entity`,
);
}
const entity: PartitionRowKeyValues = {
...entityDescriptor,
};
for (const key in partialDto) {
if (entityDescriptor[key]) {
entity[key] = partialDto[key];
}
}
if (!entity.rowKey) {
entity.rowKey = randomUUID() as string;
}
return entity as D & PartitionRowKeyValues;
}
}