2.1.15 • Published 1 month ago

@jcbuisson/express-x v2.1.15

Weekly downloads
-
License
MIT
Repository
github
Last release
1 month ago

Getting started

Create a project

Let's create a new folder for our application:

mkdir expressx-project
cd expressx-project

Since any ExpressX application is a Node application, we can create a default package.json using npm:

npm init es6 --yes

The es6 argument adds "type": "module" in package.json. Beware! All further module imports must made with es6/esm import syntax.

Install ExpressX

npm install @jcbuisson/express-x

Our first server application

Now we can create an ExpressX application which will provide a complete REST API on a user resource backed in a Prisma database

// app.js
import { expressXServer } from '@jcbuisson/express-x'

// `app` is a regular express application, enhanced with service and real-time features
const app = expressX()

// configure prisma client from schema
const prisma = new PrismaClient()

// create two CRUD database services. They provide Prisma methods: `create`, 'createMany', 'find', 'findMany', 'upsert', etc.
app.createService('User', prisma.User)
app.createService('Post', prisma.Post)

app.server.listen(8000, () => console.log(`App listening at http://localhost:8000`))

Before running it we need to setup the corresponding database.

Create the database

Presently, ExpressX only handles Prisma. Prisma is able to connect to most brands of relational or NoSQL databases.

First, provide the database schema in prisma/schema.prisma:

generator client {
   provider = "prisma-client-js"
}

model User {
   id          Int       @default(autoincrement()) @id
   name        String
   posts       Post[]
}

model Post {
   id          Int       @default(autoincrement()) @id
   text        String
   author      User      @relation(fields: [authorId], references: [id], onDelete: Cascade)
   authorId    Int
}

datasource db {
   provider = "sqlite"
   url      = "file:./dev.db"
}

Then create the database:

npx prisma db push

The sqlite database file is created at prisma/dev.db

Run the application on the server side

node app.js

Use it with a websocket client

Create the following client NodeJS script:

// client.js
import io from 'socket.io-client'
import { expressXClient } from '@jcbuisson/express-x'

const socket = io('http://localhost:8000', { transports: ["websocket"] })

const app = expressXClient(socket)

async function main() {
   const user = await app.service('User').create({
      name: "Joe",
   })
   await app.service('Post').create({
      authorId: user.id,
      text: "Post#1"
   })
   await app.service('Post').create({
      authorId: user.id,
      text: "Post#2"
   })
   const joe = await app.service('User').findUnique({
      where: {
         id: user.id,
      },
      include: {
         posts: true,
      },
   })
   console.log('joe', joe)
   process.exit(0)
}
main()

For simplicity we use a node client, but you would write something similar with your favorite front-end framework.

You can use the exact same statements on services on the client side as you would on the server side, such as: app.service('User').create(...). Of course the app object here on the client is quite different that the app object on the server; you can find explanations here.

Now run the client script:

node client.js

It prints the following lines in the console:

joe {
  id: 11,
  name: 'Joe',
  posts: [
    { id: 12, text: 'Post#1', authorId: 11 },
    { id: 13, text: 'Post#2', authorId: 11 }
  ]
}

We have a GraphQL-like experience with the nested posts, thanks to Prisma and its use through ExpressX services.

::: info We could have removed from app.js all lines related to HTTP, since we are only using the websocket transport. :::

Real-time applications

When websocket transport is used (default situation) and when a connected client calls a service method, two twings happen on method completion:

  • the resulting value is sent to the client
  • an event is emitted, and sent to connected clients we'll call subscribers. The calling client may or not be one of those subscribers.

For example in a medical application, whenever a patients's record is modified, an event could be sent to all his/her caregivers.

Channels are used for this pub/sub mechanism. Service methods publish events on channels, and clients subscribe to channels in order to receive those events. ExpressX provides functions to configure which events are published to which channels. A channel is represented by a name and you can create and use as many channels as you need.

In the following example, every time a client connects to the server, it joins (= is subscribed to) the 'anonymous' channel. And whenever an event is emited by the post or user service, this event is published on this channel, and then broacasted to all connected clients, leading to real-time updates.

// app.js
import { expressXServer } from '@jcbuisson/express-x'
import { PrismaClient } from '@prisma/client'

// `app` is a regular express application, enhanced with service and real-time features
const app = expressX()

// configure prisma client from schema
const prisma = new PrismaClient()

// create two CRUD database services. They provide Prisma methods: `create`, 'createMany', 'find', 'findMany', 'upsert', etc.
app.createService('User', prisma.User)
app.createService('Post', prisma.Post)

