Skip to content

Commit bb05330

Browse files
authored
refactoring method createClass (#4465)
* refactoring method createClass Removing error-analysis conditions that are irrelevant, i.e. since we are only returning the batch, the error will always have `data` set to the size of the input array, as per [the library's API](http://vitaly-t.github.io/spex/errors.BatchError.html). * Update PostgresStorageAdapter.js removing the use of the default separator.
1 parent 7bb9c6d commit bb05330

File tree

1 file changed

+17
-19
lines changed

1 file changed

+17
-19
lines changed

src/Adapters/Storage/Postgres/PostgresStorageAdapter.js

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ const buildWhereClause = ({ schema, query, index }) => {
237237
inPatterns.push(`${listElem}`);
238238
}
239239
});
240-
patterns.push(`(${name})::jsonb @> '[${inPatterns.join(',')}]'::jsonb`);
240+
patterns.push(`(${name})::jsonb @> '[${inPatterns.join()}]'::jsonb`);
241241
} else if (fieldValue.$regex) {
242242
// Handle later
243243
} else {
@@ -320,9 +320,9 @@ const buildWhereClause = ({ schema, query, index }) => {
320320
}
321321
});
322322
if (allowNull) {
323-
patterns.push(`($${index}:name IS NULL OR $${index}:name && ARRAY[${inPatterns.join(',')}])`);
323+
patterns.push(`($${index}:name IS NULL OR $${index}:name && ARRAY[${inPatterns.join()}])`);
324324
} else {
325-
patterns.push(`$${index}:name && ARRAY[${inPatterns.join(',')}]`);
325+
patterns.push(`$${index}:name && ARRAY[${inPatterns.join()}]`);
326326
}
327327
index = index + 1 + inPatterns.length;
328328
} else if (isInOrNin) {
@@ -346,7 +346,7 @@ const buildWhereClause = ({ schema, query, index }) => {
346346
inPatterns.push(`$${index + 1 + listIndex}`);
347347
}
348348
});
349-
patterns.push(`$${index}:name ${not} IN (${inPatterns.join(',')})`);
349+
patterns.push(`$${index}:name ${not} IN (${inPatterns.join()})`);
350350
index = index + 1 + inPatterns.length;
351351
}
352352
} else if (!notIn) {
@@ -666,17 +666,15 @@ export class PostgresStorageAdapter {
666666
const q1 = this.createTable(className, schema, t);
667667
const q2 = t.none('INSERT INTO "_SCHEMA" ("className", "schema", "isParseClass") VALUES ($<className>, $<schema>, true)', { className, schema });
668668
const q3 = this.setIndexesWithSchemaFormat(className, schema.indexes, {}, schema.fields, t);
669-
670669
return t.batch([q1, q2, q3]);
671670
})
672671
.then(() => {
673-
return toParseSchema(schema)
672+
return toParseSchema(schema);
674673
})
675-
.catch((err) => {
676-
if (Array.isArray(err.data) && err.data.length > 1 && err.data[0].result.code === PostgresTransactionAbortedError) {
674+
.catch(err => {
675+
if (err.data[0].result.code === PostgresTransactionAbortedError) {
677676
err = err.data[1].result;
678677
}
679-
680678
if (err.code === PostgresUniqueIndexViolationError && err.detail.includes(className)) {
681679
throw new Parse.Error(Parse.Error.DUPLICATE_VALUE, `Class ${className} already exists.`)
682680
}
@@ -723,7 +721,7 @@ export class PostgresStorageAdapter {
723721
}
724722
index = index + 2;
725723
});
726-
const qs = `CREATE TABLE IF NOT EXISTS $1:name (${patternsArray.join(',')})`;
724+
const qs = `CREATE TABLE IF NOT EXISTS $1:name (${patternsArray.join()})`;
727725
const values = [className, ...valuesArray];
728726

729727
return conn.task('create-table', function * (t) {
@@ -994,8 +992,8 @@ export class PostgresStorageAdapter {
994992
return `POINT($${l}, $${l + 1})`;
995993
});
996994

997-
const columnsPattern = columnsArray.map((col, index) => `$${index + 2}:name`).join(',');
998-
const valuesPattern = initialValues.concat(geoPointsInjects).join(',')
995+
const columnsPattern = columnsArray.map((col, index) => `$${index + 2}:name`).join();
996+
const valuesPattern = initialValues.concat(geoPointsInjects).join()
999997

1000998
const qs = `INSERT INTO $1:name (${columnsPattern}) VALUES (${valuesPattern})`
1001999
const values = [className, ...columnsArray, ...valuesArray]
@@ -1233,7 +1231,7 @@ export class PostgresStorageAdapter {
12331231
values.push(...where.values);
12341232

12351233
const whereClause = where.pattern.length > 0 ? `WHERE ${where.pattern}` : '';
1236-
const qs = `UPDATE $1:name SET ${updatePatterns.join(',')} ${whereClause} RETURNING *`;
1234+
const qs = `UPDATE $1:name SET ${updatePatterns.join()} ${whereClause} RETURNING *`;
12371235
debug('update: ', qs, values);
12381236
return this._client.any(qs, values);
12391237
}
@@ -1277,11 +1275,11 @@ export class PostgresStorageAdapter {
12771275
return `"${key}" ASC`;
12781276
}
12791277
return `"${key}" DESC`;
1280-
}).join(',');
1278+
}).join();
12811279
sortPattern = sort !== undefined && Object.keys(sort).length > 0 ? `ORDER BY ${sorting}` : '';
12821280
}
12831281
if (where.sorts && Object.keys(where.sorts).length > 0) {
1284-
sortPattern = `ORDER BY ${where.sorts.join(',')}`;
1282+
sortPattern = `ORDER BY ${where.sorts.join()}`;
12851283
}
12861284

