UNPKG

5.88 kBJavaScriptView Raw
1"use strict";
2// Copyright IBM Corp. and LoopBack contributors 2018,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
6Object.defineProperty(exports, "__esModule", { value: true });
7exports.CrudRepositoryImpl = void 0;
8const errors_1 = require("../errors");
9/**
10 * Repository implementation
11 *
12 * @example
13 *
14 * User can import `CrudRepositoryImpl` and call its functions like:
15 * `CrudRepositoryImpl.find(somefilters, someoptions)`
16 *
17 * Or extend class `CrudRepositoryImpl` and override its functions:
18 * ```ts
19 * export class TestRepository extends CrudRepositoryImpl<Test> {
20 * constructor(dataSource: DataSource, model: Test) {
21 * super(dataSource, Customer);
22 * }
23 *
24 * // Override `deleteAll` to disable the operation
25 * deleteAll(where?: Where, options?: Options) {
26 * return Promise.reject(new Error('deleteAll is disabled'));
27 * }
28 * }
29 * ```
30 */
31class CrudRepositoryImpl {
32 constructor(dataSource,
33 // model should have type "typeof T", but that's not supported by TSC
34 entityClass) {
35 this.dataSource = dataSource;
36 this.entityClass = entityClass;
37 this.inclusionResolvers = new Map();
38 this.connector = dataSource.connector;
39 }
40 toModels(data) {
41 return data.then(items => items.map(i => new this.entityClass(i)));
42 }
43 toModel(data) {
44 return data.then(d => new this.entityClass(d));
45 }
46 create(entity, options) {
47 return this.toModel(this.connector.create(this.entityClass, entity, options));
48 }
49 createAll(entities, options) {
50 return this.toModels(this.connector.createAll(this.entityClass, entities, options));
51 }
52 async save(entity, options) {
53 if (typeof this.connector.save === 'function') {
54 return this.toModel(this.connector.save(this.entityClass, entity, options));
55 }
56 else {
57 const id = this.entityClass.getIdOf(entity);
58 if (id != null) {
59 await this.replaceById(id, entity, options);
60 return this.toModel(Promise.resolve(entity));
61 }
62 else {
63 return this.create(entity, options);
64 }
65 }
66 }
67 find(filter, options) {
68 return this.toModels(this.connector.find(this.entityClass, filter, options));
69 }
70 async findById(id, filter, options) {
71 if (typeof this.connector.findById === 'function') {
72 return this.toModel(this.connector.findById(this.entityClass, id, options));
73 }
74 const where = this.entityClass.buildWhereForId(id);
75 const entities = await this.toModels(this.connector.find(this.entityClass, { where: where }, options));
76 if (!entities.length) {
77 throw new errors_1.EntityNotFoundError(this.entityClass, id);
78 }
79 return entities[0];
80 }
81 update(entity, options) {
82 return this.updateById(this.entityClass.getIdOf(entity), entity, options);
83 }
84 delete(entity, options) {
85 return this.deleteById(this.entityClass.getIdOf(entity), options);
86 }
87 updateAll(data, where, options) {
88 return this.connector.updateAll(this.entityClass, data, where, options);
89 }
90 async updateById(id, data, options) {
91 let success;
92 if (typeof this.connector.updateById === 'function') {
93 success = await this.connector.updateById(this.entityClass, id, data, options);
94 }
95 else {
96 const where = this.entityClass.buildWhereForId(id);
97 const result = await this.updateAll(data, where, options);
98 success = result.count > 0;
99 }
100 if (!success) {
101 throw new errors_1.EntityNotFoundError(this.entityClass, id);
102 }
103 }
104 async replaceById(id, data, options) {
105 let success;
106 if (typeof this.connector.replaceById === 'function') {
107 success = await this.connector.replaceById(this.entityClass, id, data, options);
108 }
109 else {
110 // FIXME: populate inst with all properties
111 const inst = data;
112 const where = this.entityClass.buildWhereForId(id);
113 const result = await this.updateAll(data, where, options);
114 success = result.count > 0;
115 }
116 if (!success) {
117 throw new errors_1.EntityNotFoundError(this.entityClass, id);
118 }
119 }
120 deleteAll(where, options) {
121 return this.connector.deleteAll(this.entityClass, where, options);
122 }
123 async deleteById(id, options) {
124 let success;
125 if (typeof this.connector.deleteById === 'function') {
126 success = await this.connector.deleteById(this.entityClass, id, options);
127 }
128 else {
129 const where = this.entityClass.buildWhereForId(id);
130 const result = await this.deleteAll(where, options);
131 success = result.count > 0;
132 }
133 if (!success) {
134 throw new errors_1.EntityNotFoundError(this.entityClass, id);
135 }
136 }
137 count(where, options) {
138 return this.connector.count(this.entityClass, where, options);
139 }
140 exists(id, options) {
141 if (typeof this.connector.exists === 'function') {
142 return this.connector.exists(this.entityClass, id, options);
143 }
144 else {
145 const where = this.entityClass.buildWhereForId(id);
146 return this.count(where, options).then(result => result.count > 0);
147 }
148 }
149 execute(command, parameters, options) {
150 if (typeof this.connector.execute !== 'function') {
151 throw new Error('Not implemented');
152 }
153 return this.connector.execute(command, parameters, options);
154 }
155}
156exports.CrudRepositoryImpl = CrudRepositoryImpl;
157//# sourceMappingURL=repository.js.map
\No newline at end of file