@jcbuisson/express-x 1.1.2 → 1.1.3

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/README.md CHANGED
@@ -1 +1,263 @@
1
1
 
2
+
3
+ # Getting started
4
+
5
+ ## Create a project
6
+
7
+ Let's create a new folder for our application:
8
+
9
+ ```bash
10
+ mkdir expressx-project
11
+ cd expressx-project
12
+ ```
13
+
14
+ Since any ExpressX application is a Node application, we can create a default package.json using npm:
15
+
16
+ ```bash
17
+ npm init es6 --yes
18
+ ```
19
+
20
+ The `es6` argument adds `"type": "module"` in `package.json`. Beware! All further module imports must made with es6/esm `import` syntax.
21
+
22
+
23
+ ## Install ExpressX
24
+
25
+ ```bash
26
+ npm install @jcbuisson/express-x
27
+ ```
28
+
29
+ ## Our first server application
30
+
31
+ Now we can create an ExpressX application which will provide a complete REST API on a `user` resource
32
+ backed in a [Prisma](https://www.prisma.io/) database
33
+
34
+ ```js
35
+ // app.js
36
+ import express from 'express'
37
+ import bodyParser from 'body-parser'
38
+ import expressX from '@jcbuisson/express-x'
39
+
40
+ // `app` is a regular express application, enhanced with express-x features
41
+ const app = expressX(express())
42
+
43
+ // create two CRUD database services. They provide Prisma methods: `create`, 'createMany', 'find', 'findMany', 'upsert', etc.
44
+ app.createDatabaseService('User')
45
+ app.createDatabaseService('Post')
46
+
47
+ // add body parsers for http requests
48
+ app.use(bodyParser.json())
49
+ app.use(bodyParser.urlencoded({ extended: false }))
50
+
51
+ // add http/rest endpoints
52
+ app.addHttpRest('/api/user', app.service('User'))
53
+ app.addHttpRest('/api/post', app.service('Post'))
54
+
55
+ app.server.listen(8000, () => console.log(`App listening at http://localhost:8000`))
56
+ ```
57
+
58
+ Before running it we need to setup the corresponding database.
59
+
60
+
61
+ ## Create the database
62
+
63
+ Presently, ExpressX only handles [Prisma](https://www.prisma.io/). Prisma is able to connect to most brands of relational or NoSQL databases.
64
+
65
+ First, provide the database schema in `prisma/schema.prisma`:
66
+ ```prisma
67
+ generator client {
68
+ provider = "prisma-client-js"
69
+ }
70
+
71
+ model User {
72
+ id Int @default(autoincrement()) @id
73
+ name String
74
+ posts Post[]
75
+ }
76
+
77
+ model Post {
78
+ id Int @default(autoincrement()) @id
79
+ text String
80
+ author User @relation(fields: [authorId], references: [id], onDelete: Cascade)
81
+ authorId Int
82
+ }
83
+
84
+ datasource db {
85
+ provider = "sqlite"
86
+ url = "file:./dev.db"
87
+ }
88
+ ```
89
+
90
+ Then create the database:
91
+ ```bash
92
+ npx prisma migrate dev --name init
93
+ ```
94
+
95
+ The sqlite database file is created at `prisma/dev.db`
96
+
97
+
98
+ ## Run the application
99
+
100
+ ```bash
101
+ node app.js
102
+ ```
103
+
104
+ It prints the following lines in the console:
105
+ ```bash
106
+ created service 'user' over table 'User'
107
+ created service 'post' over table 'Post'
108
+ added HTTP endpoints for service 'user' at path '/api/user'
109
+ added HTTP endpoints for service 'post' at path '/api/post'
110
+ App listening at http://localhost:8000
111
+ ```
112
+
113
+
114
+ ## Enjoy your HTTP REST API
115
+
116
+ Now you can try the HTTP endpoints `/api/user` and `/api/post`; open a new console and run HTTP requests:
117
+ ```bash
118
+ curl -X POST -H 'Content-Type: application/json' -d '{"name":"JC"}' http://localhost:8000/api/user
119
+ # --> {"id":1,"name":"JC"}
120
+ curl http://localhost:8000/api/user
121
+ # --> [{"id":1,"name":"JC"}]
122
+ ```
123
+
124
+ With a few lines of code, we got a complete REST API over the database tables. But there is more!
125
+
126
+
127
+ ## Use it with a websocket client
128
+
129
+ First install ExpressX client library:
130
+
131
+ ```bash
132
+ npm i @jcbuisson/express-x-client
133
+ ```
134
+
135
+ Create the following client NodeJS script:
136
+
137
+ ```js
138
+ // client.js
139
+ import io from 'socket.io-client'
140
+ import expressxClient from '@jcbuisson/express-x-client'
141
+
142
+ const socket = io('http://localhost:8000', { transports: ["websocket"] })
143
+
144
+ const app = expressxClient(socket)
145
+
146
+ async function main() {
147
+ const user = await app.service('User').create({
148
+ name: "Joe",
149
+ })
150
+ await app.service('Post').create({
151
+ authorId: user.id,
152
+ text: "Post#1"
153
+ })
154
+ await app.service('Post').create({
155
+ authorId: user.id,
156
+ text: "Post#2"
157
+ })
158
+ const joe = await app.service('User').findUnique({
159
+ where: {
160
+ id: user.id,
161
+ },
162
+ include: {
163
+ posts: true,
164
+ },
165
+ })
166
+ console.log('joe', joe)
167
+ process.exit(0)
168
+ }
169
+ main()
170
+ ```
171
+
172
+ For simplicity we use a node client, but you of course you would write something similar with your favorite front-end framework.
173
+
174
+ You can use on the client side the exact same statements on services as you would on the server side, such as: `app.service('User').create(...)`.
175
+ Of course the `app` object here on the client is quite different that the `app` object on the server; you can find explanations [here]().
176
+
177
+ Now run the client script:
178
+ ```bash
179
+ node client.js
180
+ ```
181
+
182
+ It prints the following lines in the console:
183
+ ```
184
+ joe {
185
+ id: 11,
186
+ name: 'Joe',
187
+ posts: [
188
+ { id: 12, text: 'Post#1', authorId: 11 },
189
+ { id: 13, text: 'Post#2', authorId: 11 }
190
+ ]
191
+ }
192
+ ```
193
+
194
+ We have a GraphQL-like experience with the nested posts, thanks to Prisma and its use through ExpressX services.
195
+
196
+ ::: info
197
+ We could have removed from `app.js` all lines related to HTTP, since we are only using the websocket transport.
198
+ :::
199
+
200
+
201
+ ## Real-time applications
202
+
203
+ When websocket transport is used (default situation) and when a connected client calls a service method,
204
+ two twings happen on method completion:
205
+
206
+ - the resulting value is sent to the client
207
+ - an event is emitted, and sent to connected clients we'll call subscribers. The calling client may or not be one of those subscribers.
208
+
209
+ For example in a medical application, whenever a patients's record is modified, an event could be sent to all his/her caregivers.
210
+
211
+ ***Channels*** are used for this pub/sub mechanism. Service methods ***publish*** events on ***channels***, and clients ***subscribe***
212
+ to channels in order to receive those events. ExpressX provides functions to configure which events are published to which channels.
213
+ A channel is represented by a name and you can create and use as many channels as you need.
214
+
215
+ In the following example, every time a client connects to the server, it joins (= is subscribed to) the 'anonymous' channel.
216
+ And whenever an event is emited by the `post` or `user` service, this event is published on this channel,
217
+ and then broacasted to all connected clients, leading to real-time updates.
218
+
219
+ ```js
220
+ // app.js
221
+ import express from 'express'
222
+ import expressX from '@jcbuisson/express-x'
223
+ import { PrismaClient } from '@prisma/client'
224
+
225
+ // `app` is a regular express application, enhanced with express-x features
226
+ const app = expressX(express())
227
+
228
+ // configure prisma client from schema
229
+ app.set('prisma', new PrismaClient())
230
+
231
+ // create two CRUD database services. They provide Prisma methods: `create`, 'createMany', 'find', 'findMany', 'upsert', etc.
232
+ app.createDatabaseService('User')
233
+ app.createDatabaseService('Post')
234
+
235
+ // publish
236
+ app.service('User').publish(async (post, context) => {
237
+ return ['anonymous']
238
+ })
239
+ app.service('Post').publish(async (post, context) => {
240
+ return ['anonymous']
241
+ })
242
+
243
+ // subscribe
244
+ app.on('connection', (connection) => {
245
+ console.log('connection', connection.id)
246
+ app.joinChannel('anonymous', connection)
247
+ })
248
+
249
+ app.server.listen(8000, () => console.log(`App listening at http://localhost:8000`))
250
+ ```
251
+
252
+ Here is how a client may listen to channel events:
253
+
254
+ ```js
255
+ // client.js
256
+ ...
257
+ app.service('Post').on('create', post => {
258
+ console.log('post event created', post)
259
+ })
260
+ ```
261
+
262
+ The listener is triggered whenever the client receives from the server a `create` event from the service `post`.
263
+ This event results from the completion on the server of a call `app.service('Post').create()`
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@jcbuisson/express-x",
3
- "version": "1.1.2",
3
+ "version": "1.1.3",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "src/index.mjs",
7
7
  "repository": {
8
8
  "type": "git",
9
- "url": "git@gitlab.com:buisson31/express-x.git"
9
+ "url": "git@github.com:jcbuisson/express-x.git"
10
10
  },
11
11
  "author": "Jean-Christophe Buisson <buisson@enseeiht.fr> (jcbuisson.dev)",
12
12
  "license": "MIT",
package/prisma/dev.db CHANGED
Binary file
package/src/index.mjs CHANGED
@@ -147,6 +147,7 @@ function expressX(app, options={}) {
147
147
  context.http.req = req
148
148
  try {
149
149
  const value = await service.__create(context, { data: req.body })
150
+ publish(service, 'create', value)
150
151
  res.json(value)
151
152
  } catch(err) {
152
153
  console.log('callErr', err)
@@ -158,6 +159,8 @@ function expressX(app, options={}) {
158
159
  context.http.req = req
159
160
  const query = { ...req.query }
160
161
  try {
162
+ // the values in `req.query` are all strings, but Prisma need proper types
163
+ // we need to introspect column types and do the proper transtyping
161
164
  for (const fieldName in query) {
162
165
  if (!service.fieldTypes) service.fieldTypes = await getFieldTypes()
163
166
  const fieldType = service.fieldTypes[fieldName]
@@ -179,6 +182,7 @@ function expressX(app, options={}) {
179
182
  const values = await service.__findMany(context, {
180
183
  where: query,
181
184
  })
185
+ publish(service, 'findMany', values)
182
186
  res.json(values)
183
187
  } catch(err) {
184
188
  console.log('callErr', err)
@@ -194,6 +198,7 @@ function expressX(app, options={}) {
194
198
  id: parseInt(req.params.id)
195
199
  }
196
200
  })
201
+ publish(service, 'findUnique', value)
197
202
  res.json(value)
198
203
  } catch(err) {
199
204
  console.log('callErr', err)
@@ -210,6 +215,7 @@ function expressX(app, options={}) {
210
215
  },
211
216
  data: req.body,
212
217
  })
218
+ publish(service, 'update', value)
213
219
  res.json(value)
214
220
  } catch(err) {
215
221
  console.log('callErr', err)
@@ -225,6 +231,7 @@ function expressX(app, options={}) {
225
231
  id: parseInt(req.params.id)
226
232
  }
227
233
  })
234
+ publish(service, 'delete', value)
228
235
  res.json(value)
229
236
  } catch(err) {
230
237
  console.log('callErr', err)
@@ -292,23 +299,7 @@ function expressX(app, options={}) {
292
299
  result,
293
300
  })
294
301
  // pub/sub: send event on associated channels
295
- const publishFunc = service.publishCallback
296
- if (publishFunc) {
297
- const channelNames = await publishFunc(result, app)
298
- if (options.debug) console.log('publish channels', name, action, channelNames)
299
- for (const channelName of channelNames) {
300
- if (options.debug) console.log('service-event', name, action, channelName)
301
- const connectionList = Object.values(connections).filter(cnx => cnx.channelNames.has(channelName))
302
- for (const connection of connectionList) {
303
- if (options.debug) console.log('emit to', connection.id, name, action, result)
304
- connection.socket.emit('service-event', {
305
- name,
306
- action,
307
- result,
308
- })
309
- }
310
- }
311
- }
302
+ publish(service, action, result)
312
303
  } catch(err) {
313
304
  console.log('callErr', err)
314
305
  io.emit('client-response', {
@@ -338,6 +329,28 @@ function expressX(app, options={}) {
338
329
  })
339
330
  }
340
331
 
332
+ // publish event on associated channels
333
+ async function publish(service, action, result) {
334
+ console.log('PUB!')
335
+ const publishFunc = service.publishCallback
336
+ if (publishFunc) {
337
+ const channelNames = await publishFunc(result, app)
338
+ if (options.debug) console.log('publish channels', service.name, action, channelNames)
339
+ for (const channelName of channelNames) {
340
+ if (options.debug) console.log('service-event', service.name, action, channelName)
341
+ const connectionList = Object.values(connections).filter(cnx => cnx.channelNames.has(channelName))
342
+ for (const connection of connectionList) {
343
+ if (options.debug) console.log('emit to', connection.id, service.name, action, result)
344
+ connection.socket.emit('service-event', {
345
+ name: service.name,
346
+ action,
347
+ result,
348
+ })
349
+ }
350
+ }
351
+ }
352
+ }
353
+
341
354
  function joinChannel(channelName, connection) {
342
355
  connection.channelNames.add(channelName)
343
356
  }
@@ -19,7 +19,7 @@ describe('ExpressX API', () => {
19
19
 
20
20
  it("can delete all users", async () => {
21
21
  const res = await app.service('User').deleteMany()
22
- assert('chris' === 'chris')
22
+ assert(res.count === 1)
23
23
  })
24
24
 
25
25
  it("can create a user", async () => {