Skip to content

Commit 206af5c

Browse files
committed
fix: verify all data in insert/update methods and transform where is a valid ObjectID
1 parent 2f6de9b commit 206af5c

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
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.1.3",
3+
"version": "1.1.4",
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: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,13 +260,23 @@ export class MongoDriver implements DriverContract {
260260

261261
async insert(values: any | any[]): Promise<string[]> {
262262
if (Is.Array(values)) {
263+
values.forEach(value => {
264+
Object.keys(value).forEach(key => {
265+
value[key] = MongoDriver.stringToObjectId(value[key])
266+
})
267+
})
268+
263269
const data = await this.queryBuilder.insertMany(values)
264270

265271
return Object.keys(data.insertedIds).map(key =>
266272
data.insertedIds[key].toString(),
267273
)
268274
}
269275

276+
Object.keys(values).forEach(key => {
277+
values[key] = MongoDriver.stringToObjectId(values[key])
278+
})
279+
270280
const data = await this.queryBuilder.insertOne(values, {
271281
session: this.session,
272282
})
@@ -283,7 +293,11 @@ export class MongoDriver implements DriverContract {
283293
}
284294

285295
async update(key: any | string, value?: any): Promise<string[]> {
286-
if (typeof key === 'object') {
296+
if (Is.Object(key)) {
297+
Object.keys(key).forEach(k => {
298+
key[k] = MongoDriver.stringToObjectId(key[k])
299+
})
300+
287301
const { modifiedCount } = await this.queryBuilder.updateMany(
288302
this._where,
289303
{ $set: key },
@@ -301,6 +315,8 @@ export class MongoDriver implements DriverContract {
301315
return data.map(model => model._id.toString())
302316
}
303317

318+
value = MongoDriver.stringToObjectId(value)
319+
304320
const { modifiedCount } = await this.queryBuilder.updateMany(
305321
this._where,
306322
{ $set: { [key]: value } },
@@ -319,7 +335,11 @@ export class MongoDriver implements DriverContract {
319335
}
320336

321337
async updateAndGet(key: any | string, value?: any): Promise<any[]> {
322-
if (typeof key === 'object') {
338+
if (Is.Object(key)) {
339+
Object.keys(key).forEach(k => {
340+
key[k] = MongoDriver.stringToObjectId(key[k])
341+
})
342+
323343
const { modifiedCount } = await this.queryBuilder.updateMany(
324344
this._where,
325345
{ $set: key },
@@ -335,6 +355,8 @@ export class MongoDriver implements DriverContract {
335355
return this.queryBuilder.find(this.where).toArray()
336356
}
337357

358+
value = MongoDriver.stringToObjectId(value)
359+
338360
const { modifiedCount } = await this.queryBuilder.updateMany(
339361
this._where,
340362
{ $set: { [key]: value } },

0 commit comments

Comments
 (0)