Fix race condition for creating workspace dependencies

This commit is contained in:
Gregory Schier 2017-09-15 13:04:44 +02:00
parent 13de2194e3
commit a2c8f7f982
2 changed files with 23 additions and 26 deletions

View File

@ -40,9 +40,7 @@ export async function migrate (doc: Object) {
process.nextTick(() => update(doc, {parentId: null}));
}
// Ensure child dependencies exist
await models.cookieJar.getOrCreateForParentId(doc._id);
await models.environment.getOrCreateForWorkspaceId(doc._id);
await _ensureDependencies(doc);
return doc;
}
@ -51,8 +49,10 @@ export function getById (id: string): Promise<Workspace | null> {
return db.get(type, id);
}
export function create (patch: Object = {}): Promise<Workspace> {
return db.docCreate(type, patch);
export async function create (patch: Object = {}): Promise<Workspace> {
const doc = await db.docCreate(type, patch);
await _ensureDependencies(doc);
return doc;
}
export async function all (): Promise<Array<Workspace>> {
@ -77,3 +77,8 @@ export function update (workspace: Workspace, patch: Object): Promise<Workspace>
export function remove (workspace: Workspace): Promise<void> {
return db.remove(workspace);
}
async function _ensureDependencies (workspace: Workspace) {
await models.cookieJar.getOrCreateForParentId(workspace._id);
await models.environment.getOrCreateForWorkspaceId(workspace._id);
}

View File

@ -38,27 +38,19 @@ export const reducer = combineReducers({
*/
async function getAllDocs () {
// Restore docs in parent->child->grandchild order
const allQueryResults = await Promise.all([
models.settings.all(),
models.workspace.all(),
models.workspaceMeta.all(),
models.environment.all(),
models.cookieJar.all(),
models.requestGroup.all(),
models.requestGroupMeta.all(),
models.request.all(),
models.requestMeta.all(),
models.response.all(),
models.oAuth2Token.all()
]);
// Aggregate all results into one big array
const allDocs = [];
for (const result of allQueryResults) {
for (const doc of result) {
allDocs.push(doc);
}
}
const allDocs = [
...await models.settings.all(),
...await models.workspace.all(),
...await models.workspaceMeta.all(),
...await models.environment.all(),
...await models.cookieJar.all(),
...await models.requestGroup.all(),
...await models.requestGroupMeta.all(),
...await models.request.all(),
...await models.requestMeta.all(),
...await models.response.all(),
...await models.oAuth2Token.all()
];
return allDocs;
}