/
repository.ts
78 lines (72 loc) · 2.73 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
74
75
76
77
78
import { Optional } from 'typescript-optional';
import { Entity } from './util/entity';
import {
DeleteByIdOptions,
FindAllOptions,
FindByIdOptions,
FindOneOptions,
SaveOptions,
} from './util/operation-options';
/**
* Models an entity with partial content that specifies a Mongo `id` and (optionally) a Mongoose discriminator key.
*/
export type PartialEntityWithId<T extends Entity> = {
id: string;
} & {
__t?: string;
} & Partial<T>;
/**
* Specifies a list of common database CRUD operations.
*/
export interface Repository<T extends Entity> {
/**
* Finds an entity by ID.
* @param {string} id the ID of the entity.
* @param {FindByIdOptions=} options (optional) search operation options.
* @returns {Promise<Optional<S>>} the entity or null.
* @throws {IllegalArgumentException} if the given `id` is `undefined` or `null`.
*/
findById: <S extends T>(
id: string,
options?: FindByIdOptions,
) => Promise<Optional<S>>;
/**
* Finds an entity by some filters.
* @param {any} filters some filters for the search - Deprecated since v5.0.1, use options.filters instead.
* @param {FindOneOptions=} options (optional) search operation options.
* @returns {Promise<Optional<S>>} the entity or null.
* @throws {IllegalArgumentException} if the given `filters` parameter is `undefined` or `null`.
*/
findOne: <S extends T>(
filters: any,
options?: FindOneOptions,
) => Promise<Optional<S>>;
/**
* Finds all entities.
* @param {FindAllOptions=} options (optional) search operation options.
* @returns {Promise<S[]>} all entities.
* @throws {IllegalArgumentException} if the given `options` specifies an invalid parameter.
*/
findAll: <S extends T>(options?: FindAllOptions) => Promise<S[]>;
/**
* Saves (insert or update) an entity.
* @param {S | PartialEntityWithId<S>} entity the entity to save.
* @param {SaveOptions=} options (optional) save operation options.
* @returns {Promise<S>} the saved entity.
* @throws {IllegalArgumentException} if the given entity is `undefined` or `null` or
* specifies an `id` not matching any existing entity.
* @throws {ValidationException} if the given entity specifies a field with some invalid value.
*/
save: <S extends T>(
entity: S | PartialEntityWithId<S>,
options?: SaveOptions,
) => Promise<S>;
/**
* Deletes an entity by ID.
* @param {string} id the ID of the entity.
* @param {DeleteByIdOptions=} options (optional) delete operation options.
* @returns {Promise<boolean>} `true` if the entity was deleted, `false` otherwise.
* @throws {IllegalArgumentException} if the given `id` is `undefined` or `null`.
*/
deleteById: (id: string, options?: DeleteByIdOptions) => Promise<boolean>;
}