UNPKG

skyflow-node

Version:
91 lines 3.79 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.constructInsertRecordResponse = exports.constructInsertRecordRequest = void 0; const getUpsertColumn = (tableName, options) => { var _a; let uniqueColumn = ''; (_a = options === null || options === void 0 ? void 0 : options.upsert) === null || _a === void 0 ? void 0 : _a.forEach((upsertOptions) => { if (tableName === upsertOptions.table) { uniqueColumn = upsertOptions.column; } }); return uniqueColumn; }; const constructInsertRecordRequest = (records, options = { tokens: true }) => { let requestBody = []; records.records.forEach((record, index) => { const upsertColumn = getUpsertColumn(record.table, options); requestBody.push(Object.assign(Object.assign({ method: 'POST', quorum: true, tableName: record.table, fields: record.fields }, ((options === null || options === void 0 ? void 0 : options.upsert) ? { upsert: upsertColumn } : {})), ((options === null || options === void 0 ? void 0 : options.tokens) ? { tokenization: true } : {}))); }); requestBody = { records: requestBody, continueOnError: options.continueOnError }; return requestBody; }; exports.constructInsertRecordRequest = constructInsertRecordRequest; const constructInsertRecordResponse = (responseBody, options, records) => { if (options.continueOnError) { const successObjects = []; const failureObjects = []; responseBody.data.responses .forEach((response, index) => { const status = response['Status']; const body = response['Body']; if ('records' in body) { const record = body['records'][0]; if (options.tokens) { successObjects.push({ table: records[index].table, fields: Object.assign({ skyflow_id: record.skyflow_id }, record.tokens), request_index: index, }); } else { successObjects.push({ table: records[index].table, skyflow_id: record.skyflow_id, request_index: index, }); } } else { failureObjects.push({ error: { code: status, description: `${body['error']} - requestId: ${responseBody.metadata.requestId}`, request_index: index, } }); } }); const finalResponse = {}; if (successObjects.length > 0) { finalResponse['records'] = successObjects; } if (failureObjects.length > 0) { finalResponse['errors'] = failureObjects; } return finalResponse; } else if (options.tokens) { return { records: responseBody.data.responses .map((res, index) => { const skyflowId = res.records[0].skyflow_id; const tokens = res.records[0].tokens; return { table: records[index].table, fields: Object.assign({ skyflow_id: skyflowId }, tokens), "request_index": index, }; }), }; } return { records: responseBody.data.responses.map((res, index) => ({ table: records[index].table, skyflow_id: res.records[0].skyflow_id, "request_index": index })), }; }; exports.constructInsertRecordResponse = constructInsertRecordResponse; //# sourceMappingURL=Collect.js.map