@platformatic/sql-events 0.43.1 → 0.45.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.
package/errors.js ADDED
@@ -0,0 +1,12 @@
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
+ }
package/index.d.ts CHANGED
@@ -1,16 +1,14 @@
1
- /// <reference types="@platformatic/types" />
2
- import { FastifyPluginAsync } from 'fastify'
1
+ import { FastifyInstance } from 'fastify'
3
2
  import { Readable } from 'stream'
4
- import { SQLMapperPluginInterface } from '@platformatic/sql-mapper'
3
+ import { SQLMapperPluginInterface, Entities } from '@platformatic/sql-mapper'
4
+ import { FastifyError } from '@fastify/error'
5
5
 
6
- declare module '@platformatic/types' {
7
- interface PlatformaticApp {
8
- subscribe: (topic: string | string[]) => Promise<Readable>
9
- }
6
+ export interface SQLEventsPluginInterface {
7
+ subscribe: (topic: string | string[]) => Promise<Readable>
10
8
  }
11
9
 
12
- export interface SQLEventsPluginOptions {
13
- mapper: SQLMapperPluginInterface
10
+ export interface SQLEventsPluginOptions<T extends Entities> {
11
+ mapper: SQLMapperPluginInterface<T>
14
12
 
15
13
  // TODO mqemitter has no types
16
14
  mq?: any
@@ -20,8 +18,15 @@ export interface SQLEventsPluginOptions {
20
18
  /**
21
19
  * Fastify plugin that add events capabilities to registered sql-mapper
22
20
  */
23
- declare const plugin: FastifyPluginAsync<SQLEventsPluginOptions>
21
+ export default function plugin<T extends Entities>(app: FastifyInstance, options: SQLEventsPluginOptions<T>): Promise<SQLEventsPluginInterface>
24
22
 
25
- export default plugin
23
+ export function setupEmitter<T extends Entities>(options: SQLEventsPluginOptions<T>): void
26
24
 
27
- export function setupEmitter(options: SQLEventsPluginOptions): void
25
+ /**
26
+ * All the errors thrown by the plugin.
27
+ */
28
+ export module errors {
29
+ export const ObjectRequiredUnderTheDataProperty: () => FastifyError
30
+ export const PrimaryKeyIsNecessaryInsideData: () => FastifyError
31
+ export const NoSuchActionError: (actions: string) => FastifyError
32
+ }
package/index.js CHANGED
@@ -6,6 +6,7 @@ const camelcase = require('camelcase')
6
6
  const { PassThrough } = require('stream')
7
7
  const MQEmitterRedis = require('mqemitter-redis')
8
8
  const { promisify } = require('util')
9
+ const errors = require('./errors')
9
10
 
10
11
  async function fastifySqlEvents (app, opts) {
11
12
  setupEmitter({ ...opts, mapper: app.platformatic, log: app.log })
@@ -83,11 +84,11 @@ function setupEmitter ({ log, mq, mapper, connectionString }) {
83
84
  // getPublishTopic is async because it could be overridden to be asynchronous
84
85
  entity.getPublishTopic = async function ({ action, data }) {
85
86
  if (!data) {
86
- throw new Error('The object that will be published is required under the data property')
87
+ throw new errors.ObjectRequiredUnderTheDataProperty()
87
88
  }
88
89
 
89
90
  if (!data[primaryKey]) {
90
- throw new Error('The primaryKey is necessary inside data')
91
+ throw new errors.PrimaryKeyIsNecessaryInsideData()
91
92
  }
92
93
 
93
94
  switch (action) {
@@ -108,7 +109,7 @@ function setupEmitter ({ log, mq, mapper, connectionString }) {
108
109
  case 'delete':
109
110
  return `/entity/${entityName}/delete/+`
110
111
  default:
111
- throw new Error(`no such action ${action}`)
112
+ throw new errors.NoSuchActionError(action)
112
113
  }
113
114
  }
114
115
  }
@@ -135,3 +136,4 @@ function noop () {}
135
136
 
136
137
  module.exports = fp(fastifySqlEvents)
137
138
  module.exports.setupEmitter = setupEmitter
139
+ module.exports.errors = errors
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/sql-events",
3
- "version": "0.43.1",
3
+ "version": "0.45.0",
4
4
  "description": "Emit events via MQEmitter",
5
5
  "main": "index.js",
6
6
  "repository": {
@@ -20,14 +20,14 @@
20
20
  "standard": "^17.1.0",
21
21
  "tap": "^16.3.6",
22
22
  "tsd": "^0.29.0",
23
- "@platformatic/sql-mapper": "0.43.1"
23
+ "@platformatic/sql-mapper": "0.45.0"
24
24
  },
25
25
  "dependencies": {
26
+ "@fastify/error": "^3.2.1",
26
27
  "camelcase": "^6.3.0",
27
28
  "fastify-plugin": "^4.5.0",
28
29
  "mqemitter": "^5.0.0",
29
- "mqemitter-redis": "^5.0.0",
30
- "@platformatic/types": "0.43.1"
30
+ "mqemitter-redis": "^5.0.0"
31
31
  },
32
32
  "tsd": {
33
33
  "directory": "test/types"
@@ -1,8 +1,16 @@
1
1
  import { expectType } from 'tsd'
2
2
  import { fastify, FastifyInstance } from 'fastify'
3
- import plugin, { setupEmitter } from '../../index'
3
+ import plugin, { setupEmitter, errors } from '../../index'
4
4
  import { Readable } from 'stream'
5
- import {SQLMapperPluginInterface} from '@platformatic/sql-mapper'
5
+ import { SQLMapperPluginInterface, Entities } from '@platformatic/sql-mapper'
6
+ import { SQLEventsPluginInterface } from '../../index'
7
+ import { FastifyError } from '@fastify/error'
8
+
9
+ declare module 'fastify' {
10
+ interface FastifyInstance {
11
+ platformatic: SQLMapperPluginInterface<Entities> & SQLEventsPluginInterface
12
+ }
13
+ }
6
14
 
7
15
  const instance: FastifyInstance = fastify()
8
16
  instance.register(plugin)
@@ -12,6 +20,14 @@ instance.register(async (instance) => {
12
20
  expectType<Promise<Readable>>(instance.platformatic.subscribe(['/test']))
13
21
  })
14
22
 
15
- setupEmitter({ mapper: {} as SQLMapperPluginInterface })
16
- setupEmitter({ mapper: {} as SQLMapperPluginInterface, connectionString: 'redis://localhost:6379' })
17
- setupEmitter({ mapper: {} as SQLMapperPluginInterface, mq: {} })
23
+ setupEmitter({ mapper: {} as SQLMapperPluginInterface<Entities> })
24
+ setupEmitter({ mapper: {} as SQLMapperPluginInterface<Entities>, connectionString: 'redis://localhost:6379' })
25
+ setupEmitter({ mapper: {} as SQLMapperPluginInterface<Entities>, mq: {} })
26
+
27
+ // Errors
28
+ type ErrorWithNoParams = () => FastifyError
29
+ type ErrorWithOneParam = (param: string) => FastifyError
30
+
31
+ expectType<ErrorWithOneParam>(errors.NoSuchActionError)
32
+ expectType<ErrorWithNoParams>(errors.ObjectRequiredUnderTheDataProperty)
33
+ expectType<ErrorWithNoParams>(errors.PrimaryKeyIsNecessaryInsideData)