mirror of
https://github.com/The-Low-Code-Foundation/OpenNoodl.git
synced 2026-01-11 06:42:57 +01:00
feat(runtime): Add "className" option support to "relatedTo" (#73)
* feat(runtime): Add "className" option support to "relatedTo" Also includes error handling when "className" is not found.
This commit is contained in:
committed by
Richard Osborne
parent
767bb28d1c
commit
e8d229f385
@@ -36,16 +36,15 @@ function convertVisualFilter(query, options) {
|
|||||||
const _res = {};
|
const _res = {};
|
||||||
var cond;
|
var cond;
|
||||||
var value = query.input !== undefined ? inputs[query.input] : query.value;
|
var value = query.input !== undefined ? inputs[query.input] : query.value;
|
||||||
|
|
||||||
if (query.operator === 'exist') {
|
if (query.operator === 'exist') {
|
||||||
_res[query.property] = { $exists: true };
|
_res[query.property] = { $exists: true };
|
||||||
return _res;
|
return _res;
|
||||||
|
} else if (query.operator === 'not exist') {
|
||||||
|
_res[query.property] = { $exists: false };
|
||||||
|
return _res;
|
||||||
}
|
}
|
||||||
else if (query.operator === 'not exist') {
|
|
||||||
_res[query.property] = { $exists: false };
|
|
||||||
return _res;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (value === undefined) return;
|
if (value === undefined) return;
|
||||||
|
|
||||||
if (CloudStore._collections[options.collectionName])
|
if (CloudStore._collections[options.collectionName])
|
||||||
@@ -80,7 +79,6 @@ function convertVisualFilter(query, options) {
|
|||||||
cond = { $regex: value, $options: 'i' };
|
cond = { $regex: value, $options: 'i' };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
_res[query.property] = cond;
|
_res[query.property] = cond;
|
||||||
|
|
||||||
return _res;
|
return _res;
|
||||||
@@ -163,10 +161,22 @@ function _value(v) {
|
|||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {Record<string, unknown>} filter
|
||||||
|
* @param {{
|
||||||
|
* collectionName?: string;
|
||||||
|
* modelScope?: unknown;
|
||||||
|
* error: (error: string) => void;
|
||||||
|
* }} options
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
function convertFilterOp(filter, options) {
|
function convertFilterOp(filter, options) {
|
||||||
const keys = Object.keys(filter);
|
const keys = Object.keys(filter);
|
||||||
if (keys.length === 0) return {};
|
if (keys.length === 0) return {};
|
||||||
if (keys.length !== 1) return options.error('Filter must only have one key found ' + keys.join(','));
|
if (keys.length !== 1) {
|
||||||
|
return options.error('Filter must only have one key found ' + keys.join(','));
|
||||||
|
}
|
||||||
|
|
||||||
const res = {};
|
const res = {};
|
||||||
const key = keys[0];
|
const key = keys[0];
|
||||||
@@ -179,18 +189,27 @@ function convertFilterOp(filter, options) {
|
|||||||
} else if (filter['idContainedIn'] !== undefined) {
|
} else if (filter['idContainedIn'] !== undefined) {
|
||||||
res['objectId'] = { $in: filter['idContainedIn'] };
|
res['objectId'] = { $in: filter['idContainedIn'] };
|
||||||
} else if (filter['relatedTo'] !== undefined) {
|
} else if (filter['relatedTo'] !== undefined) {
|
||||||
var modelId = filter['relatedTo']['id'];
|
const modelId = filter['relatedTo']['id'];
|
||||||
if (modelId === undefined) return options.error('Must provide id in relatedTo filter');
|
if (modelId === undefined) {
|
||||||
|
return options.error('Must provide id in relatedTo filter');
|
||||||
|
}
|
||||||
|
|
||||||
var relationKey = filter['relatedTo']['key'];
|
const relationKey = filter['relatedTo']['key'];
|
||||||
if (relationKey === undefined) return options.error('Must provide key in relatedTo filter');
|
if (relationKey === undefined) {
|
||||||
|
return options.error('Must provide key in relatedTo filter');
|
||||||
|
}
|
||||||
|
|
||||||
|
const className = filter['relatedTo']['className'] || (options.modelScope || Model).get(modelId)?._class;
|
||||||
|
if (typeof className === 'undefined') {
|
||||||
|
// Either the pointer is loaded as an object or we allow passing in the className.
|
||||||
|
return options.error('Must preload the Pointer or include className');
|
||||||
|
}
|
||||||
|
|
||||||
var m = (options.modelScope || Model).get(modelId);
|
|
||||||
res['$relatedTo'] = {
|
res['$relatedTo'] = {
|
||||||
object: {
|
object: {
|
||||||
__type: 'Pointer',
|
__type: 'Pointer',
|
||||||
objectId: modelId,
|
objectId: modelId,
|
||||||
className: m._class
|
className
|
||||||
},
|
},
|
||||||
key: relationKey
|
key: relationKey
|
||||||
};
|
};
|
||||||
@@ -208,13 +227,14 @@ function convertFilterOp(filter, options) {
|
|||||||
else if (opAndValue['containedIn'] !== undefined) res[key] = { $in: opAndValue['containedIn'] };
|
else if (opAndValue['containedIn'] !== undefined) res[key] = { $in: opAndValue['containedIn'] };
|
||||||
else if (opAndValue['notContainedIn'] !== undefined) res[key] = { $nin: opAndValue['notContainedIn'] };
|
else if (opAndValue['notContainedIn'] !== undefined) res[key] = { $nin: opAndValue['notContainedIn'] };
|
||||||
else if (opAndValue['pointsTo'] !== undefined) {
|
else if (opAndValue['pointsTo'] !== undefined) {
|
||||||
var m = (options.modelScope || Model).get(opAndValue['pointsTo']);
|
let schema = null;
|
||||||
if (CloudStore._collections[options.collectionName])
|
if (CloudStore._collections[options.collectionName]) {
|
||||||
var schema = CloudStore._collections[options.collectionName].schema;
|
schema = CloudStore._collections[options.collectionName].schema;
|
||||||
|
}
|
||||||
|
|
||||||
var targetClass =
|
const targetClass =
|
||||||
schema && schema.properties && schema.properties[key] ? schema.properties[key].targetClass : undefined;
|
schema && schema.properties && schema.properties[key] ? schema.properties[key].targetClass : undefined;
|
||||||
var type = schema && schema.properties && schema.properties[key] ? schema.properties[key].type : undefined;
|
const type = schema && schema.properties && schema.properties[key] ? schema.properties[key].type : undefined;
|
||||||
|
|
||||||
if (type === 'Relation') {
|
if (type === 'Relation') {
|
||||||
res[key] = {
|
res[key] = {
|
||||||
@@ -223,13 +243,13 @@ function convertFilterOp(filter, options) {
|
|||||||
className: targetClass
|
className: targetClass
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
if (Array.isArray(opAndValue['pointsTo']))
|
if (Array.isArray(opAndValue['pointsTo'])) {
|
||||||
res[key] = {
|
res[key] = {
|
||||||
$in: opAndValue['pointsTo'].map((v) => {
|
$in: opAndValue['pointsTo'].map((v) => {
|
||||||
return { __type: 'Pointer', objectId: v, className: targetClass };
|
return { __type: 'Pointer', objectId: v, className: targetClass };
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
else
|
} else {
|
||||||
res[key] = {
|
res[key] = {
|
||||||
$eq: {
|
$eq: {
|
||||||
__type: 'Pointer',
|
__type: 'Pointer',
|
||||||
@@ -237,6 +257,7 @@ function convertFilterOp(filter, options) {
|
|||||||
className: targetClass
|
className: targetClass
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else if (opAndValue['matchesRegex'] !== undefined) {
|
} else if (opAndValue['matchesRegex'] !== undefined) {
|
||||||
res[key] = {
|
res[key] = {
|
||||||
@@ -257,43 +278,42 @@ function convertFilterOp(filter, options) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
// Geo points
|
// Geo points
|
||||||
} else if (opAndValue['nearSphere'] !== undefined) {
|
} else if (opAndValue['nearSphere'] !== undefined) {
|
||||||
var _v = opAndValue['nearSphere'];
|
var _v = opAndValue['nearSphere'];
|
||||||
res[key] = {
|
res[key] = {
|
||||||
$nearSphere: {
|
$nearSphere: {
|
||||||
__type: "GeoPoint",
|
__type: 'GeoPoint',
|
||||||
latitude: _v.latitude,
|
latitude: _v.latitude,
|
||||||
longitude: _v.longitude,
|
longitude: _v.longitude
|
||||||
},
|
},
|
||||||
$maxDistanceInMiles:_v.$maxDistanceInMiles,
|
$maxDistanceInMiles: _v.$maxDistanceInMiles,
|
||||||
$maxDistanceInKilometers:_v.maxDistanceInKilometers,
|
$maxDistanceInKilometers: _v.maxDistanceInKilometers,
|
||||||
$maxDistanceInRadians:_v.maxDistanceInRadians
|
$maxDistanceInRadians: _v.maxDistanceInRadians
|
||||||
};
|
};
|
||||||
} else if (opAndValue['withinBox'] !== undefined) {
|
} else if (opAndValue['withinBox'] !== undefined) {
|
||||||
var _v = opAndValue['withinBox'];
|
var _v = opAndValue['withinBox'];
|
||||||
res[key] = {
|
res[key] = {
|
||||||
$within:{
|
$within: {
|
||||||
$box: _v.map(gp => ({
|
$box: _v.map((gp) => ({
|
||||||
__type:"GeoPoint",
|
__type: 'GeoPoint',
|
||||||
latitude:gp.latitude,
|
latitude: gp.latitude,
|
||||||
longitude:gp.longitude
|
longitude: gp.longitude
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
} else if (opAndValue['withinPolygon'] !== undefined) {
|
} else if (opAndValue['withinPolygon'] !== undefined) {
|
||||||
var _v = opAndValue['withinPolygon'];
|
var _v = opAndValue['withinPolygon'];
|
||||||
res[key] = {
|
res[key] = {
|
||||||
$geoWithin:{
|
$geoWithin: {
|
||||||
$polygon: _v.map(gp => ({
|
$polygon: _v.map((gp) => ({
|
||||||
__type:"GeoPoint",
|
__type: 'GeoPoint',
|
||||||
latitude:gp.latitude,
|
latitude: gp.latitude,
|
||||||
longitude:gp.longitude
|
longitude: gp.longitude
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
options.error('Unrecognized filter keys ' + keys.join(','));
|
options.error('Unrecognized filter keys ' + keys.join(','));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user