PG: Fix updating mixed array (#5552)

* PG: Fix updating mixed array

* Revert "PG: Fix updating mixed array"

This reverts commit 5a441413c083747d9e51767be7b2e9298bd4f8ba.

* simple fix
This commit is contained in:
Diamond Lewis
2019-05-02 12:44:17 -05:00
committed by GitHub
parent d4f4667008
commit 005fd78275
2 changed files with 22 additions and 27 deletions

View File

@@ -4637,4 +4637,23 @@ describe('Parse.Query testing', () => {
const results = await query.find();
equal(results[0].get('array'), data2);
});
it('can update mixed array more than 100 elements', async () => {
const array = [0, 1.1, 'hello world', { foo: 'bar' }, null];
const obj = new TestObject({ array });
await obj.save();
const query = new Parse.Query(TestObject);
const result = await query.get(obj.id);
equal(result.get('array').length, 5);
for (let i = 0; i < 100; i += 1) {
array.push(i);
}
obj.set('array', array);
await obj.save();
const results = await query.find();
equal(results[0].get('array').length, 105);
});
});

View File

@@ -1672,33 +1672,9 @@ export class PostgresStorageAdapter implements StorageAdapter {
values.push(fieldName, fieldValue);
index += 2;
} else {
values.push(fieldName);
const buildSQLArray = fieldValue => {
let pattern = 'json_build_array(';
for (let i = 0; i < fieldValue.length; i += 1) {
const element = fieldValue[i];
let type = '';
if (Array.isArray(element)) {
pattern += buildSQLArray(element) + ',';
continue;
} else if (typeof element == 'object') {
type = '::json';
}
values.push(element);
pattern += `$${index + 1}${type},`;
index += 1;
}
// remove last comma
if (fieldValue.length > 0) {
pattern = pattern.slice(0, -1);
}
pattern += ')';
return pattern;
};
const sql = `$${index}:name = ${buildSQLArray(fieldValue)}`;
updatePatterns.push(sql);
index += 1;
updatePatterns.push(`$${index}:name = $${index + 1}::jsonb`);
values.push(fieldName, JSON.stringify(fieldValue));
index += 2;
}
} else {
debug('Not supported update', fieldName, fieldValue);