Skip to content

Instantly share code, notes, and snippets.

@jdltechworks
Last active September 3, 2019 07:33
Show Gist options
  • Save jdltechworks/1c1ce8ee5927f92bcab8db66ddb5f181 to your computer and use it in GitHub Desktop.
Save jdltechworks/1c1ce8ee5927f92bcab8db66ddb5f181 to your computer and use it in GitHub Desktop.
Kabuang na Seeing pattern
'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