-
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.
refactor(examples): fix and add seed
- Loading branch information
Showing
2 changed files
with
92 additions
and
74 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,29 @@ | ||
'use strict'; | ||
// dependencies | ||
const app = require('@lykmapipo/express-common'); | ||
const { connect, jsonSchema } = require('@lykmapipo/mongoose-common'); | ||
const { router, info, apiVersion } = require('../lib/index'); | ||
|
||
app.mount(router); | ||
|
||
/* ensure mongo uri */ | ||
process.env.MONGODB_URI = | ||
(process.env.MONGODB_URI) || 'mongodb://localhost/majifix-priority'; | ||
connect(connectionError => { | ||
if (connectionError) { | ||
throw connectionError; | ||
} | ||
|
||
app.get('/', (request, response) => { | ||
response.status(200); | ||
response.json(info); | ||
}); | ||
|
||
/* dependencies */ | ||
const path = require('path'); | ||
const _ = require('lodash'); | ||
const async = require('async'); | ||
const mongoose = require('mongoose'); | ||
// mongoose.set('debug', true); | ||
const { start } = require('@lykmapipo/express-common'); | ||
const { Jurisdiction } = require('@codetanzania/majifix-jurisdiction'); | ||
const { | ||
Priority, | ||
info, | ||
apiVersion, | ||
app | ||
} = require(path.join(__dirname, '..', 'index')); | ||
let samples = require('./samples')(20); | ||
|
||
/* connect to mongoose */ | ||
mongoose.connect(process.env.MONGODB_URI); | ||
|
||
|
||
function boot() { | ||
|
||
async.waterfall([ | ||
|
||
function clear(next) { | ||
Priority.remove(function () { | ||
next(); | ||
}); | ||
}, | ||
|
||
function clear(next) { | ||
Jurisdiction.remove(function () { | ||
next(); | ||
}); | ||
}, | ||
|
||
function seedJurisdiction(next) { | ||
const jurisdiction = Jurisdiction.fake(); | ||
jurisdiction.post(next); | ||
}, | ||
|
||
function seed(jurisdiction, next) { | ||
/* fake priorities */ | ||
samples = | ||
_.map(samples, function (sample) { | ||
if ((sample.weight % 2 === 0)) { | ||
sample.jurisdiction = jurisdiction; | ||
} | ||
return sample; | ||
}); | ||
Priority.create(samples, next); | ||
} | ||
|
||
], function (error, results) { | ||
|
||
/* expose module info */ | ||
app.get('/', function (request, response) { | ||
response.status(200); | ||
response.json(info); | ||
}); | ||
|
||
/* fire the app */ | ||
start(function (error, env) { | ||
console.log( | ||
`visit http://0.0.0.0:${env.PORT}/${apiVersion}/priorities` | ||
); | ||
}); | ||
|
||
app.get(`/${apiVersion}/schemas`, (request, response) => { | ||
const schema = jsonSchema(); | ||
response.status(200); | ||
response.json(schema); | ||
}); | ||
} | ||
|
||
boot(); | ||
/* fire the app */ | ||
app.start((error, env) => { | ||
// eslint-disable-next-line no-console | ||
console.log(`visit http://0.0.0.0:${env.PORT}/${apiVersion}/priorities`); | ||
}); | ||
}); |
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,69 @@ | ||
const _ = require('lodash'); | ||
const { waterfall } = require('async'); | ||
const { connect } = require('@lykmapipo/mongoose-common'); | ||
const { Jurisdiction } = require('@codetanzania/majifix-jurisdiction'); | ||
const { Priority } = require('../lib'); | ||
|
||
// track seeding time | ||
let seedStart; | ||
let seedEnd; | ||
|
||
/* eslint-disable */ | ||
const log = (stage, error, results) => { | ||
if (error) { | ||
console.error(`${stage} seed error`, error); | ||
} | ||
|
||
if (results) { | ||
const val = _.isArray(results) ? results.length : results; | ||
console.info(`${stage} seed result`, val); | ||
} | ||
}; | ||
/* eslint-enable */ | ||
|
||
connect(err => { | ||
if (err) { | ||
throw err; | ||
} | ||
|
||
waterfall( | ||
[ | ||
function clearPriority(next) { | ||
Priority.deleteMany(() => next()); | ||
}, | ||
|
||
function clearJurisdiction(next) { | ||
Jurisdiction.deleteMany(() => next()); | ||
}, | ||
|
||
function seedJurisdiction(next) { | ||
const jurisdiction = Jurisdiction.fake(); | ||
jurisdiction.post(next); | ||
}, | ||
|
||
function seedPriority(jurisdiction, next) { | ||
seedStart = Date.now(); | ||
let priorities = Priority.fake(50); | ||
|
||
priorities = _.forEach(priorities, priority => { | ||
const sample = priority; | ||
sample.jurisdiction = jurisdiction; | ||
return sample; | ||
}); | ||
|
||
Priority.create(priorities, next); | ||
}, | ||
], | ||
(error, results) => { | ||
if (error) { | ||
throw error; | ||
} | ||
|
||
seedEnd = Date.now(); | ||
|
||
log('time', null, seedEnd - seedStart); | ||
log('final', error, results); | ||
process.exit(0); | ||
} | ||
); | ||
}); |