MCPcopy Create free account
hub / github.com/Automattic/mongoose / autoInferredWithCustomVersionKey

Function autoInferredWithCustomVersionKey

test/types/document.test.ts:710–745  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

708 }
709
710 function autoInferredWithCustomVersionKey() {
711 interface RawDocType {
712 _id: Types.ObjectId;
713 testProperty?: number | null;
714 }
715
716 const ASchema = new Schema({
717 testProperty: Number
718 }, { versionKey: 'taco' });
719
720 const AModel = model('YourModel', ASchema);
721
722 type TSchemaOptions = ResolveSchemaOptions<ObtainSchemaGeneric<typeof ASchema, 'TSchemaOptions'>>;
723
724 const a = new AModel({ testProperty: 8 });
725 const toObjectFlattened: Omit<RawDocType, '_id'> & { _id: string } = a.toObject({ flattenObjectIds: true });
726 const toObjectWithVirtuals: Omit<RawDocType, '_id'> & { _id: string } = a.toObject({ virtuals: true, flattenObjectIds: true });
727 const toObjectWithoutVirtuals: Omit<RawDocType, '_id'> & { _id: string } = a.toObject({ virtuals: false, flattenObjectIds: true });
728 const toJSONFlattened: Omit<RawDocType, '_id'> & { _id: string } = a.toJSON({ flattenObjectIds: true });
729 const toJSONWithVirtuals: Omit<RawDocType, '_id'> & { _id: string } = a.toJSON({ virtuals: true, flattenObjectIds: true });
730 const toJSONWithoutVirtuals: Omit<RawDocType, '_id'> & { _id: string } = a.toJSON({ virtuals: false, flattenObjectIds: true });
731
732 // When passing options, custom version key should still be present
733 expect(a.toObject({ flattenObjectIds: true }).taco).type.toBe<number>();
734 expect(a.toJSON({ flattenObjectIds: true }).taco).type.toBe<number>();
735
736 const objWithoutVersionKey = a.toObject({ versionKey: false });
737 const jsonWithoutVersionKey = a.toJSON({ versionKey: false });
738 expect(objWithoutVersionKey).type.not.toHaveProperty('__v');
739 expect(jsonWithoutVersionKey).type.not.toHaveProperty('__v');
740
741 const objWithVersionKey = a.toObject();
742 const jsonWithVersionKey = a.toJSON();
743 expect(objWithVersionKey.taco).type.toBe<number>();
744 expect(jsonWithVersionKey.taco).type.toBe<number>();
745 }
746}
747
748async function gh15594() {

Callers

nothing calls this directly

Calls 3

modelFunction · 0.85
toObjectMethod · 0.80
toJSONMethod · 0.45

Tested by

no test coverage detected