@jcbuisson/express-x 1.2.0 → 1.2.2
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 +9 -18
- package/package.json +1 -1
- package/prisma/dev.db +0 -0
- package/src/index.mjs +2 -2
- package/src/server.mjs +11 -8
- package/test/index.test.js +3 -4
package/README.md
CHANGED
|
@@ -33,12 +33,11 @@ backed in a [Prisma](https://www.prisma.io/) database
|
|
|
33
33
|
|
|
34
34
|
```js
|
|
35
35
|
// app.js
|
|
36
|
-
import express from 'express'
|
|
37
36
|
import bodyParser from 'body-parser'
|
|
38
|
-
import
|
|
37
|
+
import { expressXServer } from '@jcbuisson/express-x'
|
|
39
38
|
|
|
40
|
-
// `app` is a regular express application, enhanced with
|
|
41
|
-
const app = expressX(
|
|
39
|
+
// `app` is a regular express application, enhanced with service and real-time features
|
|
40
|
+
const app = expressX()
|
|
42
41
|
|
|
43
42
|
// create two CRUD database services. They provide Prisma methods: `create`, 'createMany', 'find', 'findMany', 'upsert', etc.
|
|
44
43
|
app.createDatabaseService('User')
|
|
@@ -126,22 +125,16 @@ With a few lines of code, we got a complete REST API over the database tables. B
|
|
|
126
125
|
|
|
127
126
|
## Use it with a websocket client
|
|
128
127
|
|
|
129
|
-
First install ExpressX client library:
|
|
130
|
-
|
|
131
|
-
```bash
|
|
132
|
-
npm i @jcbuisson/express-x-client
|
|
133
|
-
```
|
|
134
|
-
|
|
135
128
|
Create the following client NodeJS script:
|
|
136
129
|
|
|
137
130
|
```js
|
|
138
131
|
// client.js
|
|
139
132
|
import io from 'socket.io-client'
|
|
140
|
-
import
|
|
133
|
+
import { expressXClient } from '@jcbuisson/express-x'
|
|
141
134
|
|
|
142
135
|
const socket = io('http://localhost:8000', { transports: ["websocket"] })
|
|
143
136
|
|
|
144
|
-
const app =
|
|
137
|
+
const app = expressXClient(socket)
|
|
145
138
|
|
|
146
139
|
async function main() {
|
|
147
140
|
const user = await app.service('User').create({
|
|
@@ -180,7 +173,7 @@ node client.js
|
|
|
180
173
|
```
|
|
181
174
|
|
|
182
175
|
It prints the following lines in the console:
|
|
183
|
-
```
|
|
176
|
+
```json
|
|
184
177
|
joe {
|
|
185
178
|
id: 11,
|
|
186
179
|
name: 'Joe',
|
|
@@ -218,12 +211,11 @@ and then broacasted to all connected clients, leading to real-time updates.
|
|
|
218
211
|
|
|
219
212
|
```js
|
|
220
213
|
// app.js
|
|
221
|
-
import
|
|
222
|
-
import expressX from '@jcbuisson/express-x'
|
|
214
|
+
import { expressXServer } from '@jcbuisson/express-x'
|
|
223
215
|
import { PrismaClient } from '@prisma/client'
|
|
224
216
|
|
|
225
|
-
// `app` is a regular express application, enhanced with
|
|
226
|
-
const app = expressX(
|
|
217
|
+
// `app` is a regular express application, enhanced with service and real-time features
|
|
218
|
+
const app = expressX()
|
|
227
219
|
|
|
228
220
|
// configure prisma client from schema
|
|
229
221
|
app.set('prisma', new PrismaClient())
|
|
@@ -252,7 +244,6 @@ app.server.listen(8000, () => console.log(`App listening at http://localhost:800
|
|
|
252
244
|
Here is how a client may listen to channel events:
|
|
253
245
|
|
|
254
246
|
```js
|
|
255
|
-
// client.js
|
|
256
247
|
...
|
|
257
248
|
app.service('Post').on('create', post => {
|
|
258
249
|
console.log('post event created', post)
|
package/package.json
CHANGED
package/prisma/dev.db
CHANGED
|
Binary file
|
package/src/index.mjs
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
|
|
2
|
-
import {
|
|
2
|
+
import { expressX } from './server.mjs'
|
|
3
3
|
import { expressXClient } from './client.mjs'
|
|
4
4
|
import { hashPassword, protect, setSessionJWT, } from './common-hooks.mjs'
|
|
5
5
|
|
|
6
6
|
export {
|
|
7
|
-
|
|
7
|
+
expressX,
|
|
8
8
|
expressXClient,
|
|
9
9
|
|
|
10
10
|
hashPassword,
|
package/src/server.mjs
CHANGED
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
|
|
2
2
|
import http from 'http'
|
|
3
3
|
import { Server } from "socket.io"
|
|
4
|
+
import express from 'express'
|
|
4
5
|
import { PrismaClient } from '@prisma/client'
|
|
5
6
|
|
|
6
7
|
/*
|
|
7
|
-
* Enhance `app` express application with
|
|
8
|
+
* Enhance `app` express application with services and real-time features
|
|
8
9
|
*/
|
|
9
|
-
export function
|
|
10
|
+
export function expressX(options = {}) {
|
|
10
11
|
|
|
11
|
-
|
|
12
|
+
const app = express()
|
|
13
|
+
|
|
14
|
+
if (options.debug === undefined) options.debug = true
|
|
12
15
|
if (options.ws === undefined) options.ws = { ws_prefix: "expressx" }
|
|
13
16
|
|
|
14
17
|
const services = {}
|
|
@@ -144,7 +147,7 @@ export function expressXServer(app, options={}) {
|
|
|
144
147
|
|
|
145
148
|
|
|
146
149
|
app.post(path, async (req, res) => {
|
|
147
|
-
if (options.debug) console.log("http request POST", req)
|
|
150
|
+
if (options.debug) console.log("http request POST", req.url)
|
|
148
151
|
context.http.req = req
|
|
149
152
|
try {
|
|
150
153
|
const value = await service.__create(context, { data: req.body })
|
|
@@ -157,7 +160,7 @@ export function expressXServer(app, options={}) {
|
|
|
157
160
|
})
|
|
158
161
|
|
|
159
162
|
app.get(path, async (req, res) => {
|
|
160
|
-
if (options.debug) console.log("http request GET", req)
|
|
163
|
+
if (options.debug) console.log("http request GET", req.url)
|
|
161
164
|
context.http.req = req
|
|
162
165
|
const query = { ...req.query }
|
|
163
166
|
try {
|
|
@@ -193,7 +196,7 @@ export function expressXServer(app, options={}) {
|
|
|
193
196
|
})
|
|
194
197
|
|
|
195
198
|
app.get(`${path}/:id`, async (req, res) => {
|
|
196
|
-
if (options.debug) console.log("http request GET", req)
|
|
199
|
+
if (options.debug) console.log("http request GET", req.url)
|
|
197
200
|
context.http.req = req
|
|
198
201
|
try {
|
|
199
202
|
const value = await service.__findUnique(context, {
|
|
@@ -210,7 +213,7 @@ export function expressXServer(app, options={}) {
|
|
|
210
213
|
})
|
|
211
214
|
|
|
212
215
|
app.patch(`${path}/:id`, async (req, res) => {
|
|
213
|
-
if (options.debug) console.log("http request PATCH", req)
|
|
216
|
+
if (options.debug) console.log("http request PATCH", req.url)
|
|
214
217
|
context.http.req = req
|
|
215
218
|
try {
|
|
216
219
|
const value = await service.__update(context, {
|
|
@@ -228,7 +231,7 @@ export function expressXServer(app, options={}) {
|
|
|
228
231
|
})
|
|
229
232
|
|
|
230
233
|
app.delete(`${path}/:id`, async (req, res) => {
|
|
231
|
-
if (options.debug) console.log("http request DELETE", req)
|
|
234
|
+
if (options.debug) console.log("http request DELETE", req.url)
|
|
232
235
|
context.http.req = req
|
|
233
236
|
try {
|
|
234
237
|
const value = await service.__delete(context, {
|
package/test/index.test.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
|
|
2
|
-
import express from 'express'
|
|
3
2
|
import bodyParser from 'body-parser'
|
|
4
3
|
import axios from 'axios'
|
|
5
4
|
import io from 'socket.io-client'
|
|
@@ -7,11 +6,11 @@ import io from 'socket.io-client'
|
|
|
7
6
|
|
|
8
7
|
import { assert } from 'chai'
|
|
9
8
|
|
|
10
|
-
import {
|
|
9
|
+
import { expressX, expressXClient } from '../src/index.mjs'
|
|
11
10
|
|
|
12
11
|
|
|
13
|
-
// `app` is a regular express application, enhanced with
|
|
14
|
-
const app =
|
|
12
|
+
// `app` is a regular express application, enhanced with services and real-time features
|
|
13
|
+
const app = expressX()
|
|
15
14
|
|
|
16
15
|
app.createDatabaseService('User')
|
|
17
16
|
app.createDatabaseService('Post')
|