Skip to content

Commit 015893f

Browse files
committed
fix: if _id is string, trasform to ObjectID
1 parent dfa88e5 commit 015893f

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@secjs/database",
3-
"version": "1.0.8",
3+
"version": "1.0.9",
44
"description": "Handle your application database with factories, seeders and query builder in Node.js",
55
"license": "MIT",
66
"author": "João Lenon <lenon@secjs.com.br>",

src/Drivers/MongoDriver.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,10 @@ export class MongoDriver implements DriverContract {
655655
return this
656656
}
657657

658+
if (statement._id && Is.String(statement._id)) {
659+
statement._id = new ObjectID(statement._id)
660+
}
661+
658662
this._where = {
659663
...this._where,
660664
...statement,
@@ -670,12 +674,20 @@ export class MongoDriver implements DriverContract {
670674
value?: any,
671675
): DriverContract {
672676
if (typeof statement === 'string') {
677+
if (isValidObjectId(value) && Is.String(value)) {
678+
value = new ObjectID(value)
679+
}
680+
673681
this._where[statement] = { $regex: value }
674682
this._pipeline.push({ $match: this._where })
675683

676684
return this
677685
}
678686

687+
if (statement._id && Is.String(statement._id)) {
688+
statement._id = new ObjectID(statement._id)
689+
}
690+
679691
this._where = {
680692
...this._where,
681693
...statement,
@@ -689,12 +701,20 @@ export class MongoDriver implements DriverContract {
689701
value?: any,
690702
): DriverContract {
691703
if (typeof statement === 'string') {
704+
if (isValidObjectId(value) && Is.String(value)) {
705+
value = new ObjectID(value)
706+
}
707+
692708
this._where[statement] = { $regex: value, $options: 'i' }
693709
this._pipeline.push({ $match: this._where })
694710

695711
return this
696712
}
697713

714+
if (statement._id && Is.String(statement._id)) {
715+
statement._id = new ObjectID(statement._id)
716+
}
717+
698718
this._where = {
699719
...this._where,
700720
...statement,
@@ -710,12 +730,20 @@ export class MongoDriver implements DriverContract {
710730
value?: any,
711731
): DriverContract {
712732
if (typeof statement === 'string') {
733+
if (isValidObjectId(value) && Is.String(value)) {
734+
value = new ObjectID(value)
735+
}
736+
713737
this._where[statement] = { $or: value }
714738
this._pipeline.push({ $match: this._where })
715739

716740
return this
717741
}
718742

743+
if (statement._id && Is.String(statement._id)) {
744+
statement._id = new ObjectID(statement._id)
745+
}
746+
719747
this._where = {
720748
...this._where,
721749
...statement,
@@ -730,12 +758,20 @@ export class MongoDriver implements DriverContract {
730758
value?: any,
731759
): DriverContract {
732760
if (typeof statement === 'string') {
761+
if (isValidObjectId(value) && Is.String(value)) {
762+
value = new ObjectID(value)
763+
}
764+
733765
this._where[statement] = { $not: value }
734766
this._pipeline.push({ $match: this._where })
735767

736768
return this
737769
}
738770

771+
if (statement._id && Is.String(statement._id)) {
772+
statement._id = new ObjectID(statement._id)
773+
}
774+
739775
this._where = {
740776
...this._where,
741777
...statement,
@@ -746,13 +782,33 @@ export class MongoDriver implements DriverContract {
746782
}
747783

748784
buildWhereIn(columnName: string, values: any[]): DriverContract {
785+
if (columnName === '_id') {
786+
values = values.map(value => {
787+
if (Is.String(value)) {
788+
return new ObjectID(value)
789+
}
790+
791+
return value
792+
})
793+
}
794+
749795
this._where[columnName] = { $in: values }
750796
this._pipeline.push({ $match: this._where })
751797

752798
return this
753799
}
754800

755801
buildWhereNotIn(columnName: string, values: any[]): DriverContract {
802+
if (columnName === '_id') {
803+
values = values.map(value => {
804+
if (Is.String(value)) {
805+
return new ObjectID(value)
806+
}
807+
808+
return value
809+
})
810+
}
811+
756812
this._where[columnName] = { $nin: values }
757813
this._pipeline.push({ $match: this._where })
758814

@@ -788,13 +844,33 @@ export class MongoDriver implements DriverContract {
788844
}
789845

790846
buildWhereBetween(columnName: string, values: [any, any]): DriverContract {
847+
if (columnName === '_id') {
848+
if (Is.String(values[0])) {
849+
values[0] = new ObjectID(values[0])
850+
}
851+
852+
if (Is.String(values[1])) {
853+
values[1] = new ObjectID(values[1])
854+
}
855+
}
856+
791857
this._where[columnName] = { $gte: values[0], $lte: values[1] }
792858
this._pipeline.push({ $match: this._where })
793859

794860
return this
795861
}
796862

797863
buildWhereNotBetween(columnName: string, values: [any, any]): DriverContract {
864+
if (columnName === '_id') {
865+
if (Is.String(values[0])) {
866+
values[0] = new ObjectID(values[0])
867+
}
868+
869+
if (Is.String(values[1])) {
870+
values[1] = new ObjectID(values[1])
871+
}
872+
}
873+
798874
this._where[columnName] = { $not: { $gte: values[0], $lte: values[1] } }
799875
this._pipeline.push({ $match: this._where })
800876

@@ -904,6 +980,10 @@ export class MongoDriver implements DriverContract {
904980
}
905981

906982
buildHaving(column: string, operator: string, value: any): DriverContract {
983+
if (column === '_id' && Is.String(value)) {
984+
value = new ObjectID(value)
985+
}
986+
907987
const operatorDictionary = {
908988
'>=': { $gte: value },
909989
'<=': { $lte: value },

0 commit comments

Comments
 (0)