Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update deps #131

Merged
merged 3 commits into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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 <lenon@athenna.io>",
Expand Down
65 changes: 20 additions & 45 deletions src/database/drivers/MongoDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class MongoDriver extends Driver<Connection, Collection> {
/**
* The where clause used in update queries.
*/
private _where: Record<string, any> = {}
private _where: Record<string, any>[] = []

/**
* The or where clause used in update queries.
Expand Down Expand Up @@ -1059,21 +1059,18 @@ export class MongoDriver extends Driver<Connection, Collection> {
}

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
}
Expand Down Expand Up @@ -1127,7 +1124,7 @@ export class MongoDriver extends Driver<Connection, Collection> {
* 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
}
Expand All @@ -1136,7 +1133,7 @@ export class MongoDriver extends Driver<Connection, Collection> {
* 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
}
Expand All @@ -1145,7 +1142,7 @@ export class MongoDriver extends Driver<Connection, Collection> {
* 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
}
Expand All @@ -1154,7 +1151,9 @@ export class MongoDriver extends Driver<Connection, Collection> {
* 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
}
Expand All @@ -1163,7 +1162,7 @@ export class MongoDriver extends Driver<Connection, Collection> {
* Set a where null statement in your query.
*/
public whereNull(column: string) {
this._where[column] = null
this._where.push({ [column]: null })

return this
}
Expand All @@ -1172,7 +1171,7 @@ export class MongoDriver extends Driver<Connection, Collection> {
* 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
}
Expand Down Expand Up @@ -1407,20 +1406,18 @@ export class MongoDriver extends Driver<Connection, Collection> {
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) {
Expand Down Expand Up @@ -1452,29 +1449,7 @@ export class MongoDriver extends Driver<Connection, Collection> {
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
}
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/drivers/MongoDriverTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Loading