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

marshaller

v0.1.1

Published

[![Build Status](https://travis-ci.com/marcj/marshaller.svg?branch=master)](https://travis-ci.com/marcj/marshaller) [![npm version](https://badge.fury.io/js/marshaller.svg)](https://badge.fury.io/js/marshaller)

Downloads

10

Readme

Marshaller

Build Status npm version

Marshaller is a JSON/HTTP-Request serialiser and MongoDB entity manager for TypeScript and has been built to make data transportation between HTTP, Node and MongoDB super easy.

Diagram

Install

npm install marshaller

Example Entity

import {
    ClassArray,
    ClassMap,
    DateType,
    Entity,
    ID,
    UUIDType,
    NumberType,
    plainToClass,
    StringType,
    uuid,
} from 'marshaller';


@Entity('sub')
class SubModel {
    @StringType()
    label: string;
}

@Entity('SimpleModel')
class SimpleModel {
    @ID()
    @UUIDType()
    id: string = uuid();

    @StringType()
    name: string;

    @NumberType()
    type: number = 0;

    @DateType()
    created: Date = new Date;

    @ClassArray(SubModel)
    children: SubModel[] = [];

    @ClassMap(SubModel)
    childrenMap: {[key: string]: SubModel} = {};

    constructor(name: string) {
        this.name = name;
    }
}

const instance = plainToClass(SimpleModel, {
    id: 'f2ee05ad-ca77-49ea-a571-8f0119e03038',
    name: 'myName',
    created: 'Sat Oct 13 2018 14:17:35 GMT+0200',
    children: [{label: 'foo'}],
    childrenMap: {'foo': {label: 'foo'}},
});
console.log(instance);
/*
    SimpleModel {
      id: 'my-super-id',
      name: 'myName',
      type: 0,
      plan: 0,
      created: 2018-10-13T17:02:34.456Z,
      children: [ SubModel { label: 'foo' } ],
      childrenMap: { foo: SubModel { label: 'bar' } }
    }
*/

Types

ID

@ID() allows you to define an unique index to your entity. In MongoDB we store it automatically using Mongo's ObjectID. In JSON and JavaScript you work with it using the string type.

UUID

@UUID() stores a UUID. In TypeScript and JSON it's string, and in MongoDB we store it automatically using Mongo's UUID.

Data types:

| Plain | Class | Mongo | |:-------|:-------|:-------| | string | string | UUID() |

String

@String() makes sure the property has always a string type.

Data types:

| JSON | Class | Mongo | |:-------|:-------|:-------| | string | string | string |

Number

@Number() makes sure the property has always a number type.

Data types:

| JSON | Class | Mongo | |:-------|:-------|:-------| | number | number | number |

Date

@Date() makes sure the property has always a date type. In JSON transport (using classToPlain, or mongoToPlain) we use strings.

Data types:

| JSON | Class | Mongo | |:-------|:------|:------| | string | Date | Date |

Enum

@Enum() makes sure the property has always a valid enum value. In JSON transport (using classToPlain, or mongoToPlain) we use strings.

Data types:

| JSON | Class | Mongo | |:-------|:------|:-------| | String | Enum | String |

Class

@Class(ClassDefinition) makes sure you have in Javascript (plainToClass, or mongoToClass) always an instance of ClassDefinition. In JSON and MongoDB it is stored as plain object.

Data types:

| JSON | Class | Mongo | |:-------|:------|:-------| | object | class | object |

ClassArray

@ClassArray(ClassDefinition) makes sure you have in Javascript (plainToClass, or mongoToClass) always an instance of Array<ClassDefinition>. In JSON and MongoDB it is stored as plain array.

Data types:

| JSON | Class | Mongo | |:------|:------|:------| | array | array | array |

ClassMap

@ClassMap(ClassDefinition) makes sure you have in Javascript (plainToClass, or mongoToClass) always an instance of {[key: string]: ClassDefinition}. In JSON and MongoDB it is stored as plain object.

Data types:

| JSON | Class | Mongo | |:-------|:-------|:-------| | object | object | object |

Database

TypeMapper's MongoDB database abstraction makes it super easy to retrieve and store data from and into your MongoDB. We make sure the data from your JSON or class instance is correctly converted to MongoDB specific types.

Example:

import {MongoClient} from "mongodb";
import {Database, plainToClass} from "marshaller";

const connection = await MongoClient.connect(
    'mongodb://localhost:27017', 
    {useNewUrlParser: true}
);
await connection.db('testing').dropDatabase();
const database = new Database(connection, 'testing');

const instance: SimpleModel = plainToClass(SimpleModel, {
    id: 'my-super-id',
    name: 'myName',
});

await database.save(SimpleModel, instance);

const list: SimpleModel[] = await database.find(SimpleModel);
const oneItem: SimpleModel = await database.get(
    SimpleModel,
    {id: 'my-super-id'}
    );

NestJS / Express

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

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

import {SimpleModel} from "./tests/entities";
import {plainToClass, Database, classToPlain} from "marshaller";
import {MongoClient} from "mongodb";

@Controller()
class MyController {
    
    private database: Database;
    
    private async getDatabase() {
        if (!this.database) {
            const connection = await MongoClient.connect(
                'mongodb://localhost:27017',
                {useNewUrlParser: true}
            );
            await connection.db('testing').dropDatabase();
            this.database = new Database(connection, 'testing');
        }
        
        return this.database;
    }
    
    @Post('/save')
    async save(
        @Body() body,
    ) {
        const instance: SimpleModel = plainToClass(SimpleModel, body);
        const versionNumber = await this.database.save(SimpleModel, instance);
        
        return instance.id;
    }
    
    @Get('/get/:id')
    async get(@Param('id') id: string) {
        const instance = await this.database.get(SimpleModel, {id: id});
        
        return classToPlain(SimpleModel, instance);
    }
}