Fixed id generation for non-draft requests
This commit is contained in:
@@ -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.
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user