fix(DatabaseController): Do not match any entry when searching for null in relation field (#3924)

This commit is contained in:
Antonio Davi Macedo Coelho de Castro
2017-06-21 09:23:20 -03:00
committed by Florent Vilmart
parent 4509d25471
commit a0d1a3517f
4 changed files with 220 additions and 33 deletions

View File

@@ -188,7 +188,7 @@ const buildWhereClause = ({ schema, query, index }) => {
// nothingin the schema, it's gonna blow up
if (!schema.fields[fieldName]) {
// as it won't exist
if (fieldValue.$exists === false) {
if (fieldValue && fieldValue.$exists === false) {
continue;
}
}
@@ -202,7 +202,16 @@ const buildWhereClause = ({ schema, query, index }) => {
});
let name = components.slice(0, components.length - 1).join('->');
name += '->>' + components[components.length - 1];
patterns.push(`${name} = '${fieldValue}'`);
if (fieldValue === null) {
patterns.push(`${name} IS NULL`);
} else {
patterns.push(`${name} = '${fieldValue}'`);
}
} else if (fieldValue === null) {
patterns.push(`$${index}:name IS NULL`);
values.push(fieldName);
index += 1;
continue;
} else if (typeof fieldValue === 'string') {
patterns.push(`$${index}:name = $${index + 1}`);
values.push(fieldName, fieldValue);
@@ -231,13 +240,16 @@ const buildWhereClause = ({ schema, query, index }) => {
values.push(...clauseValues);
}
if (fieldValue.$ne) {
if (fieldValue.$ne !== undefined) {
if (isArrayField) {
fieldValue.$ne = JSON.stringify([fieldValue.$ne]);
patterns.push(`NOT array_contains($${index}:name, $${index + 1})`);
} else {
if (fieldValue.$ne === null) {
patterns.push(`$${index}:name <> $${index + 1}`);
patterns.push(`$${index}:name IS NOT NULL`);
values.push(fieldName);
index += 1;
continue;
} else {
// if not null, we need to manually exclude null
patterns.push(`($${index}:name <> $${index + 1} OR $${index}:name IS NULL)`);
@@ -764,6 +776,9 @@ export class PostgresStorageAdapter {
validateKeys(object);
Object.keys(object).forEach(fieldName => {
if (object[fieldName] === null) {
return;
}
var authDataMatch = fieldName.match(/^_auth_data_([a-zA-Z0-9_]+)$/);
if (authDataMatch) {
var provider = authDataMatch[1];

View File

@@ -616,13 +616,14 @@ DatabaseController.prototype.reduceInRelation = function(className, query, schem
}
const promises = Object.keys(query).map((key) => {
const t = schema.getExpectedType(className, key);
if (!t || t.type !== 'Relation') {
return Promise.resolve(query);
}
let queries = null;
if (query[key] && (query[key]['$in'] || query[key]['$ne'] || query[key]['$nin'] || query[key].__type == 'Pointer')) {
const t = schema.getExpectedType(className, key);
if (!t || t.type !== 'Relation') {
return Promise.resolve(query);
}
// Build the list of queries
const queries = Object.keys(query[key]).map((constraintKey) => {
queries = Object.keys(query[key]).map((constraintKey) => {
let relatedIds;
let isNegation = false;
if (constraintKey === 'objectId') {
@@ -643,31 +644,32 @@ DatabaseController.prototype.reduceInRelation = function(className, query, schem
relatedIds
}
});
// remove the current queryKey as we don,t need it anymore
delete query[key];
// execute each query independnently to build the list of
// $in / $nin
const promises = queries.map((q) => {
if (!q) {
return Promise.resolve();
}
return this.owningIds(className, key, q.relatedIds).then((ids) => {
if (q.isNegation) {
this.addNotInObjectIdsIds(ids, query);
} else {
this.addInObjectIdsIds(ids, query);
}
return Promise.resolve();
});
});
return Promise.all(promises).then(() => {
return Promise.resolve();
})
} else {
queries = [{isNegation: false, relatedIds: []}];
}
return Promise.resolve();
// remove the current queryKey as we don,t need it anymore
delete query[key];
// execute each query independnently to build the list of
// $in / $nin
const promises = queries.map((q) => {
if (!q) {
return Promise.resolve();
}
return this.owningIds(className, key, q.relatedIds).then((ids) => {
if (q.isNegation) {
this.addNotInObjectIdsIds(ids, query);
} else {
this.addInObjectIdsIds(ids, query);
}
return Promise.resolve();
});
});
return Promise.all(promises).then(() => {
return Promise.resolve();
})
})
return Promise.all(promises).then(() => {