forked from MergeFi/backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathidempotency-key.entity.ts
More file actions
86 lines (75 loc) · 2.9 KB
/
Copy pathidempotency-key.entity.ts
File metadata and controls
86 lines (75 loc) · 2.9 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
import {
Column,
CreateDateColumn,
Entity,
Index,
PrimaryGeneratedColumn,
UpdateDateColumn,
} from 'typeorm';
import { IdempotencyKeyStatus } from '../enums';
/**
* Shape jsonb can actually hold — narrower than `unknown` so TypeORM's
* update()/insert() typings resolve cleanly. Deliberately not a fully
* recursive JSON type (nested values are `unknown`, not `JsonValue`) —
* combining self-referential types with TypeORM's own recursive
* QueryDeepPartialEntity<T> mapped type blows past TS's instantiation
* depth limit ("Type instantiation is excessively deep").
*/
export type JsonValue =
Record<string, unknown> | unknown[] | string | number | boolean | null;
/**
* One row per (Idempotency-Key header value, endpoint scope, caller) tuple
* seen on an idempotency-guarded mutation endpoint (#16).
*
* The unique index below is the actual concurrency-safety mechanism: two
* simultaneous requests carrying the same key/scope/caller both attempt an
* INSERT, the database allows exactly one to succeed, and the loser reads
* back the winner's row instead of racing into a second execution of the
* underlying mutation. This is why `IdempotencyInterceptor` uses a plain
* `repo.insert(...)` wrapped in a try/catch for the unique-violation error,
* rather than a "check then insert" — the DB constraint is what actually
* closes the TOCTOU gap; a JS-level check-then-act would just relocate it.
*/
@Entity('idempotency_keys')
@Index(['key', 'scope', 'callerId'], { unique: true })
export class IdempotencyKey {
@PrimaryGeneratedColumn('uuid')
id: string;
/** The raw Idempotency-Key header value the client supplied. */
@Column()
key: string;
/** Which endpoint this key applies to, e.g. 'bounty.fund' — keys are only unique per scope, not globally. */
@Column()
scope: string;
/**
* The caller this key belongs to — `req.user.userId` when the route is
* authenticated, or a documented fallback when it isn't (none of the
* mutation endpoints this guards currently require auth; see the
* interceptor for the exact fallback and its tracked limitation).
*/
@Column()
callerId: string;
@Column({
type: 'enum',
enum: IdempotencyKeyStatus,
default: IdempotencyKeyStatus.PROCESSING,
})
status: IdempotencyKeyStatus;
/** HTTP status of the cached outcome, set once status moves to COMPLETED. */
@Column({ type: 'int', nullable: true })
responseStatus: number | null;
/** Response body of the cached outcome, set once status moves to COMPLETED. */
@Column({ type: 'jsonb', nullable: true })
responseBody: JsonValue;
@CreateDateColumn()
createdAt: Date;
@UpdateDateColumn()
updatedAt: Date;
/**
* Retention TTL (see IdempotencyCleanupService) — completed keys older
* than this are safe to garbage-collect since no legitimate client retry
* would plausibly arrive this late.
*/
@Column({ type: 'timestamptz' })
expiresAt: Date;
}