From 5cceeb48226fd754843e19542c866f7eb333b344 Mon Sep 17 00:00:00 2001 From: jlenon7 Date: Sun, 31 Dec 2023 12:23:04 +0000 Subject: [PATCH 1/2] test(configurer): add envs in test --- tests/unit/configurer/DatabaseConfigurerTest.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/tests/unit/configurer/DatabaseConfigurerTest.ts b/tests/unit/configurer/DatabaseConfigurerTest.ts index b9c037f..e01e7ee 100644 --- a/tests/unit/configurer/DatabaseConfigurerTest.ts +++ b/tests/unit/configurer/DatabaseConfigurerTest.ts @@ -59,6 +59,7 @@ export default class DatabaseConfigurerTest { `\nDB_CONNECTION=mysql\n` + 'DB_HOST=localhost\n' + `DB_PORT=3306\n` + + 'DB_DEBUG=false\n' + 'DB_USERNAME=root\n' + 'DB_PASSWORD=root\n' + 'DB_DATABASE=athenna\n' @@ -93,6 +94,7 @@ export default class DatabaseConfigurerTest { `\nDB_CONNECTION=mysql\n` + 'DB_HOST=localhost\n' + `DB_PORT=3306\n` + + 'DB_DEBUG=false\n' + 'DB_USERNAME=root\n' + 'DB_PASSWORD=root\n' + 'DB_DATABASE=athenna\n' @@ -123,6 +125,7 @@ export default class DatabaseConfigurerTest { `\nDB_CONNECTION=postgres\n` + 'DB_HOST=localhost\n' + `DB_PORT=5432\n` + + 'DB_DEBUG=false\n' + 'DB_USERNAME=root\n' + 'DB_PASSWORD=root\n' + 'DB_DATABASE=athenna\n' @@ -159,6 +162,7 @@ export default class DatabaseConfigurerTest { `\nDB_CONNECTION=postgres\n` + 'DB_HOST=localhost\n' + `DB_PORT=5432\n` + + 'DB_DEBUG=false\n' + 'DB_USERNAME=root\n' + 'DB_PASSWORD=root\n' + 'DB_DATABASE=athenna\n' @@ -184,7 +188,10 @@ export default class DatabaseConfigurerTest { dockerComposeFile, "version: '3'\n\nservices:\n mongo:\n container_name: athenna_mongo\n image: mongo\n ports:\n - '27017:27017'\n environment:\n MONGO_INITDB_ROOT_USERNAME: root\n MONGO_INITDB_ROOT_PASSWORD: root\n" ) - assert.deepEqual(envFile, `\nDB_CONNECTION=mongo\n` + 'DB_URL=mongodb://root:root@localhost:27017/admin\n') + assert.deepEqual( + envFile, + `\nDB_CONNECTION=mongo\n` + 'DB_DEBUG=false\n' + 'DB_URL=mongodb://root:root@localhost:27017/admin\n' + ) } @Test() @@ -210,6 +217,9 @@ export default class DatabaseConfigurerTest { dockerComposeFile, "version: '3'\nservices:\n app:\n container_name: athenna_app\n mongo:\n container_name: athenna_mongo\n image: mongo\n ports:\n - '27017:27017'\n environment:\n MONGO_INITDB_ROOT_USERNAME: root\n MONGO_INITDB_ROOT_PASSWORD: root\n" ) - assert.deepEqual(envFile, `\nDB_CONNECTION=mongo\n` + 'DB_URL=mongodb://root:root@localhost:27017/admin\n') + assert.deepEqual( + envFile, + `\nDB_CONNECTION=mongo\n` + 'DB_DEBUG=false\n' + 'DB_URL=mongodb://root:root@localhost:27017/admin\n' + ) } } From c55d8079ff41eaa99d114edd07192856f90c7739 Mon Sep 17 00:00:00 2001 From: jlenon7 Date: Tue, 2 Jan 2024 23:50:32 +0000 Subject: [PATCH 2/2] feat(query): add more soft delete methods --- package-lock.json | 4 +- package.json | 2 +- src/database/builders/QueryBuilder.ts | 165 +++++----- src/database/drivers/Driver.ts | 12 +- src/database/drivers/MongoDriver.ts | 138 ++++----- src/database/drivers/MySqlDriver.ts | 136 ++++---- src/database/drivers/PostgresDriver.ts | 136 ++++---- src/database/drivers/SqliteDriver.ts | 136 ++++---- src/models/BaseModel.ts | 30 +- src/models/builders/ModelQueryBuilder.ts | 293 +++++++++++++----- src/models/factories/ModelFactory.ts | 4 +- src/models/factories/ModelGenerator.ts | 2 +- src/models/schemas/ModelSchema.ts | 46 ++- tests/fixtures/models/Order.ts | 19 ++ tests/unit/models/BaseModelTest.ts | 42 +++ .../models/builders/ModelQueryBuilderTest.ts | 160 ++++++++-- .../models/factories/ModelGeneratorTest.ts | 30 -- 17 files changed, 814 insertions(+), 541 deletions(-) create mode 100644 tests/fixtures/models/Order.ts diff --git a/package-lock.json b/package-lock.json index f48c60b..a20d9bd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@athenna/database", - "version": "4.17.0", + "version": "4.18.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@athenna/database", - "version": "4.17.0", + "version": "4.18.0", "license": "MIT", "dependencies": { "@faker-js/faker": "^8.3.0", diff --git a/package.json b/package.json index fcfa066..c919423 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@athenna/database", - "version": "4.17.0", + "version": "4.18.0", "description": "The Athenna database handler for SQL/NoSQL.", "license": "MIT", "author": "João Lenon ", diff --git a/src/database/builders/QueryBuilder.ts b/src/database/builders/QueryBuilder.ts index 263d06f..d02a3fc 100644 --- a/src/database/builders/QueryBuilder.ts +++ b/src/database/builders/QueryBuilder.ts @@ -44,7 +44,7 @@ export class QueryBuilder { * Set the driver primary key that will be used * when creating new data. */ - public setPrimaryKey(primaryKey: string): this { + public setPrimaryKey(primaryKey: string) { this.driver.setPrimaryKey(primaryKey) return this @@ -221,7 +221,7 @@ export class QueryBuilder { /** * Set a new table to work with in query builder. */ - public table(tableName: string): this { + public table(tableName: string) { this.driver.table(tableName) return this @@ -232,8 +232,8 @@ export class QueryBuilder { */ public when( criteria: any, - closure: (query: this, criteriaValue: any) => void - ): this { + closure: (query: this, criteriaValue: any) => any | Promise + ) { if (criteria) { closure(this, criteria) @@ -246,7 +246,7 @@ export class QueryBuilder { /** * Log in console the actual query built. */ - public dump(): this { + public dump() { this.driver.dump() return this @@ -255,7 +255,7 @@ export class QueryBuilder { /** * Set the columns that should be selected on query. */ - public select(...columns: string[] | ModelColumns[]): this { + public select(...columns: string[] | ModelColumns[]) { this.driver.select(...(columns as string[])) return this @@ -264,7 +264,7 @@ export class QueryBuilder { /** * Set the columns that should be selected on query raw. */ - public selectRaw(sql: string, bindings?: any): this { + public selectRaw(sql: string, bindings?: any) { this.driver.selectRaw(sql, bindings) return this @@ -275,7 +275,7 @@ export class QueryBuilder { * Different from `table()` method, this method * doesn't change the driver table. */ - public from(table: string): this { + public from(table: string) { this.driver.from(table) return this @@ -286,7 +286,7 @@ export class QueryBuilder { * Different from `table()` method, this method * doesn't change the driver table. */ - public fromRaw(sql: string, bindings?: any): this { + public fromRaw(sql: string, bindings?: any) { this.driver.fromRaw(sql, bindings) return this @@ -310,7 +310,7 @@ export class QueryBuilder { column1?: string, operation?: string | Operations, column2?: string - ): this { + ) { this.driver.join(tableName, column1, operation, column2) return this @@ -334,7 +334,7 @@ export class QueryBuilder { column1?: string, operation?: string | Operations, column2?: string - ): this { + ) { this.driver.leftJoin(tableName, column1, operation, column2) return this @@ -358,7 +358,7 @@ export class QueryBuilder { column1?: string, operation?: string | Operations, column2?: string - ): this { + ) { this.driver.rightJoin(tableName, column1, operation, column2) return this @@ -382,7 +382,7 @@ export class QueryBuilder { column1?: string, operation?: string | Operations, column2?: string - ): this { + ) { this.driver.crossJoin(tableName, column1, operation, column2) return this @@ -411,7 +411,7 @@ export class QueryBuilder { column1?: string, operation?: string | Operations, column2?: string - ): this { + ) { this.driver.fullOuterJoin(tableName, column1, operation, column2) return this @@ -440,7 +440,7 @@ export class QueryBuilder { column1?: string, operation?: string | Operations, column2?: string - ): this { + ) { this.driver.leftOuterJoin(tableName, column1, operation, column2) return this @@ -469,7 +469,7 @@ export class QueryBuilder { column1?: string, operation?: string | Operations, column2?: string - ): this { + ) { this.driver.rightOuterJoin(tableName, column1, operation, column2) return this @@ -478,7 +478,7 @@ export class QueryBuilder { /** * Set a join raw statement in your query. */ - public joinRaw(sql: string, bindings?: any): this { + public joinRaw(sql: string, bindings?: any) { this.driver.joinRaw(sql, bindings) return this @@ -487,7 +487,7 @@ export class QueryBuilder { /** * Set a group by statement in your query. */ - public groupBy(...columns: string[] | ModelColumns[]): this { + public groupBy(...columns: string[] | ModelColumns[]) { this.driver.groupBy(...(columns as string[])) return this @@ -496,7 +496,7 @@ export class QueryBuilder { /** * Set a group by raw statement in your query. */ - public groupByRaw(sql: string, bindings?: any): this { + public groupByRaw(sql: string, bindings?: any) { this.driver.groupByRaw(sql, bindings) return this @@ -509,7 +509,7 @@ export class QueryBuilder { column: string | ModelColumns, operation?: any | Operations, value?: any - ): this { + ) { this.driver.having(column, operation, value) return this @@ -518,7 +518,7 @@ export class QueryBuilder { /** * Set a having raw statement in your query. */ - public havingRaw(sql: string, bindings?: any): this { + public havingRaw(sql: string, bindings?: any) { this.driver.havingRaw(sql, bindings) return this @@ -527,7 +527,7 @@ export class QueryBuilder { /** * Set a having exists statement in your query. */ - public havingExists(closure: (query: Driver) => void): this { + public havingExists(closure: (query: Driver) => void) { this.driver.havingExists(closure) return this @@ -536,7 +536,7 @@ export class QueryBuilder { /** * Set a having not exists statement in your query. */ - public havingNotExists(closure: (query: Driver) => void): this { + public havingNotExists(closure: (query: Driver) => void) { this.driver.havingNotExists(closure) return this @@ -545,7 +545,7 @@ export class QueryBuilder { /** * Set a having in statement in your query. */ - public havingIn(column: string | ModelColumns, values: any[]): this { + public havingIn(column: string | ModelColumns, values: any[]) { this.driver.havingIn(column as string, values) return this @@ -554,7 +554,7 @@ export class QueryBuilder { /** * Set a having not in statement in your query. */ - public havingNotIn(column: string | ModelColumns, values: any[]): this { + public havingNotIn(column: string | ModelColumns, values: any[]) { this.driver.havingNotIn(column as string, values) return this @@ -563,10 +563,7 @@ export class QueryBuilder { /** * Set a having between statement in your query. */ - public havingBetween( - column: string | ModelColumns, - values: [any, any] - ): this { + public havingBetween(column: string | ModelColumns, values: [any, any]) { this.driver.havingBetween(column as string, values) return this @@ -578,7 +575,7 @@ export class QueryBuilder { public havingNotBetween( column: string | ModelColumns, values: [any, any] - ): this { + ) { this.driver.havingNotBetween(column as string, values) return this @@ -587,7 +584,7 @@ export class QueryBuilder { /** * Set a having null statement in your query. */ - public havingNull(column: string | ModelColumns): this { + public havingNull(column: string | ModelColumns) { this.driver.havingNull(column as string) return this @@ -596,7 +593,7 @@ export class QueryBuilder { /** * Set a having not null statement in your query. */ - public havingNotNull(column: string | ModelColumns): this { + public havingNotNull(column: string | ModelColumns) { this.driver.havingNotNull(column as string) return this @@ -609,7 +606,7 @@ export class QueryBuilder { column: string | ModelColumns, operation?: any | Operations, value?: any - ): this { + ) { this.driver.orHaving(column as string, operation, value) return this @@ -618,7 +615,7 @@ export class QueryBuilder { /** * Set an or having raw statement in your query. */ - public orHavingRaw(sql: string, bindings?: any): this { + public orHavingRaw(sql: string, bindings?: any) { this.driver.orHavingRaw(sql, bindings) return this @@ -627,7 +624,7 @@ export class QueryBuilder { /** * Set an or having exists statement in your query. */ - public orHavingExists(closure: (query: Driver) => void): this { + public orHavingExists(closure: (query: Driver) => void) { this.driver.orHavingExists(closure) return this @@ -636,7 +633,7 @@ export class QueryBuilder { /** * Set an or having not exists statement in your query. */ - public orHavingNotExists(closure: (query: Driver) => void): this { + public orHavingNotExists(closure: (query: Driver) => void) { this.driver.orHavingNotExists(closure) return this @@ -645,7 +642,7 @@ export class QueryBuilder { /** * Set an or having in statement in your query. */ - public orHavingIn(column: string | ModelColumns, values: any[]): this { + public orHavingIn(column: string | ModelColumns, values: any[]) { this.driver.orHavingIn(column as string, values) return this @@ -654,7 +651,7 @@ export class QueryBuilder { /** * Set an or having not in statement in your query. */ - public orHavingNotIn(column: string | ModelColumns, values: any[]): this { + public orHavingNotIn(column: string | ModelColumns, values: any[]) { this.driver.orHavingNotIn(column as string, values) return this @@ -663,10 +660,7 @@ export class QueryBuilder { /** * Set an or having between statement in your query. */ - public orHavingBetween( - column: string | ModelColumns, - values: [any, any] - ): this { + public orHavingBetween(column: string | ModelColumns, values: [any, any]) { this.driver.orHavingBetween(column as string, values) return this @@ -678,7 +672,7 @@ export class QueryBuilder { public orHavingNotBetween( column: string | ModelColumns, values: [any, any] - ): this { + ) { this.driver.orHavingNotBetween(column as string, values) return this @@ -687,7 +681,7 @@ export class QueryBuilder { /** * Set an or having null statement in your query. */ - public orHavingNull(column: string | ModelColumns): this { + public orHavingNull(column: string | ModelColumns) { this.driver.orHavingNull(column as string) return this @@ -696,7 +690,7 @@ export class QueryBuilder { /** * Set an or having not null statement in your query. */ - public orHavingNotNull(column: string | ModelColumns): this { + public orHavingNotNull(column: string | ModelColumns) { this.driver.orHavingNotNull(column as string) return this @@ -714,11 +708,7 @@ export class QueryBuilder { /** * Set a where statement in your query. */ - public where( - statement: any, - operation?: any | Operations, - value?: any - ): this { + public where(statement: any, operation?: any | Operations, value?: any) { this.driver.where(statement, operation, value) return this @@ -731,7 +721,7 @@ export class QueryBuilder { /** * Set a where not statement in your query. */ - public whereNot(statement: any, value?: any): this { + public whereNot(statement: any, value?: any) { this.driver.whereNot(statement, value) return this @@ -740,7 +730,7 @@ export class QueryBuilder { /** * Set a where raw statement in your query. */ - public whereRaw(sql: string, bindings?: any): this { + public whereRaw(sql: string, bindings?: any) { this.driver.whereRaw(sql, bindings) return this @@ -749,7 +739,7 @@ export class QueryBuilder { /** * Set a where exists statement in your query. */ - public whereExists(closure: (query: Driver) => void): this { + public whereExists(closure: (query: Driver) => void) { this.driver.whereExists(closure) return this @@ -758,7 +748,7 @@ export class QueryBuilder { /** * Set a where not exists statement in your query. */ - public whereNotExists(closure: (query: Driver) => void): this { + public whereNotExists(closure: (query: Driver) => void) { this.driver.whereNotExists(closure) return this @@ -767,7 +757,7 @@ export class QueryBuilder { /** * Set a where like statement in your query. */ - public whereLike(column: string | ModelColumns, value: any): this { + public whereLike(column: string | ModelColumns, value: any) { this.driver.whereLike(column, value) return this @@ -776,7 +766,7 @@ export class QueryBuilder { /** * Set a where ILike statement in your query. */ - public whereILike(column: string | ModelColumns, value: any): this { + public whereILike(column: string | ModelColumns, value: any) { this.driver.whereILike(column, value) return this @@ -785,7 +775,7 @@ export class QueryBuilder { /** * Set a where in statement in your query. */ - public whereIn(column: string | ModelColumns, values: any[]): this { + public whereIn(column: string | ModelColumns, values: any[]) { this.driver.whereIn(column as string, values) return this @@ -794,7 +784,7 @@ export class QueryBuilder { /** * Set a where not in statement in your query. */ - public whereNotIn(column: string | ModelColumns, values: any[]): this { + public whereNotIn(column: string | ModelColumns, values: any[]) { this.driver.whereNotIn(column as string, values) return this @@ -803,10 +793,7 @@ export class QueryBuilder { /** * Set a where between statement in your query. */ - public whereBetween( - column: string | ModelColumns, - values: [any, any] - ): this { + public whereBetween(column: string | ModelColumns, values: [any, any]) { this.driver.whereBetween(column as string, values) return this @@ -815,10 +802,7 @@ export class QueryBuilder { /** * Set a where not between statement in your query. */ - public whereNotBetween( - column: string | ModelColumns, - values: [any, any] - ): this { + public whereNotBetween(column: string | ModelColumns, values: [any, any]) { this.driver.whereNotBetween(column as string, values) return this @@ -827,7 +811,7 @@ export class QueryBuilder { /** * Set a where null statement in your query. */ - public whereNull(column: string | ModelColumns): this { + public whereNull(column: string | ModelColumns) { this.driver.whereNull(column as string) return this @@ -836,7 +820,7 @@ export class QueryBuilder { /** * Set a where not null statement in your query. */ - public whereNotNull(column: string | ModelColumns): this { + public whereNotNull(column: string | ModelColumns) { this.driver.whereNotNull(column as string) return this @@ -854,11 +838,7 @@ export class QueryBuilder { /** * Set a or where statement in your query. */ - public orWhere( - statement: any, - operation?: any | Operations, - value?: any - ): this { + public orWhere(statement: any, operation?: any | Operations, value?: any) { this.driver.orWhere(statement, operation, value) return this @@ -871,7 +851,7 @@ export class QueryBuilder { /** * Set a where not statement in your query. */ - public orWhereNot(statement: any, value?: any): this { + public orWhereNot(statement: any, value?: any) { this.driver.orWhereNot(statement, value) return this @@ -880,7 +860,7 @@ export class QueryBuilder { /** * Set a or where raw statement in your query. */ - public orWhereRaw(sql: string, bindings?: any): this { + public orWhereRaw(sql: string, bindings?: any) { this.driver.orWhereRaw(sql, bindings) return this @@ -889,7 +869,7 @@ export class QueryBuilder { /** * Set an or where exists statement in your query. */ - public orWhereExists(closure: (query: Driver) => void): this { + public orWhereExists(closure: (query: Driver) => void) { this.driver.orWhereExists(closure) return this @@ -898,7 +878,7 @@ export class QueryBuilder { /** * Set an or where not exists statement in your query. */ - public orWhereNotExists(closure: (query: Driver) => void): this { + public orWhereNotExists(closure: (query: Driver) => void) { this.driver.orWhereNotExists(closure) return this @@ -911,7 +891,7 @@ export class QueryBuilder { /** * Set an or where like statement in your query. */ - public orWhereLike(statement: any, value?: any): this { + public orWhereLike(statement: any, value?: any) { this.driver.orWhereLike(statement, value) return this @@ -924,7 +904,7 @@ export class QueryBuilder { /** * Set an or where ILike statement in your query. */ - public orWhereILike(statement: any, value?: any): this { + public orWhereILike(statement: any, value?: any) { this.driver.orWhereILike(statement, value) return this @@ -933,7 +913,7 @@ export class QueryBuilder { /** * Set an or where in statement in your query. */ - public orWhereIn(column: string | ModelColumns, values: any[]): this { + public orWhereIn(column: string | ModelColumns, values: any[]) { this.driver.orWhereIn(column as string, values) return this @@ -942,7 +922,7 @@ export class QueryBuilder { /** * Set an or where not in statement in your query. */ - public orWhereNotIn(column: string | ModelColumns, values: any[]): this { + public orWhereNotIn(column: string | ModelColumns, values: any[]) { this.driver.orWhereNotIn(column as string, values) return this @@ -951,10 +931,7 @@ export class QueryBuilder { /** * Set an or where between statement in your query. */ - public orWhereBetween( - column: string | ModelColumns, - values: [any, any] - ): this { + public orWhereBetween(column: string | ModelColumns, values: [any, any]) { this.driver.orWhereBetween(column as string, values) return this @@ -966,7 +943,7 @@ export class QueryBuilder { public orWhereNotBetween( column: string | ModelColumns, values: [any, any] - ): this { + ) { this.driver.orWhereNotBetween(column as string, values) return this @@ -975,7 +952,7 @@ export class QueryBuilder { /** * Set an or where null statement in your query. */ - public orWhereNull(column: string | ModelColumns): this { + public orWhereNull(column: string | ModelColumns) { this.driver.orWhereNull(column as string) return this @@ -984,7 +961,7 @@ export class QueryBuilder { /** * Set an or where not null statement in your query. */ - public orWhereNotNull(column: string | ModelColumns): this { + public orWhereNotNull(column: string | ModelColumns) { this.driver.orWhereNotNull(column as string) return this @@ -996,7 +973,7 @@ export class QueryBuilder { public orderBy( column: string | ModelColumns, direction: Direction = 'ASC' - ): this { + ) { this.driver.orderBy(column as string, direction.toUpperCase() as Direction) return this @@ -1005,7 +982,7 @@ export class QueryBuilder { /** * Set an order by raw statement in your query. */ - public orderByRaw(sql: string, bindings?: any): this { + public orderByRaw(sql: string, bindings?: any) { this.driver.orderByRaw(sql, bindings) return this @@ -1015,7 +992,7 @@ export class QueryBuilder { * Order the results easily by the latest date. By default, the result will * be ordered by the table's "createdAt" column. */ - public latest(column: string | ModelColumns = 'createdAt'): this { + public latest(column: string | ModelColumns = 'createdAt') { this.driver.latest(column as string) return this @@ -1025,7 +1002,7 @@ export class QueryBuilder { * Order the results easily by the oldest date. By default, the result will * be ordered by the table's "createdAt" column. */ - public oldest(column: string | ModelColumns = 'createdAt'): this { + public oldest(column: string | ModelColumns = 'createdAt') { this.driver.oldest(column as string) return this @@ -1034,7 +1011,7 @@ export class QueryBuilder { /** * Set the skip number in your query. */ - public offset(number: number): this { + public offset(number: number) { this.driver.offset(number) return this @@ -1043,7 +1020,7 @@ export class QueryBuilder { /** * Set the limit number in your query. */ - public limit(number: number): this { + public limit(number: number) { this.driver.limit(number) return this diff --git a/src/database/drivers/Driver.ts b/src/database/drivers/Driver.ts index fe5dc83..cb9ae16 100644 --- a/src/database/drivers/Driver.ts +++ b/src/database/drivers/Driver.ts @@ -87,7 +87,7 @@ export abstract class Driver { /** * Set a client in driver. */ - public setClient(client: Client): this { + public setClient(client: Client) { this.client = client return this @@ -106,7 +106,7 @@ export abstract class Driver { public setQueryBuilder( queryBuilder: QB, options: { useSetQB?: boolean } = {} - ): this { + ) { options = Options.create(options, { useSetQB: false }) @@ -120,7 +120,7 @@ export abstract class Driver { /** * Set the primary key of the driver. */ - public setPrimaryKey(primaryKey: string): this { + public setPrimaryKey(primaryKey: string) { this.primaryKey = primaryKey return this @@ -135,7 +135,7 @@ export abstract class Driver { column1?: string, operation?: string | Operations, column2?: string - ): this { + ) { if (!column1) { this.qb[joinType](table) @@ -415,8 +415,8 @@ export abstract class Driver { */ public when( criteria: any, - closure: (query: this, criteriaValue?: any) => void - ): this { + closure: (query: this, criteriaValue?: any) => any | Promise + ) { if (!criteria) { return this } diff --git a/src/database/drivers/MongoDriver.ts b/src/database/drivers/MongoDriver.ts index 079fcdf..5a43784 100644 --- a/src/database/drivers/MongoDriver.ts +++ b/src/database/drivers/MongoDriver.ts @@ -50,7 +50,7 @@ export class MongoDriver extends Driver { /** * Set the mongo session that should be used by the driver. */ - public setSession(session: ClientSession): this { + public setSession(session: ClientSession) { this.session = session return this @@ -660,7 +660,7 @@ export class MongoDriver extends Driver { /** * Set the table that this query will be executed. */ - public table(table: string): this { + public table(table: string) { if (!this.isConnected) { throw new NotConnectedDatabaseException() } @@ -674,7 +674,7 @@ export class MongoDriver extends Driver { /** * Log in console the actual query built. */ - public dump(): this { + public dump() { console.log({ where: this._where, orWhere: this._orWhere, @@ -687,7 +687,7 @@ export class MongoDriver extends Driver { /** * Set the columns that should be selected on query. */ - public select(...columns: string[]): this { + public select(...columns: string[]) { if (columns.includes('*')) { return this } @@ -743,7 +743,7 @@ export class MongoDriver extends Driver { /** * Set the columns that should be selected on query raw. */ - public selectRaw(): this { + public selectRaw() { throw new NotImplementedMethodException(this.selectRaw.name, 'mongo') } @@ -752,7 +752,7 @@ export class MongoDriver extends Driver { * Different from `table()` method, this method * doesn't change the driver table. */ - public from(): this { + public from() { throw new NotImplementedMethodException(this.from.name, 'mongo') } @@ -761,7 +761,7 @@ export class MongoDriver extends Driver { * Different from `table()` method, this method * doesn't change the driver table. */ - public fromRaw(): this { + public fromRaw() { throw new NotImplementedMethodException(this.selectRaw.name, 'mongo') } @@ -773,7 +773,7 @@ export class MongoDriver extends Driver { column1?: any, operation?: any | Operations, column2?: any - ): this { + ) { let foreignField = column2 || operation || this.primaryKey if (foreignField.includes('.')) { @@ -801,7 +801,7 @@ export class MongoDriver extends Driver { column1?: any, operation?: any | Operations, column2?: any - ): this { + ) { return this.join(table, column1, operation, column2) } @@ -813,7 +813,7 @@ export class MongoDriver extends Driver { column1?: any, operation?: any | Operations, column2?: any - ): this { + ) { return this.join(table, column1, operation, column2) } @@ -825,7 +825,7 @@ export class MongoDriver extends Driver { column1?: any, operation?: any | Operations, column2?: any - ): this { + ) { return this.join(table, column1, operation, column2) } @@ -837,7 +837,7 @@ export class MongoDriver extends Driver { column1?: any, operation?: any | Operations, column2?: any - ): this { + ) { return this.join(table, column1, operation, column2) } @@ -849,7 +849,7 @@ export class MongoDriver extends Driver { column1?: any, operation?: any | Operations, column2?: any - ): this { + ) { return this.join(table, column1, operation, column2) } @@ -861,21 +861,21 @@ export class MongoDriver extends Driver { column1?: any, operation?: any | Operations, column2?: any - ): this { + ) { return this.join(table, column1, operation, column2) } /** * Set a join raw statement in your query. */ - public joinRaw(): this { + public joinRaw() { throw new NotImplementedMethodException(this.joinRaw.name, 'mongo') } /** * Set a group by statement in your query. */ - public groupBy(...columns: string[]): this { + public groupBy(...columns: string[]) { const $group = { [this.primaryKey]: {} } columns.forEach(column => ($group[this.primaryKey][column] = `$${column}`)) @@ -889,7 +889,7 @@ export class MongoDriver extends Driver { /** * Set a group by raw statement in your query. */ - public groupByRaw(): this { + public groupByRaw() { throw new NotImplementedMethodException(this.groupByRaw.name, 'mongo') } @@ -900,70 +900,70 @@ export class MongoDriver extends Driver { /** * Set a having statement in your query. */ - public having(column: any, operation?: Operations, value?: any): this { + public having(column: any, operation?: Operations, value?: any) { return this.where(column, operation, value) } /** * Set a having raw statement in your query. */ - public havingRaw(): this { + public havingRaw() { throw new NotImplementedMethodException(this.havingRaw.name, 'mongo') } /** * Set a having exists statement in your query. */ - public havingExists(): this { + public havingExists() { throw new NotImplementedMethodException(this.havingExists.name, 'mongo') } /** * Set a having not exists statement in your query. */ - public havingNotExists(): this { + public havingNotExists() { throw new NotImplementedMethodException(this.havingNotExists.name, 'mongo') } /** * Set a having in statement in your query. */ - public havingIn(column: string, values: any[]): this { + public havingIn(column: string, values: any[]) { return this.whereIn(column, values) } /** * Set a having not in statement in your query. */ - public havingNotIn(column: string, values: any[]): this { + public havingNotIn(column: string, values: any[]) { return this.whereNotIn(column, values) } /** * Set a having between statement in your query. */ - public havingBetween(column: string, values: [any, any]): this { + public havingBetween(column: string, values: [any, any]) { return this.whereBetween(column, values) } /** * Set a having not between statement in your query. */ - public havingNotBetween(column: string, values: [any, any]): this { + public havingNotBetween(column: string, values: [any, any]) { return this.whereNotBetween(column, values) } /** * Set a having null statement in your query. */ - public havingNull(column: string): this { + public havingNull(column: string) { return this.whereNull(column) } /** * Set a having not null statement in your query. */ - public havingNotNull(column: string): this { + public havingNotNull(column: string) { return this.whereNotNull(column) } @@ -974,28 +974,28 @@ export class MongoDriver extends Driver { /** * Set an or having statement in your query. */ - public orHaving(column: any, operation?: Operations, value?: any): this { + public orHaving(column: any, operation?: Operations, value?: any) { return this.orWhere(column, operation, value) } /** * Set an or having raw statement in your query. */ - public orHavingRaw(): this { + public orHavingRaw() { throw new NotImplementedMethodException(this.orHavingRaw.name, 'mongo') } /** * Set an or having exists statement in your query. */ - public orHavingExists(): this { + public orHavingExists() { throw new NotImplementedMethodException(this.orHavingExists.name, 'mongo') } /** * Set an or having not exists statement in your query. */ - public orHavingNotExists(): this { + public orHavingNotExists() { throw new NotImplementedMethodException( this.orHavingNotExists.name, 'mongo' @@ -1005,42 +1005,42 @@ export class MongoDriver extends Driver { /** * Set an or having in statement in your query. */ - public orHavingIn(column: string, values: any[]): this { + public orHavingIn(column: string, values: any[]) { return this.orWhereIn(column, values) } /** * Set an or having not in statement in your query. */ - public orHavingNotIn(column: string, values: any[]): this { + public orHavingNotIn(column: string, values: any[]) { return this.orWhereNotIn(column, values) } /** * Set an or having between statement in your query. */ - public orHavingBetween(column: string, values: [any, any]): this { + public orHavingBetween(column: string, values: [any, any]) { return this.orWhereBetween(column, values) } /** * Set an or having not between statement in your query. */ - public orHavingNotBetween(column: string, values: [any, any]): this { + public orHavingNotBetween(column: string, values: [any, any]) { return this.orWhereNotBetween(column, values) } /** * Set an or having null statement in your query. */ - public orHavingNull(column: string): this { + public orHavingNull(column: string) { return this.whereNull(column) } /** * Set an or having not null statement in your query. */ - public orHavingNotNull(column: string): this { + public orHavingNotNull(column: string) { return this.whereNotNull(column) } @@ -1051,7 +1051,7 @@ export class MongoDriver extends Driver { /** * Set a where statement in your query. */ - public where(statement: any, operation?: Operations, value?: any): this { + public where(statement: any, operation?: Operations, value?: any) { if (Is.Function(statement)) { statement(this) @@ -1084,49 +1084,49 @@ export class MongoDriver extends Driver { /** * Set a where not statement in your query. */ - public whereNot(statement: any, value?: any): this { + public whereNot(statement: any, value?: any) { return this.where(statement, '<>', value) } /** * Set a where raw statement in your query. */ - public whereRaw(): this { + public whereRaw() { throw new NotImplementedMethodException(this.whereRaw.name, 'mongo') } /** * Set a where exists statement in your query. */ - public whereExists(): this { + public whereExists() { throw new NotImplementedMethodException(this.whereExists.name, 'mongo') } /** * Set a where not exists statement in your query. */ - public whereNotExists(): this { + public whereNotExists() { throw new NotImplementedMethodException(this.whereNotExists.name, 'mongo') } /** * Set a where like statement in your query. */ - public whereLike(column: string, value: any): this { + public whereLike(column: string, value: any) { return this.where(column, 'like', value) } /** * Set a where ILike statement in your query. */ - public whereILike(column: string, value: any): this { + public whereILike(column: string, value: any) { return this.where(column, 'ilike', value) } /** * Set a where in statement in your query. */ - public whereIn(column: string, values: any[]): this { + public whereIn(column: string, values: any[]) { this._where[column] = { $in: values } return this @@ -1135,7 +1135,7 @@ export class MongoDriver extends Driver { /** * Set a where not in statement in your query. */ - public whereNotIn(column: string, values: any[]): this { + public whereNotIn(column: string, values: any[]) { this._where[column] = { $nin: values } return this @@ -1144,7 +1144,7 @@ export class MongoDriver extends Driver { /** * Set a where between statement in your query. */ - public whereBetween(column: string, values: [any, any]): this { + public whereBetween(column: string, values: [any, any]) { this._where[column] = { $gte: values[0], $lte: values[1] } return this @@ -1153,7 +1153,7 @@ export class MongoDriver extends Driver { /** * Set a where not between statement in your query. */ - public whereNotBetween(column: string, values: [any, any]): this { + public whereNotBetween(column: string, values: [any, any]) { this._where[column] = { $not: { $gte: values[0], $lte: values[1] } } return this @@ -1162,7 +1162,7 @@ export class MongoDriver extends Driver { /** * Set a where null statement in your query. */ - public whereNull(column: string): this { + public whereNull(column: string) { this._where[column] = null return this @@ -1171,7 +1171,7 @@ export class MongoDriver extends Driver { /** * Set a where not null statement in your query. */ - public whereNotNull(column: string): this { + public whereNotNull(column: string) { this._where[column] = { $ne: null } return this @@ -1184,7 +1184,7 @@ export class MongoDriver extends Driver { /** * Set a or where statement in your query. */ - public orWhere(statement: any, operation?: Operations, value?: any): this { + public orWhere(statement: any, operation?: Operations, value?: any) { if (Is.Function(statement)) { statement(this) @@ -1214,49 +1214,49 @@ export class MongoDriver extends Driver { /** * Set an or where not statement in your query. */ - public orWhereNot(statement: any, value?: any): this { + public orWhereNot(statement: any, value?: any) { return this.orWhere(statement, '<>', value) } /** * Set a or where raw statement in your query. */ - public orWhereRaw(): this { + public orWhereRaw() { throw new NotImplementedMethodException(this.orWhereRaw.name, 'mongo') } /** * Set an or where exists statement in your query. */ - public orWhereExists(): this { + public orWhereExists() { throw new NotImplementedMethodException(this.orWhereExists.name, 'mongo') } /** * Set an or where not exists statement in your query. */ - public orWhereNotExists(): this { + public orWhereNotExists() { throw new NotImplementedMethodException(this.orWhereNotExists.name, 'mongo') } /** * Set an or where like statement in your query. */ - public orWhereLike(column: string, value: any): this { + public orWhereLike(column: string, value: any) { return this.orWhere(column, 'like', value) } /** * Set an or where ILike statement in your query. */ - public orWhereILike(column: string, value: any): this { + public orWhereILike(column: string, value: any) { return this.orWhere(column, 'ilike', value) } /** * Set an or where in statement in your query. */ - public orWhereIn(column: string, values: any[]): this { + public orWhereIn(column: string, values: any[]) { this._orWhere.push({ [column]: { $in: values } }) return this @@ -1265,7 +1265,7 @@ export class MongoDriver extends Driver { /** * Set an or where not in statement in your query. */ - public orWhereNotIn(column: string, values: any[]): this { + public orWhereNotIn(column: string, values: any[]) { this._orWhere.push({ [column]: { $nin: values } }) return this @@ -1274,7 +1274,7 @@ export class MongoDriver extends Driver { /** * Set an or where between statement in your query. */ - public orWhereBetween(column: string, values: [any, any]): this { + public orWhereBetween(column: string, values: [any, any]) { this._orWhere.push({ [column]: { $gte: values[0], $lte: values[1] } }) return this @@ -1283,7 +1283,7 @@ export class MongoDriver extends Driver { /** * Set an or where not between statement in your query. */ - public orWhereNotBetween(column: string, values: [any, any]): this { + public orWhereNotBetween(column: string, values: [any, any]) { this._orWhere.push({ [column]: { $not: { $gte: values[0], $lte: values[1] } } }) @@ -1294,7 +1294,7 @@ export class MongoDriver extends Driver { /** * Set an or where null statement in your query. */ - public orWhereNull(column: string): this { + public orWhereNull(column: string) { this._orWhere.push({ [column]: null }) return this @@ -1303,7 +1303,7 @@ export class MongoDriver extends Driver { /** * Set an or where not null statement in your query. */ - public orWhereNotNull(column: string): this { + public orWhereNotNull(column: string) { this._orWhere.push({ [column]: { $ne: null } }) return this @@ -1312,7 +1312,7 @@ export class MongoDriver extends Driver { /** * Set an order by statement in your query. */ - public orderBy(column: string, direction: Direction = 'ASC'): this { + public orderBy(column: string, direction: Direction = 'ASC') { this.pipeline.push({ $sort: { [column]: direction.toLowerCase() === 'asc' ? 1 : -1 } }) @@ -1323,7 +1323,7 @@ export class MongoDriver extends Driver { /** * Set an order by raw statement in your query. */ - public orderByRaw(): this { + public orderByRaw() { throw new NotImplementedMethodException(this.orderByRaw.name, 'mongo') } @@ -1331,7 +1331,7 @@ export class MongoDriver extends Driver { * Order the results easily by the latest date. By default, the result will * be ordered by the table's "createdAt" column. */ - public latest(column: string = 'createdAt'): this { + public latest(column: string = 'createdAt') { return this.orderBy(column, 'DESC') } @@ -1339,14 +1339,14 @@ export class MongoDriver extends Driver { * Order the results easily by the oldest date. By default, the result will * be ordered by the table's "createdAt" column. */ - public oldest(column: string = 'createdAt'): this { + public oldest(column: string = 'createdAt') { return this.orderBy(column, 'ASC') } /** * Set the skip number in your query. */ - public offset(number: number): this { + public offset(number: number) { this.pipeline.push({ $skip: number }) return this @@ -1355,7 +1355,7 @@ export class MongoDriver extends Driver { /** * Set the limit number in your query. */ - public limit(number: number): this { + public limit(number: number) { this.pipeline.push({ $limit: number }) return this diff --git a/src/database/drivers/MySqlDriver.ts b/src/database/drivers/MySqlDriver.ts index 9f4788e..c4995a7 100644 --- a/src/database/drivers/MySqlDriver.ts +++ b/src/database/drivers/MySqlDriver.ts @@ -461,7 +461,7 @@ export class MySqlDriver extends Driver { /** * Set the table that this query will be executed. */ - public table(table: string): this { + public table(table: string) { if (!this.isConnected) { throw new NotConnectedDatabaseException() } @@ -475,7 +475,7 @@ export class MySqlDriver extends Driver { /** * Log in console the actual query built. */ - public dump(): this { + public dump() { console.log(this.qb.toSQL().toNative()) return this @@ -484,7 +484,7 @@ export class MySqlDriver extends Driver { /** * Set the columns that should be selected on query. */ - public select(...columns: string[]): this { + public select(...columns: string[]) { this.qb.select(...columns) return this @@ -493,7 +493,7 @@ export class MySqlDriver extends Driver { /** * Set the columns that should be selected on query raw. */ - public selectRaw(sql: string, bindings?: any): this { + public selectRaw(sql: string, bindings?: any) { return this.select(this.raw(sql, bindings) as any) } @@ -502,7 +502,7 @@ export class MySqlDriver extends Driver { * Different from `table()` method, this method * doesn't change the driver table. */ - public from(table: string): this { + public from(table: string) { this.qb.from(table) return this @@ -513,7 +513,7 @@ export class MySqlDriver extends Driver { * Different from `table()` method, this method * doesn't change the driver table. */ - public fromRaw(sql: string, bindings?: any): this { + public fromRaw(sql: string, bindings?: any) { return this.from(this.raw(sql, bindings) as any) } @@ -525,7 +525,7 @@ export class MySqlDriver extends Driver { column1?: any, operation?: any | Operations, column2?: any - ): this { + ) { return this.joinByType('join', table, column1, operation, column2) } @@ -537,7 +537,7 @@ export class MySqlDriver extends Driver { column1?: any, operation?: any | Operations, column2?: any - ): this { + ) { return this.joinByType('leftJoin', table, column1, operation, column2) } @@ -549,7 +549,7 @@ export class MySqlDriver extends Driver { column1?: any, operation?: any | Operations, column2?: any - ): this { + ) { return this.joinByType('rightJoin', table, column1, operation, column2) } @@ -561,7 +561,7 @@ export class MySqlDriver extends Driver { column1?: any, operation?: any | Operations, column2?: any - ): this { + ) { return this.joinByType('crossJoin', table, column1, operation, column2) } @@ -573,7 +573,7 @@ export class MySqlDriver extends Driver { column1?: any, operation?: any | Operations, column2?: any - ): this { + ) { // TODO https://github.com/knex/knex/issues/3949 return this.joinByType('leftJoin', table, column1, operation, column2) } @@ -586,7 +586,7 @@ export class MySqlDriver extends Driver { column1?: any, operation?: any | Operations, column2?: any - ): this { + ) { return this.joinByType('leftOuterJoin', table, column1, operation, column2) } @@ -598,14 +598,14 @@ export class MySqlDriver extends Driver { column1?: any, operation?: any | Operations, column2?: any - ): this { + ) { return this.joinByType('rightOuterJoin', table, column1, operation, column2) } /** * Set a join raw statement in your query. */ - public joinRaw(sql: string, bindings?: any): this { + public joinRaw(sql: string, bindings?: any) { this.qb.joinRaw(sql, bindings) return this @@ -614,7 +614,7 @@ export class MySqlDriver extends Driver { /** * Set a group by statement in your query. */ - public groupBy(...columns: string[]): this { + public groupBy(...columns: string[]) { this.qb.groupBy(...columns) return this @@ -623,7 +623,7 @@ export class MySqlDriver extends Driver { /** * Set a group by raw statement in your query. */ - public groupByRaw(sql: string, bindings?: any): this { + public groupByRaw(sql: string, bindings?: any) { this.qb.groupByRaw(sql, bindings) return this @@ -636,7 +636,7 @@ export class MySqlDriver extends Driver { /** * Set a having statement in your query. */ - public having(column: any, operation?: Operations, value?: any): this { + public having(column: any, operation?: Operations, value?: any) { if (operation === undefined) { this.qb.having(column) @@ -657,7 +657,7 @@ export class MySqlDriver extends Driver { /** * Set a having raw statement in your query. */ - public havingRaw(sql: string, bindings?: any): this { + public havingRaw(sql: string, bindings?: any) { this.qb.havingRaw(sql, bindings) return this @@ -666,7 +666,7 @@ export class MySqlDriver extends Driver { /** * Set a having exists statement in your query. */ - public havingExists(closure: (query: MySqlDriver) => void): this { + public havingExists(closure: (query: MySqlDriver) => void) { const driver = this.clone() as MySqlDriver // @ts-ignore @@ -680,7 +680,7 @@ export class MySqlDriver extends Driver { /** * Set a having not exists statement in your query. */ - public havingNotExists(closure: (query: MySqlDriver) => void): this { + public havingNotExists(closure: (query: MySqlDriver) => void) { const driver = this.clone() as MySqlDriver // @ts-ignore @@ -694,7 +694,7 @@ export class MySqlDriver extends Driver { /** * Set a having in statement in your query. */ - public havingIn(column: string, values: any[]): this { + public havingIn(column: string, values: any[]) { this.qb.havingIn(column, values) return this @@ -703,7 +703,7 @@ export class MySqlDriver extends Driver { /** * Set a having not in statement in your query. */ - public havingNotIn(column: string, values: any[]): this { + public havingNotIn(column: string, values: any[]) { this.qb.havingNotIn(column, values) return this @@ -712,7 +712,7 @@ export class MySqlDriver extends Driver { /** * Set a having between statement in your query. */ - public havingBetween(column: string, values: [any, any]): this { + public havingBetween(column: string, values: [any, any]) { this.qb.havingBetween(column, values) return this @@ -721,7 +721,7 @@ export class MySqlDriver extends Driver { /** * Set a having not between statement in your query. */ - public havingNotBetween(column: string, values: [any, any]): this { + public havingNotBetween(column: string, values: [any, any]) { this.qb.havingNotBetween(column, values) return this @@ -730,7 +730,7 @@ export class MySqlDriver extends Driver { /** * Set a having null statement in your query. */ - public havingNull(column: string): this { + public havingNull(column: string) { this.qb.havingNull(column) return this @@ -739,7 +739,7 @@ export class MySqlDriver extends Driver { /** * Set a having not null statement in your query. */ - public havingNotNull(column: string): this { + public havingNotNull(column: string) { this.qb.havingNotNull(column) return this @@ -752,7 +752,7 @@ export class MySqlDriver extends Driver { /** * Set an or having statement in your query. */ - public orHaving(column: any, operation?: Operations, value?: any): this { + public orHaving(column: any, operation?: Operations, value?: any) { if (operation === undefined) { this.qb.orHaving(column) @@ -773,7 +773,7 @@ export class MySqlDriver extends Driver { /** * Set an or having raw statement in your query. */ - public orHavingRaw(sql: string, bindings?: any): this { + public orHavingRaw(sql: string, bindings?: any) { this.qb.orHavingRaw(sql, bindings) return this @@ -782,7 +782,7 @@ export class MySqlDriver extends Driver { /** * Set an or having exists statement in your query. */ - public orHavingExists(closure: (query: MySqlDriver) => void): this { + public orHavingExists(closure: (query: MySqlDriver) => void) { const driver = this.clone() as MySqlDriver // @ts-ignore @@ -796,7 +796,7 @@ export class MySqlDriver extends Driver { /** * Set an or having not exists statement in your query. */ - public orHavingNotExists(closure: (query: MySqlDriver) => void): this { + public orHavingNotExists(closure: (query: MySqlDriver) => void) { const driver = this.clone() as MySqlDriver // @ts-ignore @@ -810,7 +810,7 @@ export class MySqlDriver extends Driver { /** * Set an or having in statement in your query. */ - public orHavingIn(column: string, values: any[]): this { + public orHavingIn(column: string, values: any[]) { // @ts-ignore this.qb.orHavingIn(column, values) @@ -820,7 +820,7 @@ export class MySqlDriver extends Driver { /** * Set an or having not in statement in your query. */ - public orHavingNotIn(column: string, values: any[]): this { + public orHavingNotIn(column: string, values: any[]) { this.qb.orHavingNotIn(column, values) return this @@ -829,7 +829,7 @@ export class MySqlDriver extends Driver { /** * Set an or having between statement in your query. */ - public orHavingBetween(column: string, values: [any, any]): this { + public orHavingBetween(column: string, values: [any, any]) { this.qb.orHavingBetween(column, values) return this @@ -838,7 +838,7 @@ export class MySqlDriver extends Driver { /** * Set an or having not between statement in your query. */ - public orHavingNotBetween(column: string, values: [any, any]): this { + public orHavingNotBetween(column: string, values: [any, any]) { this.qb.orHavingNotBetween(column, values) return this @@ -847,7 +847,7 @@ export class MySqlDriver extends Driver { /** * Set an or having null statement in your query. */ - public orHavingNull(column: string): this { + public orHavingNull(column: string) { // @ts-ignore this.qb.orHavingNull(column) @@ -857,7 +857,7 @@ export class MySqlDriver extends Driver { /** * Set an or having not null statement in your query. */ - public orHavingNotNull(column: string): this { + public orHavingNotNull(column: string) { // @ts-ignore this.qb.orHavingNotNull(column) @@ -871,7 +871,7 @@ export class MySqlDriver extends Driver { /** * Set a where statement in your query. */ - public where(statement: any, operation?: Operations, value?: any): this { + public where(statement: any, operation?: Operations, value?: any) { if (Is.Function(statement)) { const driver = this.clone() @@ -905,7 +905,7 @@ export class MySqlDriver extends Driver { /** * Set a where not statement in your query. */ - public whereNot(statement: any, value?: any): this { + public whereNot(statement: any, value?: any) { if (Is.Function(statement)) { const driver = this.clone() @@ -930,7 +930,7 @@ export class MySqlDriver extends Driver { /** * Set a where raw statement in your query. */ - public whereRaw(sql: string, bindings?: any): this { + public whereRaw(sql: string, bindings?: any) { this.qb.whereRaw(sql, bindings) return this @@ -939,7 +939,7 @@ export class MySqlDriver extends Driver { /** * Set a where exists statement in your query. */ - public whereExists(closure: (query: MySqlDriver) => void): this { + public whereExists(closure: (query: MySqlDriver) => void) { const driver = this.clone() as MySqlDriver this.qb.whereExists(function () { @@ -952,7 +952,7 @@ export class MySqlDriver extends Driver { /** * Set a where not exists statement in your query. */ - public whereNotExists(closure: (query: MySqlDriver) => void): this { + public whereNotExists(closure: (query: MySqlDriver) => void) { const driver = this.clone() as MySqlDriver this.qb.whereNotExists(function () { @@ -965,7 +965,7 @@ export class MySqlDriver extends Driver { /** * Set a where like statement in your query. */ - public whereLike(column: string, value: any): this { + public whereLike(column: string, value: any) { this.qb.whereLike(column, value) return this @@ -974,7 +974,7 @@ export class MySqlDriver extends Driver { /** * Set a where ILike statement in your query. */ - public whereILike(column: string, value: any): this { + public whereILike(column: string, value: any) { this.qb.whereILike(column, value) return this @@ -983,7 +983,7 @@ export class MySqlDriver extends Driver { /** * Set a where in statement in your query. */ - public whereIn(column: string, values: any[]): this { + public whereIn(column: string, values: any[]) { this.qb.whereIn(column, values) return this @@ -992,7 +992,7 @@ export class MySqlDriver extends Driver { /** * Set a where not in statement in your query. */ - public whereNotIn(column: string, values: any[]): this { + public whereNotIn(column: string, values: any[]) { this.qb.whereNotIn(column, values) return this @@ -1001,7 +1001,7 @@ export class MySqlDriver extends Driver { /** * Set a where between statement in your query. */ - public whereBetween(column: string, values: [any, any]): this { + public whereBetween(column: string, values: [any, any]) { this.qb.whereBetween(column, values) return this @@ -1010,7 +1010,7 @@ export class MySqlDriver extends Driver { /** * Set a where not between statement in your query. */ - public whereNotBetween(column: string, values: [any, any]): this { + public whereNotBetween(column: string, values: [any, any]) { this.qb.whereNotBetween(column, values) return this @@ -1019,7 +1019,7 @@ export class MySqlDriver extends Driver { /** * Set a where null statement in your query. */ - public whereNull(column: string): this { + public whereNull(column: string) { this.qb.whereNull(column) return this @@ -1028,7 +1028,7 @@ export class MySqlDriver extends Driver { /** * Set a where not null statement in your query. */ - public whereNotNull(column: string): this { + public whereNotNull(column: string) { this.qb.whereNotNull(column) return this @@ -1041,7 +1041,7 @@ export class MySqlDriver extends Driver { /** * Set a or where statement in your query. */ - public orWhere(statement: any, operation?: Operations, value?: any): this { + public orWhere(statement: any, operation?: Operations, value?: any) { if (Is.Function(statement)) { const driver = this.clone() @@ -1075,7 +1075,7 @@ export class MySqlDriver extends Driver { /** * Set an or where not statement in your query. */ - public orWhereNot(statement: any, value?: any): this { + public orWhereNot(statement: any, value?: any) { if (Is.Function(statement)) { const driver = this.clone() @@ -1100,7 +1100,7 @@ export class MySqlDriver extends Driver { /** * Set a or where raw statement in your query. */ - public orWhereRaw(sql: string, bindings?: any): this { + public orWhereRaw(sql: string, bindings?: any) { this.qb.orWhereRaw(sql, bindings) return this @@ -1109,7 +1109,7 @@ export class MySqlDriver extends Driver { /** * Set an or where exists statement in your query. */ - public orWhereExists(closure: (query: MySqlDriver) => void): this { + public orWhereExists(closure: (query: MySqlDriver) => void) { const driver = this.clone() as MySqlDriver this.qb.orWhereExists(function () { @@ -1122,7 +1122,7 @@ export class MySqlDriver extends Driver { /** * Set an or where not exists statement in your query. */ - public orWhereNotExists(closure: (query: MySqlDriver) => void): this { + public orWhereNotExists(closure: (query: MySqlDriver) => void) { const driver = this.clone() as MySqlDriver this.qb.orWhereNotExists(function () { @@ -1135,7 +1135,7 @@ export class MySqlDriver extends Driver { /** * Set an or where like statement in your query. */ - public orWhereLike(column: string, value: any): this { + public orWhereLike(column: string, value: any) { this.qb.orWhereLike(column, value) return this @@ -1144,7 +1144,7 @@ export class MySqlDriver extends Driver { /** * Set an or where ILike statement in your query. */ - public orWhereILike(column: string, value: any): this { + public orWhereILike(column: string, value: any) { this.qb.orWhereILike(column, value) return this @@ -1153,7 +1153,7 @@ export class MySqlDriver extends Driver { /** * Set an or where in statement in your query. */ - public orWhereIn(column: string, values: any[]): this { + public orWhereIn(column: string, values: any[]) { this.qb.orWhereIn(column, values) return this @@ -1162,7 +1162,7 @@ export class MySqlDriver extends Driver { /** * Set an or where not in statement in your query. */ - public orWhereNotIn(column: string, values: any[]): this { + public orWhereNotIn(column: string, values: any[]) { this.qb.orWhereNotIn(column, values) return this @@ -1171,7 +1171,7 @@ export class MySqlDriver extends Driver { /** * Set an or where between statement in your query. */ - public orWhereBetween(column: string, values: [any, any]): this { + public orWhereBetween(column: string, values: [any, any]) { this.qb.orWhereBetween(column, values) return this @@ -1180,7 +1180,7 @@ export class MySqlDriver extends Driver { /** * Set an or where not between statement in your query. */ - public orWhereNotBetween(column: string, values: [any, any]): this { + public orWhereNotBetween(column: string, values: [any, any]) { this.qb.orWhereNotBetween(column, values) return this @@ -1189,7 +1189,7 @@ export class MySqlDriver extends Driver { /** * Set an or where null statement in your query. */ - public orWhereNull(column: string): this { + public orWhereNull(column: string) { this.qb.orWhereNull(column) return this @@ -1198,7 +1198,7 @@ export class MySqlDriver extends Driver { /** * Set an or where not null statement in your query. */ - public orWhereNotNull(column: string): this { + public orWhereNotNull(column: string) { this.qb.orWhereNotNull(column) return this @@ -1207,7 +1207,7 @@ export class MySqlDriver extends Driver { /** * Set an order by statement in your query. */ - public orderBy(column: string, direction: Direction = 'ASC'): this { + public orderBy(column: string, direction: Direction = 'ASC') { this.qb.orderBy(column, direction.toUpperCase()) return this @@ -1216,7 +1216,7 @@ export class MySqlDriver extends Driver { /** * Set an order by raw statement in your query. */ - public orderByRaw(sql: string, bindings?: any): this { + public orderByRaw(sql: string, bindings?: any) { this.qb.orderByRaw(sql, bindings) return this @@ -1226,7 +1226,7 @@ export class MySqlDriver extends Driver { * Order the results easily by the latest date. By default, the result will * be ordered by the table's "createdAt" column. */ - public latest(column: string = 'createdAt'): this { + public latest(column: string = 'createdAt') { return this.orderBy(column, 'DESC') } @@ -1234,14 +1234,14 @@ export class MySqlDriver extends Driver { * Order the results easily by the oldest date. By default, the result will * be ordered by the table's "createdAt" column. */ - public oldest(column: string = 'createdAt'): this { + public oldest(column: string = 'createdAt') { return this.orderBy(column, 'ASC') } /** * Set the skip number in your query. */ - public offset(number: number): this { + public offset(number: number) { this.qb.offset(number) return this @@ -1250,7 +1250,7 @@ export class MySqlDriver extends Driver { /** * Set the limit number in your query. */ - public limit(number: number): this { + public limit(number: number) { this.qb.limit(number) return this diff --git a/src/database/drivers/PostgresDriver.ts b/src/database/drivers/PostgresDriver.ts index 14a9dfd..b25f18c 100644 --- a/src/database/drivers/PostgresDriver.ts +++ b/src/database/drivers/PostgresDriver.ts @@ -456,7 +456,7 @@ export class PostgresDriver extends Driver { /** * Set the table that this query will be executed. */ - public table(table: string): this { + public table(table: string) { if (!this.isConnected) { throw new NotConnectedDatabaseException() } @@ -470,7 +470,7 @@ export class PostgresDriver extends Driver { /** * Log in console the actual query built. */ - public dump(): this { + public dump() { console.log(this.qb.toSQL().toNative()) return this @@ -479,7 +479,7 @@ export class PostgresDriver extends Driver { /** * Set the columns that should be selected on query. */ - public select(...columns: string[]): this { + public select(...columns: string[]) { this.qb.select(...columns) return this @@ -488,7 +488,7 @@ export class PostgresDriver extends Driver { /** * Set the columns that should be selected on query raw. */ - public selectRaw(sql: string, bindings?: any): this { + public selectRaw(sql: string, bindings?: any) { return this.select(this.raw(sql, bindings) as any) } @@ -497,7 +497,7 @@ export class PostgresDriver extends Driver { * Different from `table()` method, this method * doesn't change the driver table. */ - public from(table: string): this { + public from(table: string) { this.qb.from(table) return this @@ -508,7 +508,7 @@ export class PostgresDriver extends Driver { * Different from `table()` method, this method * doesn't change the driver table. */ - public fromRaw(sql: string, bindings?: any): this { + public fromRaw(sql: string, bindings?: any) { return this.from(this.raw(sql, bindings) as any) } @@ -520,7 +520,7 @@ export class PostgresDriver extends Driver { column1?: any, operation?: any | Operations, column2?: any - ): this { + ) { return this.joinByType('join', table, column1, operation, column2) } @@ -532,7 +532,7 @@ export class PostgresDriver extends Driver { column1?: any, operation?: any | Operations, column2?: any - ): this { + ) { return this.joinByType('leftJoin', table, column1, operation, column2) } @@ -544,7 +544,7 @@ export class PostgresDriver extends Driver { column1?: any, operation?: any | Operations, column2?: any - ): this { + ) { return this.joinByType('rightJoin', table, column1, operation, column2) } @@ -556,7 +556,7 @@ export class PostgresDriver extends Driver { column1?: any, operation?: any | Operations, column2?: any - ): this { + ) { return this.joinByType('crossJoin', table, column1, operation, column2) } @@ -568,7 +568,7 @@ export class PostgresDriver extends Driver { column1?: any, operation?: any | Operations, column2?: any - ): this { + ) { return this.joinByType('fullOuterJoin', table, column1, operation, column2) } @@ -580,7 +580,7 @@ export class PostgresDriver extends Driver { column1?: any, operation?: any | Operations, column2?: any - ): this { + ) { return this.joinByType('leftOuterJoin', table, column1, operation, column2) } @@ -592,14 +592,14 @@ export class PostgresDriver extends Driver { column1?: any, operation?: any | Operations, column2?: any - ): this { + ) { return this.joinByType('rightOuterJoin', table, column1, operation, column2) } /** * Set a join raw statement in your query. */ - public joinRaw(sql: string, bindings?: any): this { + public joinRaw(sql: string, bindings?: any) { this.qb.joinRaw(sql, bindings) return this @@ -608,7 +608,7 @@ export class PostgresDriver extends Driver { /** * Set a group by statement in your query. */ - public groupBy(...columns: string[]): this { + public groupBy(...columns: string[]) { this.qb.groupBy(...columns) return this @@ -617,7 +617,7 @@ export class PostgresDriver extends Driver { /** * Set a group by raw statement in your query. */ - public groupByRaw(sql: string, bindings?: any): this { + public groupByRaw(sql: string, bindings?: any) { this.qb.groupByRaw(sql, bindings) return this @@ -630,7 +630,7 @@ export class PostgresDriver extends Driver { /** * Set a having statement in your query. */ - public having(column: any, operation?: Operations, value?: any): this { + public having(column: any, operation?: Operations, value?: any) { if (operation === undefined) { this.qb.having(column) @@ -651,7 +651,7 @@ export class PostgresDriver extends Driver { /** * Set a having raw statement in your query. */ - public havingRaw(sql: string, bindings?: any): this { + public havingRaw(sql: string, bindings?: any) { this.qb.havingRaw(sql, bindings) return this @@ -660,7 +660,7 @@ export class PostgresDriver extends Driver { /** * Set a having exists statement in your query. */ - public havingExists(closure: (query: PostgresDriver) => void): this { + public havingExists(closure: (query: PostgresDriver) => void) { const driver = this.clone() as PostgresDriver // @ts-ignore @@ -674,7 +674,7 @@ export class PostgresDriver extends Driver { /** * Set a having not exists statement in your query. */ - public havingNotExists(closure: (query: PostgresDriver) => void): this { + public havingNotExists(closure: (query: PostgresDriver) => void) { const driver = this.clone() as PostgresDriver // @ts-ignore @@ -688,7 +688,7 @@ export class PostgresDriver extends Driver { /** * Set a having in statement in your query. */ - public havingIn(column: string, values: any[]): this { + public havingIn(column: string, values: any[]) { this.qb.havingIn(column, values) return this @@ -697,7 +697,7 @@ export class PostgresDriver extends Driver { /** * Set a having not in statement in your query. */ - public havingNotIn(column: string, values: any[]): this { + public havingNotIn(column: string, values: any[]) { this.qb.havingNotIn(column, values) return this @@ -706,7 +706,7 @@ export class PostgresDriver extends Driver { /** * Set a having between statement in your query. */ - public havingBetween(column: string, values: [any, any]): this { + public havingBetween(column: string, values: [any, any]) { this.qb.havingBetween(column, values) return this @@ -715,7 +715,7 @@ export class PostgresDriver extends Driver { /** * Set a having not between statement in your query. */ - public havingNotBetween(column: string, values: [any, any]): this { + public havingNotBetween(column: string, values: [any, any]) { this.qb.havingNotBetween(column, values) return this @@ -724,7 +724,7 @@ export class PostgresDriver extends Driver { /** * Set a having null statement in your query. */ - public havingNull(column: string): this { + public havingNull(column: string) { this.qb.havingNull(column) return this @@ -733,7 +733,7 @@ export class PostgresDriver extends Driver { /** * Set a having not null statement in your query. */ - public havingNotNull(column: string): this { + public havingNotNull(column: string) { this.qb.havingNotNull(column) return this @@ -746,7 +746,7 @@ export class PostgresDriver extends Driver { /** * Set an or having statement in your query. */ - public orHaving(column: any, operation?: Operations, value?: any): this { + public orHaving(column: any, operation?: Operations, value?: any) { if (operation === undefined) { this.qb.orHaving(column) @@ -767,7 +767,7 @@ export class PostgresDriver extends Driver { /** * Set an or having raw statement in your query. */ - public orHavingRaw(sql: string, bindings?: any): this { + public orHavingRaw(sql: string, bindings?: any) { this.qb.orHavingRaw(sql, bindings) return this @@ -776,7 +776,7 @@ export class PostgresDriver extends Driver { /** * Set an or having exists statement in your query. */ - public orHavingExists(closure: (query: PostgresDriver) => void): this { + public orHavingExists(closure: (query: PostgresDriver) => void) { const driver = this.clone() as PostgresDriver // @ts-ignore @@ -790,7 +790,7 @@ export class PostgresDriver extends Driver { /** * Set an or having not exists statement in your query. */ - public orHavingNotExists(closure: (query: PostgresDriver) => void): this { + public orHavingNotExists(closure: (query: PostgresDriver) => void) { const driver = this.clone() as PostgresDriver // @ts-ignore @@ -804,7 +804,7 @@ export class PostgresDriver extends Driver { /** * Set an or having in statement in your query. */ - public orHavingIn(column: string, values: any[]): this { + public orHavingIn(column: string, values: any[]) { // @ts-ignore this.qb.orHavingIn(column, values) @@ -814,7 +814,7 @@ export class PostgresDriver extends Driver { /** * Set an or having not in statement in your query. */ - public orHavingNotIn(column: string, values: any[]): this { + public orHavingNotIn(column: string, values: any[]) { this.qb.orHavingNotIn(column, values) return this @@ -823,7 +823,7 @@ export class PostgresDriver extends Driver { /** * Set an or having between statement in your query. */ - public orHavingBetween(column: string, values: [any, any]): this { + public orHavingBetween(column: string, values: [any, any]) { this.qb.orHavingBetween(column, values) return this @@ -832,7 +832,7 @@ export class PostgresDriver extends Driver { /** * Set an or having not between statement in your query. */ - public orHavingNotBetween(column: string, values: [any, any]): this { + public orHavingNotBetween(column: string, values: [any, any]) { this.qb.orHavingNotBetween(column, values) return this @@ -841,7 +841,7 @@ export class PostgresDriver extends Driver { /** * Set an or having null statement in your query. */ - public orHavingNull(column: string): this { + public orHavingNull(column: string) { // @ts-ignore this.qb.orHavingNull(column) @@ -851,7 +851,7 @@ export class PostgresDriver extends Driver { /** * Set an or having not null statement in your query. */ - public orHavingNotNull(column: string): this { + public orHavingNotNull(column: string) { // @ts-ignore this.qb.orHavingNotNull(column) @@ -865,7 +865,7 @@ export class PostgresDriver extends Driver { /** * Set a where statement in your query. */ - public where(statement: any, operation?: Operations, value?: any): this { + public where(statement: any, operation?: Operations, value?: any) { if (Is.Function(statement)) { const driver = this.clone() @@ -899,7 +899,7 @@ export class PostgresDriver extends Driver { /** * Set a where not statement in your query. */ - public whereNot(statement: any, value?: any): this { + public whereNot(statement: any, value?: any) { if (Is.Function(statement)) { const driver = this.clone() @@ -924,7 +924,7 @@ export class PostgresDriver extends Driver { /** * Set a where raw statement in your query. */ - public whereRaw(sql: string, bindings?: any): this { + public whereRaw(sql: string, bindings?: any) { this.qb.whereRaw(sql, bindings) return this @@ -933,7 +933,7 @@ export class PostgresDriver extends Driver { /** * Set a where exists statement in your query. */ - public whereExists(closure: (query: PostgresDriver) => void): this { + public whereExists(closure: (query: PostgresDriver) => void) { const driver = this.clone() as PostgresDriver this.qb.whereExists(function () { @@ -946,7 +946,7 @@ export class PostgresDriver extends Driver { /** * Set a where not exists statement in your query. */ - public whereNotExists(closure: (query: PostgresDriver) => void): this { + public whereNotExists(closure: (query: PostgresDriver) => void) { const driver = this.clone() as PostgresDriver this.qb.whereNotExists(function () { @@ -959,7 +959,7 @@ export class PostgresDriver extends Driver { /** * Set a where like statement in your query. */ - public whereLike(column: string, value: any): this { + public whereLike(column: string, value: any) { this.qb.whereLike(column, value) return this @@ -968,7 +968,7 @@ export class PostgresDriver extends Driver { /** * Set a where ILike statement in your query. */ - public whereILike(column: string, value: any): this { + public whereILike(column: string, value: any) { this.qb.whereILike(column, value) return this @@ -977,7 +977,7 @@ export class PostgresDriver extends Driver { /** * Set a where in statement in your query. */ - public whereIn(column: string, values: any[]): this { + public whereIn(column: string, values: any[]) { this.qb.whereIn(column, values) return this @@ -986,7 +986,7 @@ export class PostgresDriver extends Driver { /** * Set a where not in statement in your query. */ - public whereNotIn(column: string, values: any[]): this { + public whereNotIn(column: string, values: any[]) { this.qb.whereNotIn(column, values) return this @@ -995,7 +995,7 @@ export class PostgresDriver extends Driver { /** * Set a where between statement in your query. */ - public whereBetween(column: string, values: [any, any]): this { + public whereBetween(column: string, values: [any, any]) { this.qb.whereBetween(column, values) return this @@ -1004,7 +1004,7 @@ export class PostgresDriver extends Driver { /** * Set a where not between statement in your query. */ - public whereNotBetween(column: string, values: [any, any]): this { + public whereNotBetween(column: string, values: [any, any]) { this.qb.whereNotBetween(column, values) return this @@ -1013,7 +1013,7 @@ export class PostgresDriver extends Driver { /** * Set a where null statement in your query. */ - public whereNull(column: string): this { + public whereNull(column: string) { this.qb.whereNull(column) return this @@ -1022,7 +1022,7 @@ export class PostgresDriver extends Driver { /** * Set a where not null statement in your query. */ - public whereNotNull(column: string): this { + public whereNotNull(column: string) { this.qb.whereNotNull(column) return this @@ -1035,7 +1035,7 @@ export class PostgresDriver extends Driver { /** * Set a or where statement in your query. */ - public orWhere(statement: any, operation?: Operations, value?: any): this { + public orWhere(statement: any, operation?: Operations, value?: any) { if (Is.Function(statement)) { const driver = this.clone() @@ -1069,7 +1069,7 @@ export class PostgresDriver extends Driver { /** * Set an or where not statement in your query. */ - public orWhereNot(statement: any, value?: any): this { + public orWhereNot(statement: any, value?: any) { if (Is.Function(statement)) { const driver = this.clone() @@ -1094,7 +1094,7 @@ export class PostgresDriver extends Driver { /** * Set a or where raw statement in your query. */ - public orWhereRaw(sql: string, bindings?: any): this { + public orWhereRaw(sql: string, bindings?: any) { this.qb.orWhereRaw(sql, bindings) return this @@ -1103,7 +1103,7 @@ export class PostgresDriver extends Driver { /** * Set an or where exists statement in your query. */ - public orWhereExists(closure: (query: PostgresDriver) => void): this { + public orWhereExists(closure: (query: PostgresDriver) => void) { const driver = this.clone() as PostgresDriver this.qb.orWhereExists(function () { @@ -1116,7 +1116,7 @@ export class PostgresDriver extends Driver { /** * Set an or where not exists statement in your query. */ - public orWhereNotExists(closure: (query: PostgresDriver) => void): this { + public orWhereNotExists(closure: (query: PostgresDriver) => void) { const driver = this.clone() as PostgresDriver this.qb.orWhereNotExists(function () { @@ -1129,7 +1129,7 @@ export class PostgresDriver extends Driver { /** * Set an or where like statement in your query. */ - public orWhereLike(column: string, value: any): this { + public orWhereLike(column: string, value: any) { this.qb.orWhereLike(column, value) return this @@ -1138,7 +1138,7 @@ export class PostgresDriver extends Driver { /** * Set an or where ILike statement in your query. */ - public orWhereILike(column: string, value: any): this { + public orWhereILike(column: string, value: any) { this.qb.orWhereILike(column, value) return this @@ -1147,7 +1147,7 @@ export class PostgresDriver extends Driver { /** * Set an or where in statement in your query. */ - public orWhereIn(column: string, values: any[]): this { + public orWhereIn(column: string, values: any[]) { this.qb.orWhereIn(column, values) return this @@ -1156,7 +1156,7 @@ export class PostgresDriver extends Driver { /** * Set an or where not in statement in your query. */ - public orWhereNotIn(column: string, values: any[]): this { + public orWhereNotIn(column: string, values: any[]) { this.qb.orWhereNotIn(column, values) return this @@ -1165,7 +1165,7 @@ export class PostgresDriver extends Driver { /** * Set an or where between statement in your query. */ - public orWhereBetween(column: string, values: [any, any]): this { + public orWhereBetween(column: string, values: [any, any]) { this.qb.orWhereBetween(column, values) return this @@ -1174,7 +1174,7 @@ export class PostgresDriver extends Driver { /** * Set an or where not between statement in your query. */ - public orWhereNotBetween(column: string, values: [any, any]): this { + public orWhereNotBetween(column: string, values: [any, any]) { this.qb.orWhereNotBetween(column, values) return this @@ -1183,7 +1183,7 @@ export class PostgresDriver extends Driver { /** * Set an or where null statement in your query. */ - public orWhereNull(column: string): this { + public orWhereNull(column: string) { this.qb.orWhereNull(column) return this @@ -1192,7 +1192,7 @@ export class PostgresDriver extends Driver { /** * Set an or where not null statement in your query. */ - public orWhereNotNull(column: string): this { + public orWhereNotNull(column: string) { this.qb.orWhereNotNull(column) return this @@ -1201,7 +1201,7 @@ export class PostgresDriver extends Driver { /** * Set an order by statement in your query. */ - public orderBy(column: string, direction: Direction = 'ASC'): this { + public orderBy(column: string, direction: Direction = 'ASC') { this.qb.orderBy(column, direction.toUpperCase()) return this @@ -1210,7 +1210,7 @@ export class PostgresDriver extends Driver { /** * Set an order by raw statement in your query. */ - public orderByRaw(sql: string, bindings?: any): this { + public orderByRaw(sql: string, bindings?: any) { this.qb.orderByRaw(sql, bindings) return this @@ -1220,7 +1220,7 @@ export class PostgresDriver extends Driver { * Order the results easily by the latest date. By default, the result will * be ordered by the table's "createdAt" column. */ - public latest(column: string = 'createdAt'): this { + public latest(column: string = 'createdAt') { return this.orderBy(column, 'DESC') } @@ -1228,14 +1228,14 @@ export class PostgresDriver extends Driver { * Order the results easily by the oldest date. By default, the result will * be ordered by the table's "createdAt" column. */ - public oldest(column: string = 'createdAt'): this { + public oldest(column: string = 'createdAt') { return this.orderBy(column, 'ASC') } /** * Set the skip number in your query. */ - public offset(number: number): this { + public offset(number: number) { this.qb.offset(number) return this @@ -1244,7 +1244,7 @@ export class PostgresDriver extends Driver { /** * Set the limit number in your query. */ - public limit(number: number): this { + public limit(number: number) { this.qb.limit(number) return this diff --git a/src/database/drivers/SqliteDriver.ts b/src/database/drivers/SqliteDriver.ts index aebaeab..5f61a10 100644 --- a/src/database/drivers/SqliteDriver.ts +++ b/src/database/drivers/SqliteDriver.ts @@ -454,7 +454,7 @@ export class SqliteDriver extends Driver { /** * Set the table that this query will be executed. */ - public table(table: string): this { + public table(table: string) { if (!this.isConnected) { throw new NotConnectedDatabaseException() } @@ -468,7 +468,7 @@ export class SqliteDriver extends Driver { /** * Log in console the actual query built. */ - public dump(): this { + public dump() { console.log(this.qb.toSQL().toNative()) return this @@ -477,7 +477,7 @@ export class SqliteDriver extends Driver { /** * Set the columns that should be selected on query. */ - public select(...columns: string[]): this { + public select(...columns: string[]) { this.qb.select(...columns) return this @@ -486,7 +486,7 @@ export class SqliteDriver extends Driver { /** * Set the columns that should be selected on query raw. */ - public selectRaw(sql: string, bindings?: any): this { + public selectRaw(sql: string, bindings?: any) { return this.select(this.raw(sql, bindings) as any) } @@ -495,7 +495,7 @@ export class SqliteDriver extends Driver { * Different from `table()` method, this method * doesn't change the driver table. */ - public from(table: string): this { + public from(table: string) { this.qb.from(table) return this @@ -506,7 +506,7 @@ export class SqliteDriver extends Driver { * Different from `table()` method, this method * doesn't change the driver table. */ - public fromRaw(sql: string, bindings?: any): this { + public fromRaw(sql: string, bindings?: any) { return this.from(this.raw(sql, bindings) as any) } @@ -518,7 +518,7 @@ export class SqliteDriver extends Driver { column1?: any, operation?: any | Operations, column2?: any - ): this { + ) { return this.joinByType('join', table, column1, operation, column2) } @@ -530,7 +530,7 @@ export class SqliteDriver extends Driver { column1?: any, operation?: any | Operations, column2?: any - ): this { + ) { return this.joinByType('leftJoin', table, column1, operation, column2) } @@ -542,7 +542,7 @@ export class SqliteDriver extends Driver { column1?: any, operation?: any | Operations, column2?: any - ): this { + ) { return this.joinByType('rightJoin', table, column1, operation, column2) } @@ -554,7 +554,7 @@ export class SqliteDriver extends Driver { column1?: any, operation?: any | Operations, column2?: any - ): this { + ) { return this.joinByType('crossJoin', table, column1, operation, column2) } @@ -566,7 +566,7 @@ export class SqliteDriver extends Driver { column1?: any, operation?: any | Operations, column2?: any - ): this { + ) { return this.joinByType('fullOuterJoin', table, column1, operation, column2) } @@ -578,7 +578,7 @@ export class SqliteDriver extends Driver { column1?: any, operation?: any | Operations, column2?: any - ): this { + ) { return this.joinByType('leftOuterJoin', table, column1, operation, column2) } @@ -590,14 +590,14 @@ export class SqliteDriver extends Driver { column1?: any, operation?: any | Operations, column2?: any - ): this { + ) { return this.joinByType('rightOuterJoin', table, column1, operation, column2) } /** * Set a join raw statement in your query. */ - public joinRaw(sql: string, bindings?: any): this { + public joinRaw(sql: string, bindings?: any) { this.qb.joinRaw(sql, bindings) return this @@ -606,7 +606,7 @@ export class SqliteDriver extends Driver { /** * Set a group by statement in your query. */ - public groupBy(...columns: string[]): this { + public groupBy(...columns: string[]) { this.qb.groupBy(...columns) return this @@ -615,7 +615,7 @@ export class SqliteDriver extends Driver { /** * Set a group by raw statement in your query. */ - public groupByRaw(sql: string, bindings?: any): this { + public groupByRaw(sql: string, bindings?: any) { this.qb.groupByRaw(sql, bindings) return this @@ -628,7 +628,7 @@ export class SqliteDriver extends Driver { /** * Set a having statement in your query. */ - public having(column: any, operation?: Operations, value?: any): this { + public having(column: any, operation?: Operations, value?: any) { if (operation === undefined) { this.qb.having(column) @@ -649,7 +649,7 @@ export class SqliteDriver extends Driver { /** * Set a having raw statement in your query. */ - public havingRaw(sql: string, bindings?: any): this { + public havingRaw(sql: string, bindings?: any) { this.qb.havingRaw(sql, bindings) return this @@ -658,7 +658,7 @@ export class SqliteDriver extends Driver { /** * Set a having exists statement in your query. */ - public havingExists(closure: (query: SqliteDriver) => void): this { + public havingExists(closure: (query: SqliteDriver) => void) { const driver = this.clone() as SqliteDriver // @ts-ignore @@ -672,7 +672,7 @@ export class SqliteDriver extends Driver { /** * Set a having not exists statement in your query. */ - public havingNotExists(closure: (query: SqliteDriver) => void): this { + public havingNotExists(closure: (query: SqliteDriver) => void) { const driver = this.clone() as SqliteDriver // @ts-ignore @@ -686,7 +686,7 @@ export class SqliteDriver extends Driver { /** * Set a having in statement in your query. */ - public havingIn(column: string, values: any[]): this { + public havingIn(column: string, values: any[]) { this.qb.havingIn(column, values) return this @@ -695,7 +695,7 @@ export class SqliteDriver extends Driver { /** * Set a having not in statement in your query. */ - public havingNotIn(column: string, values: any[]): this { + public havingNotIn(column: string, values: any[]) { this.qb.havingNotIn(column, values) return this @@ -704,7 +704,7 @@ export class SqliteDriver extends Driver { /** * Set a having between statement in your query. */ - public havingBetween(column: string, values: [any, any]): this { + public havingBetween(column: string, values: [any, any]) { this.qb.havingBetween(column, values) return this @@ -713,7 +713,7 @@ export class SqliteDriver extends Driver { /** * Set a having not between statement in your query. */ - public havingNotBetween(column: string, values: [any, any]): this { + public havingNotBetween(column: string, values: [any, any]) { this.qb.havingNotBetween(column, values) return this @@ -722,7 +722,7 @@ export class SqliteDriver extends Driver { /** * Set a having null statement in your query. */ - public havingNull(column: string): this { + public havingNull(column: string) { this.qb.havingNull(column) return this @@ -731,7 +731,7 @@ export class SqliteDriver extends Driver { /** * Set a having not null statement in your query. */ - public havingNotNull(column: string): this { + public havingNotNull(column: string) { this.qb.havingNotNull(column) return this @@ -744,7 +744,7 @@ export class SqliteDriver extends Driver { /** * Set an or having statement in your query. */ - public orHaving(column: any, operation?: Operations, value?: any): this { + public orHaving(column: any, operation?: Operations, value?: any) { if (operation === undefined) { this.qb.orHaving(column) @@ -765,7 +765,7 @@ export class SqliteDriver extends Driver { /** * Set an or having raw statement in your query. */ - public orHavingRaw(sql: string, bindings?: any): this { + public orHavingRaw(sql: string, bindings?: any) { this.qb.orHavingRaw(sql, bindings) return this @@ -774,7 +774,7 @@ export class SqliteDriver extends Driver { /** * Set an or having exists statement in your query. */ - public orHavingExists(closure: (query: SqliteDriver) => void): this { + public orHavingExists(closure: (query: SqliteDriver) => void) { const driver = this.clone() as SqliteDriver // @ts-ignore @@ -788,7 +788,7 @@ export class SqliteDriver extends Driver { /** * Set an or having not exists statement in your query. */ - public orHavingNotExists(closure: (query: SqliteDriver) => void): this { + public orHavingNotExists(closure: (query: SqliteDriver) => void) { const driver = this.clone() as SqliteDriver // @ts-ignore @@ -802,7 +802,7 @@ export class SqliteDriver extends Driver { /** * Set an or having in statement in your query. */ - public orHavingIn(column: string, values: any[]): this { + public orHavingIn(column: string, values: any[]) { // @ts-ignore this.qb.orHavingIn(column, values) @@ -812,7 +812,7 @@ export class SqliteDriver extends Driver { /** * Set an or having not in statement in your query. */ - public orHavingNotIn(column: string, values: any[]): this { + public orHavingNotIn(column: string, values: any[]) { this.qb.orHavingNotIn(column, values) return this @@ -821,7 +821,7 @@ export class SqliteDriver extends Driver { /** * Set an or having between statement in your query. */ - public orHavingBetween(column: string, values: [any, any]): this { + public orHavingBetween(column: string, values: [any, any]) { this.qb.orHavingBetween(column, values) return this @@ -830,7 +830,7 @@ export class SqliteDriver extends Driver { /** * Set an or having not between statement in your query. */ - public orHavingNotBetween(column: string, values: [any, any]): this { + public orHavingNotBetween(column: string, values: [any, any]) { this.qb.orHavingNotBetween(column, values) return this @@ -839,7 +839,7 @@ export class SqliteDriver extends Driver { /** * Set an or having null statement in your query. */ - public orHavingNull(column: string): this { + public orHavingNull(column: string) { // @ts-ignore this.qb.orHavingNull(column) @@ -849,7 +849,7 @@ export class SqliteDriver extends Driver { /** * Set an or having not null statement in your query. */ - public orHavingNotNull(column: string): this { + public orHavingNotNull(column: string) { // @ts-ignore this.qb.orHavingNotNull(column) @@ -863,7 +863,7 @@ export class SqliteDriver extends Driver { /** * Set a where statement in your query. */ - public where(statement: any, operation?: Operations, value?: any): this { + public where(statement: any, operation?: Operations, value?: any) { if (Is.Function(statement)) { const driver = this.clone() @@ -897,7 +897,7 @@ export class SqliteDriver extends Driver { /** * Set a where not statement in your query. */ - public whereNot(statement: any, value?: any): this { + public whereNot(statement: any, value?: any) { if (Is.Function(statement)) { const driver = this.clone() @@ -922,7 +922,7 @@ export class SqliteDriver extends Driver { /** * Set a where raw statement in your query. */ - public whereRaw(sql: string, bindings?: any): this { + public whereRaw(sql: string, bindings?: any) { this.qb.whereRaw(sql, bindings) return this @@ -931,7 +931,7 @@ export class SqliteDriver extends Driver { /** * Set a where exists statement in your query. */ - public whereExists(closure: (query: SqliteDriver) => void): this { + public whereExists(closure: (query: SqliteDriver) => void) { const driver = this.clone() as SqliteDriver this.qb.whereExists(function () { @@ -944,7 +944,7 @@ export class SqliteDriver extends Driver { /** * Set a where not exists statement in your query. */ - public whereNotExists(closure: (query: SqliteDriver) => void): this { + public whereNotExists(closure: (query: SqliteDriver) => void) { const driver = this.clone() as SqliteDriver this.qb.whereNotExists(function () { @@ -957,7 +957,7 @@ export class SqliteDriver extends Driver { /** * Set a where like statement in your query. */ - public whereLike(column: string, value: any): this { + public whereLike(column: string, value: any) { this.qb.whereLike(column, value) return this @@ -966,7 +966,7 @@ export class SqliteDriver extends Driver { /** * Set a where ILike statement in your query. */ - public whereILike(column: string, value: any): this { + public whereILike(column: string, value: any) { this.qb.whereLike(column, value) return this @@ -975,7 +975,7 @@ export class SqliteDriver extends Driver { /** * Set a where in statement in your query. */ - public whereIn(column: string, values: any[]): this { + public whereIn(column: string, values: any[]) { this.qb.whereIn(column, values) return this @@ -984,7 +984,7 @@ export class SqliteDriver extends Driver { /** * Set a where not in statement in your query. */ - public whereNotIn(column: string, values: any[]): this { + public whereNotIn(column: string, values: any[]) { this.qb.whereNotIn(column, values) return this @@ -993,7 +993,7 @@ export class SqliteDriver extends Driver { /** * Set a where between statement in your query. */ - public whereBetween(column: string, values: [any, any]): this { + public whereBetween(column: string, values: [any, any]) { this.qb.whereBetween(column, values) return this @@ -1002,7 +1002,7 @@ export class SqliteDriver extends Driver { /** * Set a where not between statement in your query. */ - public whereNotBetween(column: string, values: [any, any]): this { + public whereNotBetween(column: string, values: [any, any]) { this.qb.whereNotBetween(column, values) return this @@ -1011,7 +1011,7 @@ export class SqliteDriver extends Driver { /** * Set a where null statement in your query. */ - public whereNull(column: string): this { + public whereNull(column: string) { this.qb.whereNull(column) return this @@ -1020,7 +1020,7 @@ export class SqliteDriver extends Driver { /** * Set a where not null statement in your query. */ - public whereNotNull(column: string): this { + public whereNotNull(column: string) { this.qb.whereNotNull(column) return this @@ -1033,7 +1033,7 @@ export class SqliteDriver extends Driver { /** * Set a or where statement in your query. */ - public orWhere(statement: any, operation?: Operations, value?: any): this { + public orWhere(statement: any, operation?: Operations, value?: any) { if (Is.Function(statement)) { const driver = this.clone() @@ -1067,7 +1067,7 @@ export class SqliteDriver extends Driver { /** * Set an or where not statement in your query. */ - public orWhereNot(statement: any, value?: any): this { + public orWhereNot(statement: any, value?: any) { if (Is.Function(statement)) { const driver = this.clone() @@ -1092,7 +1092,7 @@ export class SqliteDriver extends Driver { /** * Set a or where raw statement in your query. */ - public orWhereRaw(sql: string, bindings?: any): this { + public orWhereRaw(sql: string, bindings?: any) { this.qb.orWhereRaw(sql, bindings) return this @@ -1101,7 +1101,7 @@ export class SqliteDriver extends Driver { /** * Set an or where exists statement in your query. */ - public orWhereExists(closure: (query: SqliteDriver) => void): this { + public orWhereExists(closure: (query: SqliteDriver) => void) { const driver = this.clone() as SqliteDriver this.qb.orWhereExists(function () { @@ -1114,7 +1114,7 @@ export class SqliteDriver extends Driver { /** * Set an or where not exists statement in your query. */ - public orWhereNotExists(closure: (query: SqliteDriver) => void): this { + public orWhereNotExists(closure: (query: SqliteDriver) => void) { const driver = this.clone() as SqliteDriver this.qb.orWhereNotExists(function () { @@ -1127,7 +1127,7 @@ export class SqliteDriver extends Driver { /** * Set an or where like statement in your query. */ - public orWhereLike(column: string, value: any): this { + public orWhereLike(column: string, value: any) { this.qb.orWhereLike(column, value) return this @@ -1136,7 +1136,7 @@ export class SqliteDriver extends Driver { /** * Set an or where ILike statement in your query. */ - public orWhereILike(column: string, value: any): this { + public orWhereILike(column: string, value: any) { this.qb.orWhereLike(column, value) return this @@ -1145,7 +1145,7 @@ export class SqliteDriver extends Driver { /** * Set an or where in statement in your query. */ - public orWhereIn(column: string, values: any[]): this { + public orWhereIn(column: string, values: any[]) { this.qb.orWhereIn(column, values) return this @@ -1154,7 +1154,7 @@ export class SqliteDriver extends Driver { /** * Set an or where not in statement in your query. */ - public orWhereNotIn(column: string, values: any[]): this { + public orWhereNotIn(column: string, values: any[]) { this.qb.orWhereNotIn(column, values) return this @@ -1163,7 +1163,7 @@ export class SqliteDriver extends Driver { /** * Set an or where between statement in your query. */ - public orWhereBetween(column: string, values: [any, any]): this { + public orWhereBetween(column: string, values: [any, any]) { this.qb.orWhereBetween(column, values) return this @@ -1172,7 +1172,7 @@ export class SqliteDriver extends Driver { /** * Set an or where not between statement in your query. */ - public orWhereNotBetween(column: string, values: [any, any]): this { + public orWhereNotBetween(column: string, values: [any, any]) { this.qb.orWhereNotBetween(column, values) return this @@ -1181,7 +1181,7 @@ export class SqliteDriver extends Driver { /** * Set an or where null statement in your query. */ - public orWhereNull(column: string): this { + public orWhereNull(column: string) { this.qb.orWhereNull(column) return this @@ -1190,7 +1190,7 @@ export class SqliteDriver extends Driver { /** * Set an or where not null statement in your query. */ - public orWhereNotNull(column: string): this { + public orWhereNotNull(column: string) { this.qb.orWhereNotNull(column) return this @@ -1199,7 +1199,7 @@ export class SqliteDriver extends Driver { /** * Set an order by statement in your query. */ - public orderBy(column: string, direction: Direction = 'ASC'): this { + public orderBy(column: string, direction: Direction = 'ASC') { this.qb.orderBy(column, direction.toUpperCase()) return this @@ -1208,7 +1208,7 @@ export class SqliteDriver extends Driver { /** * Set an order by raw statement in your query. */ - public orderByRaw(sql: string, bindings?: any): this { + public orderByRaw(sql: string, bindings?: any) { this.qb.orderByRaw(sql, bindings) return this @@ -1218,7 +1218,7 @@ export class SqliteDriver extends Driver { * Order the results easily by the latest date. By default, the result will * be ordered by the table's "createdAt" column. */ - public latest(column: string = 'createdAt'): this { + public latest(column: string = 'createdAt') { return this.orderBy(column, 'DESC') } @@ -1226,14 +1226,14 @@ export class SqliteDriver extends Driver { * Order the results easily by the oldest date. By default, the result will * be ordered by the table's "createdAt" column. */ - public oldest(column: string = 'createdAt'): this { + public oldest(column: string = 'createdAt') { return this.orderBy(column, 'ASC') } /** * Set the skip number in your query. */ - public offset(number: number): this { + public offset(number: number) { this.qb.offset(number) return this @@ -1242,7 +1242,7 @@ export class SqliteDriver extends Driver { /** * Set the limit number in your query. */ - public limit(number: number): this { + public limit(number: number) { this.qb.limit(number) return this diff --git a/src/models/BaseModel.ts b/src/models/BaseModel.ts index bf6da7d..5411f7c 100644 --- a/src/models/BaseModel.ts +++ b/src/models/BaseModel.ts @@ -255,9 +255,10 @@ export class BaseModel { */ public static async create( this: T, - data: Partial> = {} + data: Partial> = {}, + cleanPersist = true ): Promise> { - return this.query().create(data) + return this.query().create(data, cleanPersist) } /** @@ -265,9 +266,10 @@ export class BaseModel { */ public static async createMany( this: T, - data: Partial>[] + data: Partial>[], + cleanPersist = true ): Promise[]> { - return this.query().createMany(data) + return this.query().createMany(data, cleanPersist) } /** @@ -276,7 +278,8 @@ export class BaseModel { public static async createOrUpdate( this: T, where: Partial>, - data: Partial> + data: Partial>, + cleanPersist = true ): Promise | InstanceType[]> { const query = this.query() @@ -284,7 +287,7 @@ export class BaseModel { query.where(where) } - return query.createOrUpdate(data) + return query.createOrUpdate(data, cleanPersist) } /** @@ -293,7 +296,8 @@ export class BaseModel { public static async update( this: T, where: Partial>, - data: Partial> + data: Partial>, + cleanPersist = true ): Promise | InstanceType[]> { const query = this.query() @@ -301,7 +305,7 @@ export class BaseModel { query.where(where) } - return query.update(data) + return query.update(data, cleanPersist) } /** @@ -333,7 +337,7 @@ export class BaseModel { * Set the original model values by deep copying * the model state. */ - public setOriginal(): this { + public setOriginal() { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore this.original = {} @@ -462,7 +466,7 @@ export class BaseModel { /** * Save the changes done in the model in database. */ - public async save() { + public async save(cleanPersist = true) { const Model = this.constructor as any const schema = Model.schema() const primaryKey = schema.getMainPrimaryKeyProperty() @@ -495,7 +499,7 @@ export class BaseModel { const data = this.dirty() if (!this.isPersisted()) { - const created = await Model.create(data) + const created = await Model.create(data, cleanPersist) Object.keys(created).forEach(key => (this[key] = created[key])) @@ -511,7 +515,7 @@ export class BaseModel { } const where = { [primaryKey]: this[primaryKey] } - const updated = await Model.update(where, data) + const updated = await Model.update(where, data, cleanPersist) Object.keys(updated).forEach(key => (this[key] = updated[key])) @@ -584,7 +588,7 @@ export class BaseModel { const restored = await Model.query() .where(primaryKey, this[primaryKey]) - .update({ deletedAt: null }) + .restore() Object.keys(restored).forEach(key => (this[key] = restored[key])) diff --git a/src/models/builders/ModelQueryBuilder.ts b/src/models/builders/ModelQueryBuilder.ts index d6d0bee..d9af223 100644 --- a/src/models/builders/ModelQueryBuilder.ts +++ b/src/models/builders/ModelQueryBuilder.ts @@ -13,8 +13,8 @@ import type { ModelColumns, ModelRelations } from '#src/types' -import { Collection, Is } from '@athenna/common' import type { BaseModel } from '#src/models/BaseModel' +import { Collection, Is, Options } from '@athenna/common' import type { Driver } from '#src/database/drivers/Driver' import { QueryBuilder } from '#src/database/builders/QueryBuilder' import type { ModelSchema } from '#src/models/schemas/ModelSchema' @@ -35,6 +35,11 @@ export class ModelQueryBuilder< private isToSetAttributes: boolean = true private isToValidateUnique: boolean = true private isToValidateNullable: boolean = true + private selectColumns: string[] = [] + private DELETED_AT_PROP: any = null + private DELETED_AT_NAME: any = null + private isSoftDelete: boolean = false + private hasCustomSelect: boolean = false public constructor(model: any, driver: D) { super(driver, model.table()) @@ -46,11 +51,17 @@ export class ModelQueryBuilder< this.primaryKeyProperty = this.schema.getMainPrimaryKeyProperty() as any const deletedAtColumn = this.schema.getDeletedAtColumn() + const properties = this.schema.getAllColumnProperties({ + removeHidden: true + }) if (deletedAtColumn) { - this.whereNull(deletedAtColumn.property as any) + this.isSoftDelete = true + this.DELETED_AT_NAME = deletedAtColumn.name + this.DELETED_AT_PROP = deletedAtColumn.property } + this.selectColumns = properties this.setPrimaryKey(this.primaryKeyName) } @@ -58,100 +69,122 @@ export class ModelQueryBuilder< * Calculate the average of a given column. */ public async avg(column: ModelColumns): Promise { + this.setInternalQueries() + const name = this.schema.getColumnNameByProperty(column) - return super.avg(name) + return super.avg(name as any) } /** * Calculate the average of a given column. */ public async avgDistinct(column: ModelColumns): Promise { + this.setInternalQueries() + const name = this.schema.getColumnNameByProperty(column) - return super.avgDistinct(name) + return super.avgDistinct(name as any) } /** * Get the max number of a given column. */ public async max(column: ModelColumns): Promise { + this.setInternalQueries() + const name = this.schema.getColumnNameByProperty(column) - return super.max(name) + return super.max(name as any) } /** * Get the min number of a given column. */ public async min(column: ModelColumns): Promise { + this.setInternalQueries() + const name = this.schema.getColumnNameByProperty(column) - return super.min(name) + return super.min(name as any) } /** * Sum all numbers of a given column. */ public async sum(column: ModelColumns): Promise { + this.setInternalQueries() + const name = this.schema.getColumnNameByProperty(column) - return super.sum(name) + return super.sum(name as any) } /** * Sum all numbers of a given column. */ public async sumDistinct(column: ModelColumns): Promise { + this.setInternalQueries() + const name = this.schema.getColumnNameByProperty(column) - return super.sumDistinct(name) + return super.sumDistinct(name as any) } /** * Increment a value of a given column. */ public async increment(column: ModelColumns): Promise { + this.setInternalQueries() + const name = this.schema.getColumnNameByProperty(column) - return super.increment(name) + return super.increment(name as any) } /** * Decrement a value of a given column. */ public async decrement(column: ModelColumns): Promise { + this.setInternalQueries() + const name = this.schema.getColumnNameByProperty(column) - await super.decrement(name) + await super.decrement(name as any) } /** * Calculate the average of a given column using distinct. */ public async count(column?: ModelColumns): Promise { + this.setInternalQueries() + if (!column) { return super.count() } const name = this.schema.getColumnNameByProperty(column) - return super.count(name) + return super.count(name as any) } /** * Calculate the average of a given column using distinct. */ public async countDistinct(column: ModelColumns): Promise { + this.setInternalQueries() + const name = this.schema.getColumnNameByProperty(column) - return super.countDistinct(name) + return super.countDistinct(name as any) } /** * Find a value in database. */ public async find() { + this.setInternalQueries() + const data = await super.find() return this.generator.generateOne(data) @@ -190,6 +223,8 @@ export class ModelQueryBuilder< * Find many values in database. */ public async findMany() { + this.setInternalQueries() + const data = await super.findMany() return this.generator.generateMany(data) @@ -200,8 +235,7 @@ export class ModelQueryBuilder< * as a collection instance. */ public async collection() { - const data = await super.findMany() - const models = await this.generator.generateMany(data) + const models = await this.findMany() return new Collection(models) } @@ -209,8 +243,8 @@ export class ModelQueryBuilder< /** * Create a value in database. */ - public async create(data: Partial = {}) { - const created = await this.createMany([data]) + public async create(data: Partial = {}, cleanPersist = true) { + const created = await this.createMany([data], cleanPersist) return created[0] } @@ -218,7 +252,7 @@ export class ModelQueryBuilder< /** * Create many values in database. */ - public async createMany(data: Partial[]) { + public async createMany(data: Partial[], cleanPersist = true) { data = await Promise.all( data.map(async d => { const date = new Date() @@ -229,7 +263,7 @@ export class ModelQueryBuilder< const parsed = this.schema.propertiesToColumnNames(d, { attributes, - cleanPersist: true + cleanPersist }) if (createdAt && parsed[createdAt.name] === undefined) { @@ -259,29 +293,31 @@ export class ModelQueryBuilder< /** * Create or update a value in database. */ - public async createOrUpdate(data: Partial) { + public async createOrUpdate(data: Partial, cleanPersist = true) { const hasValue = await this.find() if (hasValue) { const pk = this.primaryKeyProperty - return this.where(pk, hasValue[pk as any]).update(data) + return this.where(pk, hasValue[pk as any]).update(data, cleanPersist) } - return this.create(data) + return this.create(data, cleanPersist) } /** * Update a value in database. */ - public async update(data: Partial) { + public async update(data: Partial, cleanPersist = true) { + this.setInternalQueries() + const date = new Date() const updatedAt = this.schema.getUpdatedAtColumn() const attributes = this.isToSetAttributes ? this.Model.attributes() : {} const parsed = this.schema.propertiesToColumnNames(data, { attributes, - cleanPersist: true + cleanPersist }) if (updatedAt && parsed[updatedAt.name] === undefined) { @@ -303,15 +339,81 @@ export class ModelQueryBuilder< * Delete or soft delete a value in database. */ public async delete(force = false): Promise { - const column = this.schema.getDeletedAtColumn() + this.setInternalQueries({ addSelect: false }) - if (!column || force) { + if (!this.DELETED_AT_NAME || force) { await super.delete() return } - await this.update({ [column.property]: new Date() } as any) + await this.update({ [this.DELETED_AT_PROP]: new Date() } as any) + } + + /** + * Restore one or multiple soft deleted models. + */ + public async restore() { + this.setInternalQueries({ addSoftDelete: false }) + + if (!this.DELETED_AT_PROP) { + return + } + + const updatedAt = this.schema.getUpdatedAtColumn() + const data = { [this.DELETED_AT_PROP]: null } as any + + if (updatedAt) { + data[updatedAt.name] = new Date() + } + + const updated = await super.update(data) + + if (Is.Array(updated)) { + return this.generator.generateMany(updated) + } + + return this.generator.generateOne(updated) + } + + /** + * Retrieve only the values that are soft deleted in + * database. + */ + public onlyTrashed() { + this.isSoftDelete = false + + if (!this.DELETED_AT_PROP) { + return this + } + + return this.whereNotNull(this.DELETED_AT_PROP) + } + + /** + * Retrieve values that are soft deleted in database. + */ + public withTrashed() { + this.isSoftDelete = false + + if (!this.DELETED_AT_PROP) { + return + } + + return this.whereNull(this.DELETED_AT_PROP).whereNotNull( + this.DELETED_AT_PROP + ) + } + + /** + * Select all fields that are hidden. + */ + public withHidden() { + const properties = this.schema.getAllColumnProperties({ + removeHidden: false + }) + + return this.select(...(properties as any)) } /** @@ -328,7 +430,7 @@ export class ModelQueryBuilder< * Enable/disable the `isUnique` property validation of * models columns. */ - public uniqueValidation(value: boolean): this { + public uniqueValidation(value: boolean) { this.isToValidateUnique = value return this @@ -338,7 +440,7 @@ export class ModelQueryBuilder< * Enable/disable the `isNullable` property validation of * models columns. */ - public nullableValidation(value: boolean): this { + public nullableValidation(value: boolean) { this.isToValidateNullable = value return this @@ -355,7 +457,7 @@ export class ModelQueryBuilder< Driver > ) => any - ): this { + ) { this.schema.includeRelation(relation, closure) return this @@ -366,8 +468,8 @@ export class ModelQueryBuilder< */ public when( criteria: any, - closure: (query: this, criteriaValue: any) => void | Promise - ): this { + closure: (query: this, criteriaValue: any) => any | Promise + ) { if (criteria) { closure(this, criteria) @@ -380,8 +482,25 @@ export class ModelQueryBuilder< /** * Set the columns that should be selected on query. */ - public select(...columns: ModelColumns[]): this { - super.select(...this.schema.getColumnNamesByProperties(columns)) + public select(...columns: ModelColumns[]) { + if (!this.hasCustomSelect) { + this.hasCustomSelect = true + this.selectColumns = columns.map(c => + this.schema.getColumnNameByProperty(c) + ) + + return this + } + + columns.forEach(column => { + const index = this.selectColumns.indexOf(column) + + if (index) { + return + } + + this.selectColumns.push(this.schema.getColumnNameByProperty(column)) + }) return this } @@ -389,7 +508,7 @@ export class ModelQueryBuilder< /** * Set a group by statement in your query. */ - public groupBy(...columns: ModelColumns[]): this { + public groupBy(...columns: ModelColumns[]) { super.groupBy(...this.schema.getColumnNamesByProperties(columns)) return this @@ -410,7 +529,7 @@ export class ModelQueryBuilder< column: ModelColumns, operation?: any | Operations, value?: any - ): this { + ) { const name = this.schema.getColumnNameByProperty(column) super.having(name, operation, value) @@ -421,7 +540,7 @@ export class ModelQueryBuilder< /** * Set a having in statement in your query. */ - public havingIn(column: ModelColumns, values: any[]): this { + public havingIn(column: ModelColumns, values: any[]) { const name = this.schema.getColumnNameByProperty(column) super.havingIn(name, values) @@ -432,7 +551,7 @@ export class ModelQueryBuilder< /** * Set a having not in statement in your query. */ - public havingNotIn(column: ModelColumns, values: any[]): this { + public havingNotIn(column: ModelColumns, values: any[]) { const name = this.schema.getColumnNameByProperty(column) super.havingNotIn(name, values) @@ -443,7 +562,7 @@ export class ModelQueryBuilder< /** * Set a having between statement in your query. */ - public havingBetween(column: ModelColumns, values: [any, any]): this { + public havingBetween(column: ModelColumns, values: [any, any]) { const name = this.schema.getColumnNameByProperty(column) super.havingBetween(name, values) @@ -454,7 +573,7 @@ export class ModelQueryBuilder< /** * Set a having not between statement in your query. */ - public havingNotBetween(column: ModelColumns, values: [any, any]): this { + public havingNotBetween(column: ModelColumns, values: [any, any]) { const name = this.schema.getColumnNameByProperty(column) super.havingNotBetween(name, values) @@ -465,7 +584,7 @@ export class ModelQueryBuilder< /** * Set a having null statement in your query. */ - public havingNull(column: ModelColumns): this { + public havingNull(column: ModelColumns) { const name = this.schema.getColumnNameByProperty(column) super.havingNull(name) @@ -476,7 +595,7 @@ export class ModelQueryBuilder< /** * Set a having not null statement in your query. */ - public havingNotNull(column: ModelColumns): this { + public havingNotNull(column: ModelColumns) { const name = this.schema.getColumnNameByProperty(column) super.havingNotNull(name) @@ -499,7 +618,7 @@ export class ModelQueryBuilder< column: ModelColumns, operation?: any | Operations, value?: any - ): this { + ) { const name = this.schema.getColumnNameByProperty(column) super.orHaving(name, operation, value) @@ -510,7 +629,7 @@ export class ModelQueryBuilder< /** * Set a orHaving in statement in your query. */ - public orHavingIn(column: ModelColumns, values: any[]): this { + public orHavingIn(column: ModelColumns, values: any[]) { const name = this.schema.getColumnNameByProperty(column) super.orHavingIn(name, values) @@ -521,7 +640,7 @@ export class ModelQueryBuilder< /** * Set a orHaving not in statement in your query. */ - public orHavingNotIn(column: ModelColumns, values: any[]): this { + public orHavingNotIn(column: ModelColumns, values: any[]) { const name = this.schema.getColumnNameByProperty(column) super.orHavingNotIn(name, values) @@ -532,7 +651,7 @@ export class ModelQueryBuilder< /** * Set a orHaving between statement in your query. */ - public orHavingBetween(column: ModelColumns, values: [any, any]): this { + public orHavingBetween(column: ModelColumns, values: [any, any]) { const name = this.schema.getColumnNameByProperty(column) super.orHavingBetween(name, values) @@ -543,7 +662,7 @@ export class ModelQueryBuilder< /** * Set a orHaving not between statement in your query. */ - public orHavingNotBetween(column: ModelColumns, values: [any, any]): this { + public orHavingNotBetween(column: ModelColumns, values: [any, any]) { const name = this.schema.getColumnNameByProperty(column) super.orHavingNotBetween(name, values) @@ -554,7 +673,7 @@ export class ModelQueryBuilder< /** * Set a orHaving null statement in your query. */ - public orHavingNull(column: ModelColumns): this { + public orHavingNull(column: ModelColumns) { const name = this.schema.getColumnNameByProperty(column) super.orHavingNull(name) @@ -565,7 +684,7 @@ export class ModelQueryBuilder< /** * Set a orHaving not null statement in your query. */ - public orHavingNotNull(column: ModelColumns): this { + public orHavingNotNull(column: ModelColumns) { const name = this.schema.getColumnNameByProperty(column) super.orHavingNotNull(name) @@ -581,11 +700,7 @@ export class ModelQueryBuilder< /** * Set a where statement in your query. */ - public where( - statement: any, - operation?: any | Operations, - value?: any - ): this { + public where(statement: any, operation?: any | Operations, value?: any) { if (!operation) { const parsed = this.schema.propertiesToColumnNames(statement) @@ -608,7 +723,7 @@ export class ModelQueryBuilder< /** * Set a where not statement in your query. */ - public whereNot(statement: any, value?: any): this { + public whereNot(statement: any, value?: any) { if (!value) { const parsed = this.schema.propertiesToColumnNames(statement) @@ -627,7 +742,7 @@ export class ModelQueryBuilder< /** * Set a where like statement in your query. */ - public whereLike(column: ModelColumns, value: any): this { + public whereLike(column: ModelColumns, value: any) { const name = this.schema.getColumnNameByProperty(column) super.whereLike(name, value) @@ -638,7 +753,7 @@ export class ModelQueryBuilder< /** * Set a where ILike statement in your query. */ - public whereILike(column: ModelColumns, value: any): this { + public whereILike(column: ModelColumns, value: any) { const name = this.schema.getColumnNameByProperty(column) super.whereILike(name, value) @@ -649,7 +764,7 @@ export class ModelQueryBuilder< /** * Set a where in statement in your query. */ - public whereIn(column: ModelColumns, values: any[]): this { + public whereIn(column: ModelColumns, values: any[]) { const name = this.schema.getColumnNameByProperty(column) super.whereIn(name, values) @@ -660,7 +775,7 @@ export class ModelQueryBuilder< /** * Set a where not in statement in your query. */ - public whereNotIn(column: ModelColumns, values: any[]): this { + public whereNotIn(column: ModelColumns, values: any[]) { const name = this.schema.getColumnNameByProperty(column) super.whereNotIn(name, values) @@ -671,7 +786,7 @@ export class ModelQueryBuilder< /** * Set a where between statement in your query. */ - public whereBetween(column: ModelColumns, values: [any, any]): this { + public whereBetween(column: ModelColumns, values: [any, any]) { const name = this.schema.getColumnNameByProperty(column) super.whereBetween(name, values) @@ -682,7 +797,7 @@ export class ModelQueryBuilder< /** * Set a where not between statement in your query. */ - public whereNotBetween(column: ModelColumns, values: [any, any]): this { + public whereNotBetween(column: ModelColumns, values: [any, any]) { const name = this.schema.getColumnNameByProperty(column) super.whereNotBetween(name, values) @@ -693,7 +808,7 @@ export class ModelQueryBuilder< /** * Set a where null statement in your query. */ - public whereNull(column: ModelColumns): this { + public whereNull(column: ModelColumns) { const name = this.schema.getColumnNameByProperty(column) super.whereNull(name) @@ -704,7 +819,7 @@ export class ModelQueryBuilder< /** * Set a where not null statement in your query. */ - public whereNotNull(column: ModelColumns): this { + public whereNotNull(column: ModelColumns) { const name = this.schema.getColumnNameByProperty(column) super.whereNotNull(name) @@ -720,11 +835,7 @@ export class ModelQueryBuilder< /** * Set a orWhere statement in your query. */ - public orWhere( - statement: any, - operation?: any | Operations, - value?: any - ): this { + public orWhere(statement: any, operation?: any | Operations, value?: any) { if (!operation) { const parsed = this.schema.propertiesToColumnNames(statement) @@ -747,7 +858,7 @@ export class ModelQueryBuilder< /** * Set a orWhere not statement in your query. */ - public orWhereNot(statement: any, value?: any): this { + public orWhereNot(statement: any, value?: any) { if (!value) { const parsed = this.schema.propertiesToColumnNames(statement) @@ -770,7 +881,7 @@ export class ModelQueryBuilder< /** * Set a orWhere like statement in your query. */ - public orWhereLike(statement: any, value?: any): this { + public orWhereLike(statement: any, value?: any) { if (!value) { const parsed = this.schema.propertiesToColumnNames(statement) @@ -793,7 +904,7 @@ export class ModelQueryBuilder< /** * Set a orWhere ILike statement in your query. */ - public orWhereILike(statement: any, value?: any): this { + public orWhereILike(statement: any, value?: any) { if (!value) { const parsed = this.schema.propertiesToColumnNames(statement) @@ -812,7 +923,7 @@ export class ModelQueryBuilder< /** * Set a orWhere in statement in your query. */ - public orWhereIn(column: ModelColumns, values: any[]): this { + public orWhereIn(column: ModelColumns, values: any[]) { const name = this.schema.getColumnNameByProperty(column) super.orWhereIn(name, values) @@ -823,7 +934,7 @@ export class ModelQueryBuilder< /** * Set a orWhere not in statement in your query. */ - public orWhereNotIn(column: ModelColumns, values: any[]): this { + public orWhereNotIn(column: ModelColumns, values: any[]) { const name = this.schema.getColumnNameByProperty(column) super.orWhereNotIn(name, values) @@ -834,7 +945,7 @@ export class ModelQueryBuilder< /** * Set a orWhere between statement in your query. */ - public orWhereBetween(column: ModelColumns, values: [any, any]): this { + public orWhereBetween(column: ModelColumns, values: [any, any]) { const name = this.schema.getColumnNameByProperty(column) super.orWhereBetween(name, values) @@ -845,7 +956,7 @@ export class ModelQueryBuilder< /** * Set a orWhere not between statement in your query. */ - public orWhereNotBetween(column: ModelColumns, values: [any, any]): this { + public orWhereNotBetween(column: ModelColumns, values: [any, any]) { const name = this.schema.getColumnNameByProperty(column) super.orWhereNotBetween(name, values) @@ -856,7 +967,7 @@ export class ModelQueryBuilder< /** * Set a orWhere null statement in your query. */ - public orWhereNull(column: ModelColumns): this { + public orWhereNull(column: ModelColumns) { const name = this.schema.getColumnNameByProperty(column) super.orWhereNull(name) @@ -867,7 +978,7 @@ export class ModelQueryBuilder< /** * Set a orWhere not null statement in your query. */ - public orWhereNotNull(column: ModelColumns): this { + public orWhereNotNull(column: ModelColumns) { const name = this.schema.getColumnNameByProperty(column) super.orWhereNotNull(name) @@ -878,7 +989,7 @@ export class ModelQueryBuilder< /** * Set an order by statement in your query. */ - public orderBy(column: ModelColumns, direction: Direction = 'ASC'): this { + public orderBy(column: ModelColumns, direction: Direction = 'ASC') { const name = this.schema.getColumnNameByProperty(column) super.orderBy(name, direction) @@ -890,7 +1001,7 @@ export class ModelQueryBuilder< * Order the results easily by the latest date. By default, the result will * be ordered by the table's "createdAt" column. */ - public latest(column?: ModelColumns): this { + public latest(column?: ModelColumns) { if (!column) { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore @@ -908,7 +1019,7 @@ export class ModelQueryBuilder< * Order the results easily by the oldest date. By default, the result will * be ordered by the table's "createdAt" column. */ - public oldest(column?: ModelColumns): this { + public oldest(column?: ModelColumns) { if (!column) { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore @@ -922,6 +1033,30 @@ export class ModelQueryBuilder< return this } + /** + * Set the internal selected properties and soft delete + * queries. + */ + private setInternalQueries(options?: { + addSelect?: boolean + addSoftDelete?: boolean + }) { + options = Options.create(options, { + addSelect: true, + addSoftDelete: true + }) + + if (options.addSelect) { + super.select(...this.selectColumns) + } + + if (options.addSoftDelete) { + super.when(this.isSoftDelete, query => + query.whereNull(this.DELETED_AT_NAME as any) + ) + } + } + /** * Verify that columns with `isNullable` property * can be created in database. diff --git a/src/models/factories/ModelFactory.ts b/src/models/factories/ModelFactory.ts index 47b042c..61e0558 100644 --- a/src/models/factories/ModelFactory.ts +++ b/src/models/factories/ModelFactory.ts @@ -69,7 +69,7 @@ export class ModelFactory { * Set the soft delete state in your model to * fabricate deleted data. */ - public trashed(): this { + public trashed() { this._trashed = true return this @@ -79,7 +79,7 @@ export class ModelFactory { * Remove the soft delete state in your model to * not fabricate deleted data. */ - public untrashed(): this { + public untrashed() { this._trashed = false return this diff --git a/src/models/factories/ModelGenerator.ts b/src/models/factories/ModelGenerator.ts index 2805096..b2ef3dd 100644 --- a/src/models/factories/ModelGenerator.ts +++ b/src/models/factories/ModelGenerator.ts @@ -77,7 +77,7 @@ export class ModelGenerator { Object.keys(object).forEach(key => { const column = this.schema.getColumnByName(key) - if (!column || column.isHidden) { + if (!column) { return } diff --git a/src/models/schemas/ModelSchema.ts b/src/models/schemas/ModelSchema.ts index eff23ed..39f4a33 100644 --- a/src/models/schemas/ModelSchema.ts +++ b/src/models/schemas/ModelSchema.ts @@ -180,9 +180,7 @@ export class ModelSchema { * as true. */ public getCreatedAtColumn(): ColumnOptions { - const columns = Annotation.getColumnsMeta(this.Model) - - return columns.find(c => c.isCreateDate) + return this.columns.find(c => c.isCreateDate) } /** @@ -190,9 +188,7 @@ export class ModelSchema { * as true. */ public getUpdatedAtColumn(): ColumnOptions { - const columns = Annotation.getColumnsMeta(this.Model) - - return columns.find(c => c.isUpdateDate) + return this.columns.find(c => c.isUpdateDate) } /** @@ -200,27 +196,47 @@ export class ModelSchema { * as true. */ public getDeletedAtColumn(): ColumnOptions { - const columns = Annotation.getColumnsMeta(this.Model) + return this.columns.find(c => c.isDeleteDate) + } + + /** + * Get all column properties as an array of string. + */ + public getAllColumnProperties(options?: { removeHidden: boolean }): string[] { + options = Options.create(options, { + removeHidden: false + }) + + return this.columns + .map(column => { + if (column.isHidden && options.removeHidden) { + return null + } - return columns.find(c => c.isDeleteDate) + return column.property + }) + .filter(Boolean) } /** * Get all columns where unique option is true. */ public getAllUniqueColumns(): ColumnOptions[] { - const columns = Annotation.getColumnsMeta(this.Model) + return this.columns.filter(column => column.isUnique) + } - return columns.filter(column => column.isUnique) + /** + * Get all columns where hidden option is true. + */ + public getAllHiddenColumns(): ColumnOptions[] { + return this.columns.filter(column => column.isHidden) } /** * Get all columns where nullable option is false. */ public getAllNotNullableColumns(): ColumnOptions[] { - const columns = Annotation.getColumnsMeta(this.Model) - - return columns.filter(column => !column.isNullable) + return this.columns.filter(column => !column.isNullable) } /** @@ -235,9 +251,7 @@ export class ModelSchema { * Get the column options by the column database name. */ public getColumnByName(column: string | ModelColumns): ColumnOptions { - const columns = Annotation.getColumnsMeta(this.Model) - - return columns.find(c => c.name === column) + return this.columns.find(c => c.name === column) } /** diff --git a/tests/fixtures/models/Order.ts b/tests/fixtures/models/Order.ts new file mode 100644 index 0000000..8673715 --- /dev/null +++ b/tests/fixtures/models/Order.ts @@ -0,0 +1,19 @@ +/** + * @athenna/database + * + * (c) João Lenon + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +import { BaseModel } from '#src/models/BaseModel' +import { Column } from '#src/models/annotations/Column' + +export class Order extends BaseModel { + @Column() + public id: string + + @Column({ isHidden: true }) + public price: string +} diff --git a/tests/unit/models/BaseModelTest.ts b/tests/unit/models/BaseModelTest.ts index 8176322..ad39612 100644 --- a/tests/unit/models/BaseModelTest.ts +++ b/tests/unit/models/BaseModelTest.ts @@ -240,6 +240,16 @@ export default class BaseModelTest { assert.deepEqual(data.id, '1') } + @Test() + public async shouldBeAbleToCreateValueInDatabaseUsingCreateMethodWithoutCleaningPersist({ assert }: Context) { + Mock.when(Database.driver, 'find').resolve(undefined) + Mock.when(Database.driver, 'createMany').resolve([{ id: '1' }]) + + const data = await User.create({}, false) + + assert.deepEqual(data.id, '1') + } + @Test() public async shouldBeAbleToCreateManyValuesInDatabaseUsingCreateManyMethod({ assert }: Context) { Mock.when(Database.driver, 'find').resolve(undefined) @@ -250,6 +260,18 @@ export default class BaseModelTest { assert.deepEqual(data[0].id, '1') } + @Test() + public async shouldBeAbleToCreateManyValuesInDatabaseUsingCreateManyMethodWithoutCleaningPersist({ + assert + }: Context) { + Mock.when(Database.driver, 'find').resolve(undefined) + Mock.when(Database.driver, 'createMany').resolve([{ id: '1' }]) + + const data = await User.createMany([{ id: '1' }], false) + + assert.deepEqual(data[0].id, '1') + } + @Test() public async shouldBeAbleToCreateValueInDatabaseUsingCreateOrUpdateMethod({ assert }: Context) { Mock.when(Database.driver, 'find').resolve(undefined) @@ -260,6 +282,16 @@ export default class BaseModelTest { assert.deepEqual(data.id, '1') } + @Test() + public async shouldBeAbleToCreateValueInDatabaseUsingCreateOrUpdateMethodWithoutCleaningPersist({ assert }: Context) { + Mock.when(Database.driver, 'find').resolve(undefined) + Mock.when(Database.driver, 'createMany').resolve([{ id: '1' }]) + + const data = (await User.createOrUpdate({ id: '1' }, { id: '1' }, false)) as User + + assert.deepEqual(data.id, '1') + } + @Test() public async shouldBeAbleToUpdateValueInDatabaseUsingCreateOrUpdateMethod({ assert }: Context) { Mock.when(Database.driver, 'find').resolve({ id: '1' }) @@ -280,6 +312,16 @@ export default class BaseModelTest { assert.deepEqual(data.id, '1') } + @Test() + public async shouldBeAbleToUpdateValueInDatabaseUsingUpdateMethodWithoutCleaningPersist({ assert }: Context) { + Mock.when(Database.driver, 'find').resolve(undefined) + Mock.when(Database.driver, 'update').resolve({ id: '1' }) + + const data = (await User.update({ id: '1' }, { id: '1' }, false)) as User + + assert.deepEqual(data.id, '1') + } + @Test() public async shouldBeAbleToUpdateManyValuesInDatabaseUsingUpdateMethod({ assert }: Context) { Mock.when(Database.driver, 'find').resolve(undefined) diff --git a/tests/unit/models/builders/ModelQueryBuilderTest.ts b/tests/unit/models/builders/ModelQueryBuilderTest.ts index b221360..b4ce951 100644 --- a/tests/unit/models/builders/ModelQueryBuilderTest.ts +++ b/tests/unit/models/builders/ModelQueryBuilderTest.ts @@ -11,6 +11,7 @@ import { Config } from '@athenna/config' import { Database } from '#src/facades/Database' import { Collection, Path } from '@athenna/common' import { User } from '#tests/fixtures/models/User' +import { Order } from '#tests/fixtures/models/Order' import { DatabaseProvider } from '#src/providers/DatabaseProvider' import { UniqueValueException } from '#src/exceptions/UniqueValueException' import { UserNotSoftDelete } from '#tests/fixtures/models/UserNotSoftDelete' @@ -34,6 +35,24 @@ export default class ModelQueryBuilderTest { User.setAttributes(true).uniqueValidation(true).nullableValidation(true) } + @Test() + public async shouldBeAbleToAutomaticallySetNoHiddenFieldsToModelQueryBuilder({ assert }: Context) { + Mock.when(Database.driver, 'select').resolve(undefined) + + await Order.query().find() + + assert.calledOnceWith(Database.driver.select, 'id') + } + + @Test() + public async shouldBeAbleToSetHiddenFieldsToModelQueryBuilder({ assert }: Context) { + Mock.when(Database.driver, 'select').resolve(undefined) + + await Order.query().withHidden().find() + + assert.calledWith(Database.driver.select, 'id', 'price') + } + @Test() public async shouldBeAbleToGetTheDriverClientOfTheModelQueryBuilder({ assert }: Context) { const result = await User.query().getClient() @@ -426,6 +445,17 @@ export default class ModelQueryBuilderTest { assert.notCalledWith(Database.driver.createMany, [Mock.match({ score: 200 })]) } + @Test() + public async shouldBeAbleToCreateDataAndDoNotCleanPersist({ assert }: Context) { + Mock.when(Database.driver, 'find').resolve(undefined) + Mock.when(Database.driver, 'createMany').resolve([{ id: '3', score: 1 }]) + + const result = await User.query().create({ score: 1 }, false) + + assert.calledOnceWith(Database.driver.createMany, [Mock.match({ score: 1 })]) + assert.instanceOf(result, User) + } + @Test() public async shouldBeAbleToAutomaticallyDefineDefaultAttributesWhenUsingCreateMethod({ assert }: Context) { const dataToCreate = { name: 'New User' } @@ -598,6 +628,19 @@ export default class ModelQueryBuilderTest { assert.notCalledWith(Database.driver.createMany, [Mock.match({ score: 200 })]) } + @Test() + public async shouldBeAbleToCreateDataUsingCreateOrUpdateDataResolvingObjectWithoutCleaningPersist({ + assert + }: Context) { + const dataToCreateOrUpdate = { id: '1', name: 'Updated User', score: 200 } + Mock.when(Database.driver, 'find').resolve(undefined) + Mock.when(Database.driver, 'createMany').resolve([dataToCreateOrUpdate]) + + await User.query().uniqueValidation(false).createOrUpdate(dataToCreateOrUpdate, false) + + assert.calledWith(Database.driver.createMany, [Mock.match({ score: 200 })]) + } + @Test() public async shouldBeAbleToAutomaticallyDefineDefaultAttributesWhenUsingCreateOrUpdateMethodToCreateData({ assert @@ -626,6 +669,18 @@ export default class ModelQueryBuilderTest { assert.instanceOf(result, User) } + @Test() + public async shouldBeAbleToCreateDataUsingCreateOrUpdateWithoutCleaningPersist({ assert }: Context) { + const dataToCreateOrUpdate = { id: '1', name: 'Updated User', score: 200 } + Mock.when(Database.driver, 'find').resolve(undefined) + Mock.when(Database.driver, 'createMany').resolve([dataToCreateOrUpdate]) + + const result = await User.query().uniqueValidation(false).createOrUpdate(dataToCreateOrUpdate, false) + + assert.calledOnceWith(Database.driver.createMany, [Mock.match({ score: 200 })]) + assert.instanceOf(result, User) + } + @Test() public async shouldBeAbleToUpdateDataUsingCreateOrUpdate({ assert }: Context) { const dataToCreateOrUpdate = { id: '1', name: 'Updated User' } @@ -637,6 +692,17 @@ export default class ModelQueryBuilderTest { assert.instanceOf(result, User) } + @Test() + public async shouldBeAbleToUpdateDataUsingCreateOrUpdateWithoutCleaningPersist({ assert }: Context) { + const dataToCreateOrUpdate = { id: '1', name: 'Updated User', score: 200 } + Mock.when(Database.driver, 'update').resolve(dataToCreateOrUpdate) + + const result = await User.query().uniqueValidation(false).createOrUpdate(dataToCreateOrUpdate, false) + + assert.calledOnceWith(Database.driver.update, Mock.match({ name: 'Updated User', score: 200 })) + assert.instanceOf(result, User) + } + @Test() public async shouldBeAbleToUpdateDataUsingCreateOrUpdateParsingColumnNames({ assert }: Context) { Mock.when(Database.driver, 'update').resolve({ id: '1', name: 'Updated User', rate: 1 }) @@ -706,13 +772,23 @@ export default class ModelQueryBuilderTest { @Test() public async shouldBeAbleToUpdateData({ assert }: Context) { - const dataToUpdate = { name: 'Updated User' } Mock.when(Database.driver, 'find').resolve(undefined) - Mock.when(Database.driver, 'update').resolve({ id: '1', ...dataToUpdate }) + Mock.when(Database.driver, 'update').resolve({ id: '1', name: 'Updated User' }) - const result = await User.query().update(dataToUpdate) + const result = await User.query().update({ name: 'Updated User' }) - assert.calledOnceWith(Database.driver.update, Mock.match(dataToUpdate)) + assert.calledOnceWith(Database.driver.update, Mock.match({ name: 'Updated User' })) + assert.instanceOf(result, User) + } + + @Test() + public async shouldBeAbleToUpdateDataUsingUpdateMethodWithoutCleaningPersist({ assert }: Context) { + Mock.when(Database.driver, 'find').resolve(undefined) + Mock.when(Database.driver, 'update').resolve({ id: '1', name: 'Updated User', score: 200 }) + + const result = await User.query().update({ name: 'Updated User', score: 200 }, false) + + assert.calledOnceWith(Database.driver.update, Mock.match({ name: 'Updated User', score: 200 })) assert.instanceOf(result, User) } @@ -725,25 +801,22 @@ export default class ModelQueryBuilderTest { @Test() public async shouldBeAbleToUpdateDataAndUpdateUpdatedAtColumn({ assert }: Context) { - const dataToUpdate = { name: 'Updated User' } Mock.when(Database.driver, 'find').resolve(undefined) - Mock.when(Database.driver, 'update').resolve({ id: '1', ...dataToUpdate }) + Mock.when(Database.driver, 'update').resolve({ id: '1', name: 'Updated User' }) - const result = await User.query().update(dataToUpdate) + const result = await User.query().update({ name: 'Updated User' }) - assert.notCalledWith(Database.driver.update, dataToUpdate) - assert.calledOnceWith(Database.driver.update, Mock.match(dataToUpdate)) + assert.notCalledWith(Database.driver.update, { name: 'Updated User' }) + assert.calledOnceWith(Database.driver.update, Mock.match({ name: 'Updated User' })) assert.instanceOf(result, User) } @Test() public async shouldBeAbleToUpdateDataAndParseColumns({ assert }: Context) { - const dataToUpdate = { name: 'Updated User', rate: 1 } - const updatedData = { id: '1', ...dataToUpdate } Mock.when(Database.driver, 'find').resolve(undefined) - Mock.when(Database.driver, 'update').resolve(updatedData) + Mock.when(Database.driver, 'update').resolve({ id: '1', name: 'Updated User', rate_number: 1 }) - const result = await User.query().update(dataToUpdate) + const result = await User.query().update({ name: 'Updated User', rate: 1 }) assert.calledOnceWith(Database.driver.update, Mock.match({ rate_number: 1 })) assert.instanceOf(result, User) @@ -752,9 +825,9 @@ export default class ModelQueryBuilderTest { @Test() public async shouldBeAbleToUpdateDataIgnoringPersistColumns({ assert }: Context) { const dataToUpdate = { name: 'Updated User', score: 200 } - const updatedData = { id: '1', ...dataToUpdate } + Mock.when(Database.driver, 'find').resolve(undefined) - Mock.when(Database.driver, 'update').resolve(updatedData) + Mock.when(Database.driver, 'update').resolve({ id: '1', name: 'Updated User', score: 200 }) await User.query().update(dataToUpdate) @@ -899,7 +972,7 @@ export default class ModelQueryBuilderTest { public async shouldAutomaticallySetWhereNullDeletedAtClauseWhenIsDeleteDateColumnOptionIsTrue({ assert }: Context) { Mock.when(Database.driver, 'whereNull').return(undefined) - User.query() + await User.query().find() assert.calledOnceWith(Database.driver.whereNull, 'deletedAt') } @@ -915,6 +988,42 @@ export default class ModelQueryBuilderTest { assert.notCalled(Database.driver.whereNull) } + @Test() + public async shouldBeAbleToGetSoftDeletedData({ assert }: Context) { + Mock.when(Database.driver, 'whereNull').resolve(undefined) + Mock.when(Database.driver, 'whereNotNull').resolve(undefined) + Mock.when(Database.driver, 'find').resolve({ id: '1' }) + + const result = await User.query().withTrashed().find() + + assert.instanceOf(result, User) + assert.calledOnceWith(Database.driver.whereNull, 'deletedAt') + assert.calledOnceWith(Database.driver.whereNotNull, 'deletedAt') + } + + @Test() + public async shouldBeAbleToGetOnlySoftDeletedData({ assert }: Context) { + Mock.when(Database.driver, 'whereNull').resolve(undefined) + Mock.when(Database.driver, 'whereNotNull').resolve(undefined) + Mock.when(Database.driver, 'find').resolve({ id: '1' }) + + const result = await User.query().onlyTrashed().find() + + assert.instanceOf(result, User) + assert.notCalled(Database.driver.whereNull) + assert.calledOnceWith(Database.driver.whereNotNull, 'deletedAt') + } + + @Test() + public async shouldBeAbleToRestoreSoftDeletedDate({ assert }: Context) { + Mock.when(Database.driver, 'update').resolve({ id: '1' }) + + const result = await User.query().uniqueValidation(false).restore() + + assert.instanceOf(result, User) + assert.calledOnceWith(Database.driver.update, Mock.match({ deletedAt: null })) + } + @Test() public async shouldBeAbleToForceDeleteDataWhenUsingSoftDelete({ assert }: Context) { Mock.when(Database.driver, 'delete').resolve(undefined) @@ -1005,18 +1114,20 @@ export default class ModelQueryBuilderTest { const columns: any[] = ['id', 'name'] Mock.when(Database.driver, 'select').resolve(undefined) - User.query().select(...columns) + await User.query() + .select(...columns) + .find() - assert.calledOnceWith(Database.driver.select, ...columns) + assert.calledWith(Database.driver.select, ...columns) } @Test() public async shouldAllowSelectingSpecificColumnsFromTableParsingColumnNames({ assert }: Context) { Mock.when(Database.driver, 'select').resolve(undefined) - User.query().select('id', 'name', 'rate') + await User.query().select('id', 'name', 'rate').find() - assert.calledOnceWith(Database.driver.select, 'id', 'name', 'rate_number') + assert.calledWith(Database.driver.select, 'id', 'name', 'rate_number') } @Test() @@ -1035,11 +1146,12 @@ export default class ModelQueryBuilderTest { Mock.when(Database.driver, 'select').resolve(undefined) Mock.when(Database.driver, 'from').resolve(undefined) - User.query() + await User.query() .select(...columns) .from('users') + .find() - assert.calledOnceWith(Database.driver.select, ...columns) + assert.calledWith(Database.driver.select, ...columns) assert.calledOnceWith(Database.driver.from, 'users') } @@ -1048,9 +1160,9 @@ export default class ModelQueryBuilderTest { Mock.when(Database.driver, 'select').resolve(undefined) Mock.when(Database.driver, 'from').resolve(undefined) - User.query().select('id', 'name', 'rate').from('users') + await User.query().select('id', 'name', 'rate').from('users').find() - assert.calledOnceWith(Database.driver.select, 'id', 'name', 'rate_number') + assert.calledWith(Database.driver.select, 'id', 'name', 'rate_number') assert.calledOnceWith(Database.driver.from, 'users') } diff --git a/tests/unit/models/factories/ModelGeneratorTest.ts b/tests/unit/models/factories/ModelGeneratorTest.ts index a5e3195..b001b77 100644 --- a/tests/unit/models/factories/ModelGeneratorTest.ts +++ b/tests/unit/models/factories/ModelGeneratorTest.ts @@ -56,21 +56,6 @@ export default class ModelGeneratorTest { assert.isUndefined(data) } - @Test() - public async shouldNotSetPropertyInModelThatGotOptionIsHiddenAsTrueWhenUsingGeneratorOne({ assert }: Context) { - class User extends BaseModel { - @Column({ name: '_id' }) - public id: string - - @Column({ isHidden: true }) - public name?: string - } - - const data = await new ModelGenerator(User, User.schema()).generateOne({ _id: '1', name: 'lenon' }) - - assert.deepEqual(data, { id: '1', original: { id: '1' } }) - } - @Test() public async shouldBeAbleToGenerateManyInstancesOfModel({ assert }: Context) { class User extends BaseModel { @@ -119,21 +104,6 @@ export default class ModelGeneratorTest { assert.isEmpty(data) } - @Test() - public async shouldNotSetPropertyInModelThatGotOptionIsHiddenAsTrueWhenUsingGenerateMany({ assert }: Context) { - class User extends BaseModel { - @Column({ name: '_id' }) - public id: string - - @Column({ isHidden: true }) - public name?: string - } - - const data = await new ModelGenerator(User, User.schema()).generateMany([{ _id: '1', name: 'lenon' }]) - - assert.deepEqual(data, [{ id: '1', original: { id: '1' } }]) - } - @Test() public async shouldBeAbleToGenerateOneModelAndIncludeAHasOneRelation({ assert }: Context) { class Profile extends BaseModel {