Skip to content

feat(compass-collection): (re-merge) Schema Analysis Redux Integration for Collection Plugin CLOUDP-333846 #7203

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

Merged
merged 8 commits into from
Aug 15, 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 package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion packages/compass-collection/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"reformat": "npm run eslint . -- --fix && npm run prettier -- --write ."
},
"dependencies": {
"@mongodb-js/compass-app-registry": "^9.4.18",
"@mongodb-js/compass-app-stores": "^7.55.0",
"@mongodb-js/compass-components": "^1.47.0",
"@mongodb-js/compass-connections": "^1.69.0",
Expand All @@ -57,9 +58,11 @@
"@mongodb-js/connection-info": "^0.17.0",
"@mongodb-js/mongodb-constants": "^0.12.2",
"compass-preferences-model": "^2.49.0",
"@mongodb-js/compass-app-registry": "^9.4.18",
"hadron-document": "^8.9.4",
"mongodb": "^6.17.0",
"mongodb-collection-model": "^5.31.0",
"mongodb-ns": "^2.4.2",
"mongodb-schema": "^12.6.2",
"react": "^17.0.2",
"react-redux": "^8.1.3",
"redux": "^4.2.1",
Expand Down
165 changes: 165 additions & 0 deletions packages/compass-collection/src/calculate-schema-depth.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
import { expect } from 'chai';
import { calculateSchemaDepth } from './calculate-schema-depth';
import type {
Schema,
SchemaField,
DocumentSchemaType,
ArraySchemaType,
} from 'mongodb-schema';

describe('calculateSchemaDepth', function () {
it('returns 1 for flat schema', async function () {
const schema: Schema = {
fields: [
{ name: 'a', types: [{ bsonType: 'String' }] } as SchemaField,
{ name: 'b', types: [{ bsonType: 'Number' }] } as SchemaField,
],
count: 2,
};
const depth = await calculateSchemaDepth(schema);
expect(depth).to.equal(1);
});

it('returns correct depth for nested document', async function () {
const schema: Schema = {
fields: [
{
name: 'a',
types: [
{
bsonType: 'Document',
fields: [
{
name: 'b',
types: [
{
bsonType: 'Document',
fields: [
{
name: 'c',
types: [{ bsonType: 'String' }],
} as SchemaField,
],
} as DocumentSchemaType,
],
} as SchemaField,
],
} as DocumentSchemaType,
],
} as SchemaField,
],
count: 1,
};
const depth = await calculateSchemaDepth(schema);
expect(depth).to.equal(3);
});

it('returns correct depth for nested arrays', async function () {
const schema: Schema = {
fields: [
{
name: 'arr',
types: [
{
bsonType: 'Array',
types: [
{
bsonType: 'Array',
types: [
{
bsonType: 'Document',
fields: [
{
name: 'x',
types: [{ bsonType: 'String' }],
} as SchemaField,
],
} as DocumentSchemaType,
],
} as ArraySchemaType,
],
} as ArraySchemaType,
],
} as SchemaField,
],
count: 1,
};
const depth = await calculateSchemaDepth(schema);
expect(depth).to.equal(4);
});

it('returns 0 for empty schema', async function () {
const schema: Schema = { fields: [], count: 0 };
const depth = await calculateSchemaDepth(schema);
expect(depth).to.equal(0);
});

it('handles mixed types at root', async function () {
const schema: Schema = {
fields: [
{
name: 'a',
types: [
{ bsonType: 'String' },
{
bsonType: 'Document',
fields: [
{
name: 'b',
types: [{ bsonType: 'Number' }],
} as SchemaField,
],
} as DocumentSchemaType,
],
} as SchemaField,
],
count: 1,
};
const depth = await calculateSchemaDepth(schema);
expect(depth).to.equal(2);
});

it('handles deeply nested mixed arrays and documents', async function () {
const schema: Schema = {
fields: [
{
name: 'root',
types: [
{
bsonType: 'Array',
types: [
{
bsonType: 'Document',
fields: [
{
name: 'nestedArr',
types: [
{
bsonType: 'Array',
types: [
{
bsonType: 'Document',
fields: [
{
name: 'leaf',
types: [{ bsonType: 'String' }],
} as SchemaField,
],
} as DocumentSchemaType,
],
} as ArraySchemaType,
],
} as SchemaField,
],
} as DocumentSchemaType,
],
} as ArraySchemaType,
],
} as SchemaField,
],
count: 1,
};
const depth = await calculateSchemaDepth(schema);
expect(depth).to.equal(5);
});
});
57 changes: 57 additions & 0 deletions packages/compass-collection/src/calculate-schema-depth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import type {
ArraySchemaType,
DocumentSchemaType,
Schema,
SchemaField,
SchemaType,
} from 'mongodb-schema';

// Every 1000 iterations, unblock the thread.
const UNBLOCK_INTERVAL_COUNT = 1000;
const unblockThread = async () =>
new Promise<void>((resolve) => setTimeout(resolve));

export async function calculateSchemaDepth(schema: Schema): Promise<number> {
Copy link
Preview

Copilot AI Aug 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The magic number 1000 should be documented or made configurable. Consider adding a comment explaining why this specific interval was chosen for thread unblocking.

Suggested change
export async function calculateSchemaDepth(schema: Schema): Promise<number> {
/**
* Number of iterations after which to unblock the thread.
* The value 1000 was chosen as a balance between responsiveness and performance.
* Unblocking too frequently can slow down processing, while unblocking too rarely
* can make the UI unresponsive. Adjust as needed for your environment.
*/
const DEFAULT_UNBLOCK_INTERVAL_COUNT = 1000;
const unblockThread = async () =>
new Promise<void>((resolve) => setTimeout(resolve));
export async function calculateSchemaDepth(
schema: Schema,
unblockIntervalCount: number = DEFAULT_UNBLOCK_INTERVAL_COUNT
): Promise<number> {

Copilot uses AI. Check for mistakes.

let unblockThreadCounter = 0;
let deepestPath = 0;

async function traverseSchemaTree(
fieldsOrTypes: SchemaField[] | SchemaType[],
depth: number
): Promise<void> {
unblockThreadCounter++;
if (unblockThreadCounter === UNBLOCK_INTERVAL_COUNT) {
unblockThreadCounter = 0;
await unblockThread();
}

if (!fieldsOrTypes || fieldsOrTypes.length === 0) {
return;
}

deepestPath = Math.max(depth, deepestPath);

for (const fieldOrType of fieldsOrTypes) {
if ((fieldOrType as DocumentSchemaType).bsonType === 'Document') {
await traverseSchemaTree(
(fieldOrType as DocumentSchemaType).fields,
depth + 1 // Increment by one when we go a level deeper.
);
} else if (
(fieldOrType as ArraySchemaType).bsonType === 'Array' ||
(fieldOrType as SchemaField).types
) {
const increment =
(fieldOrType as ArraySchemaType).bsonType === 'Array' ? 1 : 0;
await traverseSchemaTree(
(fieldOrType as ArraySchemaType | SchemaField).types,
depth + increment // Increment by one when we go a level deeper.
);
}
}
}

await traverseSchemaTree(schema.fields, 1);

return deepestPath;
}
Loading
Loading