Last active
September 3, 2019 07:33
-
-
Save jdltechworks/1c1ce8ee5927f92bcab8db66ddb5f181 to your computer and use it in GitHub Desktop.
Kabuang na Seeing pattern
This file contains hidden or 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
'use strict' | |
/* | |
|-------------------------------------------------------------------------- | |
| SpaceCategorySeeder | |
|-------------------------------------------------------------------------- | |
| | |
| Make use of the Factory instance to seed database with dummy data or | |
| make use of Lucid models directly. | |
| | |
*/ | |
const Drive = use('Drive') | |
const DB = use('Database') | |
const eventSpaces = [ | |
'retail-spaces', | |
'corporate-dining', | |
'event-spaces', | |
] | |
const trainingRoom = [ | |
'training-and-work-spaces', | |
] | |
const servicedOffices = [ | |
'private-offices', | |
'virtual-offices', | |
] | |
const coworkingSpaces = [ | |
'coworking-spaces', | |
'virtual-offices', | |
] | |
const officeSpaces = [ | |
'office-spaces' | |
] | |
const categoryMapper = (category) => { | |
switch (true) { | |
case eventSpaces.includes(category): return 'event-spaces' | |
case trainingRoom.includes(category): return 'training-rooms' | |
case servicedOffices.includes(category): return 'serviced-offices' | |
case coworkingSpaces.includes(category): return 'coworking-spaces' | |
case officeSpaces.includes(category): return 'office-spaces' | |
default: return null | |
} | |
} | |
class SpaceCategorySeeder { | |
async run() { | |
const spacesJSON = JSON.parse(await Drive.disk('s3').get('spaces.json')); | |
const categoriesJSON = JSON.parse(await Drive.disk('s3').get('categories.json')); | |
console.log(categoriesJSON) | |
await DB.transaction(async (trx) => { | |
const relations = spacesJSON.map(async spaceRow => { | |
const [space] = await trx.table('spaces') | |
.select('id') | |
.where('mongo_id', spaceRow._id.$oid) | |
const inserts = spaceRow.categories.map(async categoryRow => { | |
const s3Category = categoriesJSON.find(category => category._id.$oid === categoryRow) | |
const category = categoryMapper(s3Category.slug) | |
const [dbCategory] = await trx.table('categories') | |
.select('name', 'id') | |
.where('slug', category) | |
console.log(dbCategory) | |
}) | |
await Promise.all(inserts) | |
}) | |
await Promise.all(relations) | |
}) | |
} | |
async insert(space, category, trx) { | |
try { | |
console.log('inserting spacecategory space_id ', space.id, 'category_id ', category) | |
await trx.insert({ | |
space_id: space.id, | |
category_id: category, | |
created_at: DB.fn.now(), | |
updated_at: DB.fn.now(), | |
}).into('space_categories') | |
} catch (error) { | |
console.log(error) | |
} | |
} | |
} | |
module.exports = SpaceCategorySeeder |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment