diff --git a/sequelize/data/contact.json b/sequelize/data/contact.json index a433ceed..37d63120 100644 --- a/sequelize/data/contact.json +++ b/sequelize/data/contact.json @@ -12,6 +12,23 @@ "number": "1234567890", "isPrimary": true } - ] + ], + "EntityId": "The Leftorium" + }, + { + "name": "Moe Szyslak", + "email": [ + { + "address": "moe@moestavern.com", + "isPrimary": true + } + ], + "phone": [ + { + "number": "0987654321", + "isPrimary": true + } + ], + "EntityId": "Moes Tavern" } ] \ No newline at end of file diff --git a/sequelize/data/entity.json b/sequelize/data/entity.json index 92f42658..95c808a0 100644 --- a/sequelize/data/entity.json +++ b/sequelize/data/entity.json @@ -14,5 +14,21 @@ ] }, "description": "Everything for the left handed man, woman, and child!" + }, + { + "name": "Moes Tavern", + "address": { + "street": [ + "123 Anyplace St." + ], + "city": "Baltimore", + "state": "MD", + "zip": "12345", + "latlng": [ + 39.296399, + -76.607842 + ] + }, + "description": "Home of the Flaming Moe!" } ] \ No newline at end of file diff --git a/sequelize/seeders/04-demo-entity.js b/sequelize/seeders/03-demo-entity.js similarity index 94% rename from sequelize/seeders/04-demo-entity.js rename to sequelize/seeders/03-demo-entity.js index a2adabd9..5f16dda1 100644 --- a/sequelize/seeders/04-demo-entity.js +++ b/sequelize/seeders/03-demo-entity.js @@ -2,7 +2,7 @@ const uuid = require('uuid4'); const entities = require('../data/entity.json'); module.exports = { - up: queryInterface => { + up: async queryInterface => { for (const element of entities) { const id = uuid(); diff --git a/sequelize/seeders/03-demo-contact.js b/sequelize/seeders/04-demo-contact.js similarity index 65% rename from sequelize/seeders/03-demo-contact.js rename to sequelize/seeders/04-demo-contact.js index ec80ab2d..7beb30e7 100644 --- a/sequelize/seeders/03-demo-contact.js +++ b/sequelize/seeders/04-demo-contact.js @@ -2,7 +2,7 @@ const uuid = require('uuid4'); const contacts = require('../data/contact.json'); module.exports = { - up: queryInterface => { + up: async queryInterface => { for (const element of contacts) { const id = uuid(); @@ -11,6 +11,13 @@ module.exports = { element.updatedAt = new Date(); element.email = JSON.stringify(element.email); element.phone = JSON.stringify(element.phone); + + if (element.EntityId) { + const entityId = await queryInterface.sequelize.query( + `SELECT id FROM ${process.env.DATABASE_SCHEMA}."Entities" WHERE name = '${element.EntityId}'` + ); + element.EntityId = entityId[0][0].id; + } } return queryInterface.bulkInsert('Contacts', contacts);