This file contains 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 request from 'supertest'; | |
// Factory allows us to produce a "clean" instance of an app | |
// with stubbed services for each test | |
import factory from '../factory'; | |
describe('AccountController', () => { | |
let app; | |
let services; | |
let models; |
This file contains 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
// Modified from https://medium.com/the-everyday-developer/creating-an-ioc-container-with-dependency-injection-in-javascript-9db228d34060 | |
const proxy = { | |
get (instance, property) { | |
if (instance.$services.has(property)) { | |
return instance.get(property); | |
} | |
return instance[property]; | |
}, |
This file contains 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 Container from '../app/Container'; | |
import Express from 'express'; | |
import { createNamespace } from 'cls-hooked'; | |
import uuid4 from 'uuid/v4'; | |
import EventEmitter from 'events'; | |
import config from '../config'; | |
import Logger from './Logger'; | |
import DatabaseConnection from './DatabaseConnection'; |
This file contains 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 Express from 'express'; | |
import http from 'http'; | |
import Response from './Response'; | |
export default class App { | |
constructor (services) { | |
this.services = services; | |
} |
This file contains 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 moment from 'moment'; | |
import Response from '../app/Response'; | |
export default class AccountController { | |
static async login (request, response, next) { | |
const services = request.app.get('services'); | |
const { email, password, remember } = request.body; | |
const time = moment(); |
This file contains 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 { DataTypes } from 'sequelize'; | |
export default { | |
Upload: { | |
options: { | |
tableName: 'uploads', | |
createdAt: 'date_created', | |
updatedAt: 'date_updated', | |
}, | |
attributes: { |
This file contains 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 Sequelize from 'sequelize'; | |
import _ from 'lodash'; | |
export default class BaseModel extends Sequelize.Model { | |
static init (definition, services) { | |
const sequelize = services.db.connection; | |
this.$relations = definition.relations || {}; | |
this.services = services; |
This file contains 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
{ | |
User: { | |
// You can use an import or require | |
class: require('../models/User').default, | |
options: { | |
tableName: 'users', | |
createdAt: 'date_joined', | |
updatedAt: 'date_updated', | |
}, | |
attributes: { |
This file contains 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 BaseModel from '../db/BaseModel'; | |
export default class User extends BaseModel { | |
static registerHooks () { | |
this.beforeSave((user) => { | |
if (user.changed('password') && user.password) { | |
if (user.password.substr(0, 7) === 'bcrypt$') { | |
throw new Error('Do not bcrypt passwords before setting them'); | |
} |
This file contains 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 { inspect } from 'util'; | |
import Sequelize from 'sequelize'; | |
export default function DatabaseConnection (config, logger) { | |
const { | |
dialect, | |
name, | |
host, | |
port, | |
user, |