Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(mongoose): allow to enable debug mode #1251

Merged
merged 6 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});
});
});
Loading