Skip to content

Commit

Permalink
feat(factory): add way to extend drivers
Browse files Browse the repository at this point in the history
  • Loading branch information
jlenon7 committed May 16, 2024
1 parent 9078812 commit 5a4ae9a
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 3 deletions.
18 changes: 18 additions & 0 deletions src/factories/ConnectionFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

import { debug } from '#src/debug'
import type { Driver } from '#src/drivers/Driver'
import { FakeDriver } from '#src/drivers/FakeDriver'
import { VanillaDriver } from '#src/drivers/VanillaDriver'
import { DatabaseDriver } from '#src/drivers/DatabaseDriver'
Expand Down Expand Up @@ -115,6 +116,23 @@ export class ConnectionFactory {
return availableConnections
}

/**
* Define your own queue driver implementation to use
* within Queue facade.
*
* @example
* ```ts
* import { Driver } from '@athenna/queue'
*
* class TestDriver extends Driver {}
*
* ConnectionFactory.createDriver('test', TestDriver)
* ```
*/
public static createDriver(name: string, impl: typeof Driver) {
this.drivers.set(name, impl)
}

/**
* Parse connection config name if is default
*/
Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/config/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ export default {

'not-found': {
driver: 'not-found'
},

test: {
driver: 'test'
}
}
}
2 changes: 1 addition & 1 deletion tests/fixtures/consoles/console-mock-dest-import.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @athenna/validator
* @athenna/queue
*
* (c) João Lenon <[email protected]>
*
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/constants/products.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @athenna/worker
* @athenna/queue
*
* (c) João Lenon <[email protected]>
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import { VanillaDriver } from '#src'

export class TestDriver extends VanillaDriver {}
2 changes: 1 addition & 1 deletion tests/fixtures/workers/AnnotatedWorker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @athenna/worker
* @athenna/queue
*
* (c) João Lenon <[email protected]>
*
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/factories/ConnectionFactoryTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

import { Path } from '@athenna/common'
import { TestDriver } from '#tests/fixtures/drivers/TestDriver'
import { AfterEach, BeforeEach, Test, type Context } from '@athenna/test'
import { NotFoundDriverException } from '#src/exceptions/NotFoundDriverException'
import { ConnectionFactory, FakeDriver, DatabaseDriver, VanillaDriver } from '#src'
Expand All @@ -18,6 +19,7 @@ export class ConnectionFactoryTest {
public async beforeEach() {
await Config.loadAll(Path.fixtures('config'))
ConnectionFactory.connections = new Map()
ConnectionFactory.drivers.delete('test')
}

@AfterEach()
Expand Down Expand Up @@ -78,4 +80,16 @@ export class ConnectionFactoryTest {
public async shouldThrowNotImplementedConfigExceptionWhenTryingToUseANotImplementedDriver({ assert }: Context) {
assert.throws(() => ConnectionFactory.fabricate('not-found-con'), NotImplementedConfigException)
}

@Test()
public async shouldBeAbleToCreateOwnDriverImplementationToUseWithinQueueFacade({ assert }: Context) {
ConnectionFactory.createDriver('test', TestDriver)

const testDriver = ConnectionFactory.fabricate('test')

assert.instanceOf(testDriver, TestDriver)

ConnectionFactory.drivers.delete('test')
ConnectionFactory.connections.delete('test')
}
}

0 comments on commit 5a4ae9a

Please sign in to comment.