Muthu Kumar
6 years ago
11 changed files with 267 additions and 1245 deletions
@ -1,7 +1,9 @@ |
|||||
const request = require('./request'); |
const request = require('./request'); |
||||
const db = require('./db'); |
const db = require('./db'); |
||||
|
const seed = require('./seed'); |
||||
|
|
||||
module.exports = { |
module.exports = { |
||||
request, |
request, |
||||
db, |
db, |
||||
|
seed, |
||||
}; |
}; |
||||
|
@ -0,0 +1,56 @@ |
|||||
|
const fs = require('fs-extra'); |
||||
|
const requireDirectory = require('require-directory'); |
||||
|
const JSONT = require('@codefeathers/jsont'); |
||||
|
|
||||
|
/** |
||||
|
* Resource creator for 'seed' resource. |
||||
|
* @param {object} context |
||||
|
* @param {string} context.templates |
||||
|
* @param {object} options |
||||
|
* @param {string} options.basePath |
||||
|
*/ |
||||
|
module.exports = (context, options) => { |
||||
|
|
||||
|
if (typeof context.templates === 'object') |
||||
|
return context.templates; |
||||
|
|
||||
|
if (typeof context.templates !== 'string') |
||||
|
throw new Error( |
||||
|
`${context.templates} is not a string!` |
||||
|
+ `\n^^^` |
||||
|
) |
||||
|
|
||||
|
if (context.templates.slice(0, 3) !== '#!/') |
||||
|
throw new Error( |
||||
|
`template path does not start with '#!/'\n` |
||||
|
+ context.templates.slice(0, 10) |
||||
|
+ `\n^^^` |
||||
|
); |
||||
|
|
||||
|
const templatesPath = |
||||
|
options.basePath + context.templates.slice(2); |
||||
|
|
||||
|
return requireDirectory( |
||||
|
module, |
||||
|
options.basePath, |
||||
|
{ include: /.*template.json$/ }, |
||||
|
); |
||||
|
|
||||
|
}; |
||||
|
|
||||
|
const range = n => Array(n).fill(undefined).map((x, i) => i); |
||||
|
|
||||
|
module.exports.do = (unit, { db }) => { |
||||
|
|
||||
|
switch (unit.flow) { |
||||
|
|
||||
|
case 'createUser': |
||||
|
return [range(unit.count).map(() => { |
||||
|
|
||||
|
})].map(x => db['userdetails'].insertMany(x)); |
||||
|
// case 'createProspect':
|
||||
|
|
||||
|
} |
||||
|
|
||||
|
}; |
||||
|
|
@ -1,39 +1,12 @@ |
|||||
const requestor = async (unit, request) => { |
const resources = require('../resources'); |
||||
switch (unit.method) { |
|
||||
|
|
||||
case 'get': |
|
||||
return await request[unit.method](unit.path); |
|
||||
case 'post': |
|
||||
return await (request[unit.method](unit.path) |
|
||||
.type(unit.reqType || 'json') |
|
||||
.send(unit.body)); |
|
||||
|
|
||||
} |
|
||||
}; |
|
||||
|
|
||||
const dbAction = async (unit, db) => { |
|
||||
if (!unit.hasOwnProperty('method')) unit.method = 'findOne'; |
|
||||
const exec = [ 'create', 'insertMany' ].indexOf(unit.method) === -1; |
|
||||
const action = db[unit.collection][unit.method]( |
|
||||
unit.query || unit.insert, |
|
||||
unit.update || unit.queryList, |
|
||||
); |
|
||||
|
|
||||
return !exec ? await action : action.exec(); |
|
||||
}; |
|
||||
|
|
||||
module.exports = async (unit, state) => { |
module.exports = async (unit, state) => { |
||||
const [ request, db ] = state['@start']; |
|
||||
switch (unit.type) { |
|
||||
|
|
||||
case 'request': |
const instances = { |
||||
return await requestor(unit, request); |
request: state['@start'].request, |
||||
case 'db': |
db: state['@start'].db, |
||||
return await dbAction(unit, db); |
}; |
||||
default: |
|
||||
throw new Error( |
return resources[unit.type].do(unit, instances); |
||||
`Unknown before hook type: ${unit.type}` |
|
||||
); |
|
||||
|
|
||||
} |
|
||||
}; |
}; |
||||
|
File diff suppressed because it is too large
@ -0,0 +1 @@ |
|||||
|
new Promise(() => {}).then(console.log) |
Loading…
Reference in new issue