From a8bf5bc0beb7809016395362523f21d1c7d8761d Mon Sep 17 00:00:00 2001 From: aguestuser Date: Fri, 22 Jun 2018 16:50:51 -0400 Subject: [PATCH] [ag|#1] add has-one association btw/ events & locations --- src/db.ts | 5 +- .../20180622191543-create-location.js | 42 +++++++++++++++ src/models/{Event.ts => event.ts} | 11 ++-- src/models/location.ts | 27 ++++++++++ src/seeders/20180621194859-demoEvent.js | 2 +- src/service/eventService.ts | 2 +- src/types/Attributes.d.ts | 1 - src/types/Location.d.ts | 0 src/types/VanLocation.d.ts | 5 +- .../{actionKit.ts => actionKitEvent.ts} | 0 test/fixtures/{van.ts => vanEvent.ts} | 8 +++ test/fixtures/{vanES5.js => vanEventES5.js} | 0 test/models/event.spec.ts | 51 +++++++++++++++++++ test/models/location.spec.ts | 37 ++++++++++++++ test/service/eventService.spec.ts | 2 +- test/service/parse.spec.ts | 4 +- tslint.json | 3 +- 17 files changed, 188 insertions(+), 12 deletions(-) create mode 100644 src/migrations/20180622191543-create-location.js rename src/models/{Event.ts => event.ts} (72%) create mode 100644 src/models/location.ts create mode 100644 src/types/Location.d.ts rename test/fixtures/{actionKit.ts => actionKitEvent.ts} (100%) rename test/fixtures/{van.ts => vanEvent.ts} (95%) rename test/fixtures/{vanES5.js => vanEventES5.js} (100%) create mode 100644 test/models/event.spec.ts create mode 100644 test/models/location.spec.ts diff --git a/src/db.ts b/src/db.ts index e09c8fc..c1cafa9 100644 --- a/src/db.ts +++ b/src/db.ts @@ -4,7 +4,8 @@ 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"] @@ -12,6 +13,7 @@ export interface Database { sequelize: Sequelize, SequelizeClass: SequelizeStatic, Event: Model, + Location: Model, } export const initDb = (): Database => { @@ -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)) diff --git a/src/migrations/20180622191543-create-location.js b/src/migrations/20180622191543-create-location.js new file mode 100644 index 0000000..f757907 --- /dev/null +++ b/src/migrations/20180622191543-create-location.js @@ -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'); + } +}; \ No newline at end of file diff --git a/src/models/Event.ts b/src/models/event.ts similarity index 72% rename from src/models/Event.ts rename to src/models/event.ts index 6fc9a44..e473431 100644 --- a/src/models/Event.ts +++ b/src/models/event.ts @@ -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"] @@ -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 diff --git a/src/models/location.ts b/src/models/location.ts new file mode 100644 index 0000000..613e1ea --- /dev/null +++ b/src/models/location.ts @@ -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 +export interface LocationAttributes extends Attributes, VanLocation { + event?: EventAttributes, +} + +export const locationFactory = (s: Sequelize, t: DataTypes): Model => { + + const Location = s.define("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 +} diff --git a/src/seeders/20180621194859-demoEvent.js b/src/seeders/20180621194859-demoEvent.js index 456af25..770e787 100644 --- a/src/seeders/20180621194859-demoEvent.js +++ b/src/seeders/20180621194859-demoEvent.js @@ -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( diff --git a/src/service/eventService.ts b/src/service/eventService.ts index 178752a..cc02cde 100644 --- a/src/service/eventService.ts +++ b/src/service/eventService.ts @@ -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 => diff --git a/src/types/Attributes.d.ts b/src/types/Attributes.d.ts index 01066f6..60018b8 100644 --- a/src/types/Attributes.d.ts +++ b/src/types/Attributes.d.ts @@ -5,7 +5,6 @@ declare type SequelizeAttributes = { } declare interface Attributes { - actionKitId: number, id?: number, vanId?: number, archived?: string, diff --git a/src/types/Location.d.ts b/src/types/Location.d.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/types/VanLocation.d.ts b/src/types/VanLocation.d.ts index 624db03..50ca7cb 100644 --- a/src/types/VanLocation.d.ts +++ b/src/types/VanLocation.d.ts @@ -1,5 +1,8 @@ declare interface VanLocation { - locationId?: number, + // locationId?: number, use vanId instead + id?: number, + vanId?: number, + //////////////////// name: string, displayName?: string, address?: VanAddress, diff --git a/test/fixtures/actionKit.ts b/test/fixtures/actionKitEvent.ts similarity index 100% rename from test/fixtures/actionKit.ts rename to test/fixtures/actionKitEvent.ts diff --git a/test/fixtures/van.ts b/test/fixtures/vanEvent.ts similarity index 95% rename from test/fixtures/van.ts rename to test/fixtures/vanEvent.ts index e6bdf0d..1ba83dc 100644 --- a/test/fixtures/van.ts +++ b/test/fixtures/vanEvent.ts @@ -1,3 +1,5 @@ +import {LocationAttributes} from "../../src/models/location" + export const vanEvents: VanEvent[] = [{ actionKitId: 1049, name: "Affinity Test Event #1", @@ -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, +} \ No newline at end of file diff --git a/test/fixtures/vanES5.js b/test/fixtures/vanEventES5.js similarity index 100% rename from test/fixtures/vanES5.js rename to test/fixtures/vanEventES5.js diff --git a/test/models/event.spec.ts b/test/models/event.spec.ts new file mode 100644 index 0000000..2a3bcea --- /dev/null +++ b/test/models/event.spec.ts @@ -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) + }) +}) diff --git a/test/models/location.spec.ts b/test/models/location.spec.ts new file mode 100644 index 0000000..1c0de2d --- /dev/null +++ b/test/models/location.spec.ts @@ -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) + }) +}) diff --git a/test/service/eventService.spec.ts b/test/service/eventService.spec.ts index 37cd6a2..f0eab61 100644 --- a/test/service/eventService.spec.ts +++ b/test/service/eventService.spec.ts @@ -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 diff --git a/test/service/parse.spec.ts b/test/service/parse.spec.ts index 5213c5b..08fd54f 100644 --- a/test/service/parse.spec.ts +++ b/test/service/parse.spec.ts @@ -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", () => { diff --git a/tslint.json b/tslint.json index 938a7dd..ccc82e8 100644 --- a/tslint.json +++ b/tslint.json @@ -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": [] } \ No newline at end of file