Skip to content

Commit

Permalink
SK-1090 remove unnecessary get calls in insert batch operation
Browse files Browse the repository at this point in the history
  • Loading branch information
skyflow-bharti committed Dec 20, 2023
1 parent 5018e90 commit 6e8031a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 29 deletions.
33 changes: 5 additions & 28 deletions src/vault-api/core/Collect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ const getUpsertColumn = (tableName: string, options: Record<string, any>) => {
});
return uniqueColumn;
};

export const constructInsertRecordRequest = (
records: IInsertRecordInput,
options: Record<string, any> = { tokens: true },
) => {
const requestBody: any = [];
if (options.tokens) {
records.records.forEach((record, index) => {
const upsertColumn = getUpsertColumn(record.table, options);
requestBody.push({
Expand All @@ -28,26 +26,9 @@ export const constructInsertRecordRequest = (
tableName: record.table,
fields: record.fields,
...(options?.upsert ? { upsert: upsertColumn } : {}),
});
requestBody.push({
method: 'GET',
tableName: record.table,
ID: `$responses.${2 * index}.records.0.skyflow_id`,
tokenization: true,
});
});
} else {
records.records.forEach((record) => {
const upsertColumn = getUpsertColumn(record.table, options);
requestBody.push({
method: 'POST',
quorum: true,
tableName: record.table,
fields: record.fields,
...(options?.upsert ? { upsert: upsertColumn } : {}),
...(options?.tokens ? { tokenization: true } : {}),
});
});
}
return requestBody;
};

Expand All @@ -60,19 +41,15 @@ export const constructInsertRecordResponse = (
return {
records: responseBody.responses
.map((res, index) => {
if (index % 2 !== 0) {
const skyflowId = responseBody.responses[index - 1].records[0].skyflow_id;
delete res.fields['*'];
const skyflowId = responseBody.responses[index].records[0].skyflow_id;
return {
table: records[Math.floor(index/2)].table,
table: records[index].table,
fields: {
skyflow_id: skyflowId,
...res.fields,
...res.tokens,
},
};
}
return res;
}).filter((res, index) => index % 2 !== 0),
}),
};
}
return {
Expand Down
2 changes: 1 addition & 1 deletion test/vault-api/Skyflow.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ const options = {
tokens: true,
};

const insertResponse = {"vaultID":"<VaultID>","responses":[{"records":[{"skyflow_id":"id"}]},{"fields":{"card_number":"token","cvv":"token","expiry_date":"token","fullname":"token"}}]}
const insertResponse = {"vaultID":"<VaultID>","responses":[{"records":[{"skyflow_id":"id","tokens":{"card_number":"token","cvv":"token","expiry_date":"token","fullname":"token"}}]}]}
const insertResponseWithoutTokens = {"vaultID":"<VaultID>","responses":[{"records":[{"skyflow_id":"id"}]}]}
const on = jest.fn();

Expand Down

0 comments on commit 6e8031a

Please sign in to comment.