UNPKG

@ultipa-graph/ultipa-driver

Version:

NodeJS SDK for ultipa-server 5.1

877 lines 34.9 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.FormatResponse = exports.FormatType = exports.mergeQueryResponse = void 0; const ultipa_pb_1 = require("../proto/ultipa_pb"); const ULTIPA = __importStar(require("../types/types")); const lodash_1 = __importDefault(require("lodash")); const index_1 = require("../utils/index"); const types_1 = require("../types/types"); let ULITPA_PRIVATE_KEYS = { id: "_id", from: "_from", to: "_to", uuid: "_uuid", from_uuid: "_from_uuid", to_uuid: "_to_uuid", }; const _mapValues = (values, types, timezone) => { let result = {}; values.forEach((v, index) => { let pty = types[index]; result[pty.getPropertyName()] = index_1.DESERIALIZE.deserialize(v, pty.getPropertyType(), pty.getSubTypesList(), timezone); }); return result; }; const DataMerge = { arrayDiff: (arr1, arr2) => { let diff = []; arr2.forEach((s) => { if (!arr1.includes(s)) { diff.push(s); } }); return diff; }, merge: (o1, o2, ignoreKeys) => { if (!o2) { return o1; } function customizer(objValue, srcValue, key) { if (ignoreKeys && ignoreKeys.includes(key)) { return srcValue; } if (lodash_1.default.isArray(srcValue)) { return srcValue.concat(objValue); } } return lodash_1.default.mergeWith(o2, o1, customizer); }, concat: (arr1, arr2, byKey, ignoreKeys) => { if (!arr2?.length) { return arr1; } if (byKey) { let newArray = [...arr1]; let diff = DataMerge.arrayDiff(arr1.map((o) => o[byKey]), arr2.map((o) => o[byKey])); diff.forEach((k) => newArray.push({ [byKey]: k, })); let results = []; newArray.forEach((o) => { let newObj = o; let finds = arr2.filter((o2) => { return o2[byKey] == o[byKey]; }); finds.forEach((f) => { newObj = DataMerge.merge(newObj, f, ignoreKeys); }); results.push(newObj); }); return results; } return [...arr1, ...arr2]; }, }; const mergeQueryResponse = (res1, res2) => { if (!res1.explainPlan) { res1.explainPlan = new ULTIPA.ExplainPlan(); } res1.status = res2.status; if (!res2?.items) { return res1; } res1.aliases = res1.aliases || []; const aliasMap = new Map(); [...res1.aliases, ...(res2.aliases || [])].forEach(alias => { if (alias.type === 0) { return; } const existingAlias = aliasMap.get(alias.name); if (existingAlias) { if (existingAlias.type === 0 && alias.type !== 0) { aliasMap.set(alias.name, alias); } } else { aliasMap.set(alias.name, alias); } }); res1.aliases = Array.from(aliasMap.values()); res1.explainPlan.planNodes = res1.explainPlan?.planNodes || []; res1.explainPlan.planNodes = [...new Set([...res1.explainPlan.planNodes, ...(res2.explainPlan?.planNodes || [])].map(item => JSON.stringify(item)))].map(item => JSON.parse(item)); res1.items = res1.items || {}; for (const [key, value] of Object.entries(res2.items)) { if (key in res1.items) { const existingItem = res1.items[key]; switch (existingItem.type) { case ULTIPA.ResultType.RESULT_TYPE_TABLE: existingItem.entities.rows = [...existingItem.entities.rows, ...value.entities.rows]; break; case ULTIPA.ResultType.RESULT_TYPE_PATH: if (!existingItem.entities) { existingItem.entities = []; } existingItem.entities.paths = [...existingItem.entities.paths, ...value.entities.paths]; Object.entries(value.entities.nodes).forEach(([uuid, node]) => { existingItem.entities.nodes[uuid] = node; }); Object.entries(value.entities.edges).forEach(([uuid, edge]) => { existingItem.entities.edges[uuid] = edge; }); break; case ULTIPA.ResultType.RESULT_TYPE_NODE: case ULTIPA.ResultType.RESULT_TYPE_EDGE: if (!existingItem.entities) { existingItem.entities = []; } if (!value.entities) { value.entities = []; } existingItem.entities = [...existingItem.entities, ...value.entities]; break; case ULTIPA.ResultType.RESULT_TYPE_ATTR: if (!existingItem.entities) { existingItem.entities = new ULTIPA.Attr(); } if (!value.entities) { value.entities = new ULTIPA.Attr(); } existingItem.entities.values = [ ...(existingItem.entities.values || []), ...(value.entities.values || []) ]; break; default: if (!existingItem.entities) { existingItem.entities = []; } if (!value.entities) { value.entities = []; } existingItem.entities = [...existingItem.entities, ...value.entities]; } res1.items[key] = existingItem; } else { res1.items[key] = value; } } if (res2?.statistics) { res1.statistics = res1.statistics || { totalCost: 0, engineCost: 0 }; res1.statistics.totalCost += res2.statistics.totalCost; res1.statistics.engineCost += res2.statistics.engineCost; } return res1; }; exports.mergeQueryResponse = mergeQueryResponse; exports.FormatType = { queryResponse: (res, timezone) => { let response = new ULTIPA.Response(); if (res.hasStatus()) { response.status = exports.FormatType.status(res.getStatus()); } else { response.status = exports.FormatResponse.successStatus(); } let statistics = exports.FormatType.table(res.getStatistics(), timezone)?.toKV()?.[0]; if (statistics) { statistics = { totalCost: +statistics.total_time_cost || 0, engineCost: +statistics.engine_time_cost || 0, nodeAffected: +statistics.node_affected || 0, edgeAffected: +statistics.edge_affected || 0, }; } response.statistics = statistics; response.aliases = res.getAliasList().map(i => ({ name: i.getAlias(), type: i.getResultType() })); response.explainPlan = exports.FormatType.explainPlan(res.getExplainPlan()); let items = {}; res.getNodesList()?.forEach((nodeTableData) => { let dataItem = new types_1.DataItem(nodeTableData.getAlias(), ULTIPA.ResultType.RESULT_TYPE_NODE, exports.FormatType.nodeTable(nodeTableData.getNodeTable(), timezone)); items[dataItem.alias] = dataItem; }); res.getEdgesList()?.forEach((edgeTableData) => { let dataItem = new types_1.DataItem(edgeTableData.getAlias(), ULTIPA.ResultType.RESULT_TYPE_EDGE, exports.FormatType.edgeTable(edgeTableData.getEdgeTable(), timezone)); items[dataItem.alias] = dataItem; }); res.getPathsList()?.forEach((pathTableData) => { let dataItem = new types_1.DataItem(pathTableData.getAlias(), ULTIPA.ResultType.RESULT_TYPE_PATH, exports.FormatType.graph(pathTableData.getPathsList(), timezone)); items[dataItem.alias] = dataItem; }); res.getAttrsList()?.forEach((attrTableData) => { let attr = exports.FormatType.attr(attrTableData.getAttr(), timezone); attr.name = attrTableData.getAlias(); attr.resultType = ULTIPA.ResultType.RESULT_TYPE_ATTR; let dataItem = new types_1.DataItem(attrTableData.getAlias(), ULTIPA.ResultType.RESULT_TYPE_ATTR, attr); const existingAliasIndex = response.aliases.findIndex(a => a.name === attrTableData.getAlias()); if (existingAliasIndex !== -1) { response.aliases[existingAliasIndex].type = ULTIPA.ResultType.RESULT_TYPE_ATTR; } else { response.aliases.push({ name: attrTableData.getAlias(), type: ULTIPA.ResultType.RESULT_TYPE_ATTR }); } items[dataItem.alias] = dataItem; }); res.getTablesList()?.forEach((tableData) => { let dataItem = new types_1.DataItem(tableData.getTableName(), ULTIPA.ResultType.RESULT_TYPE_TABLE, exports.FormatType.table(tableData, timezone)); items[dataItem.alias] = dataItem; }); response.items = items; return response; }, status: (statusData, p) => { let status = { code: ULTIPA.ErrorCode.SUCCESS, message: ULTIPA.ErrorCode[ultipa_pb_1.ErrorCode.SUCCESS], }; if (statusData !== undefined) { status.code = statusData.getErrorCode(); status.message = statusData.getMsg(); } if (statusData?.getErrorCode() === ULTIPA.ErrorCode.SUCCESS) { status.message = ULTIPA.ErrorCode[ultipa_pb_1.ErrorCode.SUCCESS]; } return status; }, alias: (resultAlias) => { return resultAlias.map(alias => ({ name: alias.getAlias(), type: alias.getResultType() })); }, keyValue: (keyValuesData) => { if (!keyValuesData || keyValuesData.length == 0) { return {}; } let keyValue = {}; keyValuesData.forEach((kv) => { keyValue[kv.getKey()] = kv.getValue(); }); return keyValue; }, paths: (paths) => { let newPaths = []; paths.forEach(o => { let p = new ULTIPA.Path(); p.nodeUuids = exports.FormatType.uuids(o.getNodeTable()); p.edgeUuids = exports.FormatType.uuids(o.getEdgeTable()); // p.length = p.edges.keys.length newPaths.push(p); }); return newPaths; }, graph: (paths, timezone) => { let graph = new ULTIPA.Graph(); graph.paths = exports.FormatType.paths(paths); graph.nodes ??= new Map(); graph.edges ??= new Map(); paths.forEach(o => { let nodes = exports.FormatType.nodeTable(o.getNodeTable(), timezone); nodes.forEach(n => { graph.nodes.set(n.uuid, n); }); let edges = exports.FormatType.edgeTable(o.getEdgeTable(), timezone); edges.forEach(e => { graph.edges.set(e.uuid, e); }); }); return graph; }, explainPlan: (data) => { if (!data) { return null; } let explainPlan = { planNodes: [] }; let planNodes = []; data.getPlanNodesList().forEach(plan => { let one = { alias: plan.getAlias(), childrenNum: plan.getChildrenNum(), infos: plan.getInfos(), uql: plan.getQueryText(), }; planNodes.push(one); }); explainPlan.planNodes = planNodes; return explainPlan; }, table: (tableData, timezone) => { if (!tableData) { return null; } let tableRows = []; let tableHeaders = []; tableData.getHeadersList().forEach(h => { let header = new types_1.Header(); header.propertyName = h.getPropertyName(); header.propertyType = h.getPropertyType(); tableHeaders.push(header); }); tableData.getTableRowsList().forEach((tableRowData) => { let values = []; tableRowData.getValuesList_asU8().forEach((v, i) => { let header = tableHeaders[i]; values.push(index_1.DESERIALIZE.deserialize(v, header.propertyType, undefined, timezone)); }); tableRows.push(values); }); let t = new ULTIPA.Table(); t.name = tableData.getTableName(); t.headers = tableHeaders; t.rows = tableRows; return t; }, tables: (tablesData, timezone) => { if (!tablesData || tablesData.length == 0) { return []; } let tables = []; tablesData.forEach((tableData) => { tables.push(exports.FormatType.table(tableData, timezone)); }); return tables; }, toEdgeTable: (propertyTypeObject, schemaName, headers, rows, timezone) => { let edgeTable = new ultipa_pb_1.EntityTable(); let handles = [ULITPA_PRIVATE_KEYS.uuid, ULITPA_PRIVATE_KEYS.from, ULITPA_PRIVATE_KEYS.to, ULITPA_PRIVATE_KEYS.from_uuid, ULITPA_PRIVATE_KEYS.to_uuid]; let schemaHeaders = []; let schemaHeader = new ultipa_pb_1.Schema(); schemaHeader.setSchemaName(schemaName); let headerList = []; headers.forEach((h) => { if (handles.includes(h)) { return; } let header = new ultipa_pb_1.Property(); header.setPropertyName(h); let typeCfg = index_1.PropertyUtils.GetPropertyType(propertyTypeObject[h]); header.setPropertyType(typeCfg.type); if (typeCfg.subTypes) { header.setSubTypesList(typeCfg.subTypes); } headerList.push(header); }); schemaHeader.setPropertiesList(headerList); schemaHeaders.push(schemaHeader); edgeTable.setSchemasList(schemaHeaders); let edgeRowsList = []; rows.forEach(rowsObj => { let edgeRow = new ultipa_pb_1.EntityRow(); if (!rowsObj) { edgeRow.setIsNull(true); edgeRowsList.push(edgeRow); return; } edgeRow.setSchemaName(schemaName); let rowValues = rowsObj; let values = []; headers.forEach((h, index) => { if (h === ULITPA_PRIVATE_KEYS.from) { edgeRow.setFromId(rowValues[index]); return; } if (h === ULITPA_PRIVATE_KEYS.to) { edgeRow.setToId(rowValues[index]); return; } if (h === ULITPA_PRIVATE_KEYS.uuid) { let uuid = rowValues[index]; if (uuid) { uuid += ""; } edgeRow.setUuid(uuid); return; } if (h === ULITPA_PRIVATE_KEYS.from_uuid) { edgeRow.setFromUuid(rowValues[index]); return; } if (h === ULITPA_PRIVATE_KEYS.to_uuid) { edgeRow.setToUuid(rowValues[index]); return; } let typeCfg = index_1.PropertyUtils.GetPropertyType(propertyTypeObject[h]); values.push(index_1.SERIALIZE.serialize(rowValues[index], typeCfg.type, typeCfg.subTypes, timezone)); }); edgeRow.setValuesList(values); edgeRowsList.push(edgeRow); }); edgeTable.setEntityRowsList(edgeRowsList); return edgeTable; }, toPropertyTypeObject: (properties) => { let object = {}; properties.forEach(p => { object[p.name] = p.type; }); return object; }, toNodeTable: (propertyTypeObject, schemaName, headers, rows, timezone) => { let nodeTable = new ultipa_pb_1.EntityTable(); let handles = [ULITPA_PRIVATE_KEYS.id, ULITPA_PRIVATE_KEYS.uuid]; let schemaHeaders = []; let schemaHeader = new ultipa_pb_1.Schema(); schemaHeader.setSchemaName(schemaName); let headerList = []; headers.forEach((h) => { if (handles.includes(h)) { return; } let header = new ultipa_pb_1.Property(); header.setPropertyName(h); let typeCfg = index_1.PropertyUtils.GetPropertyType(propertyTypeObject[h]); header.setPropertyType(typeCfg.type); if (typeCfg.subTypes) { header.setSubTypesList(typeCfg.subTypes); } headerList.push(header); }); schemaHeader.setPropertiesList(headerList); schemaHeaders.push(schemaHeader); nodeTable.setSchemasList(schemaHeaders); let nodeRowsList = []; rows.forEach(rowObj => { let nodeRow = new ultipa_pb_1.EntityRow(); if (!rowObj) { nodeRow.setIsNull(true); nodeRowsList.push(nodeRow); return; } nodeRow.setSchemaName(schemaName); let values = []; let rowValues = rowObj; headers.forEach((h, index) => { if (h === ULITPA_PRIVATE_KEYS.id) { nodeRow.setId(rowValues[index]); return; } if (h === ULITPA_PRIVATE_KEYS.uuid) { let uuid = rowValues[index]; if (uuid) { uuid += ""; } nodeRow.setUuid(uuid); return; } let typeCfg = index_1.PropertyUtils.GetPropertyType(propertyTypeObject[h]); values.push(index_1.SERIALIZE.serialize(rowValues[index], typeCfg.type, typeCfg.subTypes, timezone)); }); nodeRow.setValuesList(values); nodeRowsList.push(nodeRow); }); nodeTable.setEntityRowsList(nodeRowsList); return nodeTable; }, toEdgeTableWithSchema: (schema, rows, timezone) => { let table = new ultipa_pb_1.EntityTable(); let handles = [ULITPA_PRIVATE_KEYS.uuid, ULITPA_PRIVATE_KEYS.from, ULITPA_PRIVATE_KEYS.to, ULITPA_PRIVATE_KEYS.from_uuid, ULITPA_PRIVATE_KEYS.to_uuid]; let schemaHeaders = []; let schemaHeader = new ultipa_pb_1.Schema(); schemaHeader.setSchemaName(schema.name); let headerList = []; schema.properties.forEach((o) => { let { name, type, subType } = o; if (handles.includes(name)) { return; } let header = new ultipa_pb_1.Property(); header.setPropertyName(name); header.setPropertyType(type); if (subType) { header.setSubTypesList(subType); } headerList.push(header); }); schemaHeader.setPropertiesList(headerList); schemaHeaders.push(schemaHeader); table.setSchemasList(schemaHeaders); let rowList = []; rows.forEach(rowObj => { let row = new ultipa_pb_1.EntityRow(); if (!rowObj) { row.setIsNull(true); rowList.push(row); return; } row.setSchemaName(schema.name); if (!!rowObj.uuid) { row.setUuid(rowObj.uuid); } if (!!rowObj.from) { row.setFromId(rowObj.from); } if (!!rowObj.to) { row.setToId(rowObj.to); } if (!!rowObj.fromUuid) { row.setFromUuid(rowObj.fromUuid); } if (!!rowObj.toUuid) { row.setToUuid(rowObj.toUuid); } let values = []; let rowValues = rowObj.values || {}; headerList.forEach((o, index) => { let name = o.getPropertyName(); let type = o.getPropertyType(); let subTypes = o.getSubTypesList(); values.push(index_1.SERIALIZE.serialize(rowValues[name], type, subTypes, timezone)); }); row.setValuesList(values); rowList.push(row); }); table.setEntityRowsList(rowList); return table; }, toNodeTableWithSchema: (schema, rows, timezone) => { let nodeTable = new ultipa_pb_1.EntityTable(); let handles = [ULITPA_PRIVATE_KEYS.id, ULITPA_PRIVATE_KEYS.uuid]; let schemaHeaders = []; let schemaHeader = new ultipa_pb_1.Schema(); schemaHeader.setSchemaName(schema.name); let headerList = []; schema.properties.forEach((o) => { let { name, type, subType } = o; if (handles.includes(name)) { return; } let header = new ultipa_pb_1.Property(); header.setPropertyName(name); header.setPropertyType(type); if (subType) { header.setSubTypesList(subType); } headerList.push(header); }); schemaHeader.setPropertiesList(headerList); schemaHeaders.push(schemaHeader); nodeTable.setSchemasList(schemaHeaders); let nodeRowsList = []; rows.forEach(rowObj => { let nodeRow = new ultipa_pb_1.EntityRow(); if (!rowObj) { nodeRow.setIsNull(true); nodeRowsList.push(nodeRow); return; } if (!!rowObj.id) { nodeRow.setId(rowObj.id); } if (!!rowObj.uuid) { nodeRow.setUuid(rowObj.uuid); } nodeRow.setSchemaName(schema.name); let values = []; let rowValues = rowObj.values || {}; headerList.forEach((o, index) => { let name = o.getPropertyName(); let type = o.getPropertyType(); let subTypes = o.getSubTypesList(); values.push(index_1.SERIALIZE.serialize(rowValues[name], type, subTypes, timezone)); }); nodeRow.setValuesList(values); nodeRowsList.push(nodeRow); }); nodeTable.setEntityRowsList(nodeRowsList); return nodeTable; }, uuids: (tableData) => { if (!tableData) { return []; } let rowsData = tableData.getEntityRowsList(); let uuids = []; rowsData.forEach((data) => { uuids.push(data.getUuid()); }); return uuids; }, nodeTable: (nodeTableData, timezone) => { let tables = []; if (!nodeTableData) { return tables; } let headerMap = {}; nodeTableData.getSchemasList().forEach((sh) => { let schema = sh.getSchemaName(); headerMap[schema] = sh.getPropertiesList(); }); let nodeRowsData = nodeTableData.getEntityRowsList(); nodeRowsData.forEach((nodeRowData) => { if (nodeRowData.getIsNull()) { tables.push(null); return; } let schema = nodeRowData.getSchemaName(); let ptys = headerMap[schema]; let values = nodeRowData.getValuesList_asU8(); let _v = _mapValues(values, ptys, timezone); let nodeRow = new ULTIPA.Node(); nodeRow.id = nodeRowData.getId(), nodeRow.uuid = nodeRowData.getUuid(), nodeRow.schema = schema, nodeRow.values = _v, tables.push(nodeRow); }); return tables; }, edgeTable: (edgeTableData, timezone) => { let tables = []; if (!edgeTableData) { return tables; } let headerMap = {}; edgeTableData.getSchemasList().forEach((sh) => { let schema = sh.getSchemaName(); headerMap[schema] = sh.getPropertiesList(); }); edgeTableData.getSchemasList().forEach((sh) => { let schema = sh.getSchemaName(); headerMap[schema] = sh.getPropertiesList(); }); let edgeRowsData = edgeTableData.getEntityRowsList(); edgeRowsData.forEach((rowData) => { if (rowData.getIsNull()) { tables.push(null); return; } let schema = rowData.getSchemaName(); let ptys = headerMap[schema]; let values = rowData.getValuesList_asU8(); let _v = _mapValues(values, ptys, timezone); let edgeRow = new ULTIPA.Edge(); // id: rowData.getId(), edgeRow.from = rowData.getFromId(); edgeRow.to = rowData.getToId(); edgeRow.uuid = rowData.getUuid(); edgeRow.fromUuid = rowData.getFromUuid(); edgeRow.toUuid = rowData.getToUuid(); edgeRow.schema = schema; edgeRow.values = _v; tables.push(edgeRow); }); return tables; }, attr(attr, timezone) { let type = attr.getValueType(); let result = new ULTIPA.Attr(); result.values = []; result.resultType = ultipa_pb_1.ResultType.RESULT_TYPE_ATTR; result.propertyType = type; attr.getValuesList_asU8().forEach((v) => { if (type == ULTIPA.UltipaPropertyType.NULL) { return result.values.push(null); } if (type == ULTIPA.UltipaPropertyType.LIST) { let attrListData = ultipa_pb_1.AttrListData.deserializeBinary(v); if (attrListData.getIsNull()) { return result.values.push(null); } let result_type = attrListData.getType(); switch (result_type) { case ultipa_pb_1.ResultType.RESULT_TYPE_NODE: { let v = { type: result_type, type_desc: ULTIPA.ResultType[result_type], nodes: exports.FormatType.nodeTable(attrListData.getNodes(), timezone) }; result.values.push(v); } break; case ultipa_pb_1.ResultType.RESULT_TYPE_EDGE: { let v = { type: result_type, type_desc: ULTIPA.ResultType[result_type], edges: exports.FormatType.edgeTable(attrListData.getEdges(), timezone) }; result.values.push(v); } break; case ultipa_pb_1.ResultType.RESULT_TYPE_PATH: { let v = { type: result_type, type_desc: ULTIPA.ResultType[result_type], paths: exports.FormatType.paths(attrListData.getPathsList()) }; result.values.push(v); } break; case ultipa_pb_1.ResultType.RESULT_TYPE_ATTR: { { let attrs = []; attrListData.getAttrsList().forEach(o => { let attr = exports.FormatType.attr(o, timezone); if (!attr.values) { attrs.push(attr.values); } else { attrs.push(...attr.values); } }); // let v: ULTIPA.AttrListData = { // type: result_type, // type_desc: ULTIPA.ResultType[result_type], // attrs: attrs, // } if (attrs.length != 0) { } result.values.push(attrs); } break; } } } else { let v1 = index_1.DESERIALIZE.base(v, type, timezone); result.values.push(v1); } }); return result; }, graphPrivileges(graphPrivileges) { let new_graphPrivileges = []; if (graphPrivileges) { graphPrivileges.forEach((graphPrivilege) => { let new_graphPrivilege = { ...graphPrivilege }; Object.keys(new_graphPrivilege).forEach((key) => { new_graphPrivilege["name"] = key; new_graphPrivilege["values"] = new_graphPrivilege[key]; delete new_graphPrivilege[key]; }); new_graphPrivileges.push(new_graphPrivilege); }); } return new_graphPrivileges; }, }; exports.FormatResponse = { tableToArray: (table) => { let res = []; if (table) { try { table.rows.forEach((row) => { let item = {}; row.forEach((value, index) => { let header = table.headers[index]; item[header.propertyName] = value; }); res.push(item); }); } catch (error) { console.log(error); } } return res; }, formatObjects: (objs, keysFormat) => { if (!keysFormat) { return objs; } let newObjs = []; if (objs?.length > 0) { objs.forEach((obj) => { newObjs.push(exports.FormatResponse.formatObject(obj, keysFormat)); }); } return newObjs; }, formatObject: (_obj, keysFormat) => { if (!keysFormat) { return _obj; } let obj = { ..._obj }; if (keysFormat?.jsonKeys) { keysFormat.jsonKeys.forEach((k) => { try { if (obj[k]) { obj[k] = JSON.parse(obj[k]); } } catch (error) { console.log(error, k, obj); } }); } if (keysFormat?.boolKeys) { keysFormat.boolKeys.forEach((k) => { try { if (obj[k]) { obj[k] = obj[k] == "true"; } } catch (error) { console.log(error, k, obj); } }); } if (keysFormat?.keyReplace) { Object.keys(keysFormat.keyReplace).forEach((k) => { if (obj[k]) { obj[keysFormat.keyReplace[k]] = obj[k]; delete obj[k]; } }); } if (keysFormat?.dataFormat) { obj = keysFormat.dataFormat(obj); } return obj; }, successStatus: () => { return { code: ULTIPA.ErrorCode.SUCCESS, message: ULTIPA.ErrorCode[ULTIPA.ErrorCode.SUCCESS] // message: "ok", }; }, statisticsSum: (one, sumOne) => { return { totalCost: (one?.totalCost || 0) + (sumOne?.totalCost || 0), engineCost: (one?.engineCost || 0) + (sumOne?.engineCost || 0), nodeAffected: (one?.nodeAffected || 0) + (sumOne?.nodeAffected || 0), edgeAffected: (one?.edgeAffected || 0) + (sumOne?.edgeAffected || 0), }; }, unknownError: (err, res) => { res.status = { code: ULTIPA.ErrorCode.UNKNOWN_ERROR, message: err.message || err.detail, }; return res; }, catchUltipaUqlError: (error) => { let response = new ULTIPA.Response(); response = exports.FormatResponse.unknownError(error, response); return response; }, }; //# sourceMappingURL=format.js.map