Play with db constraints

This commit is contained in:
Weinstock
2021-04-14 19:38:38 +02:00
parent 2536f36596
commit a5acea58f2
16 changed files with 235 additions and 219 deletions

View File

@@ -1,12 +1,15 @@
// sqlite: FK constraint checks need to be enabled by application at runtime
// for each db connection
const cds = require('@sap/cds')
module.exports = cds.service.impl (async function(){
module.exports = cds.service.impl (function(){
this.before ('NEW','Authors', genid)
this.before ('NEW','Books', genid)
const db = await cds.connect.to('db')
if (db.kind === 'sqlite')
{
db.before('BEGIN', function (req) {
//console.log('--- PRAGMA foreign_keys = ON ---')
return this.dbc.run('PRAGMA foreign_keys = ON;')
})
}
})
/** Generate primary keys for target entity in request */
async function genid (req) {
const {ID} = await cds.tx(req).run (SELECT.one.from(req.target).columns('max(ID) as ID'))
req.data.ID = ID - ID % 100 + 100 + 1
}