This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| describe('Todos Service', function () { | |
| beforeEach(() { | |
| subject = makeTodosService({ | |
| db: testDatabaseSomehow | |
| }) | |
| }) | |
| it('works', async function () { | |
| const todos = await subject.getTodos() | |
| expect(todos.length).to.equal(3) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import assert from 'assert' | |
| // Using object destructuring to make it look good. | |
| export function makeTodosService({ | |
| // "repository" is a fancy term to descibe an object | |
| // that is used to retrieve data from a datasource - the actual | |
| // data source does not matter. Could be a database, a REST API, | |
| // or some IoT things like sensors or whatever. | |
| todosRepository, | |
| // We also want info about the user that is using the service, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Let's do an in-memory implementation for now. | |
| const _todos = [] | |
| export default class TodosRepository { | |
| // Marking all methods async makes them return promises! | |
| async find(query) { | |
| const filtered = _todos.filter((todo) => { | |
| // Check the user ID | |
| if (todo.userId !== query.userId) | |
| return false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import makeTodosService from './todosService' | |
| import TodosRepository from './todosRepository' | |
| describe('Todos System', function () { | |
| it('works', async function () { | |
| // This is how DI is done manually | |
| const todosService = makeTodosService({ | |
| todosRepository: new TodosRepository(), | |
| // Let's fake it til we make it! | |
| currentUser: { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const currentUser = { | |
| id: 123, | |
| name: 'Jeff' | |
| } | |
| const todosRepository = new TodosRepository() | |
| const todosService = makeTodosService({ | |
| todosRepository, | |
| currentUser |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const router = new KoaRouter() | |
| router.get('/todos', async (ctx) => { | |
| const todosService = makeTodosService({ | |
| todosRepository: new TodosRepository(), | |
| // Our Koa request knows about the current user | |
| currentUser: ctx.state.user | |
| }) | |
| ctx.body = await todosService.getTodos(ctx.request.query) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { createContainer, asClass, asFunction } from 'awilix' | |
| import makeTodosService from './todosService' | |
| import TodosRepository from './todosRepository' | |
| export default function configureContainer () { | |
| const container = createContainer() | |
| // Ordering does not matter. | |
| container.register({ | |
| // Notice the scoped() at the end - this signals |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Koa from 'koa' | |
| import KoaRouter from 'koa-router' | |
| import { asValue } from 'awilix' | |
| import { scopePerRequest, makeInvoker } from 'awilix-koa' | |
| import configureContainer from './configureContainer' | |
| const app = new Koa() | |
| const router = new KoaRouter() | |
| const container = configureContainer() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // middleware/auth.js | |
| export default async function authenticate (ctx, next) { | |
| // Do your auth stuff here. | |
| const decoded = extractTokenSomehow(ctx) | |
| const teamId = decoded.teamId | |
| const userId = decoded.userId | |
| // Use the User Repository and Team Repository (or a cache?) to fetch | |
| // our entities. | |
| // Resolve instances of these using Awilix's cradle proxy. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Koa from 'koa' | |
| import parse from './busboy' | |
| import AWS from 'aws-sdk' | |
| const app = new Koa() | |
| const s3 = new AWS.S3({ | |
| params: { Bucket: 'myBucket' } | |
| }) |