Skip to content

Commit

Permalink
Merge pull request #126 from AthennaIO/develop
Browse files Browse the repository at this point in the history
feat(objectId): safe import mongoose
  • Loading branch information
jlenon7 authored Jan 3, 2024
2 parents 015c55e + 0aebe5d commit 736d823
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/database",
"version": "4.18.0",
"version": "4.19.0",
"description": "The Athenna database handler for SQL/NoSQL.",
"license": "MIT",
"author": "João Lenon <lenon@athenna.io>",
Expand Down
25 changes: 20 additions & 5 deletions src/helpers/ObjectId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,50 @@
* file that was distributed with this source code.
*/

import { Is } from '@athenna/common'
import { isValidObjectId, Types } from 'mongoose'
import { Is, Module } from '@athenna/common'

const mongoose = await Module.safeImport('mongoose')

export class ObjectId {
/**
* Validate if is a valid object id.
*/
public static isValid(objectId: any): boolean {
if (!mongoose) {
return false
}

// eslint-disable-next-line
return isValidObjectId(objectId) && new Types.ObjectId(objectId) == objectId
return mongoose.isValidObjectId(objectId) && new mongoose.Types.ObjectId(objectId) == objectId
}

/**
* Validate if is a valid object id string.
*/
public static isValidString(objectId: any): boolean {
if (!mongoose) {
return false
}

return Is.String(objectId) && this.isValid(objectId)
}

/**
* Validate if is a valid object id object.
*/
public static isValidObject(objectId: any): boolean {
if (!mongoose) {
return false
}

return (
!Is.Number(objectId) && !Is.String(objectId) && isValidObjectId(objectId)
!Is.Number(objectId) &&
!Is.String(objectId) &&
mongoose.isValidObjectId(objectId)
)
}

public constructor(value: any) {
return new Types.ObjectId(value)
return new mongoose.Types.ObjectId(value)
}
}

0 comments on commit 736d823

Please sign in to comment.