feat: PoC for dynamic constraints using standard cds views
This commit is contained in:
26
bookshop/server.js
Normal file
26
bookshop/server.js
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
//
|
||||||
|
// Quick and dirty implementation for cds.validate() using db-level constraints
|
||||||
|
//
|
||||||
|
|
||||||
|
const cds = require('@sap/cds')
|
||||||
|
cds.on('served', ()=> {
|
||||||
|
|
||||||
|
const $ = cds.validate; cds.validate = async function (entity, key, ...columns) {
|
||||||
|
if (!entity.is_entity) return $(...arguments)
|
||||||
|
if (entity.constraints) entity = entity.constraints
|
||||||
|
if (key?.results) key = key.results[0].lastInsertRowid
|
||||||
|
const checks = await SELECT.one.from (entity, key, columns.length && columns)
|
||||||
|
const failed = {}; for (let c in checks) {
|
||||||
|
if (c in entity.keys) continue
|
||||||
|
if (c[0] == '_') continue
|
||||||
|
if (checks[c]) failed[c] = checks[c]
|
||||||
|
}
|
||||||
|
if (Object.keys(failed).length) throw cds.error `Invalid input: ${failed}`
|
||||||
|
}
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
Object.defineProperties (cds.entity.prototype, {
|
||||||
|
constraints: { get() { return cds.model.definitions[this.name+'.constraints'] }},
|
||||||
|
fields: { get() { return cds.model.definitions[this.name+'.field.control'] }},
|
||||||
|
})
|
||||||
10
bookshop/srv/x-field-control.cds
Normal file
10
bookshop/srv/x-field-control.cds
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
namespace sap.capire.bookshop;
|
||||||
|
using from '../db/schema';
|
||||||
|
|
||||||
|
view Books.field.control as select from Books { ID,
|
||||||
|
genre.name == 'Drama' ? 'readonly' :
|
||||||
|
null as price
|
||||||
|
}
|
||||||
|
extend Books with {
|
||||||
|
fc : Association to Books.field.control on fc.ID = $self.ID
|
||||||
|
}
|
||||||
50
bookshop/srv/x-validation.cds
Normal file
50
bookshop/srv/x-validation.cds
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
namespace sap.capire.bookshop;
|
||||||
|
using from '../db/schema';
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validation constraints for Books
|
||||||
|
*/
|
||||||
|
view Books.constraints as select from Books { ID,
|
||||||
|
|
||||||
|
// two-step mandatory check
|
||||||
|
title = null ? 'is missing' : trim(title)='' ? 'must not be empty' :
|
||||||
|
null as title,
|
||||||
|
|
||||||
|
// range check
|
||||||
|
stock < 0 ? 'must not be negative' :
|
||||||
|
null as stock,
|
||||||
|
|
||||||
|
// range check
|
||||||
|
price < 0 ? 'must not be negative' :
|
||||||
|
null as price,
|
||||||
|
|
||||||
|
// assert target check
|
||||||
|
genre.ID is not null and not exists genre ? 'does not exist' :
|
||||||
|
null as genre,
|
||||||
|
|
||||||
|
// multiple constraints: mandatory + assert target + special
|
||||||
|
not exists author ? 'Author does not exist: ' || author.ID :
|
||||||
|
author.ID is null ? 'is missing' : // FIXME: expected author.ID to refer to foreign key, apparently that is not the case -> move one line up to see
|
||||||
|
count(author.books.ID) -1 > 1 ? author.name || ' already wrote multiple books, please choose another author' : // TODO: we should support count(author.books)
|
||||||
|
null as author,
|
||||||
|
|
||||||
|
} group by ID;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validation constraints for Authors
|
||||||
|
*/
|
||||||
|
view Authors.constraints as select from Authors { ID,
|
||||||
|
|
||||||
|
// two-step mandatory check
|
||||||
|
name = null ? 'is missing' : trim(name)='' ? 'must not be empty' :
|
||||||
|
null as name,
|
||||||
|
|
||||||
|
// constraint related to two fields
|
||||||
|
dateOfDeath > dateOfBirth ? 'date of birth must be before date of death' : null as _born_before_death,
|
||||||
|
$self._born_before_death as dateOfBirth,
|
||||||
|
$self._born_before_death as dateOfDeath,
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user