Skip to content

Commit 005fd78

Browse files
authored
PG: Fix updating mixed array (#5552)
* PG: Fix updating mixed array * Revert "PG: Fix updating mixed array" This reverts commit 5a44141. * simple fix
1 parent d4f4667 commit 005fd78

File tree

2 files changed

+22
-27
lines changed

2 files changed

+22
-27
lines changed

Diff for: spec/ParseQuery.spec.js

+19
Original file line numberDiff line numberDiff line change
@@ -4637,4 +4637,23 @@ describe('Parse.Query testing', () => {
46374637
const results = await query.find();
46384638
equal(results[0].get('array'), data2);
46394639
});
4640+
4641+
it('can update mixed array more than 100 elements', async () => {
4642+
const array = [0, 1.1, 'hello world', { foo: 'bar' }, null];
4643+
const obj = new TestObject({ array });
4644+
await obj.save();
4645+
4646+
const query = new Parse.Query(TestObject);
4647+
const result = await query.get(obj.id);
4648+
equal(result.get('array').length, 5);
4649+
4650+
for (let i = 0; i < 100; i += 1) {
4651+
array.push(i);
4652+
}
4653+
obj.set('array', array);
4654+
await obj.save();
4655+
4656+
const results = await query.find();
4657+
equal(results[0].get('array').length, 105);
4658+
});
46404659
});

Diff for: src/Adapters/Storage/Postgres/PostgresStorageAdapter.js

+3-27
Original file line numberDiff line numberDiff line change
@@ -1672,33 +1672,9 @@ export class PostgresStorageAdapter implements StorageAdapter {
16721672
values.push(fieldName, fieldValue);
16731673
index += 2;
16741674
} else {
1675-
values.push(fieldName);
1676-
const buildSQLArray = fieldValue => {
1677-
let pattern = 'json_build_array(';
1678-
for (let i = 0; i < fieldValue.length; i += 1) {
1679-
const element = fieldValue[i];
1680-
let type = '';
1681-
if (Array.isArray(element)) {
1682-
pattern += buildSQLArray(element) + ',';
1683-
continue;
1684-
} else if (typeof element == 'object') {
1685-
type = '::json';
1686-
}
1687-
values.push(element);
1688-
pattern += `$${index + 1}${type},`;
1689-
index += 1;
1690-
}
1691-
// remove last comma
1692-
if (fieldValue.length > 0) {
1693-
pattern = pattern.slice(0, -1);
1694-
}
1695-
pattern += ')';
1696-
return pattern;
1697-
};
1698-
const sql = `$${index}:name = ${buildSQLArray(fieldValue)}`;
1699-
1700-
updatePatterns.push(sql);
1701-
index += 1;
1675+
updatePatterns.push(`$${index}:name = $${index + 1}::jsonb`);
1676+
values.push(fieldName, JSON.stringify(fieldValue));
1677+
index += 2;
17021678
}
17031679
} else {
17041680
debug('Not supported update', fieldName, fieldValue);

0 commit comments

Comments
 (0)