Skip to content

Commit 161e920

Browse files
Merge pull request #245 from andrechristikan/development
Development
2 parents b66e5fd + 2b0233c commit 161e920

16 files changed

+335
-219
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ack-nestjs-mongoose-kafka",
3-
"version": "2.4.0",
3+
"version": "2.4.1",
44
"description": "Ack NestJs Mongoose Kafka",
55
"repository": {
66
"type": "git",
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { IDatabaseSoftDeleteOptions } from 'src/common/database/interfaces/database.interface';
1+
import { IDatabaseManyOptions } from 'src/common/database/interfaces/database.interface';
22

33
export interface IAuthApiBulkService {
44
deleteMany(
55
find: Record<string, any>,
6-
options?: IDatabaseSoftDeleteOptions
6+
options?: IDatabaseManyOptions
77
): Promise<boolean>;
88
}

src/common/auth/services/auth.api.bulk.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Injectable } from '@nestjs/common';
2-
import { IDatabaseSoftDeleteOptions } from 'src/common/database/interfaces/database.interface';
2+
import { IDatabaseManyOptions } from 'src/common/database/interfaces/database.interface';
33
import { IAuthApiBulkService } from 'src/common/auth/interfaces/auth.api.bulk-service.interface';
44
import { AuthApiBulkRepository } from 'src/common/auth/repositories/auth.api.bulk.repository';
55

