@jcbuisson/express-x 1.0.11 → 1.0.13

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/package.json +1 -1
  2. package/src/index.mjs +19 -26
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jcbuisson/express-x",
3
- "version": "1.0.11",
3
+ "version": "1.0.13",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "src/index.mjs",
package/src/index.mjs CHANGED
@@ -1,35 +1,33 @@
1
1
 
2
2
  import http from 'http'
3
3
  import { Server } from "socket.io"
4
- import { PrismaClient } from '@prisma/client'
5
4
 
6
5
  /*
7
6
  * Enhance `app` express application with Feathers-like services
8
7
  */
9
- function expressX(app) {
10
- const prisma = new PrismaClient()
11
- app.set('prisma', prisma) // ?? BOF
8
+ function expressX(app, options) {
12
9
 
13
10
  const services = {}
14
11
  const connections = {}
15
12
 
16
13
  let lastConnectionId = 1
17
- let isDebug = false
18
14
 
19
15
  /*
20
16
  * create a service `name` based on Prisma table `entity`
21
17
  */
22
18
  function createDatabaseService(name, { entity=name, client='prisma' }) {
19
+ const prisma = app.get('prisma')
20
+
23
21
  return createService(name, {
24
22
  create: (data) => {
25
- if (isDebug) console.log('create', name, data)
23
+ if (options.debug) console.log('create', name, data)
26
24
  return prisma[entity].create({
27
25
  data,
28
26
  })
29
27
  },
30
28
 
31
29
  get: (id) => {
32
- if (isDebug) console.log('get', name, id)
30
+ if (options.debug) console.log('get', name, id)
33
31
  return prisma[entity].findUnique({
34
32
  where: {
35
33
  id,
@@ -38,7 +36,7 @@ function expressX(app) {
38
36
  },
39
37
 
40
38
  patch: (id, data) => {
41
- if (isDebug) console.log('patch', name, id, data)
39
+ if (options.debug) console.log('patch', name, id, data)
42
40
  return prisma[entity].update({
43
41
  where: {
44
42
  id,
@@ -48,21 +46,20 @@ function expressX(app) {
48
46
  },
49
47
 
50
48
  remove: (id) => {
51
- if (isDebug) console.log('remove', name, id)
49
+ if (options.debug) console.log('remove', name, id)
52
50
  return prisma[entity].delete({
53
51
  where: {
54
52
  id,
55
53
  },
56
- })
57
- },
54
+ })},
58
55
 
59
56
  find: (options) => {
60
- if (isDebug) console.log('find', name, options)
57
+ if (options.debug) console.log('find', name, options)
61
58
  return prisma[entity].findMany(options)
62
59
  },
63
60
 
64
61
  upsert: (options) => {
65
- if (isDebug) console.log('upsert', name, options)
62
+ if (options.debug) console.log('upsert', name, options)
66
63
  return prisma[entity].upsert(options)
67
64
  },
68
65
  })
@@ -94,7 +91,7 @@ function expressX(app) {
94
91
 
95
92
  // call method
96
93
  const result = await method(...context.args)
97
- // if (isDebug) console.log('result', result)
94
+ // if (options.debug) console.log('result', result)
98
95
 
99
96
  // call 'after' hooks
100
97
  const afterMethodHooks = service?.hooks?.after && service.hooks.after[methodName] || []
@@ -181,7 +178,7 @@ function expressX(app) {
181
178
  const io = new Server(server)
182
179
 
183
180
  io.on('connection', function(socket) {
184
- if (isDebug) console.log('Client connected to the WebSocket')
181
+ if (options.debug) console.log('Client connected to the WebSocket')
185
182
  const connection = {
186
183
  id: lastConnectionId++,
187
184
  socket,
@@ -189,7 +186,7 @@ function expressX(app) {
189
186
  }
190
187
  // store connection in cache
191
188
  connections[connection.id] = connection
192
- if (isDebug) console.log('active connections', Object.keys(connections))
189
+ if (options.debug) console.log('active connections', Object.keys(connections))
193
190
 
194
191
  // emit 'connection' event for app (expressjs extends EventEmitter)
195
192
  app.emit('connection', connection)
@@ -198,7 +195,7 @@ function expressX(app) {
198
195
  socket.emit('connected', connection.id)
199
196
 
200
197
  socket.on('disconnect', () => {
201
- if (isDebug) console.log('Client disconnected', connection.id)
198
+ if (options.debug) console.log('Client disconnected', connection.id)
202
199
  delete connections[connection.id]
203
200
  })
204
201
 
@@ -208,7 +205,7 @@ function expressX(app) {
208
205
  * Emit in return a 'client-response' message
209
206
  */
210
207
  socket.on('client-request', async ({ uid, name, action, args }) => {
211
- if (isDebug) console.log("client-request", uid, name, action, args)
208
+ if (options.debug) console.log("client-request", uid, name, action, args)
212
209
  if (name in services) {
213
210
  const service = services[name]
214
211
  try {
@@ -231,12 +228,12 @@ function expressX(app) {
231
228
  const publishFunc = service.publishCallback
232
229
  if (publishFunc) {
233
230
  const channelNames = await publishFunc(result, app)
234
- if (isDebug) console.log('publish channels', name, action, channelNames)
231
+ if (options.debug) console.log('publish channels', name, action, channelNames)
235
232
  for (const channelName of channelNames) {
236
- if (isDebug) console.log('service-event', name, action, channelName)
233
+ if (options.debug) console.log('service-event', name, action, channelName)
237
234
  const connectionList = Object.values(connections).filter(cnx => cnx.channelNames.has(channelName))
238
235
  for (const connection of connectionList) {
239
- if (isDebug) console.log('emit to', connection.id, name, action, result)
236
+ if (options.debug) console.log('emit to', connection.id, name, action, result)
240
237
  connection.socket.emit('service-event', {
241
238
  name,
242
239
  action,
@@ -275,12 +272,9 @@ function expressX(app) {
275
272
  connection.channelNames.delete(channelName)
276
273
  }
277
274
 
278
- function setDebug(isOn) {
279
- isDebug = isOn
280
- }
281
-
282
275
  // enhance `app` with objects and methods
283
276
  Object.assign(app, {
277
+ options,
284
278
  createDatabaseService,
285
279
  createService,
286
280
  service,
@@ -289,7 +283,6 @@ function expressX(app) {
289
283
  server,
290
284
  joinChannel,
291
285
  leaveChannel,
292
- setDebug,
293
286
  })
294
287
  return app
295
288
  }