Skip to content

Commit

Permalink
feat(mongoose): allow to enable debug mode (#1251)
Browse files Browse the repository at this point in the history
Co-authored-by: Dogan AY <[email protected]>
Co-authored-by: steveb <[email protected]>
  • Loading branch information
3 people authored Jan 29, 2025
1 parent e2ffe2d commit db36f93
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
6 changes: 6 additions & 0 deletions packages/datasource-mongoose/src/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ export default class MongooseDatasource extends BaseDataSource<MongooseCollectio
);
}

if (options?.debug) {
connection.set('debug', (collectionName: string, method: string, args: unknown[]) => {
logger('Debug', `${collectionName}.${method}: ${JSON.stringify(args)}`);
});
}

// Create collections (with only many to one relations).
for (const model of Object.values(connection.models)) {
const schema = MongooseSchema.fromModel(model);
Expand Down
10 changes: 8 additions & 2 deletions packages/datasource-mongoose/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,17 @@ export type LegacyFlattenOptions = {
asModels?: { [modelName: string]: string[] };
};

export type MongooseOptions =
export type DebugModeOptions = {
debug?: boolean;
};

export type MongooseOptions = (
| ManualFlattenOptions
| AutoFlattenOptions
| NoFlattenOptions
| LegacyFlattenOptions;
| LegacyFlattenOptions
) &
DebugModeOptions;

export type Stack = {
prefix: string | null;
Expand Down
25 changes: 25 additions & 0 deletions packages/datasource-mongoose/test/datasource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,31 @@ describe('MongooseDatasource', () => {
spy.mockRestore();
});

describe('with debug option', () => {
it('should enable debug on mongoose', () => {
mongoose.model('books', new Schema({}));
const datasource = new MongooseDatasource(mongoose.connection, { debug: true });

expect(datasource.collections).toMatchObject([{ name: 'books' }]);
expect(mongoose.connection.get('debug')).toBeTruthy();

mongoose.connection.set('debug', false);
mongoose.deleteModel('books');
});
});

describe('without debug option', () => {
it('should not enable debug on mongoose', () => {
mongoose.model('books', new Schema({}));
const datasource = new MongooseDatasource(mongoose.connection, {});

expect(datasource.collections).toMatchObject([{ name: 'books' }]);
expect(mongoose.connection.get('debug')).toBeFalsy();

mongoose.deleteModel('books');
});
});

describe('with simple schemas', () => {
it('should give one collection by default', () => {
mongoose.model('books', new Schema({}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -566,4 +566,38 @@ describe('MongooseCollection', () => {
});
});
});

describe('with debug option', () => {
it('should log', async () => {
connection = await setupReview('collection_review_list');
const logger = jest.fn();
const dataSource = new MongooseDatasource(
connection,
{ debug: true, flattenMode: 'auto' },
logger,
);
const review = dataSource.getCollection('review');

await review.list(factories.caller.build(), factories.filter.build(), new Projection('id'));

expect(logger).toHaveBeenCalledTimes(1);
expect(logger).toHaveBeenCalledWith(
'Debug',
'reviews.aggregate: [{"$addFields":{"nestedField@@@other":"$nestedField.other"}},{"$project":{"nestedField.other":0}},{"$project":{"_id":false,"FOREST_RECORD_DOES_NOT_EXIST":true,"id":true}}]',
);
});
});

describe('without debug option', () => {
it('should not log', async () => {
connection = await setupReview('collection_review_list');
const logger = jest.fn();
const dataSource = new MongooseDatasource(connection, { flattenMode: 'auto' }, logger);
const review = dataSource.getCollection('review');

await review.list(factories.caller.build(), factories.filter.build(), new Projection('id'));

expect(logger).not.toHaveBeenCalled();
});
});
});

0 comments on commit db36f93

Please sign in to comment.