-
Notifications
You must be signed in to change notification settings - Fork 0
/
repository.ts
73 lines (64 loc) · 1.6 KB
/
repository.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import type { Result } from "./result.ts";
/**
* Enforces id attribute on a type. Id attribute name can be specified
* as the second type argument, uses `_id` by default.
*/
export type Entity<
T extends { [K in ID]?: unknown },
ID extends string = "_id",
> =
& T
& { [K in ID]: Exclude<T[ID], undefined> };
/**
* The general interface of Repositories.
*/
export interface Repository<T extends object> {
/**
* Checks if object exists in the repository.
*
* @param id id of the object
*/
has(id: unknown): Promise<Result<boolean, unknown>>;
/**
* Retrieves multiple objects from the repository.
*
* @param args search parameters
*/
list(...args: Array<unknown>): Promise<Result<Array<T>, unknown>>;
/**
* Adds a new object to the repository.
*
* @param value the object to be added
*/
create(value: T): Promise<Result<unknown, unknown>>;
/**
* Gets a single object from the repository.
*
* @param id id of the object
*/
read(id: unknown): Promise<Result<T, unknown>>;
/**
* Updates an object stored in the repository.
*
* @param args update parameters
*/
update(...args: Array<unknown>): Promise<Result<unknown, unknown>>;
/**
* Removes an object from the repository.
*
* @param id id of the object
*/
delete(id: unknown): Promise<Result<unknown, unknown>>;
/**
* Encode object for storage.
*
* @param entity object to serialize
*/
serialize(entity: Partial<T>): unknown;
/**
* Create an object from a stored value.
*
* @param value value to deserialize
*/
deserialize(value: unknown): T;
}