1 | ;
|
2 | // Copyright IBM Corp. and LoopBack contributors 2020. All Rights Reserved.
|
3 | // Node module: @loopback/repository
|
4 | // This file is licensed under the MIT License.
|
5 | // License text available at https://opensource.org/licenses/MIT
|
6 | Object.defineProperty(exports, "__esModule", { value: true });
|
7 | exports.defineKeyValueRepositoryClass = exports.defineCrudRepositoryClass = exports.defineRepositoryClass = void 0;
|
8 | const tslib_1 = require("tslib");
|
9 | const assert_1 = tslib_1.__importDefault(require("assert"));
|
10 | const repositories_1 = require("./repositories");
|
11 | /**
|
12 | * Create (define) a repository class for the given model.
|
13 | *
|
14 | * See also `defineCrudRepositoryClass` and `defineKeyValueRepositoryClass`
|
15 | * for convenience wrappers providing repository class factory for the default
|
16 | * CRUD and KeyValue implementations.
|
17 | *
|
18 | * **❗️IMPORTANT: The compiler (TypeScript 3.8) is not able to correctly infer
|
19 | * generic arguments `M` and `R` from the class constructors provided in
|
20 | * function arguments. You must always provide both M and R types explicitly.**
|
21 | *
|
22 | * @example
|
23 | *
|
24 | * ```ts
|
25 | * const AddressRepository = defineRepositoryClass<
|
26 | * typeof Address,
|
27 | * DefaultEntityCrudRepository<
|
28 | * Address,
|
29 | * typeof Address.prototype.id,
|
30 | * AddressRelations
|
31 | * >,
|
32 | * >(Address, DefaultCrudRepository);
|
33 | * ```
|
34 | *
|
35 | * @param modelClass - A model class such as `Address`.
|
36 | * @param baseRepositoryClass - Repository implementation to use as the base,
|
37 | * e.g. `DefaultCrudRepository`.
|
38 | *
|
39 | * @typeParam M - Model class constructor (e.g. `typeof Address`)
|
40 | * @typeParam R - Repository class (e.g. `DefaultCrudRepository<Address, number>`)
|
41 | */
|
42 | function defineRepositoryClass(modelClass, baseRepositoryClass) {
|
43 | const repoName = modelClass.name + 'Repository';
|
44 | const defineNamedRepo = new Function('ModelCtor', 'BaseRepository', `return class ${repoName} extends BaseRepository {
|
45 | constructor(dataSource) {
|
46 | super(ModelCtor, dataSource);
|
47 | }
|
48 | };`);
|
49 | const repo = defineNamedRepo(modelClass, baseRepositoryClass);
|
50 | assert_1.default.equal(repo.name, repoName);
|
51 | return repo;
|
52 | }
|
53 | exports.defineRepositoryClass = defineRepositoryClass;
|
54 | /**
|
55 | * Create (define) an entity CRUD repository class for the given model.
|
56 | * This function always uses `DefaultCrudRepository` as the base class,
|
57 | * use `defineRepositoryClass` if you want to use your own base repository.
|
58 | *
|
59 | * @example
|
60 | *
|
61 | * ```ts
|
62 | * const ProductRepository = defineCrudRepositoryClass<
|
63 | * Product,
|
64 | * typeof Product.prototype.id,
|
65 | * ProductRelations
|
66 | * >(Product);
|
67 | * ```
|
68 | *
|
69 | * @param entityClass - An entity class such as `Product`.
|
70 | *
|
71 | * @typeParam E - An entity class
|
72 | * @typeParam IdType - ID type for the entity
|
73 | * @typeParam Relations - Relations for the entity
|
74 | */
|
75 | function defineCrudRepositoryClass(entityClass) {
|
76 | return defineRepositoryClass(entityClass, repositories_1.DefaultCrudRepository);
|
77 | }
|
78 | exports.defineCrudRepositoryClass = defineCrudRepositoryClass;
|
79 | /**
|
80 | * Create (define) a KeyValue repository class for the given entity.
|
81 | * This function always uses `DefaultKeyValueRepository` as the base class,
|
82 | * use `defineRepositoryClass` if you want to use your own base repository.
|
83 | *
|
84 | * @example
|
85 | *
|
86 | * ```ts
|
87 | * const ProductKeyValueRepository = defineKeyValueRepositoryClass(Product);
|
88 | * ```
|
89 | *
|
90 | * @param modelClass - An entity class such as `Product`.
|
91 | *
|
92 | * @typeParam M - Model class
|
93 | */
|
94 | function defineKeyValueRepositoryClass(modelClass) {
|
95 | return defineRepositoryClass(modelClass, repositories_1.DefaultKeyValueRepository);
|
96 | }
|
97 | exports.defineKeyValueRepositoryClass = defineKeyValueRepositoryClass;
|
98 | //# sourceMappingURL=define-repository-class.js.map |
\ | No newline at end of file |