diff --git a/package-lock.json b/package-lock.json index 2e9dc2a..b732a0c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@athenna/database", - "version": "4.24.0", + "version": "4.25.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@athenna/database", - "version": "4.24.0", + "version": "4.25.0", "license": "MIT", "dependencies": { "@faker-js/faker": "^8.3.0", diff --git a/package.json b/package.json index 834cd5a..ba48a2e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@athenna/database", - "version": "4.24.0", + "version": "4.25.0", "description": "The Athenna database handler for SQL/NoSQL.", "license": "MIT", "author": "João Lenon ", diff --git a/src/database/drivers/MongoDriver.ts b/src/database/drivers/MongoDriver.ts index f42cbda..86cc99c 100644 --- a/src/database/drivers/MongoDriver.ts +++ b/src/database/drivers/MongoDriver.ts @@ -35,7 +35,7 @@ export class MongoDriver extends Driver { /** * The where clause used in update queries. */ - private _where: Record = {} + private _where: Record[] = [] /** * The or where clause used in update queries. @@ -1059,21 +1059,18 @@ export class MongoDriver extends Driver { } if (operation === undefined) { - this._where = { - ...this._where, - ...statement - } + this._where.push(statement) return this } if (value === undefined) { - this._where[statement] = this.setOperator(operation, '=') + this._where.push({ [statement]: this.setOperator(operation, '=') }) return this } - this._where[statement] = this.setOperator(value, operation) + this._where.push({ [statement]: this.setOperator(value, operation) }) return this } @@ -1127,7 +1124,7 @@ export class MongoDriver extends Driver { * Set a where in statement in your query. */ public whereIn(column: string, values: any[]) { - this._where[column] = { $in: values } + this._where.push({ [column]: { $in: values } }) return this } @@ -1136,7 +1133,7 @@ export class MongoDriver extends Driver { * Set a where not in statement in your query. */ public whereNotIn(column: string, values: any[]) { - this._where[column] = { $nin: values } + this._where.push({ [column]: { $nin: values } }) return this } @@ -1145,7 +1142,7 @@ export class MongoDriver extends Driver { * Set a where between statement in your query. */ public whereBetween(column: string, values: [any, any]) { - this._where[column] = { $gte: values[0], $lte: values[1] } + this._where.push({ [column]: { $gte: values[0], $lte: values[1] } }) return this } @@ -1154,7 +1151,9 @@ export class MongoDriver extends Driver { * Set a where not between statement in your query. */ public whereNotBetween(column: string, values: [any, any]) { - this._where[column] = { $not: { $gte: values[0], $lte: values[1] } } + this._where.push({ + [column]: { $not: { $gte: values[0], $lte: values[1] } } + }) return this } @@ -1163,7 +1162,7 @@ export class MongoDriver extends Driver { * Set a where null statement in your query. */ public whereNull(column: string) { - this._where[column] = null + this._where.push({ [column]: null }) return this } @@ -1172,7 +1171,7 @@ export class MongoDriver extends Driver { * Set a where not null statement in your query. */ public whereNotNull(column: string) { - this._where[column] = { $ne: null } + this._where.push({ [column]: { $ne: null } }) return this } @@ -1407,20 +1406,18 @@ export class MongoDriver extends Driver { clearOrWhere: true }) - if (Is.Empty(this._orWhere)) { - const where = Json.copy(this._where) + const where: any = {} - if (options.clearWhere) { - this._where = {} - } - - return where + if (!Is.Empty(this._where)) { + where.$and = Json.copy(this._where) } - const where = { $or: [Json.copy(this._where), ...Json.copy(this._orWhere)] } + if (!Is.Empty(this._orWhere)) { + where.$or = Json.copy(this._orWhere) + } if (options.clearWhere) { - this._where = {} + this._where = [] } if (options.clearOrWhere) { @@ -1452,29 +1449,7 @@ export class MongoDriver extends Driver { this.pipeline = [] } - if (!Is.Empty(this._where)) { - const $match = Json.copy(this._where) - - pipeline.push({ $match }) - } - - if (!Is.Empty(this._orWhere)) { - const $match = { $or: Json.copy(this._orWhere) } - - if (!Is.Empty(this._where)) { - $match.$or.unshift(this._where) - } - - pipeline.push({ $match }) - } - - if (options.clearWhere) { - this._where = {} - } - - if (options.clearOrWhere) { - this._orWhere = [] - } + pipeline.push({ $match: this.createWhere(options) }) return pipeline } diff --git a/tests/unit/drivers/MongoDriverTest.ts b/tests/unit/drivers/MongoDriverTest.ts index b855c13..f8703b0 100644 --- a/tests/unit/drivers/MongoDriverTest.ts +++ b/tests/unit/drivers/MongoDriverTest.ts @@ -833,7 +833,7 @@ export default class MongoDriverTest { this.driver.table('users').select('*').dump() - assert.calledWith(console.log, { where: {}, orWhere: [], pipeline: [] }) + assert.calledWith(console.log, { where: [], orWhere: [], pipeline: [] }) } @Test()