-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAttestationEntityService.ts
More file actions
127 lines (121 loc) · 3.92 KB
/
Copy pathAttestationEntityService.ts
File metadata and controls
127 lines (121 loc) · 3.92 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import { Selectable } from "kysely";
import { injectable } from "tsyringe";
import { kyselyCaching } from "../../../client/kysely.js";
import { GetAttestationsArgs } from "../../../graphql/schemas/args/attestationArgs.js";
import { CachingDatabase } from "../../../types/kyselySupabaseCaching.js";
import { Json } from "../../../types/supabaseCaching.js";
import {
createEntityService,
type EntityService,
} from "./EntityServiceFactory.js";
export type AttestationSelect = Selectable<CachingDatabase["attestations"]>;
/**
* Service for managing attestation entities in the database.
* Handles CRUD operations for attestations, including data parsing and validation.
*
* This service:
* - Provides methods for retrieving single or multiple attestations
* - Handles parsing of attestation data, particularly bigint conversions
* - Uses an EntityService for database operations
* - Supports filtering by attestation fields and related entities
*
* @injectable Marks the class as injectable for dependency injection
*/
@injectable()
export class AttestationService {
private entityService: EntityService<
CachingDatabase["attestations"],
GetAttestationsArgs
>;
constructor() {
this.entityService = createEntityService<
CachingDatabase,
"attestations",
GetAttestationsArgs
>("attestations", "AttestationEntityService", kyselyCaching);
}
/**
* Retrieves multiple attestations based on provided arguments.
* Handles filtering and parsing of attestation data.
*
* @param args - Query arguments for filtering attestations
* @returns Promise resolving to:
* - data: Array of attestations with parsed data
* - count: Total number of matching attestations
* @throws {Error} If the database query fails
*
* @example
* ```typescript
* // Get attestations by ID
* const result = await attestationService.getAttestations({
* where: { id: { eq: "123" } }
* });
*
* // Get attestations by related schema
* const result = await attestationService.getAttestations({
* where: { eas_schema: { id: { eq: "schema-id" } } }
* });
* ```
*/
async getAttestations(args: GetAttestationsArgs) {
const respone = await this.entityService.getMany(args);
return {
...respone,
data: respone.data.map(({ data, ...rest }) => ({
...rest,
data: this.parseAttestation(data),
})),
};
}
/**
* Retrieves a single attestation based on provided arguments.
*
* @param args - Query arguments for filtering attestations
* @returns Promise resolving to:
* - The found attestation if it exists
* - undefined if no attestation matches the query
* @throws {Error} If the database query fails
*
* @example
* ```typescript
* const attestation = await attestationService.getAttestation({
* where: { id: { eq: "123" } }
* });
* ```
*/
async getAttestation(args: GetAttestationsArgs) {
return await this.entityService.getSingle(args);
}
/**
* Parses attestation data, converting bigint values to strings.
* This is necessary because GraphQL cannot handle bigint values directly.
*
* @param data - Raw attestation data from the database
* @returns Parsed data with bigint values converted to strings
*
* @example
* ```typescript
* const parsed = attestationService.parseAttestation({
* token_id: 123456789n,
* other_field: "value"
* });
* // parsed = { token_id: "123456789", other_field: "value" }
* ```
*/
parseAttestation(data: Json) {
// TODO cleaner handling of bigints in created attestations
if (
typeof data === "object" &&
data !== null &&
"token_id" in data &&
data.token_id
) {
const tokenId =
typeof data.token_id === "string"
? data.token_id
: String(data.token_id);
return { ...data, token_id: BigInt(tokenId).toString() };
}
return data;
}
}