-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
side-effects: * refactor config/index for concision, allow for named imports * add type declaration to `initDb()` * fix db types for `models/Event`, provide return type for its factory * extract `vanEvent` fixture with no nested attributes
- Loading branch information
1 parent
b02da05
commit 1793086
Showing
9 changed files
with
113 additions
and
62 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,17 @@ | ||
const db = require("./db.json") | ||
import {secrets} from "./secrets" | ||
export {secrets} | ||
import {get} from "lodash" | ||
const DB = require("./db.json") | ||
import SECRETS from "./secrets" | ||
import NETWORK from "./network" | ||
|
||
const defaults = { | ||
port: 8081, | ||
hostname: "localhost", | ||
secrets, | ||
} | ||
|
||
const test = { | ||
...defaults, | ||
db: db.test, | ||
secrets: secrets.test, | ||
} | ||
|
||
const development = { | ||
...defaults, | ||
db: db.development, | ||
secrets: secrets.development, | ||
} | ||
const getEnv = (cfg) => | ||
get(cfg, [process.env.NODE_ENV || "development"]) | ||
|
||
const production = { | ||
...defaults, | ||
db: db.production, | ||
secrets: secrets.production, | ||
const config = { | ||
db: getEnv(DB), | ||
secrets: getEnv(SECRETS), | ||
network: getEnv(NETWORK), | ||
} | ||
const {db, secrets, network} = config | ||
export {db, secrets, network} | ||
|
||
export default { | ||
test, | ||
development, | ||
production, | ||
}[process.env.NODE_ENV || "development"] | ||
export default config |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const defaults = { | ||
port: 8081, | ||
hostname: "localhost", | ||
} | ||
|
||
export default { | ||
production: defaults, | ||
development: defaults, | ||
test: defaults, | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,32 @@ | ||
"use strict" | ||
|
||
import * as Sequelize from "sequelize" | ||
import * as SequelizeClass from "sequelize" | ||
import {Sequelize, SequelizeStaticAndInstance, SequelizeStatic, Models} from "sequelize" | ||
import {values, forEach} from "lodash" | ||
import config from "../config/index" | ||
import {db as config} from "../config/index" | ||
import {eventFactory} from "./models/Event" | ||
|
||
export const initDb = () => { | ||
type Model = SequelizeStaticAndInstance["Model"] | ||
|
||
export interface Database { | ||
sequelize: Sequelize, | ||
SequelizeClass: SequelizeStatic, | ||
Event: Model, | ||
} | ||
|
||
export const initDb = (): Database => { | ||
|
||
const sequelize = config.use_env_variable | ||
? new Sequelize(process.env[config.use_env_variable], config) | ||
: new Sequelize(config.database, config.username, config.password, config) | ||
? new SequelizeClass(process.env[config.use_env_variable], config) | ||
: new SequelizeClass(config.database, config.username, config.password, config) | ||
|
||
const db = { | ||
// import model factories here, like: | ||
// `Person: personFactory(sequelize, Sequelize)` | ||
Event: eventFactory(sequelize, SequelizeClass), | ||
} | ||
|
||
forEach(values(db), (mdl: any) => mdl.associate && mdl.associate(db)) | ||
forEach(values(db), (mdl: Model) => mdl.associate && mdl.associate(db)) | ||
|
||
return {...db, sequelize, Sequelize } | ||
return {...db, sequelize, SequelizeClass} | ||
} | ||
|
||
export default initDb() |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import {Database} from "../../db" | ||
import {EventAttributes, EventInstance} from "../../models/Event" | ||
import Bluebird = require("bluebird") | ||
|
||
export const create = (db: Database, attrs: EventAttributes): Bluebird<EventInstance> => | ||
db.Event.create(attrs) | ||
|
||
export const createMany = (db: Database, attrs: EventAttributes[]): Bluebird<EventInstance[]> => | ||
db.Event.bulkCreate(attrs) |
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
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import {expect} from "chai" | ||
import {describe, it, before, after, beforeEach} from "mocha" | ||
import {initDb} from "../../../src/db" | ||
import * as eventService from "../../../src/service/db/eventService" | ||
import {vanEvents} from "../../fixtures/van" | ||
|
||
describe("event service", () => { | ||
let db | ||
|
||
before(() => db = initDb()) | ||
after(async () => await db.sequelize.close()) | ||
|
||
beforeEach(async () => await db.Event.destroy({where: {}})) | ||
|
||
it("creates an event", async () => { | ||
await eventService.create(db, vanEvents[0]) | ||
expect(await db.Event.count()).to.eql(1) | ||
}) | ||
|
||
it("creates many events", async () => { | ||
await eventService.createMany(db, vanEvents) | ||
expect(await db.Event.count()).to.eql(2) | ||
}) | ||
|
||
it("creates an event with associations") | ||
|
||
it("creates many events with associations") | ||
}) |