npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@themost/express

v1.7.0

Published

MOST Data ORM Express Middleware

Downloads

281

Readme

npm 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);