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
33 changes: 25 additions & 8 deletions packages/mongodb-schema/src/schema-analyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,15 +225,32 @@ const defaultSchemaParseOptions: AllSchemaParseOptions = {
storedValuesLengthLimit: 10000,
};

const BSON_VERSION_SYMBOL = Symbol.for('@@mdb.bson.version');

function getBSONTypeTag(value: unknown): SchemaBSONType | undefined {
if (typeof value !== 'object' || value === null) {
return undefined;
}
const tag = (value as { _bsontype?: unknown })._bsontype;
if (typeof tag !== 'string') {
return undefined;
}
const isBSONValue =
BSON_VERSION_SYMBOL in value ||
!Object.getOwnPropertyDescriptor(value, '_bsontype')?.enumerable;
return isBSONValue ? (tag as SchemaBSONType) : undefined;
}

/**
* Extracts a Javascript string from a BSON type to compute unique values.
*/
function extractStringValueFromBSON(value: any): string {
if (value?._bsontype) {
if (['Decimal128', 'Long'].includes(value._bsontype)) {
const tag = getBSONTypeTag(value);
if (tag) {
if (['Decimal128', 'Long'].includes(tag)) {
return value.toString();
}
if (['Double', 'Int32'].includes(value._bsontype)) {
if (['Double', 'Int32'].includes(tag)) {
return String(value.value);
}
}
Expand Down Expand Up @@ -271,18 +288,18 @@ function fieldComparator(
* Returns the type of value as a string. BSON type aware. Replaces `Object`
* with `Document` to avoid naming conflicts with javascript Objects.
*/
function getBSONType(value: any): SchemaBSONType {
const bsonType = value?._bsontype
? value._bsontype
: Object.prototype.toString.call(value).replace(/^\[object (\w+)\]$/, '$1');
function getBSONType(value: unknown): SchemaBSONType {
const bsonType =
getBSONTypeTag(value) ??
Object.prototype.toString.call(value).replace(/^\[object (\w+)\]$/, '$1');
if (bsonType === 'Object') {
// In the resulting schema we rename `Object` to `Document`.
return 'Document';
}
if (bsonType === 'Code' && (value as Code).scope) {
return 'CodeWScope';
}
return bsonType;
return bsonType as SchemaBSONType;
}

function isNullType(type: SchemaAnalysisType): type is SchemaAnalysisNullType {
Expand Down
51 changes: 51 additions & 0 deletions packages/mongodb-schema/test/schema-analyzer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import assert from 'assert';
import BSON from 'bson';

import getSchema from '../src';

describe('schema analyzer', function () {
describe('BSON type detection', function () {
it('treats a plain object with _bsontype as a regular document', async function () {
const schema = await getSchema([
{ _id: 1, a: { _bsontype: '<b>x</b>' } },
{ _id: 2, a: { _bsontype: 'String' } },
{ _id: 3, a: { _bsontype: 'Binary', other: 1 } },
]);
const a = schema.fields.find((f) => f.name === 'a');
assert.deepStrictEqual(a?.type, 'Document');
});

it('still detects real BSON values', async function () {
const schema = await getSchema([
{ _id: new BSON.ObjectId(), n: BSON.Long.fromNumber(1) },
]);
assert.deepStrictEqual(
schema.fields.map((f) => f.type),
['ObjectId', 'Long'],
);
});

it('detects BSON values that have no bson version symbol', async function () {
const legacyLong = Object.defineProperty(
{ low: 1, high: 0 },
'_bsontype',
{
value: 'Long',
},
);
const schema = await getSchema([{ n: legacyLong }]);
assert.deepStrictEqual(schema.fields[0].type, 'Long');
});

it('detects BSON values by the bson version symbol', async function () {
const value = {
_bsontype: 'Long',
low: 1,
high: 0,
[Symbol.for('@@mdb.bson.version')]: 7,
};
const schema = await getSchema([{ n: value }]);
assert.deepStrictEqual(schema.fields[0].type, 'Long');
});
});
});
Loading