mongodb-schema
Version:
Infer the probabilistic schema for a MongoDB collection.
77 lines • 2.62 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
function widthRecursive(schema) {
var _a;
let width = 0;
if (!schema) {
return width;
}
if (schema.fields === undefined) {
return width;
}
width += schema.fields.length;
width += schema.fields.map((field) => {
const doc = field.types.find(v => v.name === 'Document');
return widthRecursive(doc);
}).reduce((p, c) => p + c || 0, 0);
width += (_a = schema.fields.map((field) => {
const arr = field.types.find((v) => v.name === 'Array');
if (arr) {
const doc = arr.types.find(v => v.name === 'Document');
return widthRecursive(doc);
}
})
.reduce((p, c) => ((p !== null && p !== void 0 ? p : 0) + (c !== null && c !== void 0 ? c : 0)) || 0, 0)) !== null && _a !== void 0 ? _a : 0;
return width;
}
function depthRecursive(schema) {
if (!schema) {
return 0;
}
let maxChildDepth = 0;
if (schema.fields !== undefined && schema.fields.length > 0) {
maxChildDepth = 1 + Math.max(Math.max(...schema.fields.map(field => {
const doc = field.types.find(v => v.name === 'Document');
return depthRecursive(doc);
})), Math.max(...schema.fields.map(field => {
const arr = field.types.find(v => v.name === 'Array');
if (arr) {
const doc = arr.types.find(v => v.name === 'Document');
return depthRecursive(doc);
}
return 0;
})));
}
return maxChildDepth;
}
function branchingFactors(schema) {
const branchArray = [];
let res;
if (!schema) {
return branchArray;
}
if (schema.fields !== undefined && schema.fields.length > 0) {
branchArray.push(schema.fields.length);
res = schema.fields.map(function (field) {
const doc = field.types.find(v => v.name === 'Document');
return branchingFactors(doc);
});
branchArray.push(...res.flat(Infinity));
res = schema.fields.map(function (field) {
const arr = field.types.find(v => v.name === 'Array');
if (arr) {
const doc = arr.types.find(v => v.name === 'Document');
return branchingFactors(doc);
}
return [];
});
branchArray.push(...res.flat(Infinity));
}
return branchArray.sort().reverse();
}
exports.default = {
width: widthRecursive,
depth: depthRecursive,
branch: branchingFactors
};
//# sourceMappingURL=stats.js.map
;