@qrvey/data-persistence
Advanced tools
Comparing version 0.3.6-beta to 0.3.6-beta.1
@@ -340,3 +340,3 @@ "use strict"; | ||
Object.keys(jsonSetExpressionGroup).forEach((groupIndex) => { | ||
const jsonSetExpression = this.buildJSONBExpression(jsonSetExpressionGroup[groupIndex]); | ||
const jsonSetExpression = this.buildJSONBExpression(jsonSetExpressionGroup[groupIndex], 'jsonb_set'); | ||
updateClauses.push(`${groupIndex} = ${jsonSetExpression}`); | ||
@@ -354,5 +354,11 @@ }); | ||
query += this.buildFilterClause(filters); | ||
return this.runQuery(query, [], 'SERIALIZABLE'); | ||
return this.runQuery(query); | ||
} | ||
buildJSONBExpression(jsonSetExpressions) { | ||
/** | ||
* @description Builds a jsonb expression like jsonb_insert, or jsonb_set | ||
* @param jsonSetExpressions | ||
* @param functionName | ||
* @returns | ||
*/ | ||
buildJSONBExpression(jsonSetExpressions, functionName) { | ||
let jsonSetStringExpr = ''; | ||
@@ -362,6 +368,6 @@ jsonSetExpressions.forEach((expression, index) => { | ||
if (index === 0) { | ||
jsonSetStringExpr = `jsonb_set(${columnName},${jsonExpr})`; | ||
jsonSetStringExpr = `${functionName}(${columnName},${jsonExpr})`; | ||
} | ||
else { | ||
jsonSetStringExpr = `jsonb_set(${jsonSetStringExpr},${jsonExpr})`; | ||
jsonSetStringExpr = `${functionName}(${jsonSetStringExpr},${jsonExpr})`; | ||
} | ||
@@ -415,3 +421,3 @@ }); | ||
path: attributesFromGroup[0].trim(), | ||
value: attributesFromGroup[1].trim(), | ||
value: attributesFromGroup.slice(1).join(',').trim(), | ||
functionExpr: '', | ||
@@ -424,3 +430,4 @@ }; | ||
else { | ||
attributes['functionExpr'] = this.buildJsonbExpr(attributes); | ||
attributes['functionExpr'] = | ||
this.buildJsonbInsertExpr(attributes); | ||
} | ||
@@ -436,11 +443,35 @@ return attributes; | ||
} | ||
buildJsonbExpr(params) { | ||
buildJsonbInsertExpr(params) { | ||
const arrayPath = params.path.split('.'); | ||
const columnName = arrayPath.shift(); | ||
if (arrayPath.length > 0) { | ||
return `jsonb_insert("${columnName}", '{${arrayPath.join(',')},0}', ${params.value})`; | ||
const jsonbInsertExpressions = this.getExpressionsByDefinitionForJSONBInsert(columnName, params.value); | ||
const expressions = this.buildJSONBExpression(jsonbInsertExpressions, 'jsonb_insert'); | ||
return expressions; | ||
} | ||
getExpressionsByDefinitionForJSONBInsert(columnName, value) { | ||
const jsonbInsertExpressions = []; | ||
try { | ||
const parsedValue = JSON.parse(value); | ||
if (Array.isArray(parsedValue)) { | ||
parsedValue.forEach((arrayValue) => { | ||
jsonbInsertExpressions.push({ | ||
jsonExpr: `'{0}','${this.serializeJSONValue(arrayValue)}'`, | ||
columnName: `"${columnName}"`, | ||
}); | ||
}); | ||
} | ||
else { | ||
jsonbInsertExpressions.push({ | ||
jsonExpr: `'{0}','${this.serializeJSONValue(parsedValue)}'`, | ||
columnName: `"${columnName}"`, | ||
}); | ||
} | ||
} | ||
else { | ||
return `jsonb_insert("${columnName}",'{0}', ${params.value})`; | ||
catch (error) { | ||
jsonbInsertExpressions.push({ | ||
jsonExpr: `'{0}','${value}'`, | ||
columnName: `"${columnName}"`, | ||
}); | ||
} | ||
return jsonbInsertExpressions; | ||
} | ||
@@ -461,6 +492,6 @@ getInsertExprFromJsonbDef(queryValue, columnType) { | ||
const column = columns[action.path]; | ||
action.value = | ||
this.replaceExpressionAttributeValuesForDynamoFunctions(action.value, options); | ||
action.value = this.getInsertExprFromJsonbDef(action.value, column.type); | ||
action.dynamoFuncName = 'list_append'; | ||
action.value = | ||
this.replaceExpressionAttributeValuesForDynamoFunctions(action.value, options); | ||
} | ||
@@ -493,3 +524,3 @@ else { | ||
const valueSerialized = this.serializeJSONValue(expressionAttributeValues[exprAttribute]); | ||
value = value.replace(exprAttribute, `'${valueSerialized}'`); | ||
value = value.replace(exprAttribute, `${valueSerialized}`); | ||
}); | ||
@@ -496,0 +527,0 @@ } |
@@ -12,3 +12,3 @@ "use strict"; | ||
} | ||
async runQuery(queryText, values, isolationLevel = 'READ COMMITTED') { | ||
async runQuery(queryText, values) { | ||
const client = await (this.poolClient | ||
@@ -18,10 +18,6 @@ ? this.poolClient | ||
try { | ||
await client.query('BEGIN'); | ||
await client.query(`SET TRANSACTION ISOLATION LEVEL ${isolationLevel}`); | ||
const result = await client.query(queryText, values); | ||
await client.query(`COMMIT`); | ||
return result; | ||
} | ||
catch (error) { | ||
await client.query('ROLLBACK'); | ||
console.log('[Postgresql-Client] Query Execution Failed:', error); | ||
@@ -28,0 +24,0 @@ throw error; |
@@ -5,3 +5,3 @@ { | ||
"main": "dist/cjs/index.js", | ||
"version": "0.3.6-beta", | ||
"version": "0.3.6-beta.1", | ||
"license": "MIT", | ||
@@ -31,3 +31,3 @@ "exports": { | ||
"publish-package-dev": "yarn prepare-publish && yarn publish-codeartifact", | ||
"publish-package": "yarn prepare-publish && npm publish" | ||
"publish-package": "yarn prepare-publish && npm publish --tag beta" | ||
}, | ||
@@ -34,0 +34,0 @@ "peerDependencies": { |
Sorry, the diff of this file is not supported yet
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
359787
3990