Rudimentary tree support for Genres on SQLite

This commit is contained in:
Daniel Hutzel
2025-04-01 22:58:18 +02:00
parent 725c57f1aa
commit c6432d1917

View File

@@ -1,17 +1,32 @@
const cds = require('@sap/cds')
cds.on('served', () => {
// register a simplified handler for tree table
// no filtering/searching/collapsing/expanding of nodes
if (cds.db.kind === 'sqlite') {
const { AdminService } = cds.services
AdminService.prepend(() => {
AdminService.on('READ', 'Genres', (req,next) => {
const { SELECT } = req.query
// Suppress error message: Feature "recurse" queries not supported.
delete SELECT.__proto__.recurse
delete SELECT.recurse
return next()
})
})
}
})
const cds = require('@sap/cds/lib')
if (cds.requires.db.kind === 'sqlite') {
// Add a column to the Genres table, to efficiently determine if a node
// is a leaf, and use it for DrillState in hierarchy queries.
cds.on('loaded', m => Object.assign (
m.definitions['sap.capire.bookshop.Genres'].elements,
cds.parse (`entity Genres {
DrillState: String = coalesce(leaf,'collapsed');
leaf: String;
}`).definitions.Genres.elements
))
// Fill in `leaf` helper column for initial data entries
cds.on('served', ()=> cds.run(`
UPDATE sap_capire_bookshop_Genres as g SET leaf='leaf' WHERE not exists (
SELECT 1 from sap_capire_bookshop_Genres WHERE parent_ID = g.ID
)`
))
// Register a simplistic handler for hierarchical queries
cds.on('serving:AdminService', srv => srv.prepend(() => srv.on('READ', 'Genres', (req,next) => {
const q = req.query, parent = {ref:['parent','ID']}
if (q.SELECT.recurse?.where?.[0].ref[0] === 'Distance') q.SELECT.where[0] = parent
else if (!q.SELECT.search) q.SELECT.where ??= [ parent, 'is null' ]
// Suppress error message: Feature "recurse" queries not supported.
delete q.SELECT.__proto__.recurse
delete q.SELECT.recurse
return next()
})))
}