@platformatic/sql-events 3.59.0 → 3.61.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +23 -4
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -27,12 +27,31 @@ export function setupEmitter ({ log, mq, mapper, connectionString }) {
27
27
  continue
28
28
  }
29
29
  const primaryKey = camelcase(entity.primaryKeys.values().next().value)
30
+
31
+ // The publish topic requires the primary key, so make sure it is always
32
+ // retrieved even when the caller did not select it
33
+ function ensurePrimaryKeyField (data) {
34
+ if (Array.isArray(data.fields) && data.fields.length > 0 && !data.fields.includes(primaryKey)) {
35
+ return { ...data, fields: data.fields.concat(primaryKey) }
36
+ }
37
+ return data
38
+ }
39
+
40
+ // Remove the primary key from the result when the caller did not select it
41
+ function stripPrimaryKey (res, requestedFields) {
42
+ if (Array.isArray(requestedFields) && requestedFields.length > 0 && !requestedFields.includes(primaryKey)) {
43
+ const { [primaryKey]: _, ...rest } = res
44
+ return rest
45
+ }
46
+ return res
47
+ }
48
+
30
49
  mapper.addEntityHooks(entityName, {
31
50
  async save (original, data) {
32
51
  const ctx = data.ctx
33
52
  /* istanbul ignore next */
34
53
  const _log = ctx?.reply?.request?.log || log
35
- const res = await original(data)
54
+ const res = await original(ensurePrimaryKeyField(data))
36
55
  const topic = await entity.getPublishTopic({ action: 'save', data: res, ctx })
37
56
  if (topic) {
38
57
  const payload = {
@@ -49,7 +68,7 @@ export function setupEmitter ({ log, mq, mapper, connectionString }) {
49
68
  )
50
69
  })
51
70
  }
52
- return res
71
+ return stripPrimaryKey(res, data.fields)
53
72
  },
54
73
 
55
74
  delete: multiElement('delete'),
@@ -61,7 +80,7 @@ export function setupEmitter ({ log, mq, mapper, connectionString }) {
61
80
  const ctx = data.ctx
62
81
  /* istanbul ignore next */
63
82
  const _log = ctx?.reply?.request?.log || log
64
- const res = await original(data)
83
+ const res = await original(ensurePrimaryKeyField(data))
65
84
 
66
85
  await Promise.all(
67
86
  res.map(async payload => {
@@ -83,7 +102,7 @@ export function setupEmitter ({ log, mq, mapper, connectionString }) {
83
102
  })
84
103
  )
85
104
 
86
- return res
105
+ return res.map(r => stripPrimaryKey(r, data.fields))
87
106
  }
88
107
  }
89
108
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/sql-events",
3
- "version": "3.59.0",
3
+ "version": "3.61.0",
4
4
  "description": "Emit events via MQEmitter",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -22,7 +22,7 @@
22
22
  "neostandard": "^0.12.0",
23
23
  "tstyche": "^6.2.0",
24
24
  "typescript": "^5.5.4",
25
- "@platformatic/sql-mapper": "3.59.0"
25
+ "@platformatic/sql-mapper": "3.61.0"
26
26
  },
27
27
  "dependencies": {
28
28
  "@fastify/error": "^4.0.0",