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

fix: do not introspect mongodb reserved system collections #1110

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
11 changes: 8 additions & 3 deletions packages/datasource-mongo/src/introspection/structure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@ export default class Structure {
}: { collectionSampleSize: number; referenceSampleSize: number },
): Promise<ModelStudy[]> {
const collections = await connection.collections();
const promises = collections.map(collection =>
this.analyzeCollection(collection, collectionSampleSize, referenceSampleSize),
);

// https://www.mongodb.com/docs/manual/reference/system-collections/
// collection within admin.system are reserved for mongodb
const promises = collections
.filter(collection => !collection.namespace?.startsWith('admin.system.'))
.map(collection =>
this.analyzeCollection(collection, collectionSampleSize, referenceSampleSize),
);
const structure = await Promise.all(promises);

return structure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ describe('Introspection > Structure', () => {
function setupConnectionMock(
collectionDefinitions: Array<{
collectionName: string;
namespace?: string;
records: Array<Record<string, unknown>>;
}>,
) {
Expand All @@ -24,7 +25,7 @@ describe('Introspection > Structure', () => {
}> = [];
const mongoRecords: Array<Record<string, unknown>[]> = [];

const collections = collectionDefinitions.map(({ collectionName, records }) => {
const collections = collectionDefinitions.map(({ collectionName, namespace, records }) => {
const collectionMongoRecords = records;
const query = {
limit: jest.fn().mockReturnValue(asyncIterate(collectionMongoRecords)),
Expand All @@ -36,6 +37,7 @@ describe('Introspection > Structure', () => {

return {
collectionName,
namespace,
find,
};
});
Expand Down Expand Up @@ -64,6 +66,28 @@ describe('Introspection > Structure', () => {
expect(structure).toEqual([]);
});

it('should not return system collections', async () => {
const { connection } = setupConnectionMock([
{
collectionName: 'users',
namespace: 'admin.system.users',
records: [{ name: 'nicolas' }],
},
{
collectionName: 'books',
namespace: 'admin.documents',
records: [{ name: 'the lord of the rings' }],
},
]);

const structure = await Structure.introspect(connection as unknown as MongoDb, {
collectionSampleSize: 1,
referenceSampleSize: 1,
});

expect(structure).toEqual([expect.objectContaining({ name: 'books' })]);
});

it('should return collections sorted by name', async () => {
const { connection } = setupConnectionMock([
{
Expand Down
Loading