From 41647d741f613860e6ba0af61b60a2cb6834067a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Lenon?= Date: Sun, 7 Jan 2024 22:14:34 +0000 Subject: [PATCH] update deps (#131) ## Proposed changes Describe the big picture of your changes here to communicate to the maintainers why we should accept this pull request. If it fixes a bug or resolves a feature request, be sure to link to that issue. ## Types of changes What types of changes does your code introduce? _Put an `x` in the boxes that apply_ - [ ] Bugfix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) ## Checklist _Put an `x` in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code._ - [ ] I have read the [CONTRIBUTING](https://github.com/AthennaIO/Database/blob/master/CONTRIBUTING.md) documentation - [ ] Lint and unit tests pass locally with my changes - [ ] I have added tests that prove my fix is effective or that my feature works. - [ ] I have added necessary documentation (if appropriate) ## Further comments If this is a relatively large or complex change, kick off the discussion by explaining why you chose the solution you did and what alternatives you considered, etc... --- package-lock.json | 4 +- package.json | 2 +- src/database/drivers/MongoDriver.ts | 65 +++++++++------------------ tests/unit/drivers/MongoDriverTest.ts | 2 +- 4 files changed, 24 insertions(+), 49 deletions(-) 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()