Skip to content

Commit 755b8ea

Browse files
committed
feat: add getPublic method for Service
1 parent 2631c12 commit 755b8ea

File tree

7 files changed

+59
-204
lines changed

7 files changed

+59
-204
lines changed

docs/packages/node-mongo/overview.mdx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,7 @@ Overrides `ServiceOptions` parameters for create operations.
222222
```typescript
223223
type CreateConfig = {
224224
validateSchema?: boolean,
225-
publishEvents?: boolean,
226-
mode?: 'public' | 'private',
225+
publishEvents?: boolean
227226
};
228227
```
229228

@@ -233,8 +232,7 @@ Overrides `ServiceOptions` parameters for read operations.
233232
```typescript
234233
type ReadConfig = {
235234
skipDeletedOnDocs?: boolean,
236-
populate?: PopulateOptions | PopulateOptions[],
237-
mode?: 'public' | 'private',
235+
populate?: PopulateOptions | PopulateOptions[]
238236
};
239237
```
240238

@@ -245,8 +243,7 @@ Overrides `ServiceOptions` parameters for update operations.
245243
type UpdateConfig = {
246244
skipDeletedOnDocs?: boolean,
247245
validateSchema?: boolean,
248-
publishEvents?: boolean,
249-
mode?: 'public' | 'private',
246+
publishEvents?: boolean
250247
};
251248
```
252249

packages/node-mongo/src/database.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class Database extends EventEmitter {
8383

8484
createService<T extends IDocument>(
8585
collectionName: string,
86-
options?: ServiceOptions | undefined,
86+
options?: ServiceOptions<T> | undefined,
8787
): Service<T> {
8888
return new Service<T>(
8989
collectionName,

packages/node-mongo/src/service.ts

Lines changed: 27 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ import {
3333
} from './types';
3434

3535
import logger from './utils/logger';
36-
import { addUpdatedOnField, generateId, getPrivateFindOptions, omitPrivateFields } from './utils/helpers';
36+
import { addUpdatedOnField, generateId, omitPrivateFields } from './utils/helpers';
3737
import PopulateUtil from './utils/populate';
3838

3939
import { inMemoryPublisher } from './events/in-memory';
4040

41-
const defaultOptions: ServiceOptions = {
41+
const defaultOptions: ServiceOptions<IDocument> = {
4242
skipDeletedOnDocs: true,
4343
publishEvents: true,
4444
outbox: false,
@@ -57,7 +57,7 @@ class Service<T extends IDocument> {
5757

5858
private _collectionName: string;
5959

60-
private options: ServiceOptions;
60+
private options: ServiceOptions<T>;
6161

6262
private db;
6363

@@ -68,7 +68,7 @@ class Service<T extends IDocument> {
6868
constructor(
6969
collectionName: string,
7070
db: IDatabase,
71-
options: ServiceOptions = {},
71+
options: ServiceOptions<T> = {},
7272
) {
7373
this._collectionName = collectionName;
7474
this.db = db;
@@ -240,21 +240,19 @@ class Service<T extends IDocument> {
240240
readConfig: ReadConfig = {},
241241
findOptions: FindOptions = {},
242242
): Promise<(U & PopulateTypes) | U | null> {
243-
const { mode = 'private', populate } = readConfig;
243+
const { populate } = readConfig;
244244

245245
const collection = await this.getCollection<U>();
246246

247247
filter = this.handleReadOperations(filter, readConfig);
248248

249-
const privateFindOptions = getPrivateFindOptions({ mode, findOptions, privateFields: this.options.privateFields });
250-
251249
if (populate) {
252-
const docs = await this.populateAggregate<U, PopulateTypes>(collection, filter, readConfig, privateFindOptions);
250+
const docs = await this.populateAggregate<U, PopulateTypes>(collection, filter, readConfig, findOptions);
253251

254252
return docs[0] || null;
255253
}
256254

257-
return collection.findOne<U>(filter, privateFindOptions);
255+
return collection.findOne<U>(filter, findOptions);
258256
}
259257

260258
// Method overloading for find
@@ -275,19 +273,17 @@ class Service<T extends IDocument> {
275273
readConfig: ReadConfig & { page?: number; perPage?: number } = {},
276274
findOptions: FindOptions = {},
277275
): Promise<FindResult<U & PopulateTypes> | FindResult<U>> {
278-
const { mode = 'private', populate, page, perPage } = readConfig;
276+
const { populate, page, perPage } = readConfig;
279277

280278
const collection = await this.getCollection<U>();
281279
const hasPaging = !!page && !!perPage;
282280

283281
filter = this.handleReadOperations(filter, readConfig);
284282

285-
const privateFindOptions = getPrivateFindOptions({ mode, findOptions, privateFields: this.options.privateFields });
286-
287283
if (!hasPaging) {
288284
const results = populate
289-
? await this.populateAggregate<U, PopulateTypes>(collection, filter, readConfig, privateFindOptions)
290-
: await collection.find<U>(filter, privateFindOptions).toArray();
285+
? await this.populateAggregate<U, PopulateTypes>(collection, filter, readConfig, findOptions)
286+
: await collection.find<U>(filter, findOptions).toArray();
291287

292288
return {
293289
pagesCount: 1,
@@ -296,13 +292,13 @@ class Service<T extends IDocument> {
296292
};
297293
}
298294

299-
privateFindOptions.skip = (page - 1) * perPage;
300-
privateFindOptions.limit = perPage;
295+
findOptions.skip = (page - 1) * perPage;
296+
findOptions.limit = perPage;
301297

302298
const [results, count] = await Promise.all([
303299
populate
304-
? this.populateAggregate<U, PopulateTypes>(collection, filter, readConfig, privateFindOptions)
305-
: collection.find<U>(filter, privateFindOptions).toArray(),
300+
? this.populateAggregate<U, PopulateTypes>(collection, filter, readConfig, findOptions)
301+
: collection.find<U>(filter, findOptions).toArray(),
306302
collection.countDocuments(filter),
307303
]);
308304

@@ -355,7 +351,7 @@ class Service<T extends IDocument> {
355351
createConfig: CreateConfig = {},
356352
insertOneOptions: InsertOneOptions = {},
357353
): Promise<U> => {
358-
const { mode = 'private', publishEvents } = createConfig;
354+
const { publishEvents } = createConfig;
359355
const collection = await this.getCollection<U>();
360356

361357
const validEntity = await this.validateCreateOperation<U>(object, createConfig);
@@ -385,19 +381,15 @@ class Service<T extends IDocument> {
385381
await collection.insertOne(validEntity as OptionalUnlessRequiredId<U>, insertOneOptions);
386382
}
387383

388-
if (mode === 'public') {
389-
return validEntity;
390-
}
391-
392-
return omitPrivateFields<U>(validEntity, this.options.privateFields);
384+
return validEntity;
393385
};
394386

395387
insertMany = async <U extends T = T>(
396388
objects: Partial<U>[],
397389
createConfig: CreateConfig = {},
398390
bulkWriteOptions: BulkWriteOptions = {},
399-
): Promise<Array<U>> => {
400-
const { mode = 'private', publishEvents } = createConfig;
391+
): Promise<U[]> => {
392+
const { publishEvents } = createConfig;
401393

402394
const collection = await this.getCollection<U>();
403395

@@ -430,11 +422,7 @@ class Service<T extends IDocument> {
430422
await collection.insertMany(validEntities as OptionalUnlessRequiredId<U>[], bulkWriteOptions);
431423
}
432424

433-
if (mode === 'public') {
434-
return validEntities;
435-
}
436-
437-
return validEntities.map((doc) => omitPrivateFields<U>(doc, this.options.privateFields));
425+
return validEntities;
438426
};
439427

440428
replaceOne = async (
@@ -474,7 +462,7 @@ class Service<T extends IDocument> {
474462
updateConfig: UpdateConfig = {},
475463
updateOptions: UpdateOptions = {},
476464
): Promise<U | null> {
477-
const { mode = 'private', validateSchema, publishEvents } = updateConfig;
465+
const { validateSchema, publishEvents } = updateConfig;
478466

479467
const collection = await this.getCollection<U>();
480468

@@ -577,11 +565,7 @@ class Service<T extends IDocument> {
577565
);
578566
}
579567

580-
if (mode === 'public') {
581-
return newDoc;
582-
}
583-
584-
return omitPrivateFields<U>(newDoc, this.options.privateFields);
568+
return newDoc;
585569
}
586570

587571
updateMany<U extends T = T>(
@@ -604,7 +588,7 @@ class Service<T extends IDocument> {
604588
updateConfig: UpdateConfig = {},
605589
updateOptions: UpdateOptions = {},
606590
): Promise<U[]> {
607-
const { mode = 'private', validateSchema, publishEvents } = updateConfig;
591+
const { validateSchema, publishEvents } = updateConfig;
608592

609593
const collection = await this.getCollection<U>();
610594

@@ -728,11 +712,7 @@ class Service<T extends IDocument> {
728712
await collection.bulkWrite(bulkWriteQuery, updateOptions);
729713
}
730714

731-
if (mode === 'public') {
732-
return updated.map((u) => u?.doc) as U[];
733-
}
734-
735-
return updated.map((u) => omitPrivateFields<U>(u?.doc as U, this.options.privateFields)) as U[];
715+
return updated.map((u) => u?.doc).filter(Boolean) as U[];
736716
}
737717

738718
deleteOne = async <U extends T = T>(
@@ -1008,6 +988,10 @@ class Service<T extends IDocument> {
1008988
this.collection = null;
1009989
}
1010990
};
991+
992+
getPublic = <U extends T = T>(doc: U | null): Partial<U> | null => {
993+
return omitPrivateFields<U>(doc, this.options.privateFields || []);
994+
};
1011995
}
1012996

1013997
export default Service;

packages/node-mongo/src/tests/service-extending.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class CustomService<T extends IDocument> extends Service<T> {
2828
};
2929
}
3030

31-
function createService<T extends IDocument>(collectionName: string, options: ServiceOptions = {}) {
31+
function createService<T extends IDocument>(collectionName: string, options: ServiceOptions<T> = {}) {
3232
return new CustomService<T>(collectionName, database, options);
3333
}
3434

0 commit comments

Comments
 (0)