-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathtechnology-data-source.ts
More file actions
80 lines (72 loc) · 1.96 KB
/
technology-data-source.ts
File metadata and controls
80 lines (72 loc) · 1.96 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { PrismaClient, Prisma, TechnologyEntity } from '@prisma/client';
import { CacheAPIWrapper } from '../../cache';
type TechnologyEntityId = TechnologyEntity['id'];
export type TechnologyEntityCollectionPage = {
totalCount: number;
edges: TechnologyEntity[];
};
export class TechnologyDataSource {
constructor(
private prismaClient: PrismaClient,
private cacheAPIWrapper?: CacheAPIWrapper<TechnologyEntity>
) {}
async getTechnologyById(id: TechnologyEntityId): Promise<TechnologyEntity | null> {
let entity = await this.cacheAPIWrapper?.getCached(id);
if (entity) {
return entity;
}
entity = await this.prismaClient.technologyEntity.findFirst({
where: {
id,
},
});
if (entity) {
await this.cacheAPIWrapper?.cache(entity, 'id');
}
return entity;
}
async getTechnologies(limit: number, offset: number): Promise<TechnologyEntityCollectionPage> {
const [totalCount, edges] = await this.prismaClient.$transaction([
this.prismaClient.technologyEntity.count(),
this.prismaClient.technologyEntity.findMany({
take: limit,
skip: offset,
}),
]);
return {
totalCount,
edges,
};
}
async createTechnology(data: Prisma.TechnologyEntityCreateInput): Promise<TechnologyEntity> {
const entity = await this.prismaClient.technologyEntity.create({
data,
});
this.cacheAPIWrapper?.cache(entity, 'id');
return entity;
}
async updateTechnology(
id: TechnologyEntityId,
updateTechnology: Prisma.TechnologyEntityUpdateInput
): Promise<TechnologyEntity> {
const entity = await this.prismaClient.technologyEntity.update({
where: {
id,
},
data: updateTechnology,
});
await this.cacheAPIWrapper?.cache(entity, 'id');
return entity;
}
async deleteTechnology(id: TechnologyEntityId): Promise<TechnologyEntity> {
const deleted = await this.prismaClient.technologyEntity.delete({
where: {
id,
},
});
if (deleted) {
await this.cacheAPIWrapper?.invalidateCached(id);
}
return deleted;
}
}