Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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": "5.15.0",
"version": "5.16.0",
"description": "The Athenna database handler for SQL/NoSQL.",
"license": "MIT",
"author": "João Lenon <lenon@athenna.io>",
Expand Down
46 changes: 30 additions & 16 deletions src/database/drivers/MongoDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1213,8 +1213,8 @@ export class MongoDriver extends Driver<Connection, Collection> {
*/
public whereIn(column: string, values: any[]) {
values = values.flatMap(value => {
if (ObjectId.isValid(value)) {
return [value, ObjectId.ifValidSwap(value)]
if (ObjectId.isValidStringOrObject(value)) {
return [value, new ObjectId(value)]
}

return [value]
Expand All @@ -1230,8 +1230,8 @@ export class MongoDriver extends Driver<Connection, Collection> {
*/
public whereNotIn(column: string, values: any[]) {
values = values.flatMap(value => {
if (ObjectId.isValid(value)) {
return [value, ObjectId.ifValidSwap(value)]
if (ObjectId.isValidStringOrObject(value)) {
return [value, new ObjectId(value)]
}

return [value]
Expand Down Expand Up @@ -1361,8 +1361,8 @@ export class MongoDriver extends Driver<Connection, Collection> {
*/
public orWhereIn(column: string, values: any[]) {
values = values.flatMap(value => {
if (ObjectId.isValid(value)) {
return [value, ObjectId.ifValidSwap(value)]
if (ObjectId.isValidStringOrObject(value)) {
return [value, new ObjectId(value)]
}

return [value]
Expand All @@ -1378,8 +1378,8 @@ export class MongoDriver extends Driver<Connection, Collection> {
*/
public orWhereNotIn(column: string, values: any[]) {
values = values.flatMap(value => {
if (ObjectId.isValid(value)) {
return [value, ObjectId.ifValidSwap(value)]
if (ObjectId.isValidStringOrObject(value)) {
return [value, new ObjectId(value)]
}

return [value]
Expand Down Expand Up @@ -1533,19 +1533,26 @@ export class MongoDriver extends Driver<Connection, Collection> {
const keysToSwap = Object.keys(condition).filter(key => {
const value = condition[key]

if (ObjectId.isValid(value)) {
if (ObjectId.isValidStringOrObject(value)) {
return true
}

return false
})

keysToSwap.forEach(key => {
if (!condition.$or) {
condition.$or = []
}

const objectId = condition[key]

condition[key] = {
$or: [{ [key]: objectId }, { [key]: new ObjectId(objectId) }]
}
condition.$or.push(
{ [key]: objectId },
{ [key]: new ObjectId(objectId) }
)

delete condition[key]
})

return condition
Expand All @@ -1557,19 +1564,26 @@ export class MongoDriver extends Driver<Connection, Collection> {
const keysToSwap = Object.keys(condition).filter(key => {
const value = condition[key]

if (ObjectId.isValid(value)) {
if (ObjectId.isValidStringOrObject(value)) {
return true
}

return false
})

keysToSwap.forEach(key => {
if (!condition.$or) {
condition.$or = []
}

const objectId = condition[key]

condition[key] = {
$or: [{ [key]: objectId }, { [key]: new ObjectId(objectId) }]
}
condition.$or.push(
{ [key]: objectId },
{ [key]: new ObjectId(objectId) }
)

delete condition[key]
})

return condition
Expand Down
7 changes: 7 additions & 0 deletions src/helpers/ObjectId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ export class ObjectId {
return mongoose.isValidObjectId(objectId) && new mongoose.Types.ObjectId(objectId) == objectId
}

/**
* Validate if is a valid object id string or ObjectID object.
*/
public static isValidStringOrObject(objectId: any): boolean {
return this.isValidString(objectId) || this.isValidObject(objectId)
}

/**
* Validate if is a valid object id string.
*/
Expand Down