diff --git a/packages/datasource-mongo/src/introspection/structure.ts b/packages/datasource-mongo/src/introspection/structure.ts index 2bd20b550e..da28ea69f9 100644 --- a/packages/datasource-mongo/src/introspection/structure.ts +++ b/packages/datasource-mongo/src/introspection/structure.ts @@ -11,9 +11,14 @@ export default class Structure { }: { collectionSampleSize: number; referenceSampleSize: number }, ): Promise { 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 diff --git a/packages/datasource-mongo/test/introspection/structure.unit.test.ts b/packages/datasource-mongo/test/introspection/structure.unit.test.ts index 52eeb80e0c..93900e16d0 100644 --- a/packages/datasource-mongo/test/introspection/structure.unit.test.ts +++ b/packages/datasource-mongo/test/introspection/structure.unit.test.ts @@ -16,6 +16,7 @@ describe('Introspection > Structure', () => { function setupConnectionMock( collectionDefinitions: Array<{ collectionName: string; + namespace?: string; records: Array>; }>, ) { @@ -24,7 +25,7 @@ describe('Introspection > Structure', () => { }> = []; const mongoRecords: Array[]> = []; - const collections = collectionDefinitions.map(({ collectionName, records }) => { + const collections = collectionDefinitions.map(({ collectionName, namespace, records }) => { const collectionMongoRecords = records; const query = { limit: jest.fn().mockReturnValue(asyncIterate(collectionMongoRecords)), @@ -36,6 +37,7 @@ describe('Introspection > Structure', () => { return { collectionName, + namespace, find, }; }); @@ -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([ {