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

@nerisma/express-extended

v1.0.10

Published

[![npm version](https://badge.fury.io/js/%40nerisma%2Fexpress-extended.svg)](https://badge.fury.io/js/%40nerisma%2Fexpress-extended) [![Build Status](https://travis-ci.com/Nerisma/express-extended.svg?branch=main)](https://travis-ci.com/Nerisma/express-e

Downloads

13

Readme

@nerisma/express-extended

npm version Build Status Coverage Status

This library provides extended functionality for creating an Express API in TypeScript.

It just extends express and integrate typeorm to create a simple API. It also provides some decorators to define routes and HTTP methods in controllers and generic CRUD controllers / service with minimal boilerplate code and within respect of the RESTful API conventions.

Installation

npm install @nerisma/express-extended

Features

  • Use it like express: Extends express.Application interface to provide additional functionality.
  • TypeORM Integration: Easily integrate TypeORM to create a database connection.
  • Generic CRUD: Create CRUD controllers / services with minimal boilerplate code.
  • Decorators: Use decorators to define routes and HTTP methods in controllers.
  • Dependency Injection: Use the @Dependency decorator to inject services into controllers.

How to use it

This is a simple example of how to create a working CRUD API for a Car entity.

1. Create an entity

The entity is a representation of the Car definition in the database.

// Car.entity.ts
@Entity()
export class Car extends MetadataEntity {
    
    @Column()
    model!: string;

    @Column()
    wheels!: number;

    @Column({type: 'timestamptz'})
    releaseDate!: Date;

}

NOTE: If you want to use the provided generic CRUD controllers, you need to extend the MetadataEntity class. It will add the id column and metadata columns like createdAt and updatedAt.

2. Create a service

The service is a class that will handle the database operations for the Car entity.

// Car.service.ts
@Dependency() // This allow the service to be injected in the controller
export class CarService extends CrudService<Car> {
    
    // This will be automatically injected
    constructor(dataSource: DataSource) {
        super(dataSource, Car);
    }
    
}

NOTE: You must provide the entity class to the CrudService constructor.

3. Create a controller

The controller is a class that will handle the HTTP requests for the Car entity.

// Car.controller.ts
@Dependency() // This allow the controller to be injected in the server
export class CarController extends CrudController<Car> {
    
    // This will be automatically injected
    constructor(service: CarService) {
        super(service);
    }
    
}

4. Create the server

Just do it as you would do with express, but use expressExtended instead of express to have access to the extended functionalities.

// server.ts
async function server() {
    // Normal express setup
    const app = expressExtended();
    
    // Setup the database connection 
    await app.useDataSource({
        type: 'sqlite',
        database: ':memory:',
        entities: [__dirname + '/**/*.entity{.ts,.js}'],
        synchronize: true,
    });
    
    // Use the controllers
    app.useControllers([CarController]);
    
    const server = app.listen(3000, async () => {
        console.log('Server is running on port 3000');
    });
}

server().catch(console.error);

And... that's it ! 🎉 You now have a running API at http://localhost:3000/cars with all CRUD operations pointing at a sqlite in-memory database.

NOTE: You can find a more complete example here that you can run by using the command npm run example.

Provide your own database connection

Notice how the useDataSource method is called without the database connection options. By default, it will use the sqlite in-memory database. But you can provide your own connection options to the method and the DataSourceOptions interface.

await app.useDataSource({
    type: 'postgres',
    host: 'localhost',
    port: 5432,
    entities: [__dirname + '/**/*.entity{.ts,.js}'],
});