Skip to content

[wip] pre calc queries #153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
fail-fast: false
matrix:
node-version: [22.x, 20.x]
cds-version: [8]
cds-version: [9, 8]
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 18
node-version: 20
registry-url: https://registry.npmjs.org/
- name: run tests
run: |
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# macOS
.DS_Store

# Logs
logs
*.log
Expand Down
94 changes: 94 additions & 0 deletions cds-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const { hasPersonalData } = require('./lib/utils')

const WRITE = ['CREATE', 'UPDATE', 'DELETE']

const $distance = Symbol('@cap-js/audit-logging:distance')

/*
* Add generic audit logging handlers
*/
Expand Down Expand Up @@ -61,6 +63,98 @@ cds.on('served', services => {
}
})

cds.on('served', services => {
// prettier-ignore
const { types, classes: { number } } = cds.builtin

const recurse = (entity, ds, ds_keys, path, definitions) => {
// forwards
for (const assoc in entity.associations) {
const target = definitions[entity.associations[assoc].target]

if (!target['@PersonalData.EntitySemantics']) continue
if (target['@PersonalData.EntitySemantics'] === 'DataSubject') continue
if (target.own($distance) && target[$distance] <= path.length) continue

target.set($distance, path.length)

// the known entity instance as starting point
const kp = Object.keys(target.keys).reduce((acc, cur) => {
if (cur !== 'IsActiveEntity') acc.push(`${cur}=%%%${cur}%%%`)
return acc
}, [])
// path.push({ id: target.name, where: kp })
path.push({ id: assoc, where: kp })

// construct path as string
const p = path.reduce((acc, cur) => {
if (!acc) {
// acc += `${cur.id}${cur.where ? `[${cur.where.join(' and ')}]` : ''}`
acc += `${cur.id}`
} else {
if (cur.id) {
const close = acc.match(/([\]]+)$/)?.[1]
if (close)
acc =
acc.slice(0, close.length * -1) +
`[exists ${cur.id}${cur.where ? `[${cur.where.join(' and ')}]` : ''}]` +
close
else acc += `[exists ${cur.id}${cur.where ? `[${cur.where.join(' and ')}]` : ''}]`
} else if (cur.to) acc += `.${cur.to}`
}
return acc
}, '')

target._getDataSubjectQuery = row => {
let path = `${p}`
for (const ph of path.match(/%%%(\w+)%%%/g)) {
const ref = ph.slice(3, -3)
const val = row[ref]
path = path.replace(ph, types[target.elements[ref]._type] instanceof number ? val : `'${val}'`)
}
return SELECT.one.from(path).columns(ds_keys)
}

delete path.at(-1).where

recurse(target, ds, ds_keys, path, definitions)

path.pop()
}

// backwards
const targets = Object.values(definitions).filter(
d =>
d['@PersonalData.EntitySemantics'] &&
d['@PersonalData.EntitySemantics'] !== 'DataSubject' &&
!d.own($distance) &&
Object.values(d.associations || {}).some(
a => a.target === entity.name && !Object.values(entity.associations || {}).some(b => b.target === d.name)
)
)

for (const target of targets) {
// debugger
}
}

for (const service of services) {
if (!(service instanceof cds.ApplicationService)) continue

const dataSubjects = []
for (const entity of service.entities)
if (entity['@PersonalData.EntitySemantics'] === 'DataSubject') dataSubjects.push(entity)
if (!dataSubjects.length) continue

const definitions = service.model.definitions
for (const ds of dataSubjects) {
const ds_keys = Object.keys(ds.keys)
const path = [{ id: ds.name }]
recurse(ds, ds, ds_keys, path, definitions)
}
}
})

/*
* Export base class for extending in custom implementations
*/
Expand Down
25 changes: 23 additions & 2 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,18 @@ const addDataSubjectForDetailsEntity = (row, log, req, entity, model) => {
const map = _getDataSubjectsMap(req)
if (map.has(role)) log.data_subject.id = map.get(role)
// REVISIT by downward lookups row might already contain ID - some potential to optimize
else map.set(role, _getDataSubjectIdQuery(dataSubjectInfo, row, model))
else {
// let q = _getDataSubjectIdQuery(dataSubjectInfo, row, model)
let q
if (entity._getDataSubjectQuery) {
q = entity._getDataSubjectQuery(row)
} else {
// debugger
q = _getDataSubjectIdQuery(dataSubjectInfo, row, model)
}
// q = _getDataSubjectIdQuery(dataSubjectInfo, row, model)
map.set(role, q)
}
}

const resolveDataSubjects = (logs, req) => {
Expand All @@ -265,7 +276,17 @@ const resolveDataSubjects = (logs, req) => {
if (each.data_subject.id instanceof cds.ql.Query) {
const q = each.data_subject.id
if (!map.has(q)) {
const p = cds.run(q).then(res => map.set(q, res))
const p = cds
.run(q)
.then(res => {
// debugger
map.set(q, res)
})
.catch(e => {
q
debugger
throw e
})
map.set(q, p)
ps.push(p)
}
Expand Down
3 changes: 3 additions & 0 deletions test/personal-data/crud.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1462,6 +1462,9 @@ describe('personal data audit logging in CRUD', () => {
})

// check only one select used to look up data subject

const _selects = _logger._logs.debug.filter(l => typeof l === 'string' && l.match(/^SELECT/))

const selects = _logger._logs.debug.filter(
l => typeof l === 'string' && l.match(/^SELECT/) && l.match(/SELECT [Customers.]*ID FROM CRUD_1_Customers/)
)
Expand Down
5 changes: 5 additions & 0 deletions test/personal-data/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"dependencies": {
"@cap-js/audit-logging": "*"
},
"cds": {
"requires": {
"outbox": false
}
}
}
Loading