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

Commit

Permalink
feat: @sap/cds 6.5.0 compatibility (#383)
Browse files Browse the repository at this point in the history
* test: draft referencing other draft Odata call

* fix: wrong alias when subquery inside "SELECT"

* fix: less disruptive changes in test model

* fix: removed Suppliers annotations

* test: draft referencing other draft Odata call

* fix: wrong alias when subquery inside "SELECT"

* fix: less disruptive changes in test model

* fix: removed Suppliers annotations

* fix: restore sample env's

* fix: cds 6.5.0 support

* fix: local implementation of "_ensureModel"

* fix: code indent , trailingComma & prettier setup

* fix: demands _ensureModel existency + indent

* fix: pull request review changes

---------

Co-authored-by: ryegros <[email protected]>
  • Loading branch information
ryegrosT8 and ryegros committed Feb 13, 2023
1 parent 38a7370 commit 28b2c42
Show file tree
Hide file tree
Showing 10 changed files with 14,092 additions and 2,890 deletions.
4 changes: 3 additions & 1 deletion .prettierrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ module.exports = {
printWidth: 120,
arrowParens: 'always',
trailingComma: 'none',
singleQuote: true
singleQuote: true,
tabWidth: 2,
proseWrap: 'never'
}
2 changes: 1 addition & 1 deletion __tests__/lib/pg/draft.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const cds = require('@sap/cds')
const deploy = require('@sap/cds/lib/deploy')
const deploy = require('@sap/cds/lib/dbs/cds-deploy')

// mock (package|.cdsrc).json entries
cds.env.requires.db = { kind: 'postgres' }
Expand Down
2 changes: 1 addition & 1 deletion __tests__/lib/pg/ql.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const cds = require('@sap/cds')
const deploy = require('@sap/cds/lib/deploy')
const deploy = require('@sap/cds/lib/dbs/cds-deploy')

// mock (package|.cdsrc).json entries
cds.env.requires.db = { kind: 'postgres' }
Expand Down
2 changes: 1 addition & 1 deletion __tests__/lib/pg/service-admin.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const cds = require('@sap/cds')
const deploy = require('@sap/cds/lib/deploy')
const deploy = require('@sap/cds/lib/dbs/cds-deploy')

cds.env.requires.db = { kind: 'postgres' }
cds.env.requires.postgres = {
Expand Down
8 changes: 4 additions & 4 deletions __tests__/lib/pg/service-types.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const cds = require('@sap/cds')
const deploy = require('@sap/cds/lib/deploy')
const deploy = require('@sap/cds/lib/dbs/cds-deploy')

// mock (package|.cdsrc).json entries
cds.env.requires.db = { kind: 'postgres' }
Expand Down Expand Up @@ -102,13 +102,13 @@ describe.each(suiteEnvironments)(
})

test(' -> Timestamp', async () => {
const value = '2012-12-03T07:16:23.574Z';
const value = '2012-12-03T07:16:23.574Z'
const response = await request.post('/beershop/TypeChecks').send({
type_Timestamp: value,
})
expect(response.status).toStrictEqual(201)
const verify = await request.get(`/beershop/TypeChecks(${response.body.ID})`).send()
expect(verify.body.type_Timestamp).toStrictEqual(value);
expect(verify.body.type_Timestamp).toStrictEqual(value)
})

test(' -> String', async () => {
Expand Down Expand Up @@ -140,5 +140,5 @@ describe.each(suiteEnvironments)(
expect(response.status).toStrictEqual(201)
})
})
}
},
)
2 changes: 1 addition & 1 deletion __tests__/lib/pg/service.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const cds = require('@sap/cds')
const deploy = require('@sap/cds/lib/deploy')
const deploy = require('@sap/cds/lib/dbs/cds-deploy')
// const path = require('path')

// mock (package|.cds'rc).json entries
Expand Down
14 changes: 7 additions & 7 deletions __tests__/lib/pg/timestamp.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const cds = require('@sap/cds')
const deploy = require('@sap/cds/lib/deploy')
const deploy = require('@sap/cds/lib/dbs/cds-deploy')

