@platformatic/sql-events 3.4.1 → 3.5.1

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.
package/eslint.config.js CHANGED
@@ -1,3 +1,3 @@
1
- 'use strict'
1
+ import neostandard from 'neostandard'
2
2
 
3
- module.exports = require('neostandard')({ ts: true })
3
+ export default neostandard({ ts: true })
package/index.d.ts CHANGED
@@ -8,7 +8,7 @@ export interface SQLEventsPluginInterface {
8
8
  }
9
9
 
10
10
  export interface SQLEventsPluginOptions<T extends Entities> {
11
- mapper: SQLMapperPluginInterface<T>
11
+ mapper?: SQLMapperPluginInterface<T>
12
12
 
13
13
  // TODO mqemitter has no types
14
14
  mq?: any
package/index.js CHANGED
@@ -1,12 +1,10 @@
1
- 'use strict'
2
-
3
- const MQEmitter = require('mqemitter')
4
- const fp = require('fastify-plugin')
5
- const camelcase = require('camelcase')
6
- const { PassThrough } = require('stream')
7
- const MQEmitterRedis = require('mqemitter-redis')
8
- const { promisify } = require('util')
9
- const errors = require('./errors')
1
+ import camelcase from 'camelcase'
2
+ import fp from 'fastify-plugin'
3
+ import MQEmitter from 'mqemitter'
4
+ import MQEmitterRedis from 'mqemitter-redis'
5
+ import { PassThrough } from 'stream'
6
+ import { promisify } from 'util'
7
+ import * as errors from './lib/errors.js'
10
8
 
11
9
  async function fastifySqlEvents (app, opts) {
12
10
  setupEmitter({ ...opts, mapper: app.platformatic, log: app.log })
@@ -14,7 +12,7 @@ async function fastifySqlEvents (app, opts) {
14
12
  app.addHook('onClose', () => promisify(mq.close.bind(mq)))
15
13
  }
16
14
 
17
- function setupEmitter ({ log, mq, mapper, connectionString }) {
15
+ export function setupEmitter ({ log, mq, mapper, connectionString }) {
18
16
  if (connectionString) {
19
17
  mq = MQEmitterRedis({ connectionString })
20
18
  } else if (!mq) {
@@ -38,21 +36,24 @@ function setupEmitter ({ log, mq, mapper, connectionString }) {
38
36
  const topic = await entity.getPublishTopic({ action: 'save', data: res, ctx })
39
37
  if (topic) {
40
38
  const payload = {
41
- [primaryKey]: res[primaryKey],
39
+ [primaryKey]: res[primaryKey]
42
40
  }
43
41
  _log.trace({ topic, payload }, 'publishing event')
44
- await new Promise((resolve) => {
45
- mq.emit({
46
- topic,
47
- payload,
48
- }, resolve)
42
+ await new Promise(resolve => {
43
+ mq.emit(
44
+ {
45
+ topic,
46
+ payload
47
+ },
48
+ resolve
49
+ )
49
50
  })
50
51
  }
51
52
  return res
52
53
  },
53
54
 
54
55
  delete: multiElement('delete'),
55
- insert: multiElement('save'),
56
+ insert: multiElement('save')
56
57
  })
57
58
 
58
59
  function multiElement (action) {
@@ -62,20 +63,25 @@ function setupEmitter ({ log, mq, mapper, connectionString }) {
62
63
  const _log = ctx?.reply?.request?.log || log
63
64
  const res = await original(data)
64
65
 
65
- await Promise.all(res.map(async (payload) => {
66
- const topic = await entity.getPublishTopic({ action, data: payload, ctx })
67
- if (topic) {
68
- _log.trace({ topic, payload }, 'publishing event')
69
- return new Promise((resolve) => {
70
- mq.emit({
71
- topic,
72
- payload: {
73
- [primaryKey]: payload[primaryKey],
74
- },
75
- }, resolve)
76
- })
77
- }
78
- }))
66
+ await Promise.all(
67
+ res.map(async payload => {
68
+ const topic = await entity.getPublishTopic({ action, data: payload, ctx })
69
+ if (topic) {
70
+ _log.trace({ topic, payload }, 'publishing event')
71
+ return new Promise(resolve => {
72
+ mq.emit(
73
+ {
74
+ topic,
75
+ payload: {
76
+ [primaryKey]: payload[primaryKey]
77
+ }
78
+ },
79
+ resolve
80
+ )
81
+ })
82
+ }
83
+ })
84
+ )
79
85
 
80
86
  return res
81
87
  }
@@ -126,14 +132,15 @@ function setupEmitter ({ log, mq, mapper, connectionString }) {
126
132
  mq.removeListener(topic, forward, noop)
127
133
  }
128
134
  })
129
- return Promise.all(topics.map((topic) => {
130
- return new Promise((resolve) => mq.on(topic, forward, resolve))
131
- })).then(() => stream)
135
+ return Promise.all(
136
+ topics.map(topic => {
137
+ return new Promise(resolve => mq.on(topic, forward, resolve))
138
+ })
139
+ ).then(() => stream)
132
140
  }
133
141
  }
134
142
 
135
143
  function noop () {}
136
144
 
137
- module.exports = fp(fastifySqlEvents)
138
- module.exports.setupEmitter = setupEmitter
139
- module.exports.errors = errors
145
+ export default fp(fastifySqlEvents)
146
+ export * as errors from './lib/errors.js'
package/lib/errors.js ADDED
@@ -0,0 +1,13 @@
1
+ import createError from '@fastify/error'
2
+
3
+ export const ERROR_PREFIX = 'PLT_SQL_EVENTS'
4
+
5
+ export const ObjectRequiredUnderTheDataProperty = createError(
6
+ `${ERROR_PREFIX}_OBJECT_IS_REQUIRED_UNDER_THE_DATA_PROPERTY`,
7
+ 'The object that will be published is required under the data property'
8
+ )
9
+ export const PrimaryKeyIsNecessaryInsideData = createError(
10
+ `${ERROR_PREFIX}_PRIMARY_KEY_IS_NECESSARY_INSIDE_DATA`,
11
+ 'The primaryKey is necessary inside data'
12
+ )
13
+ export const NoSuchActionError = createError(`${ERROR_PREFIX}_NO_SUCH_ACTION`, 'No such action %s')
package/package.json CHANGED
@@ -1,28 +1,28 @@
1
1
  {
2
2
  "name": "@platformatic/sql-events",
3
- "version": "3.4.1",
3
+ "version": "3.5.1",
4
4
  "description": "Emit events via MQEmitter",
5
5
  "main": "index.js",
6
+ "type": "module",
6
7
  "repository": {
7
8
  "type": "git",
8
9
  "url": "git+https://github.com/platformatic/platformatic.git"
9
10
  },
10
- "author": "Matteo Collina <hello@matteocollina.com>",
11
+ "author": "Platformatic Inc. <oss@platformatic.dev> (https://platformatic.dev)",
11
12
  "license": "Apache-2.0",
12
13
  "bugs": {
13
14
  "url": "https://github.com/platformatic/platformatic/issues"
14
15
  },
15
16
  "homepage": "https://github.com/platformatic/platformatic#readme",
16
17
  "devDependencies": {
17
- "@matteo.collina/tspl": "^0.1.1",
18
- "borp": "^0.17.0",
18
+ "cleaner-spec-reporter": "^0.5.0",
19
19
  "eslint": "9",
20
20
  "fastify": "^5.0.0",
21
21
  "ioredis": "^5.3.2",
22
- "neostandard": "^0.11.1",
23
- "tsd": "^0.31.0",
22
+ "neostandard": "^0.12.0",
23
+ "tsd": "^0.33.0",
24
24
  "typescript": "^5.5.4",
25
- "@platformatic/sql-mapper": "3.4.1"
25
+ "@platformatic/sql-mapper": "3.5.1"
26
26
  },
27
27
  "dependencies": {
28
28
  "@fastify/error": "^4.0.0",
@@ -34,13 +34,16 @@
34
34
  "tsd": {
35
35
  "directory": "test/types"
36
36
  },
37
+ "engines": {
38
+ "node": ">=22.19.0"
39
+ },
37
40
  "scripts": {
38
- "test": "npm run lint && npm run test:typescript && npm run test:postgresql && npm run test:mariadb && npm run test:mysql && npm run test:mysql8 && npm run test:sqlite",
39
- "test:postgresql": "DB=postgresql borp --timeout=180000 --concurrency=1 test/*.test.js",
40
- "test:mariadb": "DB=mariadb borp --timeout=180000 --concurrency=1 test/*.test.js",
41
- "test:mysql": "DB=mysql borp --timeout=180000 --concurrency=1 test/*.test.js",
42
- "test:mysql8": "DB=mysql8 borp --timeout=180000 --concurrency=1 test/*.test.js",
43
- "test:sqlite": "DB=sqlite borp --timeout=180000 --concurrency=1 test/*.test.js",
41
+ "test": "npm run test:typescript && npm run test:postgresql && npm run test:mariadb && npm run test:mysql && npm run test:mysql8 && npm run test:sqlite",
42
+ "test:postgresql": "DB=postgresql node --test --test-reporter=cleaner-spec-reporter --test-concurrency=1 --test-timeout=2000000 test/*.test.js",
43
+ "test:mariadb": "DB=mariadb node --test --test-reporter=cleaner-spec-reporter --test-concurrency=1 --test-timeout=2000000 test/*.test.js",
44
+ "test:mysql": "DB=mysql node --test --test-reporter=cleaner-spec-reporter --test-concurrency=1 --test-timeout=2000000 test/*.test.js",
45
+ "test:mysql8": "DB=mysql8 node --test --test-reporter=cleaner-spec-reporter --test-concurrency=1 --test-timeout=2000000 test/*.test.js",
46
+ "test:sqlite": "DB=sqlite node --test --test-reporter=cleaner-spec-reporter --test-concurrency=1 --test-timeout=2000000 test/*.test.js",
44
47
  "test:typescript": "tsd",
45
48
  "lint": "eslint"
46
49
  }
package/errors.js DELETED
@@ -1,12 +0,0 @@
1
- 'use strict'
2
-
3
- const createError = require('@fastify/error')
4
-
5
- const ERROR_PREFIX = 'PLT_SQL_EVENTS'
6
-
7
- module.exports = {
8
- ObjectRequiredUnderTheDataProperty: createError(`${ERROR_PREFIX}_OBJECT_IS_REQUIRED_UNDER_THE_DATA_PROPERTY`, 'The object that will be published is required under the data property'),
9
- PrimaryKeyIsNecessaryInsideData: createError(`${ERROR_PREFIX}_PRIMARY_KEY_IS_NECESSARY_INSIDE_DATA`, 'The primaryKey is necessary inside data'),
10
- NoSuchActionError: createError(`${ERROR_PREFIX}_NO_SUCH_ACTION`, 'No such action %s'),
11
-
12
- }