Skip to content

Commit

Permalink
chore(examples): improve sample app and seed
Browse files Browse the repository at this point in the history
  • Loading branch information
lykmapipo committed Aug 29, 2019
1 parent 09a4b0a commit bd9ee4d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 56 deletions.
34 changes: 20 additions & 14 deletions examples/app.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
// dependencies
const { app, mount, start } = require('@lykmapipo/express-common');
const { get, mount, start } = require('@lykmapipo/express-common');
const { connect, jsonSchema } = require('@lykmapipo/mongoose-common');
const { router, info, apiVersion } = require('../lib/index');
const { priorityRouter, info, apiVersion } = require('../lib');

mount(router);

connect(connectionError => {
if (connectionError) {
throw connectionError;
}

app.get('/', (request, response) => {
const startHttpServer = () => {
get('/', (request, response) => {
response.status(200);
response.json(info);
});

app.get(`/${apiVersion}/schemas`, (request, response) => {
get(`/${apiVersion}/schemas`, (request, response) => {
const schema = jsonSchema();
response.status(200);
response.json(schema);
});

/* fire the app */
// mount routers
mount(priorityRouter);

// fire http serve
start((error, env) => {
// eslint-disable-next-line no-console
if (error) {
throw error;
}
console.log(`visit http://0.0.0.0:${env.PORT}/${apiVersion}/priorities`);
});
};

// connect and start http server
connect(error => {
if (error) {
throw error;
}
startHttpServer();
});
72 changes: 30 additions & 42 deletions examples/seed.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
const _ = require('lodash');
const { waterfall } = require('async');
const { connect } = require('@lykmapipo/mongoose-common');
const { connect, clear } = require('@lykmapipo/mongoose-common');
const { Jurisdiction } = require('@codetanzania/majifix-jurisdiction');
const { Priority } = require('../lib');

// track seeding time
/* track seeding time */
let seedStart;
let seedEnd;

/* eslint-disable */
const log = (stage, error, results) => {
if (error) {
console.error(`${stage} seed error`, error);
Expand All @@ -19,51 +18,40 @@ const log = (stage, error, results) => {
console.info(`${stage} seed result`, val);
}
};
/* eslint-enable */

connect(err => {
if (err) {
throw err;
}

waterfall(
[
function clearPriority(next) {
Priority.deleteMany(() => next());
},
const clearSeed = next => clear(Priority, Jurisdiction, () => next());

function clearJurisdiction(next) {
Jurisdiction.deleteMany(() => next());
},
const seedJurisdiction = next => Jurisdiction.fake().post(next);

function seedJurisdiction(next) {
const jurisdiction = Jurisdiction.fake();
jurisdiction.post(next);
},
const seedPriority = (jurisdiction, next) => {
let priorities = Priority.fake(50);

function seedPriority(jurisdiction, next) {
seedStart = Date.now();
let priorities = Priority.fake(50);
priorities = _.forEach(priorities, priority => {
priority.set({ jurisdiction });
return priority;
});

priorities = _.forEach(priorities, priority => {
const sample = priority;
sample.jurisdiction = jurisdiction;
return sample;
});
Priority.create(priorities, next);
};

Priority.create(priorities, next);
},
],
(error, results) => {
if (error) {
throw error;
}
const seed = () => {
seedEnd = Date.now();
waterfall([clearSeed, seedJurisdiction, seedPriority], (error, results) => {
if (error) {
throw error;
}
seedEnd = Date.now();

seedEnd = Date.now();
log('time', null, seedEnd - seedStart);
log('final', error, results);
process.exit(0);
});
};

log('time', null, seedEnd - seedStart);
log('final', error, results);
process.exit(0);
}
);
// connect and seed
connect(error => {
if (error) {
throw error;
}
seed();
});

0 comments on commit bd9ee4d

Please sign in to comment.