Skip to content

Commit

Permalink
[ag|#1] add has-one association btw/ events & locations
Browse files Browse the repository at this point in the history
  • Loading branch information
aguestuser committed Jun 22, 2018
1 parent 9c2c2df commit a8bf5bc
Show file tree
Hide file tree
Showing 17 changed files with 188 additions and 12 deletions.
5 changes: 4 additions & 1 deletion src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ import * as SequelizeClass from "sequelize"
import {Sequelize, SequelizeStaticAndInstance, SequelizeStatic, Models} from "sequelize"
import {values, forEach} from "lodash"
import {db as config} from "../config/index"
import {eventFactory} from "./models/Event"
import {eventFactory} from "./models/event"
import {locationFactory} from "./models/location"

type Model = SequelizeStaticAndInstance["Model"]

export interface Database {
sequelize: Sequelize,
SequelizeClass: SequelizeStatic,
Event: Model,
Location: Model,
}

export const initDb = (): Database => {
Expand All @@ -22,6 +24,7 @@ export const initDb = (): Database => {

const db = {
Event: eventFactory(sequelize, SequelizeClass),
Location: locationFactory(sequelize, SequelizeClass),
}

forEach(values(db), (mdl: Model) => mdl.associate && mdl.associate(db))
Expand Down
42 changes: 42 additions & 0 deletions src/migrations/20180622191543-create-location.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('locations', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
vanId: {
type: Sequelize.INTEGER
},
name: {
type: Sequelize.STRING,
allowNull: false,
},
displayName: {
type: Sequelize.STRING
},
eventId: {
type: Sequelize.INTEGER,
references: {
model: 'events',
key: 'id'
},
onDelete: 'cascade',
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('locations');
}
};
11 changes: 8 additions & 3 deletions src/models/Event.ts → src/models/event.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {DataTypes, Instance, Sequelize, SequelizeStaticAndInstance} from "sequelize"
import {DataTypes, Instance, Models, Sequelize, SequelizeStaticAndInstance} from "sequelize"
import {Attributes} from "../types/Attributes"
type Model = SequelizeStaticAndInstance["Model"]

Expand All @@ -24,8 +24,13 @@ export const eventFactory = (s: Sequelize, t: DataTypes): Model => {
tableName: "events",
})

Event.associate = db => {
// associations can be defined here
Event.associate = (db: Models) => {
Event.hasOne(db.Location, {
as: "location",
hooks: true,
onDelete: "cascade",
foreignKey: "eventId",
})
}

return Event
Expand Down
27 changes: 27 additions & 0 deletions src/models/location.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {DataTypes, Instance, Models, Sequelize, SequelizeStaticAndInstance} from "sequelize"
import {Attributes} from "../types/Attributes"
import {EventAttributes} from "./event"
type Model = SequelizeStaticAndInstance["Model"]

export type LocationInstance = Instance<LocationAttributes> & LocationAttributes
export interface LocationAttributes extends Attributes, VanLocation {
event?: EventAttributes,
}

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

const Location = s.define<LocationInstance, LocationAttributes>("Location", {
vanId: t.INTEGER,
name: t.STRING,
displayName: t.STRING,
eventId: t.INTEGER,
}, {
tableName: "locations",
})

Location.associate = (db: Models) => {
Location.belongsTo(db.Event, { as: "event" })
}

return Location
}
2 changes: 1 addition & 1 deletion src/seeders/20180621194859-demoEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const {omit, map, merge} = require("lodash");

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

const events = map(vanEventTree, event =>
merge(
Expand Down
2 changes: 1 addition & 1 deletion src/service/eventService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Database} from "../db"
import {EventAttributes, EventInstance} from "../models/Event"
import {EventAttributes, EventInstance} from "../models/event"
import Bluebird = require("bluebird")

export const create = (db: Database, attrs: EventAttributes): Bluebird<EventInstance> =>
Expand Down
1 change: 0 additions & 1 deletion src/types/Attributes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ declare type SequelizeAttributes<T extends{ [key: string]: any}> = {
}

declare interface Attributes {
actionKitId: number,
id?: number,
vanId?: number,
archived?: string,
Expand Down
Empty file added src/types/Location.d.ts
Empty file.
5 changes: 4 additions & 1 deletion src/types/VanLocation.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
declare interface VanLocation {
locationId?: number,
// locationId?: number, use vanId instead
id?: number,
vanId?: number,
////////////////////
name: string,
displayName?: string,
address?: VanAddress,
Expand Down
File renamed without changes.
8 changes: 8 additions & 0 deletions test/fixtures/van.ts → test/fixtures/vanEvent.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {LocationAttributes} from "../../src/models/location"

export const vanEvents: VanEvent[] = [{
actionKitId: 1049,
name: "Affinity Test Event #1",
Expand Down Expand Up @@ -181,3 +183,9 @@ export const vanEventTree: VanEvent[] = [
// updated_at: "2018-06-07T16:09:34",
},
]

export const locationAttrs: LocationAttributes = {
name: "Space Jam",
displayName: "Really professional space Jam",
vanId: 1,
}
File renamed without changes.
51 changes: 51 additions & 0 deletions test/models/event.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import {describe, it, test, before, after} from "mocha"
import {expect} from "chai"
import {initDb} from "../../src/db"
import {keys, pick} from "lodash"
import {locationAttrs, vanEvents} from "../fixtures/vanEvent"

describe("Event model", () => {

let db, event
before(async () => {
db = initDb()
event = await db.Event.create({
...vanEvents[0],
location: locationAttrs,
}, {
include: [{ model: db.Location, as: "location" }],
})
})

after(async () => {
await db.Event.destroy({where: {}})
await db.Location.destroy({where: {}})
await db.sequelize.close()
})

test("fields", () => {
expect(keys(event.dataValues)).to.eql([
"id",
"actionKitId",
"name",
"description",
"createdDate",
"startDate",
"endDate",
"eventType",
"codes",
"notes",
"location",
"updatedAt",
"createdAt",
"vanId",
"eventId",
"shortName"
])
})

test("associations", async () => {
const ascLocation = await event.getLocation()
expect(pick(ascLocation.dataValues, keys(locationAttrs))).to.eql(locationAttrs)
})
})
37 changes: 37 additions & 0 deletions test/models/location.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {describe, it, test, before, after} from "mocha"
import {expect} from "chai"
import {initDb} from "../../src/db"
import {keys, pick} from "lodash"
import {locationAttrs, vanEvents} from "../fixtures/vanEvent"

describe("Location model", () => {


let db, location, event

before(async () => {
db = initDb()
event = await db.Event.create(vanEvents[0])
location = await db.Location.create({
...locationAttrs,
eventId: event.id,
}, {
include: [{ model: db.Event, as: "event" }],
})
})

after(async () => {
await db.Event.destroy({where: {}})
await db.Location.destroy({where: {}})
await db.sequelize.close()
})

test("fields", () => {
expect(pick(location.dataValues, keys(locationAttrs))).to.eql(locationAttrs)
})

test("associations", async () => {
const associatedEvent = await location.getEvent()
expect(associatedEvent.dataValues).to.eql(event.dataValues)
})
})
2 changes: 1 addition & 1 deletion test/service/eventService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {expect} from "chai"
import {describe, it, before, after, beforeEach} from "mocha"
import {initDb} from "../../src/db"
import * as eventService from "../../src/service/eventService"
import {vanEvents} from "../fixtures/van"
import {vanEvents} from "../fixtures/vanEvent"

describe("event service", () => {
let db
Expand Down
4 changes: 2 additions & 2 deletions test/service/parse.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {expect} from "chai"
import {describe, it} from "mocha"
import {parseVanEvents} from "../../src/service/parse"
import {actionKitEventTree} from "../fixtures/actionKit"
import {vanEventTree} from "../fixtures/van"
import {actionKitEventTree} from "../fixtures/actionKitEvent"
import {vanEventTree} from "../fixtures/vanEvent"

describe("parse module", () => {
it("parses a van event tree from an action kit event tree", () => {
Expand Down
3 changes: 2 additions & 1 deletion tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"object-literal-sort-keys": false,
"indent": [true, "spaces", 2],
"no-var-requires": false,
"arrow-parens": false
"arrow-parens": false,
"one-variable-per-declaration": false
},
"rulesDirectory": []
}

0 comments on commit a8bf5bc

Please sign in to comment.