1 | // Copyright IBM Corp. and LoopBack contributors 2020. All Rights Reserved.
|
2 | // Node module: @loopback/repository
|
3 | // This file is licensed under the MIT License.
|
4 | // License text available at https://opensource.org/licenses/MIT
|
5 |
|
6 | import assert from 'assert';
|
7 | import {PrototypeOf} from './common-types';
|
8 | import {Entity, Model} from './model';
|
9 | import {
|
10 | DefaultCrudRepository,
|
11 | DefaultKeyValueRepository,
|
12 | juggler,
|
13 | Repository,
|
14 | } from './repositories';
|
15 |
|
16 | /**
|
17 | * Signature for a Repository class bound to a given model. The constructor
|
18 | * accepts only the dataSource to use for persistence.
|
19 | *
|
20 | * `define*` functions return a class implementing this interface.
|
21 | *
|
22 | * @typeParam M - Model class
|
23 | * @typeParam R - Repository class/interface
|
24 | */
|
25 | export interface ModelRepositoryClass<
|
26 | M extends Model,
|
27 | R extends Repository<M>,
|
28 | > {
|
29 | /**
|
30 | * The constructor for the generated repository class
|
31 | * @param dataSource - DataSource object
|
32 | */
|
33 | new (dataSource: juggler.DataSource): R;
|
34 | prototype: R;
|
35 | }
|
36 |
|
37 | /**
|
38 | * Signature for repository classes that can be used as the base class for
|
39 | * `define*` functions. The constructor of a base repository class accepts
|
40 | * the target model constructor and the datasource to use.
|
41 | *
|
42 | * `define*` functions require a class implementing this interface on input.
|
43 | *
|
44 | * @typeParam M - Model class constructor, e.g `typeof Model`.
|
45 | * **❗️IMPORTANT: The type argument `M` is describing the model constructor type
|
46 | * (e.g. `typeof Model`), not the model instance type (`Model`) as is the case
|
47 | * in other repository-related types. The constructor type is required
|
48 | * to support custom repository classes requiring a Model subclass in the
|
49 | * constructor arguments, e.g. `Entity` or a user-provided model.**
|
50 | *
|
51 | * @typeParam R - Repository class/interface
|
52 | */
|
53 | export interface BaseRepositoryClass<
|
54 | M extends typeof Model,
|
55 | R extends Repository<PrototypeOf<M>>,
|
56 | > {
|
57 | /**
|
58 | * The constructor for the generated repository class
|
59 | * @param modelClass - Model class
|
60 | * @param dataSource - DataSource object
|
61 | */
|
62 | new (modelClass: M, dataSource: juggler.DataSource): R;
|
63 | prototype: R;
|
64 | }
|
65 |
|
66 | /**
|
67 | * Create (define) a repository class for the given model.
|
68 | *
|
69 | * See also `defineCrudRepositoryClass` and `defineKeyValueRepositoryClass`
|
70 | * for convenience wrappers providing repository class factory for the default
|
71 | * CRUD and KeyValue implementations.
|
72 | *
|
73 | * **❗️IMPORTANT: The compiler (TypeScript 3.8) is not able to correctly infer
|
74 | * generic arguments `M` and `R` from the class constructors provided in
|
75 | * function arguments. You must always provide both M and R types explicitly.**
|
76 | *
|
77 | * @example
|
78 | *
|
79 | * ```ts
|
80 | * const AddressRepository = defineRepositoryClass<
|
81 | * typeof Address,
|
82 | * DefaultEntityCrudRepository<
|
83 | * Address,
|
84 | * typeof Address.prototype.id,
|
85 | * AddressRelations
|
86 | * >,
|
87 | * >(Address, DefaultCrudRepository);
|
88 | * ```
|
89 | *
|
90 | * @param modelClass - A model class such as `Address`.
|
91 | * @param baseRepositoryClass - Repository implementation to use as the base,
|
92 | * e.g. `DefaultCrudRepository`.
|
93 | *
|
94 | * @typeParam M - Model class constructor (e.g. `typeof Address`)
|
95 | * @typeParam R - Repository class (e.g. `DefaultCrudRepository<Address, number>`)
|
96 | */
|
97 | export function defineRepositoryClass<
|
98 | M extends typeof Model,
|
99 | R extends Repository<PrototypeOf<M>>,
|
100 | >(
|
101 | modelClass: M,
|
102 | baseRepositoryClass: BaseRepositoryClass<M, R>,
|
103 | ): ModelRepositoryClass<PrototypeOf<M>, R> {
|
104 | const repoName = modelClass.name + 'Repository';
|
105 | const defineNamedRepo = new Function(
|
106 | 'ModelCtor',
|
107 | 'BaseRepository',
|
108 | `return class ${repoName} extends BaseRepository {
|
109 | constructor(dataSource) {
|
110 | super(ModelCtor, dataSource);
|
111 | }
|
112 | };`,
|
113 | );
|
114 |
|
115 | const repo = defineNamedRepo(modelClass, baseRepositoryClass);
|
116 | assert.equal(repo.name, repoName);
|
117 | return repo;
|
118 | }
|
119 |
|
120 | /**
|
121 | * Create (define) an entity CRUD repository class for the given model.
|
122 | * This function always uses `DefaultCrudRepository` as the base class,
|
123 | * use `defineRepositoryClass` if you want to use your own base repository.
|
124 | *
|
125 | * @example
|
126 | *
|
127 | * ```ts
|
128 | * const ProductRepository = defineCrudRepositoryClass<
|
129 | * Product,
|
130 | * typeof Product.prototype.id,
|
131 | * ProductRelations
|
132 | * >(Product);
|
133 | * ```
|
134 | *
|
135 | * @param entityClass - An entity class such as `Product`.
|
136 | *
|
137 | * @typeParam E - An entity class
|
138 | * @typeParam IdType - ID type for the entity
|
139 | * @typeParam Relations - Relations for the entity
|
140 | */
|
141 | export function defineCrudRepositoryClass<
|
142 | E extends Entity,
|
143 | IdType,
|
144 | Relations extends object,
|
145 | >(
|
146 | entityClass: typeof Entity & {prototype: E},
|
147 | ): ModelRepositoryClass<E, DefaultCrudRepository<E, IdType, Relations>> {
|
148 | return defineRepositoryClass(entityClass, DefaultCrudRepository);
|
149 | }
|
150 |
|
151 | /**
|
152 | * Create (define) a KeyValue repository class for the given entity.
|
153 | * This function always uses `DefaultKeyValueRepository` as the base class,
|
154 | * use `defineRepositoryClass` if you want to use your own base repository.
|
155 | *
|
156 | * @example
|
157 | *
|
158 | * ```ts
|
159 | * const ProductKeyValueRepository = defineKeyValueRepositoryClass(Product);
|
160 | * ```
|
161 | *
|
162 | * @param modelClass - An entity class such as `Product`.
|
163 | *
|
164 | * @typeParam M - Model class
|
165 | */
|
166 | export function defineKeyValueRepositoryClass<M extends Model>(
|
167 | modelClass: typeof Model & {prototype: M},
|
168 | ): ModelRepositoryClass<M, DefaultKeyValueRepository<M>> {
|
169 | return defineRepositoryClass(modelClass, DefaultKeyValueRepository);
|
170 | }
|