@@ -11,7 +11,7 @@ export class AuthApiBulkService implements IAuthApiBulkService {
1111

1212
async deleteMany(
1313
find: Record<string, any>,
14-
options?: IDatabaseSoftDeleteOptions
14+
options?: IDatabaseManyOptions
1515
): Promise<boolean> {
1616
return this.authApiBulkRepository.deleteMany(find, options);
1717
}

src/common/database/abstracts/database.mongo-bulk-repository.abstract.ts

Lines changed: 136 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,36 @@
1-
import { Model, Types } from 'mongoose';
1+
import { Model, PopulateOptions, Types } from 'mongoose';
22
import { IDatabaseBulkRepositoryAbstract } from 'src/common/database/interfaces/database.bulk.repository.interface';
33
import {
44
IDatabaseCreateManyOptions,
5-
IDatabaseSoftDeleteOptions,
6-
IDatabaseOptions,
7-
IDatabaseRestoreOptions,
5+
IDatabaseManyOptions,
6+
IDatabaseSoftDeleteManyOptions,
7+
IDatabaseRestoreManyOptions,
88
} from 'src/common/database/interfaces/database.interface';
99

1010
export abstract class DatabaseMongoBulkRepositoryAbstract<T>
1111
implements IDatabaseBulkRepositoryAbstract
1212
{
1313
protected _repository: Model<T>;
14+
protected _populateOnFind?: PopulateOptions | PopulateOptions[];
1415

15-
constructor(repository: Model<T>) {
16+
constructor(
17+
repository: Model<T>,
18+
populateOnFind?: PopulateOptions | PopulateOptions[]
19+
) {
1620
this._repository = repository;
21+
this._populateOnFind = populateOnFind;
1722
}
1823

1924
async createMany<N>(
2025
data: N[],
2126
options?: IDatabaseCreateManyOptions
2227
): Promise<boolean> {
23-
try {
24-
await this._repository.insertMany(data, {
25-
session: options ? options.session : undefined,
26-
});
28+
const create = this._repository.insertMany(data, {
29+
session: options ? options.session : undefined,
30+
});
2731

32+
try {
33+
await create;
2834
return true;
2935
} catch (err: any) {
3036
throw err;
@@ -33,28 +39,31 @@ export abstract class DatabaseMongoBulkRepositoryAbstract<T>
3339

3440
async deleteManyById(
3541
_id: string[],
36-
options?: IDatabaseOptions
42+
options?: IDatabaseManyOptions
3743
): Promise<boolean> {
3844
const map: Types.ObjectId[] = _id.map((val) => new Types.ObjectId(val));
3945

40-
try {
41-
const del = this._repository.deleteMany(
42-
{
43-
_id: {
44-
$in: map,
45-
},
46-
},
47-
{
48-
session: options ? options.session : undefined,
49-
}
50-
);
46+
const del = this._repository.deleteMany({
47+
_id: {
48+
$in: map,
49+
},
50+
});
51+
52+
if (options && options.withDeleted) {
53+
del.where('deletedAt').exists(true);
54+
} else {
55+
del.where('deletedAt').exists(false);
56+
}
57+
58+
if (options && options.session) {
59+
del.session(options.session);
60+
}
5161

52-
if (options && options.withDeleted) {
53-
del.where('deletedAt').exists(true);
54-
} else {
55-
del.where('deletedAt').exists(false);
56-
}
62+
if (options && options.populate) {
63+
del.populate(this._populateOnFind);
64+
}
5765

66+
try {
5867
await del;
5968
return true;
6069
} catch (err: any) {
@@ -64,19 +73,25 @@ export abstract class DatabaseMongoBulkRepositoryAbstract<T>
6473

6574
async deleteMany(
6675
find: Record<string, any>,
67-
options?: IDatabaseOptions
76+
options?: IDatabaseManyOptions
6877
): Promise<boolean> {
69-
try {
70-
const del = this._repository.deleteMany(find, {
71-
session: options ? options.session : undefined,
72-
});
78+
const del = this._repository.deleteMany(find);
7379

74-
if (options && options.withDeleted) {
75-
del.where('deletedAt').exists(true);
76-
} else {
77-
del.where('deletedAt').exists(false);
78-
}
80+
if (options && options.withDeleted) {
81+
del.where('deletedAt').exists(true);
82+
} else {
83+
del.where('deletedAt').exists(false);
84+
}
85+
86+
if (options && options.session) {
87+
del.session(options.session);
88+
}
89+
90+
if (options && options.populate) {
91+
del.populate(this._populateOnFind);
92+
}
7993

94+
try {
8095
await del;
8196
return true;
8297
} catch (err: any) {
@@ -86,30 +101,36 @@ export abstract class DatabaseMongoBulkRepositoryAbstract<T>
86101

87102
async softDeleteManyById(
88103
_id: string[],
89-
options?: IDatabaseSoftDeleteOptions
104+
options?: IDatabaseSoftDeleteManyOptions
90105
): Promise<boolean> {
91106
const map: Types.ObjectId[] = _id.map((val) => new Types.ObjectId(val));
92107

93-
try {
94-
await this._repository
95-
.updateMany(
96-
{
97-
_id: {
98-
$in: map,
99-
},
108+
const softDel = this._repository
109+
.updateMany(
110+
{
111+
_id: {
112+
$in: map,
100113
},
101-
{
102-
$set: {
103-
deletedAt: new Date(),
104-
},
114+
},
115+
{
116+
$set: {
117+
deletedAt: new Date(),
105118
},
106-
{
107-
session: options ? options.session : undefined,
108-
}
109-
)
110-
.where('deletedAt')
111-
.exists(false);
119+
}
120+
)
121+
.where('deletedAt')
122+
.exists(false);
123+
124+
if (options && options.session) {
125+
softDel.session(options.session);
126+
}
112127

128+
if (options && options.populate) {
129+
softDel.populate(this._populateOnFind);
130+
}
131+
132+
try {
133+
await softDel;
113134
return true;
114135
} catch (err: any) {
115136
throw err;
@@ -118,24 +139,27 @@ export abstract class DatabaseMongoBulkRepositoryAbstract<T>
118139

119140
async softDeleteMany(
120141
find: Record<string, any>,
121-
options?: IDatabaseSoftDeleteOptions
142+
options?: IDatabaseSoftDeleteManyOptions
122143
): Promise<boolean> {
123-
try {
124-
await this._repository
125-
.updateMany(
126-
find,
127-
{
128-
$set: {
129-
deletedAt: new Date(),
130-
},
131-
},
132-
{
133-
session: options ? options.session : undefined,
134-
}
135-
)
136-
.where('deletedAt')
137-
.exists(false);
144+
const softDel = this._repository
145+
.updateMany(find, {
146+
$set: {
147+
deletedAt: new Date(),
148+
},
149+
})
150+
.where('deletedAt')
151+
.exists(false);
152+
153+
if (options && options.session) {
154+
softDel.session(options.session);
155+
}
156+
157+
if (options && options.populate) {
158+
softDel.populate(this._populateOnFind);
159+
}
138160

161+
try {
162+
await softDel;
139163
return true;
140164
} catch (err: any) {
141165
throw err;
@@ -144,30 +168,36 @@ export abstract class DatabaseMongoBulkRepositoryAbstract<T>
144168

145169
async restore(
146170
_id: string[],
147-
options?: IDatabaseRestoreOptions
171+
options?: IDatabaseRestoreManyOptions
148172
): Promise<boolean> {
149173
const map: Types.ObjectId[] = _id.map((val) => new Types.ObjectId(val));
150174

151-
try {
152-
await this._repository
153-
.updateMany(
154-
{
155-
_id: {
156-
$in: map,
157-
},
175+
const rest = this._repository
176+
.updateMany(
177+
{
178+
_id: {
179+
$in: map,
158180
},
159-
{
160-
$set: {
161-
deletedAt: undefined,
162-
},
181+
},
182+
{
183+
$set: {
184+
deletedAt: undefined,
163185
},
164-
{
165-
session: options ? options.session : undefined,
166-
}
167-
)
168-
.where('deletedAt')
169-
.exists(true);
186+
}
187+
)
188+
.where('deletedAt')
189+
.exists(true);
190+
191+
if (options && options.session) {
192+
rest.session(options.session);
193+
}
194+
195+
if (options && options.populate) {
196+
rest.populate(this._populateOnFind);
197+
}
170198

199+
try {
200+
await rest;
171201
return true;
172202
} catch (err: any) {
173203
throw err;
@@ -177,25 +207,27 @@ export abstract class DatabaseMongoBulkRepositoryAbstract<T>
177207
async updateMany<N>(
178208
find: Record<string, any>,
179209
data: N,
180-
options?: IDatabaseOptions
210+
options?: IDatabaseManyOptions
181211
): Promise<boolean> {
182-
try {
183-
const update = this._repository.updateMany(
184-
find,
185-
{
186-
$set: data,
187-
},
188-
{
189-
session: options ? options.session : undefined,
190-
}
191-
);
212+
const update = this._repository.updateMany(find, {
213+
$set: data,
214+
});
215+
216+
if (options && options.withDeleted) {
217+
update.where('deletedAt').exists(true);
218+
} else {
219+
update.where('deletedAt').exists(false);
220+
}
192221

193-
if (options && options.withDeleted) {
194-
update.where('deletedAt').exists(true);
195-
} else {
196-
update.where('deletedAt').exists(false);
197-
}
222+
if (options && options.session) {
223+
update.session(options.session);
224+
}
225+
226+
if (options && options.populate) {
227+
update.populate(this._populateOnFind);
228+
}
198229

230+
try {
199231
await update;
200232
return true;
201233
} catch (err: any) {

0 commit comments

Comments
 (0)