1 | import { PrototypeOf } from './common-types';
|
2 | import { Entity, Model } from './model';
|
3 | import { DefaultCrudRepository, DefaultKeyValueRepository, juggler, Repository } from './repositories';
|
4 | /**
|
5 | * Signature for a Repository class bound to a given model. The constructor
|
6 | * accepts only the dataSource to use for persistence.
|
7 | *
|
8 | * `define*` functions return a class implementing this interface.
|
9 | *
|
10 | * @typeParam M - Model class
|
11 | * @typeParam R - Repository class/interface
|
12 | */
|
13 | export interface ModelRepositoryClass<M extends Model, R extends Repository<M>> {
|
14 | /**
|
15 | * The constructor for the generated repository class
|
16 | * @param dataSource - DataSource object
|
17 | */
|
18 | new (dataSource: juggler.DataSource): R;
|
19 | prototype: R;
|
20 | }
|
21 | /**
|
22 | * Signature for repository classes that can be used as the base class for
|
23 | * `define*` functions. The constructor of a base repository class accepts
|
24 | * the target model constructor and the datasource to use.
|
25 | *
|
26 | * `define*` functions require a class implementing this interface on input.
|
27 | *
|
28 | * @typeParam M - Model class constructor, e.g `typeof Model`.
|
29 | * **❗️IMPORTANT: The type argument `M` is describing the model constructor type
|
30 | * (e.g. `typeof Model`), not the model instance type (`Model`) as is the case
|
31 | * in other repository-related types. The constructor type is required
|
32 | * to support custom repository classes requiring a Model subclass in the
|
33 | * constructor arguments, e.g. `Entity` or a user-provided model.**
|
34 | *
|
35 | * @typeParam R - Repository class/interface
|
36 | */
|
37 | export interface BaseRepositoryClass<M extends typeof Model, R extends Repository<PrototypeOf<M>>> {
|
38 | /**
|
39 | * The constructor for the generated repository class
|
40 | * @param modelClass - Model class
|
41 | * @param dataSource - DataSource object
|
42 | */
|
43 | new (modelClass: M, dataSource: juggler.DataSource): R;
|
44 | prototype: R;
|
45 | }
|
46 | /**
|
47 | * Create (define) a repository class for the given model.
|
48 | *
|
49 | * See also `defineCrudRepositoryClass` and `defineKeyValueRepositoryClass`
|
50 | * for convenience wrappers providing repository class factory for the default
|
51 | * CRUD and KeyValue implementations.
|
52 | *
|
53 | * **❗️IMPORTANT: The compiler (TypeScript 3.8) is not able to correctly infer
|
54 | * generic arguments `M` and `R` from the class constructors provided in
|
55 | * function arguments. You must always provide both M and R types explicitly.**
|
56 | *
|
57 | * @example
|
58 | *
|
59 | * ```ts
|
60 | * const AddressRepository = defineRepositoryClass<
|
61 | * typeof Address,
|
62 | * DefaultEntityCrudRepository<
|
63 | * Address,
|
64 | * typeof Address.prototype.id,
|
65 | * AddressRelations
|
66 | * >,
|
67 | * >(Address, DefaultCrudRepository);
|
68 | * ```
|
69 | *
|
70 | * @param modelClass - A model class such as `Address`.
|
71 | * @param baseRepositoryClass - Repository implementation to use as the base,
|
72 | * e.g. `DefaultCrudRepository`.
|
73 | *
|
74 | * @typeParam M - Model class constructor (e.g. `typeof Address`)
|
75 | * @typeParam R - Repository class (e.g. `DefaultCrudRepository<Address, number>`)
|
76 | */
|
77 | export declare function defineRepositoryClass<M extends typeof Model, R extends Repository<PrototypeOf<M>>>(modelClass: M, baseRepositoryClass: BaseRepositoryClass<M, R>): ModelRepositoryClass<PrototypeOf<M>, R>;
|
78 | /**
|
79 | * Create (define) an entity CRUD repository class for the given model.
|
80 | * This function always uses `DefaultCrudRepository` as the base class,
|
81 | * use `defineRepositoryClass` if you want to use your own base repository.
|
82 | *
|
83 | * @example
|
84 | *
|
85 | * ```ts
|
86 | * const ProductRepository = defineCrudRepositoryClass<
|
87 | * Product,
|
88 | * typeof Product.prototype.id,
|
89 | * ProductRelations
|
90 | * >(Product);
|
91 | * ```
|
92 | *
|
93 | * @param entityClass - An entity class such as `Product`.
|
94 | *
|
95 | * @typeParam E - An entity class
|
96 | * @typeParam IdType - ID type for the entity
|
97 | * @typeParam Relations - Relations for the entity
|
98 | */
|
99 | export declare function defineCrudRepositoryClass<E extends Entity, IdType, Relations extends object>(entityClass: typeof Entity & {
|
100 | prototype: E;
|
101 | }): ModelRepositoryClass<E, DefaultCrudRepository<E, IdType, Relations>>;
|
102 | /**
|
103 | * Create (define) a KeyValue repository class for the given entity.
|
104 | * This function always uses `DefaultKeyValueRepository` as the base class,
|
105 | * use `defineRepositoryClass` if you want to use your own base repository.
|
106 | *
|
107 | * @example
|
108 | *
|
109 | * ```ts
|
110 | * const ProductKeyValueRepository = defineKeyValueRepositoryClass(Product);
|
111 | * ```
|
112 | *
|
113 | * @param modelClass - An entity class such as `Product`.
|
114 | *
|
115 | * @typeParam M - Model class
|
116 | */
|
117 | export declare function defineKeyValueRepositoryClass<M extends Model>(modelClass: typeof Model & {
|
118 | prototype: M;
|
119 | }): ModelRepositoryClass<M, DefaultKeyValueRepository<M>>;
|