diff --git a/packages/bson/src/bson-serializer.ts b/packages/bson/src/bson-serializer.ts index b57fa463f..158c18c8e 100644 --- a/packages/bson/src/bson-serializer.ts +++ b/packages/bson/src/bson-serializer.ts @@ -36,6 +36,7 @@ import { isUUIDType, JitStack, memberNameToString, + MongoId, mongoIdAnnotation, NamingStrategy, ReceiveType, @@ -57,10 +58,12 @@ import { TypeIndexSignature, TypeLiteral, TypeObjectLiteral, + typeOf, typeSettings, TypeTuple, UnpopulatedCheck, unpopulatedSymbol, + UUID, uuidAnnotation, } from '@deepkit/type'; import { @@ -90,7 +93,14 @@ import { } from './bson-deserializer-templates.js'; import { seekElementSize } from './continuation.js'; import { BSONError } from './model.js'; -import { BSON_BINARY_SUBTYPE_DEFAULT, BSON_BINARY_SUBTYPE_UUID, BSONType, digitByteSize, isSerializable, TWO_PWR_32_DBL_N } from './utils.js'; +import { + BSON_BINARY_SUBTYPE_DEFAULT, + BSON_BINARY_SUBTYPE_UUID, + BSONType, + digitByteSize, + isSerializable, + TWO_PWR_32_DBL_N, +} from './utils.js'; export function createBuffer(size: number): Uint8Array { return 'undefined' !== typeof Buffer && 'function' === typeof Buffer.allocUnsafe ? Buffer.allocUnsafe(size) : new Uint8Array(size); @@ -244,6 +254,18 @@ export function wrapValue(value: any, type?: ReceiveType) { return new ValueWithBSONSerializer(value, resolveReceiveType(type)); } +const mongoIdType = typeOf(); + +export function wrapObjectId(value: string) { + return new ValueWithBSONSerializer(value, mongoIdType); +} + +const uuidIdType = typeOf(); + +export function wrapUUID(value: string) { + return new ValueWithBSONSerializer(value, uuidIdType); +} + export class Writer { public dataView: DataView; public typeOffset: number = 0; diff --git a/packages/bson/tests/bson-serialize.spec.ts b/packages/bson/tests/bson-serialize.spec.ts index cff579bb9..fc67a6304 100644 --- a/packages/bson/tests/bson-serialize.spec.ts +++ b/packages/bson/tests/bson-serialize.spec.ts @@ -1,26 +1,13 @@ import { expect, test } from '@jest/globals'; -import { getBSONSerializer, getBSONSizer, getValueSize, hexToByte, serializeBSONWithoutOptimiser, uuidStringToByte, wrapValue } from '../src/bson-serializer.js'; -import { - BinaryBigInt, - createReference, - Excluded, - hasCircularReference, - MongoId, - nodeBufferToArrayBuffer, - PrimaryKey, - Reference, - SignedBinaryBigInt, - typeOf, - uuid, - UUID, -} from '@deepkit/type'; +import { getBSONSerializer, getBSONSizer, getValueSize, hexToByte, serializeBSONWithoutOptimiser, uuidStringToByte, wrapObjectId, wrapUUID, wrapValue } from '../src/bson-serializer.js'; +import { BinaryBigInt, createReference, Excluded, hasCircularReference, MongoId, nodeBufferToArrayBuffer, PrimaryKey, Reference, SignedBinaryBigInt, typeOf, uuid, UUID } from '@deepkit/type'; import bson from 'bson'; import { randomBytes } from 'crypto'; import { BSON_BINARY_SUBTYPE_DEFAULT, BSONType } from '../src/utils.js'; import { deserializeBSONWithoutOptimiser } from '../src/bson-parser.js'; import { deserializeBSON, getBSONDeserializer } from '../src/bson-deserializer.js'; -const { Binary, calculateObjectSize, deserialize, Long, ObjectId: OfficialObjectId, serialize } = bson; +const { Binary, calculateObjectSize, deserialize, Long, ObjectId: OfficialObjectId, UUID: OfficialUUID, serialize } = bson; test('hexToByte', () => { expect(hexToByte('00')).toBe(0); @@ -1430,9 +1417,33 @@ test('undefined for required object', () => { }); test('wrapValue', () => { - const objectId = wrapValue('507f191e810c19729de860ea'); - const bson = serializeBSONWithoutOptimiser({v: objectId}); - const back = deserialize(bson); - expect(back.v).toBeInstanceOf(OfficialObjectId); - expect(back.v.toHexString()).toBe('507f191e810c19729de860ea'); + { + const objectId = wrapValue('507f191e810c19729de860ea'); + const bson = serializeBSONWithoutOptimiser({ v: objectId }); + const back = deserialize(bson); + expect(back.v).toBeInstanceOf(OfficialObjectId); + expect(back.v.toHexString()).toBe('507f191e810c19729de860ea'); + } + { + const objectId = wrapValue('507f191e810c19729de860ea'); + const serialize = getBSONSerializer<{v: any }>(); + const bson = serialize({ v: objectId }); + const back = deserialize(bson); + expect(back.v).toBeInstanceOf(OfficialObjectId); + expect(back.v.toHexString()).toBe('507f191e810c19729de860ea'); + } + { + const objectId = wrapObjectId('507f191e810c19729de860ea'); + const bson = serializeBSONWithoutOptimiser({ v: objectId }); + const back = deserialize(bson); + expect(back.v).toBeInstanceOf(OfficialObjectId); + expect(back.v.toHexString()).toBe('507f191e810c19729de860ea'); + } + { + const uuid1 = wrapUUID(uuid()); + const bson = serializeBSONWithoutOptimiser({ v: uuid1 }); + const back = deserialize(bson); + expect(back.v).toBeInstanceOf(OfficialUUID); + expect(back.v.toHexString()).toBe(uuid1.value); + } });