Skip to content

Commit

Permalink
refactor(examples): fix and add seed
Browse files Browse the repository at this point in the history
  • Loading branch information
rijkerd committed Jun 14, 2019
1 parent 346a32f commit 97c8ab4
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 74 deletions.
97 changes: 23 additions & 74 deletions examples/app.js
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`);
});
});
69 changes: 69 additions & 0 deletions examples/seed.js
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);
}
);
});

0 comments on commit 97c8ab4

Please sign in to comment.