Update PostgresStorageAdapter.js (#4094)

* proper use of the connections
* proper integrity: creating tables within a single transaction.
This commit is contained in:
Vitaly Tomilov
2017-08-23 16:34:57 +01:00
committed by Florent Vilmart
parent c6546218f4
commit 2b4c7570ea

View File

@@ -668,10 +668,12 @@ export class PostgresStorageAdapter {
throw error;
}
}).then(() => {
// Create the relation tables
return Promise.all(relations.map((fieldName) => {
return conn.none('CREATE TABLE IF NOT EXISTS $<joinTable:name> ("relatedId" varChar(120), "owningId" varChar(120), PRIMARY KEY("relatedId", "owningId") )', {joinTable: `_Join:${fieldName}:${className}`});
}));
return conn.tx('create-relation-tables', t => {
const queries = relations.map((fieldName) => {
return t.none('CREATE TABLE IF NOT EXISTS $<joinTable:name> ("relatedId" varChar(120), "owningId" varChar(120), PRIMARY KEY("relatedId", "owningId") )', {joinTable: `_Join:${fieldName}:${className}`});
});
return t.batch(queries);
});
});
}