Skip to content

Commit

Permalink
Fixed id generation for non-draft requests
Browse files Browse the repository at this point in the history
  • Loading branch information
danjoa committed Jul 11, 2024
1 parent ea79264 commit cae0d36
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions bookshop/srv/admin-service.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
const cds = require('@sap/cds')

module.exports = class AdminService extends cds.ApplicationService { init(){
this.before ('NEW','Authors', genid)
this.before ('NEW','Books', genid)
this.before (['NEW','CREATE'],'Authors', genid)
this.before (['NEW','CREATE'],'Books', genid)
return super.init()
}}

/** Generate primary keys for target entity in request */
async function genid (req) {
if (req.data.ID) return
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.
}

0 comments on commit cae0d36

Please sign in to comment.