Skip to content
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

feat: support for generic outbox #60

Merged
merged 5 commits into from
Oct 19, 2023
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).

## Version 0.4.0 - TBD

### Added

- Support for generic outbox

### Changed

- Always use outbox (as configured in project)
Expand Down
1 change: 1 addition & 0 deletions cds
Submodule cds added at 605d66
13 changes: 9 additions & 4 deletions srv/service.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const cds = require('@sap/cds')

// REVISIT: cds.OutboxService or technique to avoid extending OutboxService
const OutboxService = require('@sap/cds/libx/_runtime/messaging/Outbox')
const Base = cds.outboxed ? cds.Service : require('@sap/cds/libx/_runtime/messaging/Outbox')

module.exports = class AuditLogService extends OutboxService {
module.exports = class AuditLogService extends Base {
async init() {
const outboxed = this.immediate instanceof cds.Service

// add common audit log entry fields
this.before('*', req => {
const { tenant, user, timestamp: time } = cds.context
Expand All @@ -16,6 +17,10 @@ module.exports = class AuditLogService extends OutboxService {

// add self-explanatory api (await audit.log/logSync(event, data))
this.log = this.emit
this.logSync = this.send
// NOTE: logSync is not a public API!
this.logSync = (...args) => {
if (outboxed) return this.immediate.send(...args)
return this.send(...args)
}
}
}
4 changes: 3 additions & 1 deletion test/api/srv/api-service.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
module.exports = async function () {
const audit = await cds.connect.to('audit-log')
const outboxed = audit.immediate instanceof cds.Service

this.on('testEmit', async function () {
await audit.emit('foo', { bar: 'baz' })
})

this.on('testSend', async function () {
await audit.send('foo', { bar: 'baz' })
if (outboxed) await audit.immediate.send('foo', { bar: 'baz' })
else await audit.send('foo', { bar: 'baz' })
})

this.on('testLog', async function () {
Expand Down