-
Notifications
You must be signed in to change notification settings - Fork 489
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed id generation for non-draft requests
- Loading branch information
Showing
1 changed file
with
4 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
} |