Skip to content

Commit

Permalink
[ag|#1] add eventService, implement #create, #createMany
Browse files Browse the repository at this point in the history
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
aguestuser committed Jun 22, 2018
1 parent b02da05 commit 1793086
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 62 deletions.
42 changes: 13 additions & 29 deletions config/index.ts
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
10 changes: 10 additions & 0 deletions config/network.ts
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,
}
27 changes: 18 additions & 9 deletions src/db.ts
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()
14 changes: 9 additions & 5 deletions src/models/Event.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
import {DataTypes, Instance, Sequelize} from "sequelize"
import {DataTypes, Instance, Sequelize, SequelizeStaticAndInstance} from "sequelize"
import {Attributes} from "../types/Attributes"
type Model = SequelizeStaticAndInstance["Model"]

export interface EventAttributes extends Attributes, VanEvent {}
export type EventInstance = Instance<EventAttributes> & EventAttributes

export const eventFactory = (s: Sequelize, t: DataTypes) => {
export const eventFactory = (s: Sequelize, t: DataTypes): Model => {

const Event = s.define<EventInstance, EventAttributes>("Event", {
actionKitId: t.INTEGER,
vanId: t.NUMBER,
eventId: t.NUMBER,
vanId: t.INTEGER,
eventId: t.INTEGER,
name: t.STRING,
shortName: t.STRING,
description: t.STRING,
startDate: t.DATE,
endDate: t.DATE,
eventType: t.JSON,
codes: t.JSON,
notes: t.JSON,
createdDate: t.DATE
}, {})
}, {
tableName: "events",
})

Event.associate = db => {
// associations can be defined here
Expand Down
3 changes: 2 additions & 1 deletion src/seeders/20180621194859-demoEvent.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
'use strict';

const {omit, map, merge} = require("lodash");

// TODO: would be better to import this from compile output forvan.ts --v
const {vanEventTree} = require("../../test/fixtures/van.js");
const {vanEventTree} = require("../../test/fixtures/vanES5");

const events = map(vanEventTree, event =>
merge(
Expand Down
9 changes: 9 additions & 0 deletions src/service/db/eventService.ts
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)
42 changes: 24 additions & 18 deletions test/fixtures/van.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
export const vanEvents: VanEvent[] = [{
actionKitId: 1049,
name: "Affinity Test Event #1",
description: "Affinity Test Event #1",
createdDate: "2018-06-07T15:57:50",
startDate: "2018-07-25T16:00:00-00:00",
endDate: "2018-07-25T20:00:00-00:00",
eventType: {}, // hmmm...
codes: [{}],
notes: [{}],
}, {
actionKitId: 1049,
name: "Affinity Test Event #2",
description: "Affinity Test Event #2",
startDate: "2018-07-25T16:00:00-00:00",
endDate: "2018-07-25T20:00:00-00:00",
createdDate: "2018-06-07T15:57:50",
eventType: {}, // hmmm...
codes: [{}],
notes: [{}],
}]

export const vanEventTree: VanEvent[] = [
{ // eventsResponse.objects[0]
actionKitId: 1049,
name: "Affinity Test Event #1",
description: "Affinity Test Event #1",
createdDate: "2018-06-07T15:57:50",
startDate: "2018-07-25T16:00:00-00:00",
endDate: "2018-07-25T20:00:00-00:00",
eventType: {}, // hmmm...
codes: [{}],
notes: [{}],
...vanEvents[0],
shifts: [
{
name: "FULL SHIFT",
Expand Down Expand Up @@ -119,15 +133,7 @@ export const vanEventTree: VanEvent[] = [
// note_to_attendees: "",
},
{ // eventsResponse.objects[0]
actionKitId: 1049,
name: "Affinity Test Event #2",
description: "Affinity Test Event #2",
startDate: "2018-07-25T16:00:00-00:00",
endDate: "2018-07-25T20:00:00-00:00",
createdDate: "2018-06-07T15:57:50",
eventType: {}, // hmmm...
codes: [{}],
notes: [{}],
...vanEvents[1],
roles: [
{
name: "Host",
Expand Down
File renamed without changes.
28 changes: 28 additions & 0 deletions test/service/dao/eventService.spec.ts
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")
})

0 comments on commit 1793086

Please sign in to comment.