|
|
@@ -1,11 +1,20 @@
|
|
|
-import { Types as MongooseTypes } from 'mongoose';
|
|
|
+// !!! Do NOT import 'mongoose' to reduce bundle size !!!
|
|
|
+import ObjectId from 'bson-objectid';
|
|
|
import superjson from 'superjson';
|
|
|
|
|
|
export const registerTransformerForObjectId = (): void => {
|
|
|
- superjson.registerCustom<MongooseTypes.ObjectId|string, string>(
|
|
|
+ superjson.registerCustom<ObjectId|string, string>(
|
|
|
{
|
|
|
- isApplicable: (v): v is MongooseTypes.ObjectId => v instanceof MongooseTypes.ObjectId,
|
|
|
- serialize: v => (v instanceof MongooseTypes.ObjectId ? v.toHexString() : v),
|
|
|
+ isApplicable: (v): v is ObjectId => {
|
|
|
+ if (typeof v === 'string') {
|
|
|
+ return ObjectId.isValid(v);
|
|
|
+ }
|
|
|
+ if (typeof v.toHexString === 'function') {
|
|
|
+ return ObjectId.isValid(v.toHexString());
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ },
|
|
|
+ serialize: v => (typeof v === 'string' ? v : v.toHexString()),
|
|
|
deserialize: v => v,
|
|
|
},
|
|
|
'ObjectidTransformer',
|