Skip to content

Commit b69a0a4

Browse files
authored
pg-promise refactoring (parse-community#4401)
initial refactoring of `pg-promise` code.
1 parent 75a585a commit b69a0a4

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed

src/Adapters/Storage/Postgres/PostgresStorageAdapter.js

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -601,9 +601,7 @@ export class PostgresStorageAdapter {
601601
}
602602

603603
classExists(name) {
604-
return this._client.one(`SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = $1)`, [name]).then((res) => {
605-
return res.exists;
606-
});
604+
return this._client.one('SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = $1)', [name], a => a.exists);
607605
}
608606

609607
setClassLevelPermissions(className, CLPs) {
@@ -655,17 +653,19 @@ export class PostgresStorageAdapter {
655653
if (deletedIndexes.length > 0) {
656654
deletePromise = this.dropIndexes(className, deletedIndexes, conn);
657655
}
658-
return deletePromise
659-
.then(() => insertPromise)
660-
.then(() => this._ensureSchemaCollectionExists())
661-
.then(() => {
662-
const values = [className, 'schema', 'indexes', JSON.stringify(existingIndexes)]
663-
return conn.none(`UPDATE "_SCHEMA" SET $2:name = json_object_set_key($2:name, $3::text, $4::jsonb) WHERE "className"=$1 `, values);
664-
});
656+
return conn.task(t => {
657+
const values = [className, 'schema', 'indexes', JSON.stringify(existingIndexes)];
658+
return t.batch([
659+
deletePromise,
660+
insertPromise,
661+
this._ensureSchemaCollectionExists(t),
662+
t.none('UPDATE "_SCHEMA" SET $2:name = json_object_set_key($2:name, $3::text, $4::jsonb) WHERE "className"=$1', values)
663+
]);
664+
});
665665
}
666666

667667
createClass(className, schema) {
668-
return this._client.tx(t => {
668+
return this._client.tx('create-class', t => {
669669
const q1 = this.createTable(className, schema, t);
670670
const q2 = t.none('INSERT INTO "_SCHEMA" ("className", "schema", "isParseClass") VALUES ($<className>, $<schema>, true)', { className, schema });
671671
const q3 = this.setIndexesWithSchemaFormat(className, schema.indexes, {}, schema.fields, t);
@@ -727,15 +727,17 @@ export class PostgresStorageAdapter {
727727
});
728728
const qs = `CREATE TABLE IF NOT EXISTS $1:name (${patternsArray.join(',')})`;
729729
const values = [className, ...valuesArray];
730-
return this._ensureSchemaCollectionExists(conn)
731-
.then(() => conn.none(qs, values))
732-
.catch(error => {
733-
if (error.code === PostgresDuplicateRelationError) {
734-
// Table already exists, must have been created by a different request. Ignore error.
735-
} else {
736-
throw error;
737-
}
738-
}).then(() => {
730+
return conn.task(t => {
731+
return this._ensureSchemaCollectionExists(t)
732+
.then(() => conn.none(qs, values))
733+
.catch(error => {
734+
if (error.code === PostgresDuplicateRelationError) {
735+
// Table already exists, must have been created by a different request. Ignore error.
736+
} else {
737+
throw error;
738+
}})
739+
})
740+
.then(() => {
739741
return conn.tx('create-relation-tables', t => {
740742
const queries = relations.map((fieldName) => {
741743
return t.none('CREATE TABLE IF NOT EXISTS $<joinTable:name> ("relatedId" varChar(120), "owningId" varChar(120), PRIMARY KEY("relatedId", "owningId") )', {joinTable: `_Join:${fieldName}:${className}`});
@@ -748,7 +750,7 @@ export class PostgresStorageAdapter {
748750
addFieldIfNotExists(className, fieldName, type) {
749751
// TODO: Must be revised for invalid logic...
750752
debug('addFieldIfNotExists', {className, fieldName, type});
751-
return this._client.tx("addFieldIfNotExists", t=> {
753+
return this._client.tx('add-field-if-not-exists', t => {
752754
let promise = Promise.resolve();
753755
if (type.type !== 'Relation') {
754756
promise = t.none('ALTER TABLE $<className:name> ADD COLUMN $<fieldName:name> $<postgresType:raw>', {

0 commit comments

Comments
 (0)