// publish
app.service('User').publish(async (post, context) => {
   return ['anonymous']
})
app.service('Post').publish(async (post, context) => {
   return ['anonymous']
})

// subscribe
app.on('connection', (socket) => {
   console.log('connection', socket.id)
   app.joinChannel('anonymous', socket)
})

app.server.listen(8000, () => console.log(`App listening at http://localhost:8000`))

Here is how a client may listen to channel events:

...
app.service('Post').on('create', post => {
   console.log('post event created', post)
})

The listener is triggered whenever the client receives from the server a create event from the service post. This event results from the completion on the server of a call app.service('Post').create()

2.1.14

1 month ago

2.1.15

1 month ago

2.1.12

1 month ago

2.1.13

1 month ago

2.1.9

1 month ago

2.1.10

1 month ago

2.1.11

1 month ago

2.1.6

1 month ago

2.1.8

1 month ago

2.1.7

1 month ago

2.1.5

1 month ago

2.1.4

1 month ago

2.1.2

1 month ago

2.1.1

2 months ago

2.1.0

2 months ago

2.0.3

2 months ago

2.0.2

2 months ago

2.0.1

3 months ago

2.0.0

3 months ago

1.8.4

4 months ago

1.6.4

9 months ago

1.6.3

9 months ago

1.6.2

9 months ago

1.6.1

9 months ago

1.6.0

9 months ago

1.7.10

7 months ago

1.7.11

7 months ago

1.7.12

6 months ago

1.7.13

6 months ago

1.7.9

7 months ago

1.7.8

7 months ago

1.7.7

7 months ago

1.7.6

7 months ago

1.7.5

8 months ago

1.7.4

8 months ago

1.5.5

10 months ago

1.5.4

10 months ago

1.5.3

10 months ago

1.5.2

10 months ago

1.5.1

10 months ago

1.5.0

10 months ago

1.6.9

8 months ago

1.6.8

9 months ago

1.6.7

9 months ago

1.6.6

9 months ago

1.6.5

9 months ago

1.8.2

6 months ago

1.8.1

6 months ago

1.8.0

6 months ago

1.4.4

10 months ago

1.4.3

10 months ago

1.4.2

10 months ago

1.4.1

10 months ago

1.5.30

10 months ago

1.5.32

10 months ago

1.5.31

10 months ago

1.5.34

10 months ago

1.5.33

10 months ago

1.5.36

9 months ago

1.5.35

9 months ago

1.6.11

8 months ago

1.5.9

10 months ago

1.6.10

8 months ago

1.5.8

10 months ago

1.6.13

8 months ago

1.5.7

10 months ago

1.6.12

8 months ago

1.5.6

10 months ago

1.6.15

8 months ago

1.6.14

8 months ago

1.6.17

8 months ago

1.6.16

8 months ago

1.7.3

8 months ago

1.7.2

8 months ago

1.7.1

8 months ago

1.7.0

8 months ago

1.5.10

10 months ago

1.5.12

10 months ago

1.5.11

10 months ago

1.5.14

10 months ago

1.5.13

10 months ago

1.5.16

10 months ago

1.5.15

10 months ago

1.5.18

10 months ago

1.5.17

10 months ago

1.5.10-a

10 months ago

1.5.19

10 months ago

1.5.21

10 months ago

1.5.23

10 months ago

1.8.3

6 months ago

1.5.22

10 months ago

1.5.25

10 months ago

1.5.24

10 months ago

1.5.27

10 months ago

1.5.26

10 months ago

1.5.29

10 months ago

1.5.28

10 months ago

1.4.0

11 months ago

1.3.7

11 months ago

1.3.6

11 months ago

1.3.5

11 months ago

1.2.0

11 months ago

1.0.19

1 year ago

1.1.0

1 year ago

1.0.18

1 year ago

1.0.17

1 year ago

1.3.4

11 months ago

1.3.3

11 months ago

1.3.2

11 months ago

1.1.4

11 months ago

1.3.1

11 months ago

1.2.2

11 months ago

1.1.3

11 months ago

1.3.0

11 months ago

1.2.1

11 months ago

1.1.2

12 months ago

1.0.22

1 year ago

1.0.21

1 year ago

1.0.20

1 year ago

1.0.16

1 year ago

1.0.15

1 year ago

1.0.14

1 year ago

1.0.13

1 year ago

1.0.12

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago