@qrvey/data-persistence
Advanced tools
Comparing version 0.3.3 to 0.3.4
@@ -286,10 +286,16 @@ "use strict"; | ||
const add = actions.ADD || []; | ||
const setValues = this.replacePathAndValueByAttributeNames(set, options, constants_1.DYNAMO_DB_UPDATE_ACTIONS.SET); | ||
const addValues = this.replacePathAndValueByAttributeNames(add, options, constants_1.DYNAMO_DB_UPDATE_ACTIONS.ADD); | ||
const columns = this.crudSchema.columns; | ||
const setValues = this.replacePathAndValueByAttributeNames(set, options, columns, constants_1.DYNAMO_DB_UPDATE_ACTIONS.SET); | ||
const addValues = this.replacePathAndValueByAttributeNames(add, options, columns, constants_1.DYNAMO_DB_UPDATE_ACTIONS.ADD); | ||
const setValuesAndAddValues = setValues.concat(addValues); | ||
const updateClauses = []; | ||
const jsonSetExpressionGroup = {}; | ||
const queryFunctions = []; | ||
setValuesAndAddValues.forEach((expression) => { | ||
const { path, value, createNewColumn, actionName } = expression; | ||
if (path.includes('.')) { | ||
const { path, value, createNewColumn, actionName, dynamoFuncName } = expression; | ||
//Dynamo functions replacement | ||
if (dynamoFuncName) { | ||
queryFunctions.push({ path, value, dynamoFuncName }); | ||
} | ||
if (path.includes('.') && !dynamoFuncName) { | ||
const jsonExpr = this.getJSONBSetExpressionByAction(path, value, { | ||
@@ -307,3 +313,3 @@ createNewColumn, | ||
} | ||
else { | ||
else if (!dynamoFuncName) { | ||
let expValue; | ||
@@ -316,4 +322,6 @@ const column = this.crudSchema.columns[path]; | ||
case 'object': | ||
const valueSerialized = `${JSON.stringify(value).replace(/'/g, "''")}`; | ||
expValue = `'${valueSerialized}'::jsonb`; | ||
{ | ||
const valueSerialized = this.serializeJSONValue(value); | ||
expValue = `'${valueSerialized}'::jsonb`; | ||
} | ||
break; | ||
@@ -339,2 +347,8 @@ case 'array': | ||
} | ||
//This is for dynamo functions transformed to postgresql functions | ||
if (queryFunctions.length > 0) { | ||
queryFunctions.forEach((queryFunction) => { | ||
updateClauses.push(`${pg_format_1.default.ident(queryFunction.path)} = ${queryFunction.value}`); | ||
}); | ||
} | ||
query += ` ${updateClauses.join(', ')}`; | ||
@@ -358,2 +372,13 @@ query += ' WHERE '; | ||
} | ||
/** | ||
* @description Serializes a JSON value | ||
* @param value | ||
* @returns | ||
*/ | ||
serializeJSONValue(value) { | ||
const valueSerialized = typeof value == 'object' | ||
? `${JSON.stringify(value).replace(/'/g, "''")}` | ||
: value; | ||
return valueSerialized; | ||
} | ||
getJSONBSetExpressionByAction(path, value, options) { | ||
@@ -365,3 +390,3 @@ path = path.replace(/\[(\d+)\]/g, '.$1'); | ||
const pathSerialized = pathSplitted.slice(1).join(','); | ||
const valueSerialized = `'${JSON.stringify(value).replace(/'/g, "''")}'`; | ||
const valueSerialized = this.serializeJSONValue(value); | ||
if (actionName == constants_1.DYNAMO_DB_UPDATE_ACTIONS.ADD) { | ||
@@ -381,6 +406,64 @@ if (typeof value != 'string' && !isNaN(value)) { | ||
} | ||
replacePathAndValueByAttributeNames(actions, options, actionName) { | ||
getListAppendDefFromValue(queryValue, columnType) { | ||
const regexListAppend = /list_append\(([^)]+)\)/gm; | ||
const listAppendString = 'list_append('; | ||
const matchList = queryValue.match(regexListAppend) || []; | ||
const groupResult = matchList[0]; | ||
if (groupResult) { | ||
const attributesFromGroup = groupResult | ||
.slice(listAppendString.length, -1) | ||
.split(','); | ||
const attributes = { | ||
originalString: groupResult, | ||
path: attributesFromGroup[0].trim(), | ||
value: attributesFromGroup[1].trim(), | ||
functionExpr: '', | ||
}; | ||
if (columnType == 'array') { | ||
attributes['functionExpr'] = | ||
this.buildArrayAppendExpr(attributes); | ||
} | ||
else { | ||
attributes['functionExpr'] = this.buildJsonbExpr(attributes); | ||
} | ||
return attributes; | ||
} | ||
return null; | ||
} | ||
buildArrayAppendExpr(params) { | ||
const arrayPath = params.path.split('.'); | ||
const columnName = arrayPath.shift(); | ||
return `ARRAY_APPEND("${columnName}",${params.value})`; | ||
} | ||
buildJsonbExpr(params) { | ||
const arrayPath = params.path.split('.'); | ||
const columnName = arrayPath.shift(); | ||
if (arrayPath.length > 0) { | ||
return `jsonb_insert("${columnName}", '{${arrayPath.join(',')},0}', ${params.value})`; | ||
} | ||
else { | ||
return `jsonb_insert("${columnName}",'{0}', ${params.value})`; | ||
} | ||
} | ||
getInsertExprFromJsonbDef(queryValue, columnType) { | ||
const listAppendParams = this.getListAppendDefFromValue(queryValue, columnType); | ||
if (listAppendParams != null) { | ||
queryValue = queryValue.replace(listAppendParams.originalString, listAppendParams.functionExpr); | ||
} | ||
return queryValue; | ||
} | ||
replacePathAndValueByAttributeNames(actions, options, columns, actionName) { | ||
return actions.map((action) => { | ||
action.path = this.replaceExpressionAttributeNames(action.path, options); | ||
action.value = this.replaceExpressionAttributeValues(action.value, options); | ||
if (action.value.includes('list_append')) { | ||
action.path = action.path.split('.')[0]; | ||
const column = columns[action.path]; | ||
action.value = this.getInsertExprFromJsonbDef(action.value, column.type); | ||
action.dynamoFuncName = 'list_append'; | ||
action.value = | ||
this.replaceExpressionAttributeValuesForDynamoFunctions(action.value, options); | ||
} | ||
else { | ||
action.value = this.replaceExpressionAttributeValues(action.value, options); | ||
} | ||
action.actionName = actionName; | ||
@@ -391,8 +474,33 @@ action.createNewColumn = true; | ||
} | ||
replaceExpressionAttributeValuesForDynamoFunctions(value, options) { | ||
const { expressionAttributeNames, expressionAttributeValues } = options; | ||
const exprAttributeNamesKeys = expressionAttributeNames | ||
? Object.keys(expressionAttributeNames) | ||
: []; | ||
const exprAttributeValuesKeys = expressionAttributeValues | ||
? Object.keys(expressionAttributeValues) | ||
: []; | ||
//Set for attribute names | ||
if (exprAttributeNamesKeys.length > 0) { | ||
exprAttributeNamesKeys.forEach((exprAttribute) => { | ||
value = value.replace(exprAttribute, expressionAttributeNames[exprAttribute]); | ||
}); | ||
} | ||
//Set for attribute values | ||
if (exprAttributeValuesKeys.length > 0) { | ||
exprAttributeValuesKeys.forEach((exprAttribute) => { | ||
const valueSerialized = this.serializeJSONValue(expressionAttributeValues[exprAttribute]); | ||
value = value.replace(exprAttribute, `'${valueSerialized}'`); | ||
}); | ||
} | ||
return value; | ||
} | ||
replaceExpressionAttributeNames(path, options) { | ||
const { expressionAttributeNames } = options; | ||
Object.keys(expressionAttributeNames).forEach((attributeName) => { | ||
const attributeNameValue = expressionAttributeNames[attributeName]; | ||
path = path.replace(attributeName, attributeNameValue); | ||
}); | ||
if (expressionAttributeNames) { | ||
Object.keys(expressionAttributeNames).forEach((attributeName) => { | ||
const attributeNameValue = expressionAttributeNames[attributeName]; | ||
path = path.replace(attributeName, attributeNameValue); | ||
}); | ||
} | ||
return path; | ||
@@ -399,0 +507,0 @@ } |
@@ -5,3 +5,3 @@ { | ||
"main": "dist/cjs/index.js", | ||
"version": "0.3.3", | ||
"version": "0.3.4", | ||
"license": "MIT", | ||
@@ -8,0 +8,0 @@ "exports": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
354173
3919