12871285
let columns = '*';
@@ -1295,7 +1293,7 @@ export class PostgresStorageAdapter {
12951293
return `ts_rank_cd(to_tsvector($${2}, $${3}:name), to_tsquery($${4}, $${5}), 32) as score`;
12961294
}
12971295
return `$${index + values.length + 1}:name`;
1298-
}).join(',');
1296+
}).join();
12991297
values = values.concat(keys);
13001298
}
13011299

@@ -1398,7 +1396,7 @@ export class PostgresStorageAdapter {
13981396
// Will happily create the same index with multiple names.
13991397
const constraintName = `unique_${fieldNames.sort().join('_')}`;
14001398
const constraintPatterns = fieldNames.map((fieldName, index) => `$${index + 3}:name`);
1401-
const qs = `ALTER TABLE $1:name ADD CONSTRAINT $2:name UNIQUE (${constraintPatterns.join(',')})`;
1399+
const qs = `ALTER TABLE $1:name ADD CONSTRAINT $2:name UNIQUE (${constraintPatterns.join()})`;
14021400
return this._client.none(qs, [className, constraintName, ...fieldNames])
14031401
.catch(error => {
14041402
if (error.code === PostgresDuplicateRelationError && error.message.includes(constraintName)) {
@@ -1502,7 +1500,7 @@ export class PostgresStorageAdapter {
15021500
columns.push(`AVG(${transformAggregateField(value.$avg)}) AS "${field}"`);
15031501
}
15041502
}
1505-
columns.join(',');
1503+
columns.join();
15061504
} else {
15071505
columns.push('*');
15081506
}
@@ -1543,7 +1541,7 @@ export class PostgresStorageAdapter {
15431541
return `"${key}" ASC`;
15441542
}
15451543
return `"${key}" DESC`;
1546-
}).join(',');
1544+
}).join();
15471545
sortPattern = sort !== undefined && Object.keys(sort).length > 0 ? `ORDER BY ${sorting}` : '';
15481546
}
15491547
}

0 commit comments

Comments
 (0)