Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if (utils.isObject(query.where) && Object.keys(query.where).length !== 0) {
// Apply filter for each field
sqlBuilder = this.applyWhereFromObject(sqlBuilder, query.where, opts)
} else if (utils.isArray(query.where)) {
sqlBuilder = this.applyWhereFromArray(sqlBuilder, query.where, opts)
}
// Sort
if (query.orderBy) {
if (utils.isString(query.orderBy)) {
query.orderBy = [
[query.orderBy, 'asc']
]
}
for (var i = 0; i < query.orderBy.length; i++) {
if (utils.isString(query.orderBy[i])) {
query.orderBy[i] = [query.orderBy[i], 'asc']
}
sqlBuilder = sqlBuilder.orderBy(query.orderBy[i][0], (query.orderBy[i][1] || '').toUpperCase() === 'DESC' ? 'desc' : 'asc')
}
}
// Offset
if (query.skip) {
sqlBuilder = sqlBuilder.offset(+query.skip)
}
// Limit
if (query.limit) {
sqlBuilder = sqlBuilder.limit(+query.limit)
}
export function MongoDBAdapter (opts) {
utils.classCallCheck(this, MongoDBAdapter)
opts || (opts = {})
if (utils.isString(opts)) {
opts = { uri: opts }
}
utils.fillIn(opts, DEFAULTS)
// Setup non-enumerable properties
Object.defineProperties(this, {
/**
* A Promise that resolves to a reference to the MongoDB client being used by
* this adapter.
*
* @name MongoDBAdapter#client
* @type {Promise}
*/
client: {
writable: true,
value: undefined
// Filter
let groups
if (utils.isObject(query.where) && Object.keys(query.where).length !== 0) {
groups = this._applyWhereFromArray([query.where])
} else if (utils.isArray(query.where)) {
groups = this._applyWhereFromArray(query.where)
}
if (groups) {
rql = rql.filter((row) => this._testArrayGroup(null, groups, row, opts) || true)
}
// Sort
if (query.orderBy) {
if (utils.isString(query.orderBy)) {
query.orderBy = [
[query.orderBy, 'asc']
]
}
for (var i = 0; i < query.orderBy.length; i++) {
if (utils.isString(query.orderBy[i])) {
query.orderBy[i] = [query.orderBy[i], 'asc']
}
rql = (query.orderBy[i][1] || '').toUpperCase() === 'DESC' ? rql.orderBy(r.desc(query.orderBy[i][0])) : rql.orderBy(query.orderBy[i][0])
}
}
// Offset
if (query.skip) {
rql = rql.skip(+query.skip)
}
}
delete query[keyword]
}
})
// Filter
if (utils.isObject(query.where) && Object.keys(query.where).length !== 0) {
// Apply filter for each field
sqlBuilder = this.applyWhereFromObject(sqlBuilder, query.where, opts)
} else if (utils.isArray(query.where)) {
sqlBuilder = this.applyWhereFromArray(sqlBuilder, query.where, opts)
}
// Sort
if (query.orderBy) {
if (utils.isString(query.orderBy)) {
query.orderBy = [
[query.orderBy, 'asc']
]
}
for (var i = 0; i < query.orderBy.length; i++) {
if (utils.isString(query.orderBy[i])) {
query.orderBy[i] = [query.orderBy[i], 'asc']
}
sqlBuilder = sqlBuilder.orderBy(query.orderBy[i][0], (query.orderBy[i][1] || '').toUpperCase() === 'DESC' ? 'desc' : 'asc')
}
}
// Offset
if (query.skip) {
sqlBuilder = sqlBuilder.offset(+query.skip)
}
_pluck (mapper, rql, opts) {
if (utils.isString(opts.fields)) {
opts.fields = [opts.fields]
}
if (utils.isArray(opts.fields)) {
return rql.pluck(...opts.fields)
}
return rql
},
query.orderBy.forEach((clause) => {
if (utils.isString(clause)) {
clause = [clause, 'asc'];
}
dsQuery = clause[1].toUpperCase() === 'DESC' ? dsQuery.order(clause[0], { descending: true }) : dsQuery.order(clause[0]);
});
}
export function addAction (name, opts) {
if (!name || !utils.isString(name)) {
throw new TypeError('action(name[, opts]): Expected: string, Found: ' + typeof name)
}
return function (mapper) {
if (mapper[name]) {
throw new Error('action(name[, opts]): ' + name + ' already exists on target!')
}
opts.request = opts.request || function (config) { return config }
opts.response = opts.response || function (response) { return response }
opts.responseError = opts.responseError || function (err) { return utils.reject(err) }
mapper[name] = function (id, _opts) {
_opts = _opts || {}
if (utils.isObject(id)) {
_opts = id
}
utils.fillIn(_opts, opts)
let adapter = this.getAdapter(_opts.adapter || this.defaultAdapter || 'http')
where.forEach((_where, i) => {
if (utils.isString(_where)) {
return
}
const prev = where[i - 1]
const parser = utils.isArray(_where) ? this._applyWhereFromArray : this._applyWhereFromObject
const group = parser.call(this, _where)
if (prev === 'or') {
group.isOr = true
}
groups.push(group)
})
groups.isArray = true
getPath (method, mapper, id, opts) {
opts || (opts = {})
const args = [
opts.basePath === undefined ? (mapper.basePath === undefined ? this.basePath : mapper.basePath) : opts.basePath,
this.getEndpoint(mapper, (utils.isString(id) || utils.isNumber(id) || method === 'create') ? id : null, opts)
]
if (method === 'find' || method === 'update' || method === 'destroy') {
args.push(id)
}
return makePath.apply(utils, args)
},
export function mount (app, store, config = {}) {
if (!(store instanceof Container)) {
throw new Error('You must provide an instance of JSData.Container or JSData.DataStore!')
}
if (utils.isString(config)) {
config = { path: config }
}
config.path || (config.path = '/')
app.use(config.path, queryParser)
app.use(config.path, new Router(store, config).router)
}