# @marcj/marshal-nest

> A plugin for NestJS to convert HTTP Body to your class instance with validation using Marshal

Latest version **2.1.13** (published 2020-06-12) · 0 weekly downloads

## Install

```sh
npm install @marcj/marshal-nest
pnpm add @marcj/marshal-nest
yarn add @marcj/marshal-nest
bun add @marcj/marshal-nest
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.13 |
| Published | 2020-06-12 |
| First published | 2018-11-09 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 6.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | marcj |

## Links

- npm: https://www.npmjs.com/package/@marcj/marshal-nest
- npm.io page: https://npm.io/package/@marcj/marshal-nest

## Recent versions

- 2.1.13 (latest) — 2020-06-12
- 2.1.12 — 2020-06-11
- 2.1.11 — 2020-06-11
- 2.1.10 — 2020-06-11
- 2.1.9 — 2020-06-11
- 2.1.8 — 2020-06-11
- 2.1.7 — 2020-06-09
- 2.1.6 — 2020-05-27
- 2.1.5 — 2020-05-26
- 2.1.4 — 2020-05-21
- 2.1.3 — 2020-02-24
- 2.1.2 — 2020-02-05
- 2.1.1 — 2020-02-05
- 2.1.0 — 2020-02-03
- 2.0.0 — 2020-02-03
- … 50 more at https://npm.io/package/@marcj/marshal-nest/versions

## README

# NestJS / Express for Marshal

[![npm version](https://badge.fury.io/js/%40marcj%2Fmarshal-nest.svg)](https://badge.fury.io/js/%40marcj%2Fmarshal-nest)

It's super common to accept data from a frontend via HTTP, transform the
body into your class instance, work with it, and then store that data in
your MongoDB or somewhere else. With Marshal this scenario is super
simple and you do not need any manual transformations.

```
npm install @marcj/marshal @marcj/marshal-mongo @marcj/marshal-nest
```

```typescript
import {
    Controller, Get, Param, Post, Body
} from '@nestjs/common';

import {SimpleModel} from "@marcj/marshal/tests/entities";
import {plainToClass, Database, classToPlain} from "@marcj/marshal";
import {ValidationPipe} from "@marcj/marshal-nest";
import {createConnection} from "typeorm";

@Controller()
class MyController {
    
    private database: Database;
    
    private async getDatabase() {
        if (!this.database) {
            const connection = await createConnection({
                type: "mongodb",
                host: "localhost",
                port: 27017,
                database: "testing",
                useNewUrlParser: true,
            });
            this.database = new Database(connection, 'testing');
        }
        
        return this.database;
    }
    
    @Post('/save')
    async save(
        @Body(ValidationPipe({transform: true})) body: SimpleModel,
    ) {
        body instanceof SimpleModel; // true;

        await (await this.getDatabase()).save(SimpleModel, body);
        
        return body.id;
    }
    
    @Get('/get/:id')
    async get(@Param('id') id: string) {
        const instance = await (await this.getDatabase()).get(SimpleModel, {_id: id});

        return classToPlain(SimpleModel, instance);
    }
}

````

---
_Source: https://npm.io/package/@marcj/marshal-nest · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
