1.7.0 • Published 1 month ago

@themost/express v1.7.0

Weekly downloads
2
License
BSD-3-Clause
Repository
github
Last release
1 month ago

npm npm.io Codacy Badge GitHub top language License GitHub last commit GitHub Release Date npm Snyk Vulnerabilities for npm package

@themost/express

MOST Data ORM Extension for ExpressJS

Installation

npm i @themost/express

Generate new project

Install @themost/cli globally

npm i @themost/cli -g

Generate a new project by executing the following command:

themost new project my-app  --template express

Go to project's directory:

cd my-app

Install dependencies:

npm i

and serve the new application by executing:

npm run serve

Open your browser and navigate to http://127.0.0.1:3000

Usage

Use @themost/data application as an express middleware:

import express from 'express';
import path from 'path';
import {ExpressDataApplication, serviceRouter, dateReviver} from '@themost/express';
let app = express();
// data application setup
let dataApplication = new ExpressDataApplication(path.resolve(__dirname, 'config'));
// use @themost/express dateReviver helper function for parsing dates
app.use(express.json({
  reviver: dateReviver 
}));
// use data application middleware
app.use(dataApplication.middleware(app));

Use the service router for serving all the available data models:

var serviceRouter = require('@themost/express').serviceRouter;
app.use('/api', passport.authenticate('bearer', { session: false }), serviceRouter);

or use the traditional way of serving data:

var peopleRouter = require('./routes/people');
app.use('/people', peopleRouter);

// # people.js
var express = require('express');
var router = express.Router();

/* GET /people get persons listing. */
router.get('/', function(req, res, next) {
  req.context.model('Person').filter(req.query).then(function(q) {
      return q.getList().then(function(result) {
          return res.json(result);
      });
  }).catch(function(err) {
      return next(err);
  });
});

/* POST /people insert or update a person or an array of persons. */
router.post('/', function(req, res, next) {
  if (typeof req.body === 'undefined') {
    return res.status(400).send();
  }
  req.context.model('Person').save(req.body).then(function() {
    return res.json(req.body);
  }).catch(function(err) {
      return next(err);
  });
});

/* GET /person/:id get a person by id. */
router.get('/:id', function(req, res, next) {
  req.context.model('Person').where('id').equal(req.params.id).getItem().then(function(value) {
    if (typeof value === 'undefined') {
      return res.status(204).send();
    }
      return res.json(value);
  }).catch(function(err) {
      return next(err);
  });
});

/* DELETE /person/:id delete a person by id. */
router.delete('/:id', function(req, res, next) {
  req.context.model('Person').where('id').equal(req.params.id).count().then(function(value) {
    if (value === 0) {
      return res.status(404).send();
    }
    // construct a native object
    var obj = {
      "id": req.params.id
    };
    //try to delete
    return req.context.model('Person').remove(obj).then(function() {
      return res.json(obj);
    });
  }).catch(function(err) {
      return next(err);
  });
});

Extend application container

Use ExpressDataApplication#container to access and extend parent application. The following example represents an application service which extends container application router

# MyApplicationService.js

export class MyApplicationService extends ApplicationService {
    constructor(app) {
        super(app);
        // subscribe for container
        app.container.subscribe( container => {
            if (container) {
                // create a router
                const newRouter = express.Router();
                newRouter.get('/message', (req, res) => {
                    return res.json({
                        message: 'Hello World'
                    });
                });
                newRouter.get('/status', (req, res) => {
                    return res.json({
                        status: 'ok'
                    });
                });
                // use router
                container.use('/a', newRouter);
            }
        });
    }
}


# app.js
import {MyApplicationService} from './MyApplicationService';
...
// use data application middleware
app.use(dataApplication.middleware(app));
// add application service
dataApplication.useService(MyApplicationService);

Extend service router

@themost/express#serviceRouter router may be extended to include extra service endpoints:

# ServiceRouterExtension.js

class ServiceRouterExtension extends ApplicationService {
constructor(app) {
        super(app);
        app.serviceRouter.subscribe( serviceRouter => {
            // create new router
            const addRouter = express.Router();
            addRouter.get('/users/me/status', (req, res) => {
                return res.json({
                    status: 'ok'
                });
            });
            // insert router at the beginning of serviceRouter.stack
            serviceRouter.stack.unshift.apply(serviceRouter.stack, addRouter.stack);
        });
    }
}

# app.js

const app = express();
// create a new instance of data application
const application = new ExpressDataApplication(path.resolve(__dirname, 'test/config'));
// use extension
application.useService(ServiceRouterExtension);
app.use(express.json({
    reviver: dateReviver
}));
// hold data application
app.set('ExpressDataApplication', application);
// use data middleware (register req.context)
app.use(application.middleware(app));
...
// user service router
app.use('/api/', passport.authenticate('bearer', { session: false }), serviceRouter);
1.7.0

1 month ago

1.6.2

8 months ago

1.6.1

11 months ago

1.6.0

1 year ago

1.5.24

1 year ago

1.5.23

2 years ago

1.5.22

2 years ago

1.5.20

2 years ago

1.5.19

2 years ago

1.5.18

3 years ago

1.5.16

3 years ago

1.5.15

3 years ago

1.5.14

3 years ago

1.5.14-next.1

3 years ago

1.5.14-next.0

3 years ago

1.5.13

4 years ago

1.5.12

4 years ago

1.5.11

4 years ago

1.5.10

4 years ago

1.5.10-next.2

4 years ago

1.5.10-next.1

4 years ago

1.5.10-next.0

4 years ago

1.5.8

4 years ago

1.5.7-next.2

4 years ago

1.5.7-next.1

4 years ago

1.5.7

4 years ago

1.5.6

4 years ago

1.5.6-dev.1

4 years ago

1.5.5

4 years ago

1.5.4

4 years ago

1.5.4-dev.1

4 years ago

1.5.3

4 years ago

1.5.2

4 years ago

1.5.1

4 years ago

1.5.0

4 years ago

1.4.6

4 years ago

1.4.5

4 years ago

1.4.4

4 years ago

1.4.3

5 years ago

1.4.3-dev.1

5 years ago

1.4.2

5 years ago

1.4.1

5 years ago

1.3.9

5 years ago

1.3.8

5 years ago

1.3.7

5 years ago

1.3.7-dev.1

5 years ago

1.3.6

5 years ago

1.3.5

5 years ago

1.3.3

5 years ago

1.3.2

5 years ago

1.3.1

5 years ago

1.1.0

5 years ago

1.0.8

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago