Yuki Takei 1 год назад
Родитель
Сommit
4389bef1b4
2 измененных файлов с 10 добавлено и 5 удалено
  1. 2 1
      packages/core/package.json
  2. 8 4
      packages/core/src/interfaces/common.ts

+ 2 - 1
packages/core/package.json

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

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

@@ -2,19 +2,23 @@
  * Common types and interfaces
  * Common types and interfaces
  */
  */
 
 
+
+import type { Types } from 'mongoose';
+
 import type { HasObjectId } from './has-object-id';
 import type { HasObjectId } from './has-object-id';
 
 
+type ObjectId = Types.ObjectId;
 
 
 // Foreign key field
 // 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 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)
   return isPopulated(ref)
     ? ref._id
     ? ref._id
     : ref;
     : ref;