Skip to content

Commit

Permalink
Merge pull request #116 from AthennaIO/develop
Browse files Browse the repository at this point in the history
feat(driver): add fake driver
  • Loading branch information
jlenon7 authored Dec 29, 2023
2 parents d80a45c + 9662043 commit ac9739b
Show file tree
Hide file tree
Showing 50 changed files with 2,059 additions and 1,121 deletions.
4 changes: 0 additions & 4 deletions bin/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ import { parseArgs } from 'node:util'
import { Runner } from '@athenna/test'
import { MongoMemory } from '#tests/helpers/MongoMemory'
import { command } from '@athenna/artisan/testing/plugins'
import { DriverFactory } from '#src/factories/DriverFactory'
import { FakeDriverClass } from '#tests/fixtures/drivers/FakeDriverClass'

DriverFactory.drivers.set('fake', { Driver: FakeDriverClass, client: null })

const { values } = parseArgs({
options: {
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/database",
"version": "4.13.0",
"version": "4.14.0",
"description": "The Athenna database handler for SQL/NoSQL.",
"license": "MIT",
"author": "João Lenon <[email protected]>",
Expand Down Expand Up @@ -109,7 +109,8 @@
"src/**/*.ts"
],
"exclude": [
"src/types/*"
"src/types/*",
"src/drivers/FakeDriver"
],
"reporter": [
"text-summary",
Expand Down
2 changes: 1 addition & 1 deletion src/commands/DbSeedCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class DbSeedCommand extends BaseCommand {
public async handle(): Promise<void> {
this.logger.simple('({bold,green} [ SEEDING DATABASE ])\n')

const DB = Database.connection(this.connection).connect()
const DB = Database.connection(this.connection)
const seeds = await Module.getAllFrom(Path.seeders())
const task = this.logger.task()

Expand Down
2 changes: 1 addition & 1 deletion src/commands/MigrationRevertCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class MigrationRevertCommand extends BaseCommand {
public async handle(): Promise<void> {
this.logger.simple('({bold,green} [ REVERTING MIGRATIONS ])\n')

const DB = Database.connection(this.connection).connect()
const DB = Database.connection(this.connection)
const dbName = await DB.getCurrentDatabase()

await DB.revertMigrations().finally(() => DB.close())
Expand Down
2 changes: 1 addition & 1 deletion src/commands/MigrationRunCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class MigrationRunCommand extends BaseCommand {
public async handle(): Promise<void> {
this.logger.simple('({bold,green} [ RUNNING MIGRATIONS ])\n')

const DB = Database.connection(this.connection).connect()
const DB = Database.connection(this.connection)
const dbName = await DB.getCurrentDatabase()

await DB.runMigrations().finally(() => DB.close())
Expand Down
19 changes: 13 additions & 6 deletions src/database/DatabaseImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@
*/

import type { Knex } from 'knex'
import type { MongoDriver } from '#src/drivers/MongoDriver'
import type { MySqlDriver } from '#src/drivers/MySqlDriver'
import { DriverFactory } from '#src/factories/DriverFactory'
import type { SqliteDriver } from '#src/drivers/SqliteDriver'
import type { Driver as DriverImpl } from '#src/drivers/Driver'
import type { Connections, ConnectionOptions } from '#src/types'
import type { PostgresDriver } from '#src/drivers/PostgresDriver'
import type { FakeDriver } from '#src/database/drivers/FakeDriver'
import { QueryBuilder } from '#src/database/builders/QueryBuilder'
import { ConnectionFactory } from '#src/factories/ConnectionFactory'
import type { MongoDriver } from '#src/database/drivers/MongoDriver'
import type { MySqlDriver } from '#src/database/drivers/MySqlDriver'
import type { SqliteDriver } from '#src/database/drivers/SqliteDriver'
import type { Driver as DriverImpl } from '#src/database/drivers/Driver'
import type { Transaction } from '#src/database/transactions/Transaction'
import type { PostgresDriver } from '#src/database/drivers/PostgresDriver'

export class DatabaseImpl<Driver extends DriverImpl = any> {
/**
Expand Down Expand Up @@ -61,6 +62,11 @@ export class DatabaseImpl<Driver extends DriverImpl = any> {
options?: ConnectionOptions
): DatabaseImpl<PostgresDriver>

public connection(
con: 'fake',
options?: ConnectionOptions
): DatabaseImpl<typeof FakeDriver>

public connection(
con: Connections,
options?: ConnectionOptions
Expand All @@ -69,6 +75,7 @@ export class DatabaseImpl<Driver extends DriverImpl = any> {
| DatabaseImpl<MySqlDriver>
| DatabaseImpl<SqliteDriver>
| DatabaseImpl<PostgresDriver>
| DatabaseImpl<typeof FakeDriver>

/**
* Change the database connection.
Expand All @@ -80,7 +87,7 @@ export class DatabaseImpl<Driver extends DriverImpl = any> {
database.connectionName = con
database.driver = driver

return database
return database.connect(options)
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/database/builders/QueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import type { Operations } from '#src/types/Operations'
import type { Direction, ModelColumns } from '#src/types'
import type { Driver as DriverImpl } from '#src/drivers/Driver'
import type { Driver as DriverImpl } from '#src/database/drivers/Driver'
import type { Collection, PaginatedResponse } from '@athenna/common'

export class QueryBuilder<T = any, Driver extends DriverImpl = any> {
Expand Down
2 changes: 1 addition & 1 deletion src/drivers/Driver.ts → src/database/drivers/Driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export abstract class Driver<Client = any, QB = any> {
/**
* Create a join clause by join type
*/
protected joinByType(
public joinByType(
joinType: string,
table: any,
column1?: string,
Expand Down
Loading

0 comments on commit ac9739b

Please sign in to comment.