Skip to content
This repository has been archived by the owner on Jul 14, 2023. It is now read-only.

Commit

Permalink
fix: return timestamps, not now() for createdat and modifiedat columns (
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianesch committed Mar 19, 2022
1 parent 3ab1496 commit 8a2c05b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
24 changes: 24 additions & 0 deletions __tests__/lib/pg/ql.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,5 +157,29 @@ describe('QL to PostgreSQL', () => {
const beer = await cds.run(SELECT.one(Beers).where({ name: 'Test' }))
expect(beer).toHaveProperty('name', 'Test')
})

// see https://cap.cloud.sap/docs/node.js/databases#insertresult-beta and https://answers.sap.com/questions/13569793/api-of-insert-query-results-for-cap-nodejs.html
test('-> with InsertResult Beta API', async () => {
const { Beers } = cds.entities('csw')

const entries = [
{ name: 'Beer1', abv: 1.0, ibu: 1, brewery_ID: '0465e9ca-6255-4f5c-b8ba-7439531f8d28' },
{ name: 'Beer2', abv: 2.0, ibu: 2, brewery_ID: '0465e9ca-6255-4f5c-b8ba-7439531f8d28' },
{ name: 'Beer3', abv: 3.0, ibu: 3, brewery_ID: '0465e9ca-6255-4f5c-b8ba-7439531f8d28' }
]

const uuidRegex = /[\d|a-f]{8}-[\d|a-f]{4}-[\d|a-f]{4}-[\d|a-f]{4}-[\d|a-f]{12}/
const timestampRegex = /[\d]{4}-[\d]{2}-[\d]{2}T[\d]{2}:[\d]{2}:[\d]{2}.[\d]{3}Z/

const insertResult = await cds.run(INSERT.into(Beers).entries(entries))
expect(insertResult.affectedRows).toStrictEqual(3)
expect(insertResult == 3).toStrictEqual(true)
expect(insertResult.valueOf()).toStrictEqual(insertResult.affectedRows)
const beers = [...entries]
expect(beers.length).toStrictEqual(3)
expect(beers[0].ID).toMatch(uuidRegex)
expect(beers[0].createdAt).toMatch(timestampRegex)
expect(beers[0].modifiedAt).toMatch(timestampRegex)
})
})
})
29 changes: 15 additions & 14 deletions lib/pg/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ const DEBUG = cds.debug('cds-pg')
* @param {*} txTimestamp
* @return {import('pg').QueryArrayResult}
*/
const executeGenericCQN = (model, dbc, query, user /*, locale, txTimestamp */) => {
const { sql, values = [] } = _cqnToSQL(model, query, user)
const executeGenericCQN = (model, dbc, query, user, locale, txTimestamp) => {
const { sql, values = [] } = _cqnToSQL(model, query, user, locale, txTimestamp)
const isOne = query.SELECT && query.SELECT.one
const postPropertyMapper = getPostProcessMapper(PG_TYPE_CONVERSION_MAP, model, query)
return _executeSQLReturningRows(dbc, sql, values, isOne, postPropertyMapper)
Expand All @@ -41,11 +41,11 @@ const executeGenericCQN = (model, dbc, query, user /*, locale, txTimestamp */) =
* @param {*} txTimestamp
* @return {import('pg').QueryArrayResult}
*/
const executeSelectCQN = (model, dbc, query, user /*, locale, txTimestamp*/) => {
const executeSelectCQN = (model, dbc, query, user, locale, txTimestamp) => {
if (hasExpand(query)) {
return processExpand(dbc, query, model, user)
return processExpand(dbc, query, model, user, locale, txTimestamp)
} else {
const { sql, values = [] } = _cqnToSQL(model, query, user)
const { sql, values = [] } = _cqnToSQL(model, query, user, locale, txTimestamp)
const isOne = query.SELECT && query.SELECT.one
const postPropertyMapper = getPostProcessMapper(PG_TYPE_CONVERSION_MAP, model, query)
return _executeSQLReturningRows(dbc, sql, values, isOne, postPropertyMapper)
Expand All @@ -65,8 +65,8 @@ const executeSelectCQN = (model, dbc, query, user /*, locale, txTimestamp*/) =>
* @param {*} txTimestamp
* @return {Array}
*/
const executeInsertCQN = async (model, dbc, cqn, user) => {
const { sql, values = [] } = _cqnToSQL(model, cqn, user)
const executeInsertCQN = async (model, dbc, cqn, user, locale, txTimestamp) => {
const { sql, values = [] } = _cqnToSQL(model, cqn, user, locale, txTimestamp)
const postPropertyMapper = getPostProcessMapper(PG_TYPE_CONVERSION_MAP, model, cqn)
const resultPromises = []

Expand Down Expand Up @@ -116,13 +116,13 @@ async function executePlainSQL(dbc, rawSql, rawValues) {
* @param {*} user
* @return {import('pg').QueryArrayResult} the
*/
const processExpand = (dbc, cqn, model, user) => {
const processExpand = (dbc, cqn, model, user, locale, txTimestamp) => {
let queries = []
const expandQueries = createJoinCQNFromExpanded(cqn, model, true)
for (const cqn of expandQueries.queries) {
// REVISIT
// Why is the post processing in expand different?
const { sql, values } = _cqnToSQL(model, cqn, user, true)
const { sql, values } = _cqnToSQL(model, cqn, user, locale, txTimestamp, true)
const postPropertyMapper = getPostProcessMapper(PG_TYPE_CONVERSION_MAP, model, cqn)

queries.push(_executeSQLReturningRows(dbc, sql, values, false, postPropertyMapper))
Expand All @@ -140,7 +140,7 @@ const processExpand = (dbc, cqn, model, user) => {
* @param {Boolean} isExpand
* @return {Object} the query object containing sql and values
*/
function _cqnToSQL(model, cqn, user, isExpand = false) {
function _cqnToSQL(model, cqn, user, locale, txTimestamp, isExpand = false) {
return _replacePlaceholders(
sqlFactory(
cqn,
Expand All @@ -152,8 +152,9 @@ function _cqnToSQL(model, cqn, user, isExpand = false) {
FunctionBuilder: PGFunctionBuilder
},
isExpand, // Passed to inform the select builder that we are dealing with an expand call
now: 'NOW ()',
user
now: txTimestamp || { sql: 'NOW ()' },
user,
locale
},
model,
isExpand
Expand Down Expand Up @@ -221,8 +222,8 @@ async function _executeSQLReturningRows(dbc, sql, values, isOne, postMapper, pro
return postProcess(result, postMapper, propertyMapper, objStructMapper)
}

const executeUpdateCQN = async (model, dbc, cqn) => {
const result = await executeGenericCQN(model, dbc, cqn)
const executeUpdateCQN = async (model, dbc, cqn, user, locale, txTimestamp) => {
const result = await executeGenericCQN(model, dbc, cqn, user, locale, txTimestamp)
return Array.isArray(result) ? result.length : result
}

Expand Down

0 comments on commit 8a2c05b

Please sign in to comment.