Skip to content

Commit 2d665c9

Browse files
vitaly-tdplewis
authored andcommitted
Update PostgresStorageAdapter.js (#6275)
* Update PostgresStorageAdapter.js Improving use of the `await.async` notation in relation to `pg-promise`, and in general. * Update PostgresStorageAdapter.js * Update PostgresStorageAdapter.js Correcting some results. * Update PostgresStorageAdapter.js
1 parent a72ab50 commit 2d665c9

File tree

1 file changed

+46
-44
lines changed

1 file changed

+46
-44
lines changed

src/Adapters/Storage/Postgres/PostgresStorageAdapter.js

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -829,9 +829,9 @@ export class PostgresStorageAdapter implements StorageAdapter {
829829
this._client.$pool.end();
830830
}
831831

832-
_ensureSchemaCollectionExists(conn: any) {
832+
async _ensureSchemaCollectionExists(conn: any) {
833833
conn = conn || this._client;
834-
return conn
834+
await conn
835835
.none(
836836
'CREATE TABLE IF NOT EXISTS "_SCHEMA" ( "className" varChar(120), "schema" jsonb, "isParseClass" bool, PRIMARY KEY ("className") )'
837837
)
@@ -848,17 +848,17 @@ export class PostgresStorageAdapter implements StorageAdapter {
848848
});
849849
}
850850

851-
classExists(name: string) {
851+
async classExists(name: string) {
852852
return this._client.one(
853853
'SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = $1)',
854854
[name],
855855
a => a.exists
856856
);
857857
}
858858

859-
setClassLevelPermissions(className: string, CLPs: any) {
859+
async setClassLevelPermissions(className: string, CLPs: any) {
860860
const self = this;
861-
return this._client.task('set-class-level-permissions', async t => {
861+
await this._client.task('set-class-level-permissions', async t => {
862862
await self._ensureSchemaCollectionExists(t);
863863
const values = [
864864
className,
@@ -867,13 +867,13 @@ export class PostgresStorageAdapter implements StorageAdapter {
867867
JSON.stringify(CLPs),
868868
];
869869
await t.none(
870-
`UPDATE "_SCHEMA" SET $2:name = json_object_set_key($2:name, $3::text, $4::jsonb) WHERE "className"=$1`,
870+
`UPDATE "_SCHEMA" SET $2:name = json_object_set_key($2:name, $3::text, $4::jsonb) WHERE "className" = $1`,
871871
values
872872
);
873873
});
874874
}
875875

876-
setIndexesWithSchemaFormat(
876+
async setIndexesWithSchemaFormat(
877877
className: string,
878878
submittedIndexes: any,
879879
existingIndexes: any = {},
@@ -923,7 +923,7 @@ export class PostgresStorageAdapter implements StorageAdapter {
923923
});
924924
}
925925
});
926-
return conn.tx('set-indexes-with-schema-format', async t => {
926+
await conn.tx('set-indexes-with-schema-format', async t => {
927927
if (insertedIndexes.length > 0) {
928928
await self.createIndexes(className, insertedIndexes, t);
929929
}
@@ -932,16 +932,16 @@ export class PostgresStorageAdapter implements StorageAdapter {
932932
}
933933
await self._ensureSchemaCollectionExists(t);
934934
await t.none(
935-
'UPDATE "_SCHEMA" SET $2:name = json_object_set_key($2:name, $3::text, $4::jsonb) WHERE "className"=$1',
935+
'UPDATE "_SCHEMA" SET $2:name = json_object_set_key($2:name, $3::text, $4::jsonb) WHERE "className" = $1',
936936
[className, 'schema', 'indexes', JSON.stringify(existingIndexes)]
937937
);
938938
});
939939
}
940940

941-
createClass(className: string, schema: SchemaType, conn: ?any) {
941+
async createClass(className: string, schema: SchemaType, conn: ?any) {
942942
conn = conn || this._client;
943943
return conn
944-
.tx('create-class', t => {
944+
.tx('create-class', async t => {
945945
const q1 = this.createTable(className, schema, t);
946946
const q2 = t.none(
947947
'INSERT INTO "_SCHEMA" ("className", "schema", "isParseClass") VALUES ($<className>, $<schema>, true)',
@@ -954,6 +954,8 @@ export class PostgresStorageAdapter implements StorageAdapter {
954954
schema.fields,
955955
t
956956
);
957+
// TODO: The test should not verify the returned value, and then
958+
// the method can be simplified, to avoid returning useless stuff.
957959
return t.batch([q1, q2, q3]);
958960
})
959961
.then(() => {
@@ -977,7 +979,7 @@ export class PostgresStorageAdapter implements StorageAdapter {
977979
}
978980

979981
// Just create a table, do not insert in schema
980-
createTable(className: string, schema: SchemaType, conn: any) {
982+
async createTable(className: string, schema: SchemaType, conn: any) {
981983
conn = conn || this._client;
982984
const self = this;
983985
debug('createTable', className, schema);
@@ -1042,12 +1044,12 @@ export class PostgresStorageAdapter implements StorageAdapter {
10421044
});
10431045
}
10441046

1045-
schemaUpgrade(className: string, schema: SchemaType, conn: any) {
1047+
async schemaUpgrade(className: string, schema: SchemaType, conn: any) {
10461048
debug('schemaUpgrade', { className, schema });
10471049
conn = conn || this._client;
10481050
const self = this;
10491051

1050-
return conn.tx('schema-upgrade', async t => {
1052+
await conn.tx('schema-upgrade', async t => {
10511053
const columns = await t.map(
10521054
'SELECT column_name FROM information_schema.columns WHERE table_name = $<className>',
10531055
{ className },
@@ -1068,7 +1070,7 @@ export class PostgresStorageAdapter implements StorageAdapter {
10681070
});
10691071
}
10701072

1071-
addFieldIfNotExists(
1073+
async addFieldIfNotExists(
10721074
className: string,
10731075
fieldName: string,
10741076
type: any,
@@ -1078,7 +1080,7 @@ export class PostgresStorageAdapter implements StorageAdapter {
10781080
debug('addFieldIfNotExists', { className, fieldName, type });
10791081
conn = conn || this._client;
10801082
const self = this;
1081-
return conn.tx('add-field-if-not-exists', async t => {
1083+
await conn.tx('add-field-if-not-exists', async t => {
10821084
if (type.type !== 'Relation') {
10831085
try {
10841086
await t.none(
@@ -1091,7 +1093,7 @@ export class PostgresStorageAdapter implements StorageAdapter {
10911093
);
10921094
} catch (error) {
10931095
if (error.code === PostgresRelationDoesNotExistError) {
1094-
return await self.createClass(
1096+
return self.createClass(
10951097
className,
10961098
{ fields: { [fieldName]: type } },
10971099
t
@@ -1128,7 +1130,7 @@ export class PostgresStorageAdapter implements StorageAdapter {
11281130

11291131
// Drops a collection. Resolves with true if it was a Parse Schema (eg. _User, Custom, etc.)
11301132
// and resolves with false if it wasn't (eg. a join table). Rejects if deletion was impossible.
1131-
deleteClass(className: string) {
1133+
async deleteClass(className: string) {
11321134
const operations = [
11331135
{ query: `DROP TABLE IF EXISTS $1:name`, values: [className] },
11341136
{
@@ -1142,12 +1144,12 @@ export class PostgresStorageAdapter implements StorageAdapter {
11421144
}
11431145

11441146
// Delete all data known to this adapter. Used for testing.
1145-
deleteAllClasses() {
1147+
async deleteAllClasses() {
11461148
const now = new Date().getTime();
11471149
const helpers = this._pgp.helpers;
11481150
debug('deleteAllClasses');
11491151

1150-
return this._client
1152+
await this._client
11511153
.task('delete-all-classes', async t => {
11521154
try {
11531155
const results = await t.any('SELECT * FROM "_SCHEMA"');
@@ -1196,7 +1198,7 @@ export class PostgresStorageAdapter implements StorageAdapter {
11961198
// may do so.
11971199

11981200
// Returns a Promise.
1199-
deleteFields(
1201+
async deleteFields(
12001202
className: string,
12011203
schema: SchemaType,
12021204
fieldNames: string[]
@@ -1218,9 +1220,9 @@ export class PostgresStorageAdapter implements StorageAdapter {
12181220
})
12191221
.join(', DROP COLUMN');
12201222

1221-
return this._client.tx('delete-fields', async t => {
1223+
await this._client.tx('delete-fields', async t => {
12221224
await t.none(
1223-
'UPDATE "_SCHEMA" SET "schema"=$<schema> WHERE "className"=$<className>',
1225+
'UPDATE "_SCHEMA" SET "schema" = $<schema> WHERE "className" = $<className>',
12241226
{ schema, className }
12251227
);
12261228
if (values.length > 1) {
@@ -1232,7 +1234,7 @@ export class PostgresStorageAdapter implements StorageAdapter {
12321234
// Return a promise for all schemas known to this adapter, in Parse format. In case the
12331235
// schemas cannot be retrieved, returns a promise that rejects. Requirements for the
12341236
// rejection reason are TBD.
1235-
getAllClasses() {
1237+
async getAllClasses() {
12361238
const self = this;
12371239
return this._client.task('get-all-classes', async t => {
12381240
await self._ensureSchemaCollectionExists(t);
@@ -1245,10 +1247,10 @@ export class PostgresStorageAdapter implements StorageAdapter {
12451247
// Return a promise for the schema with the given name, in Parse format. If
12461248
// this adapter doesn't know about the schema, return a promise that rejects with
12471249
// undefined as the reason.
1248-
getClass(className: string) {
1250+
async getClass(className: string) {
12491251
debug('getClass', className);
12501252
return this._client
1251-
.any('SELECT * FROM "_SCHEMA" WHERE "className"=$<className>', {
1253+
.any('SELECT * FROM "_SCHEMA" WHERE "className" = $<className>', {
12521254
className,
12531255
})
12541256
.then(result => {
@@ -1261,7 +1263,7 @@ export class PostgresStorageAdapter implements StorageAdapter {
12611263
}
12621264

12631265
// TODO: remove the mongo format dependency in the return value
1264-
createObject(
1266+
async createObject(
12651267
className: string,
12661268
schema: SchemaType,
12671269
object: any,
@@ -1426,7 +1428,7 @@ export class PostgresStorageAdapter implements StorageAdapter {
14261428
// Remove all objects that match the given Parse Query.
14271429
// If no objects match, reject with OBJECT_NOT_FOUND. If objects are found and deleted, resolve with undefined.
14281430
// If there is some other error, reject with INTERNAL_SERVER_ERROR.
1429-
deleteObjectsByQuery(
1431+
async deleteObjectsByQuery(
14301432
className: string,
14311433
schema: SchemaType,
14321434
query: QueryType,
@@ -1469,7 +1471,7 @@ export class PostgresStorageAdapter implements StorageAdapter {
14691471
return promise;
14701472
}
14711473
// Return value not currently well specified.
1472-
findOneAndUpdate(
1474+
async findOneAndUpdate(
14731475
className: string,
14741476
schema: SchemaType,
14751477
query: QueryType,
@@ -1487,7 +1489,7 @@ export class PostgresStorageAdapter implements StorageAdapter {
14871489
}
14881490

14891491
// Apply the update to all objects that match the given Parse Query.
1490-
updateObjectsByQuery(
1492+
async updateObjectsByQuery(
14911493
className: string,
14921494
schema: SchemaType,
14931495
query: QueryType,
@@ -1980,7 +1982,7 @@ export class PostgresStorageAdapter implements StorageAdapter {
19801982
// As such, we shouldn't expose this function to users of parse until we have an out-of-band
19811983
// Way of determining if a field is nullable. Undefined doesn't count against uniqueness,
19821984
// which is why we use sparse indexes.
1983-
ensureUniqueness(
1985+
async ensureUniqueness(
19841986
className: string,
19851987
schema: SchemaType,
19861988
fieldNames: string[]
@@ -2016,7 +2018,7 @@ export class PostgresStorageAdapter implements StorageAdapter {
20162018
}
20172019

20182020
// Executes a count.
2019-
count(
2021+
async count(
20202022
className: string,
20212023
schema: SchemaType,
20222024
query: QueryType,
@@ -2055,7 +2057,7 @@ export class PostgresStorageAdapter implements StorageAdapter {
20552057
});
20562058
}
20572059

2058-
distinct(
2060+
async distinct(
20592061
className: string,
20602062
schema: SchemaType,
20612063
query: QueryType,
@@ -2121,7 +2123,7 @@ export class PostgresStorageAdapter implements StorageAdapter {
21212123
);
21222124
}
21232125

2124-
aggregate(className: string, schema: any, pipeline: any) {
2126+
async aggregate(className: string, schema: any, pipeline: any) {
21252127
debug('aggregate', className, pipeline);
21262128
const values = [className];
21272129
let index: number = 2;
@@ -2323,7 +2325,7 @@ export class PostgresStorageAdapter implements StorageAdapter {
23232325
});
23242326
}
23252327
2326-
performInitialization({ VolatileClassesSchemas }: any) {
2328+
async performInitialization({ VolatileClassesSchemas }: any) {
23272329
// TODO: This method needs to be rewritten to make proper use of connections (@vitaly-t)
23282330
debug('performInitialization');
23292331
const promises = VolatileClassesSchemas.map(schema => {
@@ -2362,7 +2364,7 @@ export class PostgresStorageAdapter implements StorageAdapter {
23622364
});
23632365
}
23642366
2365-
createIndexes(className: string, indexes: any, conn: ?any): Promise<void> {
2367+
async createIndexes(className: string, indexes: any, conn: ?any): Promise<void> {
23662368
return (conn || this._client).tx(t =>
23672369
t.batch(
23682370
indexes.map(i => {
@@ -2376,43 +2378,43 @@ export class PostgresStorageAdapter implements StorageAdapter {
23762378
);
23772379
}
23782380
2379-
createIndexesIfNeeded(
2381+
async createIndexesIfNeeded(
23802382
className: string,
23812383
fieldName: string,
23822384
type: any,
23832385
conn: ?any
23842386
): Promise<void> {
2385-
return (conn || this._client).none(
2387+
await (conn || this._client).none(
23862388
'CREATE INDEX $1:name ON $2:name ($3:name)',
23872389
[fieldName, className, type]
23882390
);
23892391
}
23902392
2391-
dropIndexes(className: string, indexes: any, conn: any): Promise<void> {
2393+
async dropIndexes(className: string, indexes: any, conn: any): Promise<void> {
23922394
const queries = indexes.map(i => ({
23932395
query: 'DROP INDEX $1:name',
23942396
values: i,
23952397
}));
2396-
return (conn || this._client).tx(t =>
2398+
await (conn || this._client).tx(t =>
23972399
t.none(this._pgp.helpers.concat(queries))
23982400
);
23992401
}
24002402
2401-
getIndexes(className: string) {
2403+
async getIndexes(className: string) {
24022404
const qs = 'SELECT * FROM pg_indexes WHERE tablename = ${className}';
24032405
return this._client.any(qs, { className });
24042406
}
24052407
2406-
updateSchemaWithIndexes(): Promise<void> {
2408+
async updateSchemaWithIndexes(): Promise<void> {
24072409
return Promise.resolve();
24082410
}
24092411
24102412
// Used for testing purposes
2411-
updateEstimatedCount(className: string) {
2413+
async updateEstimatedCount(className: string) {
24122414
return this._client.none('ANALYZE $1:name', [className]);
24132415
}
24142416
2415-
createTransactionalSession(): Promise<any> {
2417+
async createTransactionalSession(): Promise<any> {
24162418
return new Promise(resolve => {
24172419
const transactionalSession = {};
24182420
transactionalSession.result = this._client.tx(t => {

0 commit comments

Comments
 (0)