feat: Add support for dot notation on array fields of Parse Object (#9115)
This commit is contained in:
@@ -175,6 +175,8 @@ const toPostgresSchema = schema => {
|
||||
return schema;
|
||||
};
|
||||
|
||||
const isArrayIndex = (arrayIndex) => Array.from(arrayIndex).every(c => c >= '0' && c <= '9');
|
||||
|
||||
const handleDotFields = object => {
|
||||
Object.keys(object).forEach(fieldName => {
|
||||
if (fieldName.indexOf('.') > -1) {
|
||||
@@ -207,7 +209,11 @@ const transformDotFieldToComponents = fieldName => {
|
||||
if (index === 0) {
|
||||
return `"${cmpt}"`;
|
||||
}
|
||||
return `'${cmpt}'`;
|
||||
if (isArrayIndex(cmpt)) {
|
||||
return Number(cmpt);
|
||||
} else {
|
||||
return `'${cmpt}'`;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
@@ -1851,6 +1851,14 @@ class DatabaseController {
|
||||
// only valid ops that produce an actionable result
|
||||
// the op may have happened on a keypath
|
||||
this._expandResultOnKeyPath(response, key, result);
|
||||
// Revert array to object conversion on dot notation for arrays (e.g. "field.0.key")
|
||||
if (key.includes('.')) {
|
||||
const [field, index] = key.split('.');
|
||||
const isArrayIndex = Array.from(index).every(c => c >= '0' && c <= '9');
|
||||
if (isArrayIndex && Array.isArray(result[field]) && !Array.isArray(response[field])) {
|
||||
response[field] = result[field];
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
return Promise.resolve(response);
|
||||
|
||||
@@ -1096,9 +1096,17 @@ export default class SchemaController {
|
||||
maintenance?: boolean
|
||||
) {
|
||||
if (fieldName.indexOf('.') > 0) {
|
||||
// subdocument key (x.y) => ok if x is of type 'object'
|
||||
fieldName = fieldName.split('.')[0];
|
||||
type = 'Object';
|
||||
// "<array>.<index>" for Nested Arrays
|
||||
// "<embedded document>.<field>" for Nested Objects
|
||||
// JSON Arrays are treated as Nested Objects
|
||||
const [x, y] = fieldName.split('.');
|
||||
fieldName = x;
|
||||
const isArrayIndex = Array.from(y).every(c => c >= '0' && c <= '9');
|
||||
if (isArrayIndex && !['sentPerUTCOffset', 'failedPerUTCOffset'].includes(fieldName)) {
|
||||
type = 'Array';
|
||||
} else {
|
||||
type = 'Object';
|
||||
}
|
||||
}
|
||||
let fieldNameToValidate = `${fieldName}`;
|
||||
if (maintenance && fieldNameToValidate.charAt(0) === '_') {
|
||||
|
||||
Reference in New Issue
Block a user