Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
applyWhereFromObject (sqlBuilder, where, opts) {
utils.forOwn(where, (criteria, field) => {
if (!utils.isObject(criteria)) {
criteria = { '==': criteria }
}
// Apply filter for each operator
utils.forOwn(criteria, (value, operator) => {
let isOr = false
if (operator && operator[0] === '|') {
operator = operator.substr(1)
isOr = true
}
let predicateFn = this.getOperator(operator, opts)
if (predicateFn) {
sqlBuilder = predicateFn(sqlBuilder, field, value, isOr)
} else {
throw new Error(`Operator ${operator} not supported!`)
}
utils.forOwn(where, (criteria, field) => {
if (!utils.isObject(criteria)) {
criteria = { '==': criteria }
}
// Apply filter for each operator
utils.forOwn(criteria, (value, operator) => {
let isOr = false
if (operator && operator[0] === '|') {
operator = operator.substr(1)
isOr = true
}
let predicateFn = this.getOperator(operator, opts)
if (predicateFn) {
sqlBuilder = predicateFn(sqlBuilder, field, value, isOr)
} else {
throw new Error(`Operator ${operator} not supported!`)
}
})
})
return sqlBuilder
filterQuery (dsQuery, query, opts) {
query = utils.plainCopy(query || {});
opts || (opts = {});
opts.operators || (opts.operators = {});
query.where || (query.where = {});
query.orderBy || (query.orderBy = query.sort);
query.orderBy || (query.orderBy = []);
query.skip || (query.skip = query.offset);
// Transform non-keyword properties to "where" clause configuration
utils.forOwn(query, (config, keyword) => {
if (reserved.indexOf(keyword) === -1) {
if (utils.isObject(config)) {
query.where[keyword] = config;
} else {
query.where[keyword] = {
'==': config
};
}
delete query[keyword];
}
});
// Apply filter
if (Object.keys(query.where).length !== 0) {
utils.forOwn(query.where, (criteria, field) => {
if (!utils.isObject(criteria)) {
throw new Error('You must provide an instance of JSData.Container, JSData.DataStore, or JSData.Mapper!')
}
const router = this.router = express.Router()
router.use(bodyParser.json())
router.use(bodyParser.urlencoded({
extended: true
}))
if (utils.isFunction(config.request)) {
router.use(config.request)
config.request = undefined
}
if (component instanceof Container) {
utils.forOwn(component._mappers, (mapper, name) => {
let endpoint = `/${mapper.endpoint || name}`
if (utils.isFunction(config.getEndpoint)) {
endpoint = config.getEndpoint(mapper)
}
router.use(endpoint, new Router(mapper, config).router)
})
} else if (component instanceof Mapper) {
const createManyHandler = makeHandler('createMany', component, config)
const createHandler = makeHandler('create', component, config)
const updateManyHandler = makeHandler('updateMany', component, config)
const updateAllHandler = makeHandler('updateAll', component, config)
router.route('/')
// GET /:resource
.get(makeHandler('findAll', component, config))
// POST /:resource
return false
} else {
delete opts.params[parentKey]
if (utils.isObject(id)) {
item = id
}
if (item) {
parentId = parentId || def.getForeignKey(item) || (def.getLocalField(item) ? utils.get(def.getLocalField(item), parentDef.idAttribute) : null)
}
if (parentId) {
delete opts.endpoint
const _opts = {}
utils.forOwn(opts, (value, key) => {
_opts[key] = value
})
utils._(_opts, parentDef)
endpoint = makePath(this.getEndpoint(parentDef, parentId, _opts), parentId, endpoint)
return false
}
}
})
utils.forOwn(query.where, (criteria, field) => {
if (!utils.isObject(criteria)) {
query.where[field] = {
'==': criteria
};
}
utils.forOwn(criteria, (value, operator) => {
let isOr = false;
let _operator = operator;
if (_operator && _operator[0] === '|') {
_operator = _operator.substr(1);
isOr = true;
}
const predicateFn = this.getOperator(_operator, opts);
if (predicateFn) {
if (isOr) {
throw new Error(`Operator ${operator} not supported!`);
} else {
dsQuery = predicateFn(dsQuery, field, value);
}
} else {
throw new Error(`Operator ${operator} not supported!`);
}
return collectionRef.once('value').then((dataSnapshot) => {
const data = dataSnapshot.val()
if (!data) {
return [[], { ref: collectionRef }]
}
const records = []
utils.forOwn(data, (value, key) => {
records.push(value)
})
const _query = new Query({
index: {
getAll () {
return records
}
}
})
return [_query.filter(query).run(), { ref: collectionRef }]
})
},
_applyWhereFromObject (where) {
const fields = []
const ops = []
const predicates = []
utils.forOwn(where, (clause, field) => {
if (!utils.isObject(clause)) {
clause = {
'==': clause
}
}
utils.forOwn(clause, (expr, op) => {
fields.push(field)
ops.push(op)
predicates.push(expr)
})
})
return {
fields,
ops,
predicates
}
utils.forOwn(where, (clause, field) => {
if (!utils.isObject(clause)) {
clause = {
'==': clause
}
}
utils.forOwn(clause, (expr, op) => {
fields.push(field)
ops.push(op)
predicates.push(expr)
})
})
return {
utils.forOwn(query.where, function (criteria, field) {
if (!utils.isObject(criteria)) {
query.where[field] = {
'==': criteria
}
}
utils.forOwn(criteria, function (v, op) {
if (op === '==' || op === '===' || op === 'contains') {
mongoQuery[field] = v
} else if (op === '!=' || op === '!==' || op === 'notContains') {
mongoQuery[field] = mongoQuery[field] || {}
mongoQuery[field].$ne = v
} else if (op === '>') {
mongoQuery[field] = mongoQuery[field] || {}
mongoQuery[field].$gt = v
} else if (op === '>=') {
mongoQuery[field] = mongoQuery[field] || {}
mongoQuery[field].$gte = v
} else if (op === '<') {
mongoQuery[field] = mongoQuery[field] || {}
mongoQuery[field].$lt = v
} else if (op === '<=') {
mongoQuery[field] = mongoQuery[field] || {}