Kaynağa Gözat

support ObjectId

Yuki Takei 1 yıl önce
ebeveyn
işleme
4389bef1b4

+ 2 - 1
packages/core/package.json

@@ -66,7 +66,8 @@
   },
   "dependencies": {
     "bson-objectid": "^2.0.4",
-    "escape-string-regexp": "^4.0.0"
+    "escape-string-regexp": "^4.0.0",
+    "mongoose": "^6.11.3"
   },
   "devDependencies": {
     "eslint-plugin-regex": "^1.8.0",

+ 8 - 4
packages/core/src/interfaces/common.ts

@@ -2,19 +2,23 @@
  * Common types and interfaces
  */
 
+
+import type { Types } from 'mongoose';
+
 import type { HasObjectId } from './has-object-id';
 
+type ObjectId = Types.ObjectId;
 
 // Foreign key field
-export type Ref<T> = string | T & HasObjectId;
+export type Ref<T> = string | ObjectId | T & HasObjectId;
 
 export type Nullable<T> = T | null | undefined;
 
-export const isPopulated = <T>(ref: T & HasObjectId | Ref<T>): ref is T & HasObjectId => {
-  return !(typeof ref === 'string');
+export const isPopulated = <T>(ref?: T & HasObjectId | Ref<T>): ref is T & HasObjectId => {
+  return ref != null && typeof ref === 'object' && '_id' in ref;
 };
 
-export const getIdForRef = <T>(ref: T & HasObjectId | Ref<T>): string => {
+export const getIdForRef = <T>(ref: T & HasObjectId | Ref<T>): string | ObjectId => {
   return isPopulated(ref)
     ? ref._id
     : ref;