Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function jsonApiDeserialize(mapper:Mapper, res:any, opts:any){
if (!res.data || !res.data.data) return;
const collectionReceived = utils.isArray(res.data.data)
// We store all possible items stores in the response in a Object[type][id]
// structure
let itemsIndexed:any = {};
let itemCollection:Array = [].concat(res.data.included || [])
.concat(res.data.data || []);
let i = itemCollection.length;
while (i--) {
let item:any = itemCollection[i]
if (!item.type || !item.id) {
//@warn
// Just remove it so that we don't need to deal with it later
itemCollection.splice(i, 1);
continue;
}
.put(function (req, res, next) {
if (utils.isArray(req.body)) {
updateManyHandler(req, res, next)
} else {
updateAllHandler(req, res, next)
}
})
// DELETE /:resource
if (utils.isObject(config)) {
query.where[keyword] = config
} else {
query.where[keyword] = {
'==': config
}
}
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')
}
query.where[keyword] = {
'==': config
}
}
delete query[keyword]
}
})
let rql = sequence
// 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])) {
_translateFieldObjectIDs (r) {
const _checkFields = (r) => {
for (let field in r) {
if (r[field]._bsontype === 'ObjectID') {
r[field] = typeof r[field].toString() === 'string' ? r[field].toString() : r[field]
}
}
}
if (utils.isArray(r)) {
r.forEach((_r) => {
_checkFields(_r)
})
} else if (utils.isObject(r)) {
_checkFields(r)
}
return r
},
}
if (relation.type === 'belongsTo' || relation.type === 'hasOne') {
let link:any = item.relationships[relationField].data
if (!utils.isObject(link)) {
this.warn('Wrong relation somewhere, object expected', relation);
continue;
}
if (itemsIndexed[link.type] && itemsIndexed[link.type][link.id]) {
let itemLinked:any = itemsIndexed[link.type][link.id];
item.attributes[relation.localField] = itemLinked.attributes;
}
} else if (relation.type === 'hasMany') {
let links:any = item.relationships[relationField].data
if (!utils.isArray(links)) {
this.warn('Wrong relation somewhere, array expected');
continue;
}
item.attributes[relation.localField] = [];
for (let i = 0, l = links.length; i < l; i++) {
let link:any = links[i];
if (itemsIndexed[link.type] && itemsIndexed[link.type][link.id]) {
let itemLinkd:any = itemsIndexed[link.type][link.id];
item.attributes[relation.localField].push(itemLinkd.attributes);
}
}
} else { this.warn('Unknown relation'); continue; }
}
}
}
_getFields (mapper, opts) {
opts || (opts = {})
if (utils.isString(opts.fields)) {
opts.fields = { [opts.fields]: 1 }
} else if (utils.isArray(opts.fields)) {
const fields = {}
opts.fields.forEach((field) => {
fields[field] = 1
})
return fields
}
return opts.fields
},
loadBelongsTo (mapper, def, records, __opts) {
if (utils.isObject(records) && !utils.isArray(records)) {
return Adapter.prototype.loadBelongsTo.call(this, mapper, def, records, __opts);
}
throw new Error('findAll with belongsTo not supported!');
},
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
utils.forOwn(params, function (val, key) {
if (val === null || typeof val === 'undefined') {
return
}
if (!utils.isArray(val)) {
val = [val]
}
val.forEach(function (v) {
if (toString.call(v) === '[object Date]') {
v = v.toISOString()
} else if (utils.isObject(v)) {
v = utils.toJson(v)
}
parts.push(`${encode(key)}=${encode(v)}`)
})
})