Fixed id generation for non-draft requests

This commit is contained in:
Daniel Hutzel
2024-07-11 11:35:40 +02:00
parent ea79264e9d
commit cae0d36206

View File

@@ -1,13 +1,14 @@
const cds = require('@sap/cds') const cds = require('@sap/cds')
module.exports = class AdminService extends cds.ApplicationService { init(){ module.exports = class AdminService extends cds.ApplicationService { init(){
this.before ('NEW','Authors', genid) this.before (['NEW','CREATE'],'Authors', genid)
this.before ('NEW','Books', genid) this.before (['NEW','CREATE'],'Books', genid)
return super.init() return super.init()
}} }}
/** Generate primary keys for target entity in request */ /** Generate primary keys for target entity in request */
async function genid (req) { async function genid (req) {
if (req.data.ID) return
const {id} = await SELECT.one.from(req.target).columns('max(ID) as id') const {id} = await SELECT.one.from(req.target).columns('max(ID) as id')
req.data.ID = id - id % 100 + 100 + 1 req.data.ID = id + 4 // Note: that is not safe! ok for this sample only.
} }