Skip to content

Commit

Permalink
utilize bulk query transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
duranb committed Sep 11, 2024
1 parent 1bd0cdf commit 5fce14e
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 82 deletions.
23 changes: 12 additions & 11 deletions src/packages/plan/gql.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
export default {
CREATE_ACTIVITY_DIRECTIVE: `#graphql
mutation CreateActivityDirective($activityDirectiveInsertInput: activity_directive_insert_input!) {
insert_activity_directive_one(object: $activityDirectiveInsertInput) {
id
type
CREATE_ACTIVITY_DIRECTIVES: `#graphql
mutation CreateActivityDirectives($activityDirectivesInsertInput: [activity_directive_insert_input!]!) {
insert_activity_directive(objects: $activityDirectivesInsertInput) {
returning {
id
type
}
}
}
`,
Expand Down Expand Up @@ -70,13 +72,12 @@ export default {
}
}
`,
UPDATE_ACTIVITY_DIRECTIVE: `#graphql
mutation UpdateActivityDirective($id: Int!, $plan_id: Int!, $activityDirectiveSetInput: activity_directive_set_input!) {
update_activity_directive_by_pk(
pk_columns: { id: $id, plan_id: $plan_id }, _set: $activityDirectiveSetInput
UPDATE_ACTIVITY_DIRECTIVES: `#graphql
mutation UpdateActivityDirective($updates: [activity_directive_updates!]!) {
update_activity_directive_many(
updates: $updates
) {
anchor_id
id
affected_rows
}
}
`,
Expand Down
148 changes: 77 additions & 71 deletions src/packages/plan/plan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { auth } from '../auth/middleware.js';
import type {
ActivityDirective,
ActivityDirectiveInsertInput,
ActivityDirectiveSetInput,
ImportPlanPayload,
PlanInsertInput,
PlanSchema,
Expand Down Expand Up @@ -181,87 +180,94 @@ export async function importPlan(req: Request, res: Response) {
}

const activityRemap: Record<number, number> = {};
await Promise.all(
activities.map(
async ({
const activityDirectivesInsertInput = activities.map(
({
anchored_to_start: anchoredToStart,
arguments: activityArguments,
metadata,
name: activityName,
start_offset: startOffset,
tags,
type,
}) => {
const activityDirectiveInsertInput: ActivityDirectiveInsertInput = {
anchor_id: null,
anchored_to_start: anchoredToStart,
arguments: activityArguments,
id,
metadata,
name: activityName,
plan_id: (createdPlan as PlanSchema).id,
start_offset: startOffset,
tags,
tags: {
data:
tags?.map(({ tag: { name } }) => ({
tag_id: tagsMap[name].id,
})) ?? [],
},
type,
}) => {
const activityDirectiveInsertInput: ActivityDirectiveInsertInput = {
anchor_id: null,
anchored_to_start: anchoredToStart,
arguments: activityArguments,
metadata,
name: activityName,
plan_id: (createdPlan as PlanSchema).id,
start_offset: startOffset,
tags: {
data:
tags?.map(({ tag: { name } }) => ({
tag_id: tagsMap[name].id,
})) ?? [],
},
type,
};

const createdActivityDirectiveResponse = await fetch(GQL_API_URL, {
body: JSON.stringify({
query: gql.CREATE_ACTIVITY_DIRECTIVE,
variables: { activityDirectiveInsertInput },
}),
headers,
method: 'POST',
});

const createdActivityDirectiveData = (await createdActivityDirectiveResponse.json()) as {
data: {
insert_activity_directive_one: ActivityDirective;
};
} | null;

if (createdActivityDirectiveData) {
const {
data: { insert_activity_directive_one: createdActivityDirective },
} = createdActivityDirectiveData;
activityRemap[id] = createdActivityDirective.id;
}
},
),
};

return activityDirectiveInsertInput;
},
);

const createdActivitiesResponse = await fetch(GQL_API_URL, {
body: JSON.stringify({
query: gql.CREATE_ACTIVITY_DIRECTIVES,
variables: {
activityDirectivesInsertInput,
},
}),
headers,
method: 'POST',
});

const createdActivityDirectivesData = (await createdActivitiesResponse.json()) as {
data: {
insert_activity_directive: {
returning: ActivityDirective[];
};
};
} | null;

if (createdActivityDirectivesData) {
const {
data: {
insert_activity_directive: { returning: createdActivityDirectives },
},
} = createdActivityDirectivesData;

if (createdActivityDirectives.length === activities.length) {
createdActivityDirectives.forEach((createdActivityDirective, index) => {
const { id } = activities[index];

activityRemap[id] = createdActivityDirective.id;
});
} else {
throw new Error('Activity insertion failed.');
}
}

// remap all the anchor ids to the newly created activity directives
logger.info(`POST /importPlan: Re-assigning anchors: ${name}`);
await Promise.all(
activities.map(async ({ anchor_id: anchorId, id }) => {
if (anchorId !== null && activityRemap[anchorId] != null && activityRemap[id] != null) {
logger.info(
`POST /importPlan: Re-assigning anchor ${anchorId} to ${activityRemap[anchorId]} for activity ${activityRemap[id]}: ${name}`,
);
const activityDirectiveSetInput: ActivityDirectiveSetInput = {
anchor_id: activityRemap[anchorId],
};

return fetch(GQL_API_URL, {
body: JSON.stringify({
query: gql.UPDATE_ACTIVITY_DIRECTIVE,
variables: {
activityDirectiveSetInput,
id: activityRemap[id],
plan_id: (createdPlan as PlanSchema).id,
},
}),
headers,
method: 'POST',
});
}

const activityDirectivesSetInput = activities
.filter(({ anchor_id: anchorId }) => anchorId !== null)
.map(({ anchor_id: anchorId, id }) => ({
_set: { anchor_id: activityRemap[anchorId as number] },
where: { id: { _eq: activityRemap[id] }, plan_id: { _eq: (createdPlan as PlanSchema).id } },
}));

await fetch(GQL_API_URL, {
body: JSON.stringify({
query: gql.UPDATE_ACTIVITY_DIRECTIVES,
variables: {
updates: activityDirectivesSetInput,
},
}),
);
headers,
method: 'POST',
});

// associate the tags with the newly created plan
logger.info(`POST /importPlan: Importing plan tags: ${name}`);
Expand Down

0 comments on commit 5fce14e

Please sign in to comment.