Skip to content

Commit 01cf48d

Browse files
vitaly-tflovilmart
authored andcommitted
pg-promise refactoring (#4401)
initial refactoring of `pg-promise` code.
1 parent fd85334 commit 01cf48d

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
@@ -608,9 +608,7 @@ export class PostgresStorageAdapter implements StorageAdapter {
608608
}
609609

610610
classExists(name: string) {
611-
return this._client.one(`SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = $1)`, [name]).then((res) => {
612-
return res.exists;
613-
});
611+
return this._client.one('SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = $1)', [name], a => a.exists);
614612
}
615613

616614
setClassLevelPermissions(className: string, CLPs: any) {
@@ -662,17 +660,19 @@ export class PostgresStorageAdapter implements StorageAdapter {
662660
if (deletedIndexes.length > 0) {
663661
deletePromise = this.dropIndexes(className, deletedIndexes, conn);
664662
}
665-
return deletePromise
666-
.then(() => insertPromise)
667-
.then(() => this._ensureSchemaCollectionExists())
668-
.then(() => {
669-
const values = [className, 'schema', 'indexes', JSON.stringify(existingIndexes)]
670-
return conn.none(`UPDATE "_SCHEMA" SET $2:name = json_object_set_key($2:name, $3::text, $4::jsonb) WHERE "className"=$1 `, values);
671-
});
663+
return conn.task(t => {
664+
const values = [className, 'schema', 'indexes', JSON.stringify(existingIndexes)];
665+
return t.batch([
666+
deletePromise,
667+
insertPromise,
668+
this._ensureSchemaCollectionExists(t),
669+
t.none('UPDATE "_SCHEMA" SET $2:name = json_object_set_key($2:name, $3::text, $4::jsonb) WHERE "className"=$1', values)
670+
]);
671+
});
672672
}
673673

674674
createClass(className: string, schema: SchemaType) {
675-
return this._client.tx(t => {
675+
return this._client.tx('create-class', t => {
676676
const q1 = this.createTable(className, schema, t);
677677
const q2 = t.none('INSERT INTO "_SCHEMA" ("className", "schema", "isParseClass") VALUES ($<className>, $<schema>, true)', { className, schema });
678678
const q3 = this.setIndexesWithSchemaFormat(className, schema.indexes, {}, schema.fields, t);
@@ -734,15 +734,17 @@ export class PostgresStorageAdapter implements StorageAdapter {
734734
});
735735
const qs = `CREATE TABLE IF NOT EXISTS $1:name (${patternsArray.join(',')})`;
736736
const values = [className, ...valuesArray];
737-
return this._ensureSchemaCollectionExists(conn)
738-
.then(() => conn.none(qs, values))
739-
.catch(error => {
740-
if (error.code === PostgresDuplicateRelationError) {
741-
// Table already exists, must have been created by a different request. Ignore error.
742-
} else {
743-
throw error;
744-
}
745-
}).then(() => {
737+
return conn.task(t => {
738+
return this._ensureSchemaCollectionExists(t)
739+
.then(() => conn.none(qs, values))
740+
.catch(error => {
741+
if (error.code === PostgresDuplicateRelationError) {
742+
// Table already exists, must have been created by a different request. Ignore error.
743+
} else {
744+
throw error;
745+
}})
746+
})
747+
.then(() => {
746748
return conn.tx('create-relation-tables', t => {
747749
const queries = relations.map((fieldName) => {
748750
return t.none('CREATE TABLE IF NOT EXISTS $<joinTable:name> ("relatedId" varChar(120), "owningId" varChar(120), PRIMARY KEY("relatedId", "owningId") )', {joinTable: `_Join:${fieldName}:${className}`});
@@ -755,7 +757,7 @@ export class PostgresStorageAdapter implements StorageAdapter {
755757
addFieldIfNotExists(className: string, fieldName: string, type: any) {
756758
// TODO: Must be revised for invalid logic...
757759
debug('addFieldIfNotExists', {className, fieldName, type});
758-
return this._client.tx("addFieldIfNotExists", t=> {
760+
return this._client.tx('add-field-if-not-exists', t => {
759761
let promise = Promise.resolve();
760762
if (type.type !== 'Relation') {
761763
promise = t.none('ALTER TABLE $<className:name> ADD COLUMN $<fieldName:name> $<postgresType:raw>', {

0 commit comments

Comments
 (0)