{"version":3,"file":"common.cjs","sources":["../../src/interfaces/common.ts"],"sourcesContent":["/*\n * Common types and interfaces\n */\n\nimport type { Types } from 'mongoose';\n\nimport { isValidObjectId } from '../utils/objectid-utils';\n\ntype ObjectId = Types.ObjectId;\n\n// Foreign key field\nexport type Ref<T> = string | ObjectId | (T & { _id: string | ObjectId });\n\nexport type Nullable<T> = T | null | undefined;\n\nexport const isRef = <T>(obj: unknown): obj is Ref<T> => {\n  return (\n    obj != null &&\n    ((typeof obj === 'string' && isValidObjectId(obj)) ||\n      (typeof obj === 'object' &&\n        '_bsontype' in obj &&\n        obj._bsontype === 'ObjectID') ||\n      (typeof obj === 'object' && '_id' in obj))\n  );\n};\n\nexport const isPopulated = <T>(\n  ref: Ref<T>,\n): ref is T & { _id: string | ObjectId } => {\n  return (\n    ref != null &&\n    typeof ref !== 'string' &&\n    !('_bsontype' in ref && ref._bsontype === 'ObjectID')\n  );\n};\n\nexport const getIdForRef = <T>(ref: Ref<T>): string | ObjectId => {\n  return isPopulated(ref) ? ref._id : ref;\n};\n\nexport const getIdStringForRef = <T>(ref: Ref<T>): string => {\n  return getIdForRef(ref).toString();\n};\n"],"names":["isRef","obj","isValidObjectId","isPopulated","ref","getIdForRef","getIdStringForRef"],"mappings":"+HAeaA,EAAYC,GAErBA,GAAO,OACL,OAAOA,GAAQ,UAAYC,kBAAgBD,CAAG,GAC7C,OAAOA,GAAQ,UACd,cAAeA,GACfA,EAAI,YAAc,YACnB,OAAOA,GAAQ,UAAY,QAASA,GAI9BE,EACXC,GAGEA,GAAO,MACP,OAAOA,GAAQ,UACf,EAAE,cAAeA,GAAOA,EAAI,YAAc,YAIjCC,EAAkBD,GACtBD,EAAYC,CAAG,EAAIA,EAAI,IAAMA,EAGzBE,EAAwBF,GAC5BC,EAAYD,CAAG,EAAE,SAAS"}