Skip to content
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
52 changes: 29 additions & 23 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.18.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);
});
});
Loading
Loading