// mock (package|.cdsrc).json entries
cds.env.requires.db = { kind: 'postgres' }
Expand Down Expand Up @@ -42,13 +42,13 @@ describe.each(suiteEnvironments)(
})

describe('Timestamp TEST', () => {

test(' -> Check modifiedAt', async () => {
//Set Different TimeZone
await cds.run(`alter user postgres set timezone = 'EST'`, [])//UTC,EST
await cds.run(`alter user postgres set timezone = 'EST'`, []) //UTC,EST
const beforeTimestamp = new Date()
beforeTimestamp.setMilliseconds(0);
await request.put('/beershop/Beers/9e1704e3-6fd0-4a5d-bfb1-13ac47f7976b')
beforeTimestamp.setMilliseconds(0)
await request
.put('/beershop/Beers/9e1704e3-6fd0-4a5d-bfb1-13ac47f7976b')
.send({
name: 'Changed name',
ibu: 10,
Expand All @@ -58,8 +58,8 @@ describe.each(suiteEnvironments)(
const response = await request.get('/beershop/Beers/9e1704e3-6fd0-4a5d-bfb1-13ac47f7976b')
const afterTimestamp = new Date()
const modifiedAt = new Date(response.body.modifiedAt)
expect((beforeTimestamp <= modifiedAt) && (modifiedAt <= afterTimestamp)).toBe(true)
expect(beforeTimestamp <= modifiedAt && modifiedAt <= afterTimestamp).toBe(true)
})
})
}
},
)
30 changes: 25 additions & 5 deletions lib/pg/Service.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module.exports = class PostgresDatabase extends cds.DatabaseService {
}
this.options.credentials.ssl = {
rejectUnauthorized: false,
ca: this.options.credentials.sslrootcert,
ca: this.options.credentials.sslrootcert
}
}
this._pool = new Pool(this.options.credentials)
Expand Down Expand Up @@ -85,13 +85,13 @@ module.exports = class PostgresDatabase extends cds.DatabaseService {
* before
*/
// ensures the correct model is present (e.g., tenant extensions)
this._ensureModel && this.before('*', this._ensureModel)
this.before('*', this._ensureModel)
// VIRTUAL, MANAGED, Keys and null checks
this.before(['CREATE', 'UPDATE'], '*', this._input)
// "flattens" the query
// and "redirects" modification statements (CUD) from view to actual table
this.before(['CREATE', 'READ', 'UPDATE', 'DELETE'], '*', rewrite)
this.before('READ', '*', this._virtual)
this.before('READ', '*', this._virtual.bind(this))

/*
* on
Expand Down Expand Up @@ -121,6 +121,18 @@ module.exports = class PostgresDatabase extends cds.DatabaseService {
})
}

/**
* Retrieve model definition from ApplicationService
*/
_ensureModel() {
for (const service of cds.services) {
if (service instanceof cds.ApplicationService) {
this.model = service.model
break
}
}
}

/**
* assign request metadata
* @param {Object} req currently served express http request, enhanced by cds
Expand Down Expand Up @@ -180,13 +192,13 @@ module.exports = class PostgresDatabase extends cds.DatabaseService {
if (options.dry) {
const log = console.log // eslint-disable-line no-console
for (let {
DROP: { view },
DROP: { view }
} of dropViews) {
log('DROP VIEW IF EXISTS ' + view + ';')
}
log()
for (let {
DROP: { entity },
DROP: { entity }
} of dropTables) {
log('DROP TABLE IF EXISTS ' + entity + ';')
}
Expand All @@ -203,4 +215,12 @@ module.exports = class PostgresDatabase extends cds.DatabaseService {

return true
}

// Required for CDS 6.x
getDbUrl() {
const credentials = this.options.credentials || this.options || {}
const dbUrl = `postgres:https://${credentials.host}//${credentials.database}`

return dbUrl
}
}
Loading

0 comments on commit 28b2c42

Please sign in to comment.