Skip to content

Commit 1cc1669

Browse files
authored
Merge pull request #78 from CodeForBaltimore/revjtanton/issue-72
fix: Associations on seeding
2 parents beb3035 + 7953a6d commit 1cc1669

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

sequelize/data/contact.json

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,23 @@
1212
"number": "1234567890",
1313
"isPrimary": true
1414
}
15-
]
15+
],
16+
"EntityId": "The Leftorium"
17+
},
18+
{
19+
"name": "Moe Szyslak",
20+
"email": [
21+
{
22+
"address": "[email protected]",
23+
"isPrimary": true
24+
}
25+
],
26+
"phone": [
27+
{
28+
"number": "0987654321",
29+
"isPrimary": true
30+
}
31+
],
32+
"EntityId": "Moes Tavern"
1633
}
1734
]

sequelize/data/entity.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,21 @@
1414
]
1515
},
1616
"description": "Everything for the left handed man, woman, and child!"
17+
},
18+
{
19+
"name": "Moes Tavern",
20+
"address": {
21+
"street": [
22+
"123 Anyplace St."
23+
],
24+
"city": "Baltimore",
25+
"state": "MD",
26+
"zip": "12345",
27+
"latlng": [
28+
39.296399,
29+
-76.607842
30+
]
31+
},
32+
"description": "Home of the Flaming Moe!"
1733
}
1834
]

sequelize/seeders/04-demo-entity.js renamed to sequelize/seeders/03-demo-entity.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const uuid = require('uuid4');
22
const entities = require('../data/entity.json');
33

44
module.exports = {
5-
up: queryInterface => {
5+
up: async queryInterface => {
66

77
for (const element of entities) {
88
const id = uuid();

sequelize/seeders/03-demo-contact.js renamed to sequelize/seeders/04-demo-contact.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const uuid = require('uuid4');
22
const contacts = require('../data/contact.json');
33

44
module.exports = {
5-
up: queryInterface => {
5+
up: async queryInterface => {
66
for (const element of contacts) {
77
const id = uuid();
88

@@ -11,6 +11,13 @@ module.exports = {
1111
element.updatedAt = new Date();
1212
element.email = JSON.stringify(element.email);
1313
element.phone = JSON.stringify(element.phone);
14+
15+
if (element.EntityId) {
16+
const entityId = await queryInterface.sequelize.query(
17+
`SELECT id FROM ${process.env.DATABASE_SCHEMA}."Entities" WHERE name = '${element.EntityId}'`
18+
);
19+
element.EntityId = entityId[0][0].id;
20+
}
1421
}
1522

1623
return queryInterface.bulkInsert('Contacts', contacts);

0 commit comments

Comments
 (0)