Skip to content

Commit 7ec5f24

Browse files
authored
Merge pull request #192 from AthennaIO/develop
feat: add or clause to objectId
2 parents 9495c89 + 47781e8 commit 7ec5f24

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@athenna/database",
3-
"version": "5.13.0",
3+
"version": "5.14.0",
44
"description": "The Athenna database handler for SQL/NoSQL.",
55
"license": "MIT",
66
"author": "João Lenon <lenon@athenna.io>",

src/database/drivers/MongoDriver.ts

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { WrongMethodException } from '#src/exceptions/WrongMethodException'
2828
import { MONGO_OPERATIONS_DICTIONARY } from '#src/constants/MongoOperationsDictionary'
2929
import { NotConnectedDatabaseException } from '#src/exceptions/NotConnectedDatabaseException'
3030
import { NotImplementedMethodException } from '#src/exceptions/NotImplementedMethodException'
31+
import { ObjectId } from '#src/helpers/ObjectId'
3132

3233
export class MongoDriver extends Driver<Connection, Collection> {
3334
public primaryKey = '_id'
@@ -1496,11 +1497,51 @@ export class MongoDriver extends Driver<Connection, Collection> {
14961497
const where: any = {}
14971498

14981499
if (!Is.Empty(this._where)) {
1499-
where.$and = Json.copy(this._where)
1500+
where.$and = Json.copy(this._where).map(condition => {
1501+
const keysToSwap = Object.keys(condition).filter(key => {
1502+
const value = condition[key]
1503+
1504+
if (ObjectId.isValid(value)) {
1505+
return true
1506+
}
1507+
1508+
return false
1509+
})
1510+
1511+
keysToSwap.forEach(key => {
1512+
const objectId = condition[key]
1513+
1514+
condition[key] = {
1515+
$or: [{ [key]: objectId }, { [key]: new ObjectId(objectId) }]
1516+
}
1517+
})
1518+
1519+
return condition
1520+
})
15001521
}
15011522

15021523
if (!Is.Empty(this._orWhere)) {
1503-
where.$or = Json.copy(this._orWhere)
1524+
where.$or = Json.copy(this._orWhere).map(condition => {
1525+
const keysToSwap = Object.keys(condition).filter(key => {
1526+
const value = condition[key]
1527+
1528+
if (ObjectId.isValid(value)) {
1529+
return true
1530+
}
1531+
1532+
return false
1533+
})
1534+
1535+
keysToSwap.forEach(key => {
1536+
const objectId = condition[key]
1537+
1538+
condition[key] = {
1539+
$or: [{ [key]: objectId }, { [key]: new ObjectId(objectId) }]
1540+
}
1541+
})
1542+
1543+
return condition
1544+
})
15041545
}
15051546

15061547
if (options.clearWhere) {

0 commit comments

Comments
 (0)