diff --git a/package.json b/package.json index 47e3fdc..e76723d 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,7 @@ ".esm-wrapper.mjs" ], "scripts": { - "test": "nyc mocha --timeout 5000 --colors -r ts-node/register test/**/*.ts src/**/*.test.ts", + "test": "nyc mocha --timeout 5000 --colors -r ts-node/register test/*.ts test/**/*.ts src/**/*.test.ts", "test-example-parse-from-file": "ts-node examples/parse-from-file.ts", "test-example-parse-schema": "ts-node examples/parse-schema.ts", "test-time": "ts-node ./test/time-testing.ts", diff --git a/src/schema-analyzer.ts b/src/schema-analyzer.ts index 3fe9c9b..50e5370 100644 --- a/src/schema-analyzer.ts +++ b/src/schema-analyzer.ts @@ -24,6 +24,7 @@ type TypeCastMap = { Binary: Binary; Boolean: boolean; Code: Code; + CodeWScope: Code; Date: Date; Decimal128: Decimal128; Double: Double; @@ -215,6 +216,9 @@ function getBSONType(value: any): SchemaBSONType { // In the resulting schema we rename `Object` to `Document`. return 'Document'; } + if (bsonType === 'Code' && (value as Code).scope) { + return 'CodeWScope'; + } return bsonType; } diff --git a/src/schema-convertors/internalToMongoDB.test.ts b/src/schema-convertors/internalToMongoDB.test.ts index 42813bc..7c2b3d6 100644 --- a/src/schema-convertors/internalToMongoDB.test.ts +++ b/src/schema-convertors/internalToMongoDB.test.ts @@ -573,12 +573,12 @@ describe('internalSchemaToMongoDB', async function() { 'javascriptWithScope' ], count: 1, - type: 'Code', + type: 'CodeWScope', probability: 0.8, hasDuplicates: false, types: [ { - name: 'Code', + name: 'CodeWScope', path: [ 'javascriptWithScope' ], @@ -595,7 +595,7 @@ describe('internalSchemaToMongoDB', async function() { } } ], - bsonType: 'Code' + bsonType: 'CodeWScope' } ] }, @@ -964,7 +964,7 @@ describe('internalSchemaToMongoDB', async function() { bsonType: 'javascript' }, javascriptWithScope: { - bsonType: 'javascript' + bsonType: 'javascriptWithScope' }, long: { bsonType: 'long' diff --git a/src/schema-convertors/internalToMongoDB.ts b/src/schema-convertors/internalToMongoDB.ts index 5d6875b..f40586a 100644 --- a/src/schema-convertors/internalToMongoDB.ts +++ b/src/schema-convertors/internalToMongoDB.ts @@ -26,6 +26,7 @@ export const InternalTypeToBsonTypeMap: Record< BSONSymbol: 'symbol', Symbol: 'symbol', Code: 'javascript', + CodeWScope: 'javascriptWithScope', Int32: 'int', Timestamp: 'timestamp', Long: 'long', diff --git a/src/schema-convertors/internalToStandard.test.ts b/src/schema-convertors/internalToStandard.test.ts index ba00bb1..511eca2 100644 --- a/src/schema-convertors/internalToStandard.test.ts +++ b/src/schema-convertors/internalToStandard.test.ts @@ -576,12 +576,12 @@ describe('internalSchemaToStandard', async function() { 'javascriptWithScope' ], count: 1, - type: 'Code', + type: 'CodeWScope', probability: 0.8, hasDuplicates: false, types: [ { - name: 'Code', + name: 'CodeWScope', path: [ 'javascriptWithScope' ], @@ -598,7 +598,7 @@ describe('internalSchemaToStandard', async function() { } } ], - bsonType: 'Code' + bsonType: 'CodeWScope' } ] }, @@ -970,7 +970,7 @@ describe('internalSchemaToStandard', async function() { $ref: '#/$defs/Code' }, javascriptWithScope: { - $ref: '#/$defs/Code' + $ref: '#/$defs/CodeWScope' }, long: { type: 'integer' diff --git a/src/schema-convertors/internalToStandard.ts b/src/schema-convertors/internalToStandard.ts index 5b5e4ab..fa298d8 100644 --- a/src/schema-convertors/internalToStandard.ts +++ b/src/schema-convertors/internalToStandard.ts @@ -25,7 +25,7 @@ export const InternalTypeToStandardTypeMap: Record< DBPointer: { $ref: '#/$defs/DBPointer' }, BSONSymbol: { $ref: '#/$defs/BSONSymbol' }, Code: { $ref: '#/$defs/Code' }, - CodeWScope: { $ref: '#/$defs/Code' }, + CodeWScope: { $ref: '#/$defs/CodeWScope' }, Int32: { type: 'integer' }, Timestamp: { $ref: '#/$defs/Timestamp' }, Long: { type: 'integer' }, diff --git a/test/all-bson-types.test.ts b/test/all-bson-types.test.ts index 8faec27..2b9c96c 100644 --- a/test/all-bson-types.test.ts +++ b/test/all-bson-types.test.ts @@ -18,6 +18,7 @@ describe('using a document with all bson types', function() { 'Binary', 'Boolean', 'Code', + 'CodeWScope', 'Date', 'Decimal128', 'Double', diff --git a/test/integration/generateAndValidate.ts b/test/integration/generateAndValidate.test.ts similarity index 100% rename from test/integration/generateAndValidate.ts rename to test/integration/generateAndValidate.test.ts diff --git a/test/simplified-schema.test.ts b/test/simplified-schema.test.ts index 28af5e6..d90c271 100644 --- a/test/simplified-schema.test.ts +++ b/test/simplified-schema.test.ts @@ -99,7 +99,7 @@ const expected = { }, javascriptWithScope: { types: [{ - bsonType: 'Code' + bsonType: 'CodeWScope' }] }, int: {