@athenna/database
Version:
The Athenna database handler for SQL/NoSQL.
685 lines (684 loc) • 17.3 kB
JavaScript
/**
* @athenna/database
*
* (c) João Lenon <lenon@athenna.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import { Macroable } from '@athenna/common';
export class QueryBuilder extends Macroable {
/**
* Creates a new instance of QueryBuilder.
*/
constructor(driver, tableName) {
super();
this.driver = driver;
this.driver.table(tableName);
}
/**
* Return the client of driver.
*/
getClient() {
return this.driver.getClient();
}
/**
* Return the query builder of driver.
*/
getQueryBuilder() {
return this.driver.getQueryBuilder();
}
/**
* Set the driver primary key that will be used
* when creating new data.
*/
setPrimaryKey(primaryKey) {
this.driver.setPrimaryKey(primaryKey);
return this;
}
/**
* Calculate the average of a given column.
*/
async avg(column) {
return this.driver.avg(column);
}
/**
* Calculate the average of a given column.
*/
async avgDistinct(column) {
return this.driver.avgDistinct(column);
}
/**
* Get the max number of a given column.
*/
async max(column) {
return this.driver.max(column);
}
/**
* Get the min number of a given column.
*/
async min(column) {
return this.driver.min(column);
}
/**
* Sum all numbers of a given column.
*/
async sum(column) {
return this.driver.sum(column);
}
/**
* Sum all numbers of a given column.
*/
async sumDistinct(column) {
return this.driver.sumDistinct(column);
}
/**
* Increment a value of a given column.
*/
async increment(column) {
await this.driver.increment(column);
}
/**
* Decrement a value of a given column.
*/
async decrement(column) {
await this.driver.decrement(column);
}
/**
* Calculate the average of a given column using distinct.
*/
async count(column = '*') {
return this.driver.count(column);
}
/**
* Calculate the average of a given column using distinct.
*/
async countDistinct(column) {
return this.driver.countDistinct(column);
}
/**
* Find a value in database or throw exception if undefined.
*/
async findOrFail() {
return this.driver.findOrFail();
}
/**
* Return a single data or, if no results are found,
* execute the given closure.
*/
async findOr(callback) {
return this.driver.findOr(callback);
}
/**
* Find value in database but returns only the value of
* selected column directly.
*/
async pluck(column) {
return this.driver.pluck(column);
}
/**
* Find many values in database but returns only the
* values of selected column directly.
*/
async pluckMany(column) {
return this.driver.pluckMany(column);
}
/**
* Find a value in database.
*/
async find() {
return this.driver.find();
}
/**
* Find a value in database and return as boolean.
*/
async exists() {
return this.driver.exists();
}
/**
* Find many values in database.
*/
async findMany() {
return this.driver.findMany();
}
/**
* Find many values in database and return as a Collection.
*/
async collection() {
return this.driver.collection();
}
/**
* Find many values in database and return as paginated response.
*/
async paginate(page = { page: 0, limit: 10, resourceUrl: '/' }, limit = 10, resourceUrl = '/') {
return this.driver.paginate(page, limit, resourceUrl);
}
/**
* Create a value in database.
*/
async create(data) {
return this.driver.create(data);
}
/**
* Create many values in database.
*/
async createMany(data) {
return this.driver.createMany(data);
}
/**
* Create data or update if already exists.
*/
async createOrUpdate(data) {
return this.driver.createOrUpdate(data);
}
/**
* Update data in database.
*/
async update(data) {
return this.driver.update(data);
}
/**
* Delete data in database.
*/
async delete() {
return this.driver.delete();
}
/**
* Make a raw query in database.
*/
raw(sql, bindings) {
return this.driver.raw(sql, bindings);
}
/**
* Set a new table to work with in query builder.
*/
table(tableName) {
this.driver.table(tableName);
return this;
}
/**
* Executes the given closure when the first argument is true.
*/
when(criteria, closure) {
if (criteria) {
closure(this, criteria);
return this;
}
return this;
}
/**
* Log in console the actual query built.
*/
dump() {
this.driver.dump();
return this;
}
/**
* Set the columns that should be selected on query.
*/
select(...columns) {
this.driver.select(...columns);
return this;
}
/**
* Set the columns that should be selected on query raw.
*/
selectRaw(sql, bindings) {
this.driver.selectRaw(sql, bindings);
return this;
}
/**
* Set the table that should be used on query.
* Different from `table()` method, this method
* doesn't change the driver table.
*/
from(table) {
this.driver.from(table);
return this;
}
/**
* Set the table that should be used on query raw.
* Different from `table()` method, this method
* doesn't change the driver table.
*/
fromRaw(sql, bindings) {
this.driver.fromRaw(sql, bindings);
return this;
}
/**
* Set a join statement in your query.
*/
join(tableName, column1, operation, column2) {
this.driver.join(tableName, column1, operation, column2);
return this;
}
/**
* Set a left join statement in your query.
*/
leftJoin(tableName, column1, operation, column2) {
this.driver.leftJoin(tableName, column1, operation, column2);
return this;
}
/**
* Set a right join statement in your query.
*/
rightJoin(tableName, column1, operation, column2) {
this.driver.rightJoin(tableName, column1, operation, column2);
return this;
}
/**
* Set a cross join statement in your query.
*/
crossJoin(tableName, column1, operation, column2) {
this.driver.crossJoin(tableName, column1, operation, column2);
return this;
}
/**
* Set a full outer join statement in your query.
*/
fullOuterJoin(tableName, column1, operation, column2) {
this.driver.fullOuterJoin(tableName, column1, operation, column2);
return this;
}
/**
* Set a left outer join statement in your query.
*/
leftOuterJoin(tableName, column1, operation, column2) {
this.driver.leftOuterJoin(tableName, column1, operation, column2);
return this;
}
/**
* Set a right outer join statement in your query.
*/
rightOuterJoin(tableName, column1, operation, column2) {
this.driver.rightOuterJoin(tableName, column1, operation, column2);
return this;
}
/**
* Set a join raw statement in your query.
*/
joinRaw(sql, bindings) {
this.driver.joinRaw(sql, bindings);
return this;
}
/**
* Set a group by statement in your query.
*/
groupBy(...columns) {
this.driver.groupBy(...columns);
return this;
}
/**
* Set a group by raw statement in your query.
*/
groupByRaw(sql, bindings) {
this.driver.groupByRaw(sql, bindings);
return this;
}
/**
* Set a having statement in your query.
*/
having(column, operation, value) {
this.driver.having(column, operation, value);
return this;
}
/**
* Set a having raw statement in your query.
*/
havingRaw(sql, bindings) {
this.driver.havingRaw(sql, bindings);
return this;
}
/**
* Set a having exists statement in your query.
*/
havingExists(closure) {
this.driver.havingExists(closure);
return this;
}
/**
* Set a having not exists statement in your query.
*/
havingNotExists(closure) {
this.driver.havingNotExists(closure);
return this;
}
/**
* Set a having in statement in your query.
*/
havingIn(column, values) {
this.driver.havingIn(column, values);
return this;
}
/**
* Set a having not in statement in your query.
*/
havingNotIn(column, values) {
this.driver.havingNotIn(column, values);
return this;
}
/**
* Set a having between statement in your query.
*/
havingBetween(column, values) {
this.driver.havingBetween(column, values);
return this;
}
/**
* Set a having not between statement in your query.
*/
havingNotBetween(column, values) {
this.driver.havingNotBetween(column, values);
return this;
}
/**
* Set a having null statement in your query.
*/
havingNull(column) {
this.driver.havingNull(column);
return this;
}
/**
* Set a having not null statement in your query.
*/
havingNotNull(column) {
this.driver.havingNotNull(column);
return this;
}
/**
* Set an or having statement in your query.
*/
orHaving(column, operation, value) {
this.driver.orHaving(column, operation, value);
return this;
}
/**
* Set an or having raw statement in your query.
*/
orHavingRaw(sql, bindings) {
this.driver.orHavingRaw(sql, bindings);
return this;
}
/**
* Set an or having exists statement in your query.
*/
orHavingExists(closure) {
this.driver.orHavingExists(closure);
return this;
}
/**
* Set an or having not exists statement in your query.
*/
orHavingNotExists(closure) {
this.driver.orHavingNotExists(closure);
return this;
}
/**
* Set an or having in statement in your query.
*/
orHavingIn(column, values) {
this.driver.orHavingIn(column, values);
return this;
}
/**
* Set an or having not in statement in your query.
*/
orHavingNotIn(column, values) {
this.driver.orHavingNotIn(column, values);
return this;
}
/**
* Set an or having between statement in your query.
*/
orHavingBetween(column, values) {
this.driver.orHavingBetween(column, values);
return this;
}
/**
* Set an or having not between statement in your query.
*/
orHavingNotBetween(column, values) {
this.driver.orHavingNotBetween(column, values);
return this;
}
/**
* Set an or having null statement in your query.
*/
orHavingNull(column) {
this.driver.orHavingNull(column);
return this;
}
/**
* Set an or having not null statement in your query.
*/
orHavingNotNull(column) {
this.driver.orHavingNotNull(column);
return this;
}
/**
* Set a where statement in your query.
*/
where(statement, operation, value) {
this.driver.where(statement, operation, value);
return this;
}
/**
* Set a where not statement in your query.
*/
whereNot(statement, value) {
this.driver.whereNot(statement, value);
return this;
}
/**
* Set a where raw statement in your query.
*/
whereRaw(sql, bindings) {
this.driver.whereRaw(sql, bindings);
return this;
}
/**
* Set a where exists statement in your query.
*/
whereExists(closure) {
this.driver.whereExists(closure);
return this;
}
/**
* Set a where not exists statement in your query.
*/
whereNotExists(closure) {
this.driver.whereNotExists(closure);
return this;
}
/**
* Set a where like statement in your query.
*/
whereLike(column, value) {
this.driver.whereLike(column, value);
return this;
}
/**
* Set a where ILike statement in your query.
*/
whereILike(column, value) {
this.driver.whereILike(column, value);
return this;
}
/**
* Set a where in statement in your query.
*/
whereIn(column, values) {
this.driver.whereIn(column, values);
return this;
}
/**
* Set a where not in statement in your query.
*/
whereNotIn(column, values) {
this.driver.whereNotIn(column, values);
return this;
}
/**
* Set a where between statement in your query.
*/
whereBetween(column, values) {
this.driver.whereBetween(column, values);
return this;
}
/**
* Set a where not between statement in your query.
*/
whereNotBetween(column, values) {
this.driver.whereNotBetween(column, values);
return this;
}
/**
* Set a where null statement in your query.
*/
whereNull(column) {
this.driver.whereNull(column);
return this;
}
/**
* Set a where not null statement in your query.
*/
whereNotNull(column) {
this.driver.whereNotNull(column);
return this;
}
/**
* Set a or where statement in your query.
*/
orWhere(statement, operation, value) {
this.driver.orWhere(statement, operation, value);
return this;
}
/**
* Set a where not statement in your query.
*/
orWhereNot(statement, value) {
this.driver.orWhereNot(statement, value);
return this;
}
/**
* Set a or where raw statement in your query.
*/
orWhereRaw(sql, bindings) {
this.driver.orWhereRaw(sql, bindings);
return this;
}
/**
* Set an or where exists statement in your query.
*/
orWhereExists(closure) {
this.driver.orWhereExists(closure);
return this;
}
/**
* Set an or where not exists statement in your query.
*/
orWhereNotExists(closure) {
this.driver.orWhereNotExists(closure);
return this;
}
/**
* Set an or where like statement in your query.
*/
orWhereLike(statement, value) {
this.driver.orWhereLike(statement, value);
return this;
}
/**
* Set an or where ILike statement in your query.
*/
orWhereILike(statement, value) {
this.driver.orWhereILike(statement, value);
return this;
}
/**
* Set an or where in statement in your query.
*/
orWhereIn(column, values) {
this.driver.orWhereIn(column, values);
return this;
}
/**
* Set an or where not in statement in your query.
*/
orWhereNotIn(column, values) {
this.driver.orWhereNotIn(column, values);
return this;
}
/**
* Set an or where between statement in your query.
*/
orWhereBetween(column, values) {
this.driver.orWhereBetween(column, values);
return this;
}
/**
* Set an or where not between statement in your query.
*/
orWhereNotBetween(column, values) {
this.driver.orWhereNotBetween(column, values);
return this;
}
/**
* Set an or where null statement in your query.
*/
orWhereNull(column) {
this.driver.orWhereNull(column);
return this;
}
/**
* Set an or where not null statement in your query.
*/
orWhereNotNull(column) {
this.driver.orWhereNotNull(column);
return this;
}
/**
* Set an order by statement in your query.
*/
orderBy(column, direction = 'ASC') {
this.driver.orderBy(column, direction.toUpperCase());
return this;
}
/**
* Set an order by raw statement in your query.
*/
orderByRaw(sql, bindings) {
this.driver.orderByRaw(sql, bindings);
return this;
}
/**
* Order the results easily by the latest date. By default, the result will
* be ordered by the table's "createdAt" column.
*/
latest(column = 'createdAt') {
this.driver.latest(column);
return this;
}
/**
* Order the results easily by the oldest date. By default, the result will
* be ordered by the table's "createdAt" column.
*/
oldest(column = 'createdAt') {
this.driver.oldest(column);
return this;
}
/**
* Set the skip number in your query.
*/
offset(number) {
this.driver.offset(number);
return this;
}
/**
* Set the limit number in your query.
*/
limit(number) {
this.driver.limit(number);
return this;
}
}