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.
This commit is contained in:
@@ -237,7 +237,7 @@ const buildWhereClause = ({ schema, query, index }) => {
|
|||||||
inPatterns.push(`${listElem}`);
|
inPatterns.push(`${listElem}`);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
patterns.push(`(${name})::jsonb @> '[${inPatterns.join(',')}]'::jsonb`);
|
patterns.push(`(${name})::jsonb @> '[${inPatterns.join()}]'::jsonb`);
|
||||||
} else if (fieldValue.$regex) {
|
} else if (fieldValue.$regex) {
|
||||||
// Handle later
|
// Handle later
|
||||||
} else {
|
} else {
|
||||||
@@ -320,9 +320,9 @@ const buildWhereClause = ({ schema, query, index }) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
if (allowNull) {
|
if (allowNull) {
|
||||||
patterns.push(`($${index}:name IS NULL OR $${index}:name && ARRAY[${inPatterns.join(',')}])`);
|
patterns.push(`($${index}:name IS NULL OR $${index}:name && ARRAY[${inPatterns.join()}])`);
|
||||||
} else {
|
} else {
|
||||||
patterns.push(`$${index}:name && ARRAY[${inPatterns.join(',')}]`);
|
patterns.push(`$${index}:name && ARRAY[${inPatterns.join()}]`);
|
||||||
}
|
}
|
||||||
index = index + 1 + inPatterns.length;
|
index = index + 1 + inPatterns.length;
|
||||||
} else if (isInOrNin) {
|
} else if (isInOrNin) {
|
||||||
@@ -346,7 +346,7 @@ const buildWhereClause = ({ schema, query, index }) => {
|
|||||||
inPatterns.push(`$${index + 1 + listIndex}`);
|
inPatterns.push(`$${index + 1 + listIndex}`);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
patterns.push(`$${index}:name ${not} IN (${inPatterns.join(',')})`);
|
patterns.push(`$${index}:name ${not} IN (${inPatterns.join()})`);
|
||||||
index = index + 1 + inPatterns.length;
|
index = index + 1 + inPatterns.length;
|
||||||
}
|
}
|
||||||
} else if (!notIn) {
|
} else if (!notIn) {
|
||||||
@@ -666,17 +666,15 @@ export class PostgresStorageAdapter {
|
|||||||
const q1 = this.createTable(className, schema, t);
|
const q1 = this.createTable(className, schema, t);
|
||||||
const q2 = t.none('INSERT INTO "_SCHEMA" ("className", "schema", "isParseClass") VALUES ($<className>, $<schema>, true)', { className, schema });
|
const q2 = t.none('INSERT INTO "_SCHEMA" ("className", "schema", "isParseClass") VALUES ($<className>, $<schema>, true)', { className, schema });
|
||||||
const q3 = this.setIndexesWithSchemaFormat(className, schema.indexes, {}, schema.fields, t);
|
const q3 = this.setIndexesWithSchemaFormat(className, schema.indexes, {}, schema.fields, t);
|
||||||
|
|
||||||
return t.batch([q1, q2, q3]);
|
return t.batch([q1, q2, q3]);
|
||||||
})
|
})
|
||||||
.then(() => {
|
.then(() => {
|
||||||
return toParseSchema(schema)
|
return toParseSchema(schema);
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch(err => {
|
||||||
if (Array.isArray(err.data) && err.data.length > 1 && err.data[0].result.code === PostgresTransactionAbortedError) {
|
if (err.data[0].result.code === PostgresTransactionAbortedError) {
|
||||||
err = err.data[1].result;
|
err = err.data[1].result;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (err.code === PostgresUniqueIndexViolationError && err.detail.includes(className)) {
|
if (err.code === PostgresUniqueIndexViolationError && err.detail.includes(className)) {
|
||||||
throw new Parse.Error(Parse.Error.DUPLICATE_VALUE, `Class ${className} already exists.`)
|
throw new Parse.Error(Parse.Error.DUPLICATE_VALUE, `Class ${className} already exists.`)
|
||||||
}
|
}
|
||||||
@@ -723,7 +721,7 @@ export class PostgresStorageAdapter {
|
|||||||
}
|
}
|
||||||
index = index + 2;
|
index = index + 2;
|
||||||
});
|
});
|
||||||
const qs = `CREATE TABLE IF NOT EXISTS $1:name (${patternsArray.join(',')})`;
|
const qs = `CREATE TABLE IF NOT EXISTS $1:name (${patternsArray.join()})`;
|
||||||
const values = [className, ...valuesArray];
|
const values = [className, ...valuesArray];
|
||||||
|
|
||||||
return conn.task('create-table', function * (t) {
|
return conn.task('create-table', function * (t) {
|
||||||
@@ -994,8 +992,8 @@ export class PostgresStorageAdapter {
|
|||||||
return `POINT($${l}, $${l + 1})`;
|
return `POINT($${l}, $${l + 1})`;
|
||||||
});
|
});
|
||||||
|
|
||||||
const columnsPattern = columnsArray.map((col, index) => `$${index + 2}:name`).join(',');
|
const columnsPattern = columnsArray.map((col, index) => `$${index + 2}:name`).join();
|
||||||
const valuesPattern = initialValues.concat(geoPointsInjects).join(',')
|
const valuesPattern = initialValues.concat(geoPointsInjects).join()
|
||||||
|
|
||||||
const qs = `INSERT INTO $1:name (${columnsPattern}) VALUES (${valuesPattern})`
|
const qs = `INSERT INTO $1:name (${columnsPattern}) VALUES (${valuesPattern})`
|
||||||
const values = [className, ...columnsArray, ...valuesArray]
|
const values = [className, ...columnsArray, ...valuesArray]
|
||||||
@@ -1233,7 +1231,7 @@ export class PostgresStorageAdapter {
|
|||||||
values.push(...where.values);
|
values.push(...where.values);
|
||||||
|
|
||||||
const whereClause = where.pattern.length > 0 ? `WHERE ${where.pattern}` : '';
|
const whereClause = where.pattern.length > 0 ? `WHERE ${where.pattern}` : '';
|
||||||
const qs = `UPDATE $1:name SET ${updatePatterns.join(',')} ${whereClause} RETURNING *`;
|
const qs = `UPDATE $1:name SET ${updatePatterns.join()} ${whereClause} RETURNING *`;
|
||||||
debug('update: ', qs, values);
|
debug('update: ', qs, values);
|
||||||
return this._client.any(qs, values);
|
return this._client.any(qs, values);
|
||||||
}
|
}
|
||||||
@@ -1277,11 +1275,11 @@ export class PostgresStorageAdapter {
|
|||||||
return `"${key}" ASC`;
|
return `"${key}" ASC`;
|
||||||
}
|
}
|
||||||
return `"${key}" DESC`;
|
return `"${key}" DESC`;
|
||||||
}).join(',');
|
}).join();
|
||||||
sortPattern = sort !== undefined && Object.keys(sort).length > 0 ? `ORDER BY ${sorting}` : '';
|
sortPattern = sort !== undefined && Object.keys(sort).length > 0 ? `ORDER BY ${sorting}` : '';
|
||||||
}
|
}
|
||||||
if (where.sorts && Object.keys(where.sorts).length > 0) {
|
if (where.sorts && Object.keys(where.sorts).length > 0) {
|
||||||
sortPattern = `ORDER BY ${where.sorts.join(',')}`;
|
sortPattern = `ORDER BY ${where.sorts.join()}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
let columns = '*';
|
let columns = '*';
|
||||||
@@ -1295,7 +1293,7 @@ export class PostgresStorageAdapter {
|
|||||||
return `ts_rank_cd(to_tsvector($${2}, $${3}:name), to_tsquery($${4}, $${5}), 32) as score`;
|
return `ts_rank_cd(to_tsvector($${2}, $${3}:name), to_tsquery($${4}, $${5}), 32) as score`;
|
||||||
}
|
}
|
||||||
return `$${index + values.length + 1}:name`;
|
return `$${index + values.length + 1}:name`;
|
||||||
}).join(',');
|
}).join();
|
||||||
values = values.concat(keys);
|
values = values.concat(keys);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1398,7 +1396,7 @@ export class PostgresStorageAdapter {
|
|||||||
// Will happily create the same index with multiple names.
|
// Will happily create the same index with multiple names.
|
||||||
const constraintName = `unique_${fieldNames.sort().join('_')}`;
|
const constraintName = `unique_${fieldNames.sort().join('_')}`;
|
||||||
const constraintPatterns = fieldNames.map((fieldName, index) => `$${index + 3}:name`);
|
const constraintPatterns = fieldNames.map((fieldName, index) => `$${index + 3}:name`);
|
||||||
const qs = `ALTER TABLE $1:name ADD CONSTRAINT $2:name UNIQUE (${constraintPatterns.join(',')})`;
|
const qs = `ALTER TABLE $1:name ADD CONSTRAINT $2:name UNIQUE (${constraintPatterns.join()})`;
|
||||||
return this._client.none(qs, [className, constraintName, ...fieldNames])
|
return this._client.none(qs, [className, constraintName, ...fieldNames])
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
if (error.code === PostgresDuplicateRelationError && error.message.includes(constraintName)) {
|
if (error.code === PostgresDuplicateRelationError && error.message.includes(constraintName)) {
|
||||||
@@ -1502,7 +1500,7 @@ export class PostgresStorageAdapter {
|
|||||||
columns.push(`AVG(${transformAggregateField(value.$avg)}) AS "${field}"`);
|
columns.push(`AVG(${transformAggregateField(value.$avg)}) AS "${field}"`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
columns.join(',');
|
columns.join();
|
||||||
} else {
|
} else {
|
||||||
columns.push('*');
|
columns.push('*');
|
||||||
}
|
}
|
||||||
@@ -1543,7 +1541,7 @@ export class PostgresStorageAdapter {
|
|||||||
return `"${key}" ASC`;
|
return `"${key}" ASC`;
|
||||||
}
|
}
|
||||||
return `"${key}" DESC`;
|
return `"${key}" DESC`;
|
||||||
}).join(',');
|
}).join();
|
||||||
sortPattern = sort !== undefined && Object.keys(sort).length > 0 ? `ORDER BY ${sorting}` : '';
|
sortPattern = sort !== undefined && Object.keys(sort).length > 0 ? `ORDER BY ${sorting}` : '';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user