@jota-one/drosse
Advanced tools
Comparing version 1.2.0 to 1.3.0
const fs = require('fs') | ||
const path = require('path') | ||
const rrdir = require('rrdir') | ||
const { isEmpty } = require('lodash') | ||
const { v4: uuidv4 } = require('uuid') | ||
const { replaceExpress } = require('@jota-one/replacer') | ||
const { replace } = require('@jota-one/replacer') | ||
const useState = require('./use/state') | ||
@@ -62,8 +63,20 @@ const useMiddleware = require('./use/middlewares') | ||
const loadStatic = (routePath, params = {}, verb = null, skipVerb = false) => { | ||
let staticFile = replaceExpress( | ||
const findStatic = ( | ||
root, | ||
files, | ||
routePath, | ||
params = {}, | ||
verb = null, | ||
skipVerb = false | ||
) => { | ||
const comparePath = filePath => filePath.replace(root, '').substr(1) | ||
let staticFile = replace( | ||
path.join( | ||
state.get('root'), | ||
state.get('staticPath'), | ||
routePath.join('.').concat(verb && !skipVerb ? `.${verb}` : '') | ||
routePath | ||
.join('.') | ||
.concat(verb && !skipVerb ? `.${verb}` : '') | ||
.replace(/:([^\\/\\.]+)/gim, '{$1}') | ||
), | ||
@@ -76,7 +89,26 @@ params | ||
if (!fs.existsSync(staticFile)) { | ||
logger.error(`loadStatic: tried with [${staticFile}]. File not found.`) | ||
const foundFiles = files.filter( | ||
file => | ||
comparePath(file.path).replace(/\//gim, '.') === comparePath(staticFile) | ||
) | ||
if (foundFiles.length > 1) { | ||
const error = `findStatic: more than 1 file found for:\n[${staticFile}]:\n${foundFiles | ||
.map(f => f.path) | ||
.join('\n')}` | ||
logger.error(error) | ||
return { drosse: error } | ||
} | ||
if (foundFiles.length === 1) { | ||
staticFile = foundFiles[0].path | ||
logger.info(`findStatic: file used: ${staticFile}`) | ||
} | ||
if (foundFiles.length === 0) { | ||
logger.error(`findStatic: tried with [${staticFile}]. File not found.`) | ||
if (verb && !skipVerb) { | ||
return loadStatic(routePath, params, verb, true) | ||
return findStatic(root, files, routePath, params, verb, true) | ||
} | ||
@@ -86,13 +118,20 @@ | ||
if (!isEmpty(params)) { | ||
logger.error(`loadStatic: tried with [${staticFile}]. File not found.`) | ||
logger.error(`findStatic: tried with [${staticFile}]. File not found.`) | ||
const newParams = Object.keys(params) | ||
.slice(1) | ||
.reduce((acc, name) => ({ ...acc, [name]: params[name] }), {}) | ||
return loadStatic(routePath, newParams, verb) | ||
return findStatic(root, files, routePath, newParams, verb) | ||
} | ||
return { drosse: `file [${staticFile}] not found.` } | ||
} | ||
return require(staticFile) | ||
} | ||
const loadStatic = (routePath, params = {}, verb = null, skipVerb = false) => { | ||
const root = path.join(state.get('root'), state.get('staticPath')) | ||
const files = rrdir.sync(root) | ||
return findStatic(root, files, routePath, params, verb, skipVerb) | ||
} | ||
const loadUuid = () => { | ||
@@ -99,0 +138,0 @@ const uuidFile = path.join(state.get('root'), '.uuid') |
@@ -82,3 +82,3 @@ const Loki = require('lokijs') | ||
return { | ||
const service = { | ||
loadDb: function () { | ||
@@ -126,3 +126,3 @@ return new Promise((resolve, reject) => { | ||
all(collection, cleanFields = []) { | ||
const coll = db.getCollection(collection) | ||
const coll = service.collection(collection) | ||
return coll.data.map(clean(...cleanFields)) | ||
@@ -132,3 +132,3 @@ }, | ||
byId(collection, id, cleanFields = []) { | ||
const coll = db.getCollection(collection) | ||
const coll = service.collection(collection) | ||
return coll | ||
@@ -156,3 +156,3 @@ .find({ 'DROSSE.ids': { $contains: id } }) | ||
find(collection, query, cleanFields = []) { | ||
const coll = db.getCollection(collection) | ||
const coll = service.collection(collection) | ||
return coll | ||
@@ -167,5 +167,7 @@ .chain() | ||
get: { | ||
byId(collection, id) { | ||
const coll = db.getCollection(collection) | ||
return clean()(coll.findOne({ 'DROSSE.ids': { $contains: id } })) | ||
byId(collection, id, cleanFields = []) { | ||
const coll = service.collection(collection) | ||
return clean(...cleanFields)( | ||
coll.findOne({ 'DROSSE.ids': { $contains: id } }) | ||
) | ||
}, | ||
@@ -182,17 +184,21 @@ | ||
byField(collection, field, value) { | ||
return this.byFields(collection, [field], value) | ||
byField(collection, field, value, cleanFields = []) { | ||
return this.byFields(collection, [field], value, cleanFields) | ||
}, | ||
byFields(collection, fields, value) { | ||
return this.find(collection, { | ||
$or: fields.map(field => ({ | ||
[field]: { $contains: value }, | ||
})), | ||
}) | ||
byFields(collection, fields, value, cleanFields = []) { | ||
return this.find( | ||
collection, | ||
{ | ||
$or: fields.map(field => ({ | ||
[field]: { $contains: value }, | ||
})), | ||
}, | ||
cleanFields | ||
) | ||
}, | ||
find(collection, query) { | ||
const coll = db.getCollection(collection) | ||
return coll.chain().findOne(query).data().map(clean()) | ||
find(collection, query, cleanFields = []) { | ||
const coll = service.collection(collection) | ||
return clean(...cleanFields)(coll.findOne(query)) | ||
}, | ||
@@ -203,3 +209,3 @@ }, | ||
getIdMap(collection, fieldname, firstOnly = false) { | ||
const coll = db.getCollection(collection) | ||
const coll = service.collection(collection) | ||
return coll.data.reduce( | ||
@@ -215,3 +221,3 @@ (acc, item) => ({ | ||
chain(collection) { | ||
return db.getCollection(collection).chain() | ||
return service.collection(collection).chain() | ||
}, | ||
@@ -223,3 +229,3 @@ | ||
insert(collection, ids, payload) { | ||
const coll = db.getCollection(collection) | ||
const coll = service.collection(collection) | ||
return coll.insert(lodash.cloneDeep({ ...payload, DROSSE: { ids } })) | ||
@@ -230,7 +236,7 @@ }, | ||
byId(collection, id, newValue) { | ||
const coll = db.getCollection(collection) | ||
const coll = service.collection(collection) | ||
coll.findAndUpdate({ 'DROSSE.ids': { $contains: id } }, doc => { | ||
Object.entries(newValue).forEach(([key, value]) => { | ||
doc[key] = value | ||
lodash.set(doc, key, value) | ||
}) | ||
@@ -242,3 +248,3 @@ }) | ||
append(collection, id, subPath, payload) { | ||
const coll = db.getCollection(collection) | ||
const coll = service.collection(collection) | ||
coll.findAndUpdate({ 'DROSSE.ids': { $contains: id } }, doc => { | ||
@@ -252,3 +258,3 @@ if (!lodash.get(doc, subPath)) { | ||
prepend(collection, id, subPath, payload) { | ||
const coll = db.getCollection(collection) | ||
const coll = service.collection(collection) | ||
coll.findAndUpdate({ 'DROSSE.ids': { $contains: id } }, doc => { | ||
@@ -266,3 +272,3 @@ if (!lodash.get(doc, subPath)) { | ||
byId(collection, id) { | ||
const coll = db.getCollection(collection) | ||
const coll = service.collection(collection) | ||
const toDelete = coll.findOne({ 'DROSSE.ids': { $contains: id } }) | ||
@@ -273,2 +279,3 @@ return toDelete && coll.remove(toDelete) | ||
} | ||
return service | ||
} |
{ | ||
"name": "@jota-one/drosse", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Mock your backend the right way.", | ||
@@ -44,2 +44,3 @@ "main": "app/index.js", | ||
"readdirp": "^3.4.0", | ||
"rrdir": "^8.2.0", | ||
"stoppable": "^1.1.0", | ||
@@ -46,0 +47,0 @@ "stream-to-promise": "^3.0.0", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
75091
1424
18
+ Addedrrdir@^8.2.0
+ Addedhas-proto@1.1.0(transitive)
+ Addedhas-symbols@1.1.0(transitive)
+ Addedrrdir@8.2.2(transitive)
- Removedhas-proto@1.0.3(transitive)
- Removedhas-symbols@1.0.3(transitive)