Skip to content

Commit

Permalink
feat: support for generic outbox (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
sjvans authored Oct 19, 2023
1 parent 7aeda85 commit fc18b82
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
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

0 comments on commit fc18b82

Please sign in to comment.