@athenna/database
Version:
The Athenna database handler for SQL/NoSQL.
792 lines (791 loc) • 18 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 { Collection, Exec, Is, Json, Options } from '@athenna/common';
import { Transaction } from '#src/database/transactions/Transaction';
import { WrongMethodException } from '#src/exceptions/WrongMethodException';
import { NotFoundDataException } from '#src/exceptions/NotFoundDataException';
import { NotConnectedDatabaseException } from '#src/exceptions/NotConnectedDatabaseException';
export class FakeDriver {
constructor(connection, client) {
FakeDriver.connection = connection;
if (client) {
FakeDriver.client = client;
FakeDriver.isConnected = true;
FakeDriver.isSavedOnFactory = true;
}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return FakeDriver;
}
static { this.primaryKey = 'id'; }
static { this.tables = ['fake']; }
static { this.databases = ['fake']; }
static { this.isConnected = false; }
static { this.isSavedOnFactory = false; }
static { this.tableName = 'fake'; }
static { this.connection = 'fake'; }
static { this.client = null; }
static { this.qb = null; }
static { this.useSetQB = false; }
static joinByType() {
return this;
}
static getKnex() { }
static getMongoose() { }
static clone() {
return Json.copy(FakeDriver);
}
static setPrimaryKey(primaryKey) {
this.primaryKey = primaryKey;
return this;
}
static getClient() {
return this.client;
}
static setClient(client) {
this.client = client;
return this;
}
static getQueryBuilder() {
return this.client;
}
static setQueryBuilder(client) {
this.client = client;
return this;
}
/**
* Connect to database.
*/
static connect(options = {}) {
options = Options.create(options, {
force: false,
saveOnFactory: true,
connect: true
});
if (!options.connect) {
return;
}
if (this.isConnected && !options.force) {
return;
}
this.isConnected = true;
this.isSavedOnFactory = options.saveOnFactory;
}
/**
* Close the connection with database in this instance.
*/
static async close() {
if (!this.isConnected) {
return;
}
this.tableName = null;
this.isConnected = false;
}
/**
* Creates a new instance of query builder.
*/
static query() {
if (!this.isConnected) {
throw new NotConnectedDatabaseException();
}
return {};
}
/**
* Sync a model schema with database.
*/
static async sync() { }
/**
* Create a new transaction.
*/
static async startTransaction() {
return new Transaction(FakeDriver);
}
/**
* Commit the transaction.
*/
static async commitTransaction() {
this.tableName = null;
this.client = null;
this.isConnected = false;
}
/**
* Rollback the transaction.
*/
static async rollbackTransaction() {
this.tableName = null;
this.client = null;
this.isConnected = false;
}
/**
* Run database migrations.
*/
static async runMigrations() { }
/**
* Revert database migrations.
*/
static async revertMigrations() { }
/**
* List all databases available.
*/
static async getDatabases() {
return this.databases;
}
/**
* Get the current database name.
*/
static async getCurrentDatabase() {
return this.databases[0];
}
/**
* Verify if database exists.
*/
static async hasDatabase(database) {
const databases = await this.getDatabases();
return databases.includes(database);
}
/**
* Create a new database.
*/
static async createDatabase(database) {
this.databases.push(database);
}
/**
* Drop some database.
*/
static async dropDatabase(database) {
const index = this.databases.indexOf(database);
this.databases.splice(index, 1);
}
/**
* List all tables available.
*/
static async getTables() {
return this.tables;
}
/**
* Verify if table exists.
*/
static async hasTable(table) {
const tables = await this.getTables();
return tables.includes(table);
}
/**
* Create a new table in database.
*/
static async createTable(table) {
this.tables.push(table);
}
/**
* Drop a table in database.
*/
static async dropTable(table) {
const index = this.tables.indexOf(table);
this.tables.splice(index, 1);
}
/**
* Remove all data inside some database table
* and restart the identity of the table.
*/
static async truncate(table) {
this.tables.indexOf(table);
}
/**
* Make a raw query in database.
*/
static raw() {
return {};
}
/**
* Calculate the average of a given column.
*/
static async avg() {
return '1';
}
/**
* Calculate the average of a given column using distinct.
*/
static async avgDistinct() {
return '1';
}
/**
* Get the max number of a given column.
*/
static async max() {
return '1';
}
/**
* Get the min number of a given column.
*/
static async min() {
return '1';
}
/**
* Sum all numbers of a given column.
*/
static async sum() {
return '1';
}
/**
* Sum all numbers of a given column in distinct mode.
*/
static async sumDistinct() {
return '1';
}
/**
* Increment a value of a given column.
*/
static async increment() { }
/**
* Decrement a value of a given column.
*/
static async decrement() { }
/**
* Calculate the average of a given column using distinct.
*/
static async count() {
return '1';
}
/**
* Calculate the average of a given column using distinct.
*/
static async countDistinct() {
return '1';
}
/**
* Find value in database but returns only the value of
* selected column directly.
*/
static async pluck() {
return '';
}
/**
* Find many values in database but returns only the
* values of selected column directly.
*/
static async pluckMany() {
return [''];
}
/**
* Find a value in database.
*/
static async find() {
return {};
}
/**
* Find a value in database and return as boolean.
*/
static async exists() {
const data = await this.find();
return !!data;
}
/**
* Find a value in database or fail.
*/
static async findOrFail() {
const data = await this.find();
if (!data) {
throw new NotFoundDataException(this.connection);
}
return data;
}
/**
* Find a value in database or execute closure.
*/
static async findOr() {
return {};
}
/**
* Executes the given closure when the first argument is true.
*/
static when(criteria, closure) {
if (!criteria) {
return this;
}
closure(this, criteria);
return this;
}
/**
* Find many values in database.
*/
static async findMany() {
return [{}];
}
/**
* Find many values in database and return as a Collection.
*/
static async collection() {
return new Collection(await this.findMany());
}
/**
* Find many values in database and return as paginated response.
*/
static async paginate(page = { page: 0, limit: 10, resourceUrl: '/' }, limit = 10, resourceUrl = '/') {
if (Is.Number(page)) {
page = { page, limit, resourceUrl };
}
const data = await this.findMany();
return Exec.pagination(data, data.length, page);
}
/**
* Create a value in database.
*/
static async create(data = {}) {
if (Is.Array(data)) {
throw new WrongMethodException('create', 'createMany');
}
const created = await this.createMany([data]);
return created[0];
}
/**
* Create many values in database.
*/
static async createMany(data = []) {
if (!Is.Array(data)) {
throw new WrongMethodException('createMany', 'create');
}
return data;
}
/**
* Create data or update if already exists.
*/
static async createOrUpdate(data = {}) {
return data;
}
/**
* Update a value in database.
*/
static async update(data) {
return data;
}
/**
* Delete one value in database.
*/
static async delete() { }
/**
* Set the table that this query will be executed.
*/
static table(table) {
if (!this.isConnected) {
throw new NotConnectedDatabaseException();
}
this.tableName = table;
return this;
}
/**
* Log in console the actual query built.
*/
static dump() {
return this;
}
/**
* Set the columns that should be selected on query.
*/
static select(...columns) {
if (columns.includes('*')) {
return this;
}
return this;
}
/**
* Set the columns that should be selected on query raw.
*/
static selectRaw() {
return this;
}
/**
* Set the table that should be used on query.
* Different from `table()` method, this method
* doesn't change the driver table.
*/
static from() {
return this;
}
/**
* Set the table that should be used on query raw.
* Different from `table()` method, this method
* doesn't change the driver table.
*/
static fromRaw() {
return this;
}
/**
* Set a join statement in your query.
*/
static join() {
return this;
}
/**
* Set a left join statement in your query.
*/
static leftJoin() {
return this;
}
/**
* Set a right join statement in your query.
*/
static rightJoin() {
return this;
}
/**
* Set a cross join statement in your query.
*/
static crossJoin() {
return this;
}
/**
* Set a full outer join statement in your query.
*/
static fullOuterJoin() {
return this;
}
/**
* Set a left outer join statement in your query.
*/
static leftOuterJoin() {
return this;
}
/**
* Set a right outer join statement in your query.
*/
static rightOuterJoin() {
return this;
}
/**
* Set a join raw statement in your query.
*/
static joinRaw() {
return this;
}
/**
* Set a group by statement in your query.
*/
static groupBy() {
return this;
}
/**
* Set a group by raw statement in your query.
*/
static groupByRaw() {
return this;
}
/**
* Set a having statement in your query.
*/
static having() {
return this;
}
/**
* Set a having raw statement in your query.
*/
static havingRaw() {
return this;
}
/**
* Set a having exists statement in your query.
*/
static havingExists() {
return this;
}
/**
* Set a having not exists statement in your query.
*/
static havingNotExists() {
return this;
}
/**
* Set a having in statement in your query.
*/
static havingIn() {
return this;
}
/**
* Set a having not in statement in your query.
*/
static havingNotIn() {
return this;
}
/**
* Set a having between statement in your query.
*/
static havingBetween() {
return this;
}
/**
* Set a having not between statement in your query.
*/
static havingNotBetween() {
return this;
}
/**
* Set a having null statement in your query.
*/
static havingNull() {
return this;
}
/**
* Set a having not null statement in your query.
*/
static havingNotNull() {
return this;
}
/**
* Set an or having statement in your query.
*/
static orHaving() {
return this;
}
/**
* Set an or having raw statement in your query.
*/
static orHavingRaw() {
return this;
}
/**
* Set an or having exists statement in your query.
*/
static orHavingExists() {
return this;
}
/**
* Set an or having not exists statement in your query.
*/
static orHavingNotExists() {
return this;
}
/**
* Set an or having in statement in your query.
*/
static orHavingIn() {
return this;
}
/**
* Set an or having not in statement in your query.
*/
static orHavingNotIn() {
return this;
}
/**
* Set an or having between statement in your query.
*/
static orHavingBetween() {
return this;
}
/**
* Set an or having not between statement in your query.
*/
static orHavingNotBetween() {
return this;
}
/**
* Set an or having null statement in your query.
*/
static orHavingNull() {
return this;
}
/**
* Set an or having not null statement in your query.
*/
static orHavingNotNull() {
return this;
}
/**
* Set a where statement in your query.
*/
static where() {
return this;
}
/**
* Set a where not statement in your query.
*/
static whereNot() {
return this;
}
/**
* Set a where raw statement in your query.
*/
static whereRaw() {
return this;
}
/**
* Set a where exists statement in your query.
*/
static whereExists() {
return this;
}
/**
* Set a where not exists statement in your query.
*/
static whereNotExists() {
return this;
}
/**
* Set a where like statement in your query.
*/
static whereLike() {
return this;
}
/**
* Set a where ILike statement in your query.
*/
static whereILike() {
return this;
}
/**
* Set a where in statement in your query.
*/
static whereIn() {
return this;
}
/**
* Set a where not in statement in your query.
*/
static whereNotIn() {
return this;
}
/**
* Set a where between statement in your query.
*/
static whereBetween() {
return this;
}
/**
* Set a where not between statement in your query.
*/
static whereNotBetween() {
return this;
}
/**
* Set a where null statement in your query.
*/
static whereNull() {
return this;
}
/**
* Set a where not null statement in your query.
*/
static whereNotNull() {
return this;
}
/**
* Set a or where statement in your query.
*/
static orWhere() {
return this;
}
/**
* Set an or where not statement in your query.
*/
static orWhereNot() {
return this;
}
/**
* Set a or where raw statement in your query.
*/
static orWhereRaw() {
return this;
}
/**
* Set an or where exists statement in your query.
*/
static orWhereExists() {
return this;
}
/**
* Set an or where not exists statement in your query.
*/
static orWhereNotExists() {
return this;
}
/**
* Set an or where like statement in your query.
*/
static orWhereLike() {
return this;
}
/**
* Set an or where ILike statement in your query.
*/
static orWhereILike() {
return this;
}
/**
* Set an or where in statement in your query.
*/
static orWhereIn() {
return this;
}
/**
* Set an or where not in statement in your query.
*/
static orWhereNotIn() {
return this;
}
/**
* Set an or where between statement in your query.
*/
static orWhereBetween() {
return this;
}
/**
* Set an or where not between statement in your query.
*/
static orWhereNotBetween() {
return this;
}
/**
* Set an or where null statement in your query.
*/
static orWhereNull() {
return this;
}
/**
* Set an or where not null statement in your query.
*/
static orWhereNotNull() {
return this;
}
/**
* Set an order by statement in your query.
*/
static orderBy() {
return this;
}
/**
* Set an order by raw statement in your query.
*/
static orderByRaw() {
return this;
}
/**
* Order the results easily by the latest date. By default, the result will
* be ordered by the table's "createdAt" column.
*/
static latest() {
return this;
}
/**
* Order the results easily by the oldest date. By default, the result will
* be ordered by the table's "createdAt" column.
*/
static oldest() {
return this;
}
/**
* Set the skip number in your query.
*/
static offset() {
return this;
}
/**
* Set the limit number in your query.
*/
static limit() {
return this;
}
}