Skip to content

Commit 631b168

Browse files
authored
PG: Updating mixed array test (#5252)
* PG: Updating mixed array test Currently we can save a mixed array but not update * build array instead of casting * fix test * add recursion
1 parent 7b3da8b commit 631b168

File tree

2 files changed

+51
-14
lines changed

2 files changed

+51
-14
lines changed

spec/ParseQuery.spec.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4589,4 +4589,28 @@ describe('Parse.Query testing', () => {
45894589
const results = await query.find();
45904590
equal(results[0].get('array'), data2);
45914591
});
4592+
4593+
it('can update mixed array', async () => {
4594+
const data1 = [0, 1.1, 'hello world', { foo: 'bar' }];
4595+
const data2 = [0, 1, { foo: 'bar' }, [], [1, 2, 'bar']];
4596+
const obj1 = new TestObject();
4597+
obj1.set('array', data1);
4598+
await obj1.save();
4599+
equal(obj1.get('array'), data1);
4600+
4601+
const query = new Parse.Query(TestObject);
4602+
query.equalTo('objectId', obj1.id);
4603+
4604+
const result = await query.first();
4605+
equal(result.get('array'), data1);
4606+
4607+
result.set('array', data2);
4608+
equal(result.get('array'), data2);
4609+
4610+
await result.save();
4611+
equal(result.get('array'), data2);
4612+
4613+
const results = await query.find();
4614+
equal(results[0].get('array'), data2);
4615+
});
45924616
});

src/Adapters/Storage/Postgres/PostgresStorageAdapter.js

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1650,24 +1650,37 @@ export class PostgresStorageAdapter implements StorageAdapter {
16501650
const expectedType = parseTypeToPostgresType(schema.fields[fieldName]);
16511651
if (expectedType === 'text[]') {
16521652
updatePatterns.push(`$${index}:name = $${index + 1}::text[]`);
1653+
values.push(fieldName, fieldValue);
1654+
index += 2;
16531655
} else {
1654-
let type = 'text';
1655-
for (const elt of fieldValue) {
1656-
if (typeof elt == 'object') {
1657-
type = 'json';
1658-
break;
1656+
values.push(fieldName);
1657+
const buildSQLArray = fieldValue => {
1658+
let pattern = 'json_build_array(';
1659+
for (let i = 0; i < fieldValue.length; i += 1) {
1660+
const element = fieldValue[i];
1661+
let type = '';
1662+
if (Array.isArray(element)) {
1663+
pattern += buildSQLArray(element) + ',';
1664+
continue;
1665+
} else if (typeof element == 'object') {
1666+
type = '::json';
1667+
}
1668+
values.push(element);
1669+
pattern += `$${index + 1}${type},`;
1670+
index += 1;
16591671
}
1660-
if (typeof elt == 'number') {
1661-
type = 'numeric';
1662-
break;
1672+
// remove last comma
1673+
if (fieldValue.length > 0) {
1674+
pattern = pattern.slice(0, -1);
16631675
}
1664-
}
1665-
updatePatterns.push(
1666-
`$${index}:name = array_to_json($${index + 1}::${type}[])::jsonb`
1667-
);
1676+
pattern += ')';
1677+
return pattern;
1678+
};
1679+
const sql = `$${index}:name = ${buildSQLArray(fieldValue)}`;
1680+
1681+
updatePatterns.push(sql);
1682+
index += 1;
16681683
}
1669-
values.push(fieldName, fieldValue);
1670-
index += 2;
16711684
} else {
16721685
debug('Not supported update', fieldName, fieldValue);
16731686
return Promise.reject(

0 commit comments

Comments
 (0)