-
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.
[ag|#1] add has-one association btw/ events & locations
- Loading branch information
1 parent
9c2c2df
commit a8bf5bc
Showing
17 changed files
with
188 additions
and
12 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
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,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'); | ||
} | ||
}; |
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,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 | ||
} |
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
Empty file.
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
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,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) | ||
}) | ||
}) |
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,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) | ||
}) | ||
}) |
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