Skip to content

Commit 4ba581c

Browse files
author
KoLiBer
committed
feat: add models mapper method, create methods
1 parent 20de982 commit 4ba581c

File tree

1 file changed

+62
-9
lines changed

1 file changed

+62
-9
lines changed

src/repositories/filter.repository.ts

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ export interface FilterRepository<
2222
> {}
2323

2424
/**
25+
* +--------+
26+
* | create |
27+
* +----+---+
28+
* |
29+
* |
30+
* +----v------+
31+
* | createAll |
32+
* +-----------+
33+
*
34+
*
2535
* +----------+ +--------+
2636
* | findById | | exists |
2737
* +----+-----+ +---+----+
@@ -59,6 +69,10 @@ export function FilterRepositoryMixin<
5969
ID,
6070
Relations extends object = {}
6171
>(config: {
72+
models: (
73+
context: InvocationContext,
74+
models: DataObject<T>[]
75+
) => Promise<(DataObject<T> | undefined)[]>;
6276
where: (context: InvocationContext, where: Where<T>) => Promise<Where<T>>;
6377
fields: (
6478
context: InvocationContext,
@@ -71,6 +85,36 @@ export function FilterRepositoryMixin<
7185
class MixedRepository
7286
extends superClass
7387
implements FilterRepository<T, ID, Relations> {
88+
/**
89+
* Map models and create all entities
90+
*/
91+
createAll = async (
92+
entities: DataObject<T>[],
93+
options?: Options
94+
) => {
95+
const filterContext = new InvocationContext(
96+
undefined as any,
97+
this,
98+
"create",
99+
Array.from(arguments)
100+
);
101+
102+
const mappedEntities: any[] = (
103+
await config.models(filterContext, entities || [])
104+
).filter((entity) => entity);
105+
106+
return await super.createAll(mappedEntities, options);
107+
};
108+
109+
/**
110+
* Filter create() using createAll()
111+
*/
112+
create = async (entity: DataObject<T>, options?: Options) => {
113+
const result = await this.createAll([entity], options);
114+
115+
return result[0];
116+
};
117+
74118
/**
75119
* Filter where and find all entities
76120
*/
@@ -126,7 +170,7 @@ export function FilterRepositoryMixin<
126170
};
127171

128172
/**
129-
* History findById() using findOne()
173+
* Filter findById() using findOne()
130174
*/
131175
findById = async (
132176
id: ID,
@@ -166,7 +210,7 @@ export function FilterRepositoryMixin<
166210
};
167211

168212
/**
169-
* History exists() using count()
213+
* Filter exists() using count()
170214
*/
171215
exists = async (id: ID, options?: Options) => {
172216
const result = await this.count(
@@ -178,7 +222,7 @@ export function FilterRepositoryMixin<
178222
};
179223

180224
/**
181-
* Filter where and update all entities
225+
* Filter where, Map models and update all entities
182226
*/
183227
updateAll = async (
184228
data: DataObject<T>,
@@ -192,15 +236,24 @@ export function FilterRepositoryMixin<
192236
Array.from(arguments)
193237
);
194238

239+
const mappedModel = (
240+
await config.models(filterContext, [data])
241+
)[0];
242+
if (!mappedModel) {
243+
return {
244+
count: 0,
245+
};
246+
}
247+
195248
return await super.updateAll(
196-
data,
249+
mappedModel,
197250
await config.where(filterContext, where || {}),
198251
options
199252
);
200253
};
201254

202255
/**
203-
* History updateById() using updateAll()
256+
* Filter updateById() using updateAll()
204257
*/
205258
updateById = async (
206259
id: ID,
@@ -215,7 +268,7 @@ export function FilterRepositoryMixin<
215268
};
216269

217270
/**
218-
* History update() using updateAll()
271+
* Filter update() using updateAll()
219272
*/
220273
update = async (entity: T, options?: Options) => {
221274
await this.updateAll(
@@ -228,7 +281,7 @@ export function FilterRepositoryMixin<
228281
};
229282

230283
/**
231-
* History replaceById() using updateAll()
284+
* Filter replaceById() using updateAll()
232285
*/
233286
replaceById = async (
234287
id: ID,
@@ -267,7 +320,7 @@ export function FilterRepositoryMixin<
267320
};
268321

269322
/**
270-
* History delete() using deleteAll()
323+
* Filter delete() using deleteAll()
271324
*/
272325
delete = async (entity: T, options?: Options) => {
273326
await this.deleteAll(
@@ -279,7 +332,7 @@ export function FilterRepositoryMixin<
279332
};
280333

281334
/**
282-
* History deleteById() using deleteAll()
335+
* Filter deleteById() using deleteAll()
283336
*/
284337
deleteById = async (id: ID, options?: Options) => {
285338
await this.deleteAll(

0 commit comments

Comments
 (0)