Skip to content

decoverto/decoverto

Repository files navigation

npm version Build Status Code coverage License

Decoverto

Convert data into instances of first and third party classes and back using decorators. Enjoy not having to write data-to-class setup code.

Test and tinker on the Decoverto playground

Features

A model looks like this:

import {model, map, MapShape, property} from 'decoverto';

@model()
class User {

    @property()
    createdAt: Date;

    @property()
    givenName: string;

    @property(() => String)
    referralToken: string | null;

    @property(map(() => String, () => Boolean, {shape: MapShape.Object}))
    permissions: Map<string, boolean>;
}

Quickstart

  1. Install decoverto

    yarn add decoverto

    or

    npm install decoverto
  2. When using TypeScript, enable experimentalDecorators in tsconfig.json

  3. Define a model

    import {model, map, MapShape, property} from 'decoverto';
    
    @model()
    class User {
    
        @property(() => Date)
        createdAt: Date;
    
        @property(() => String)
        givenName: string;
    
        @property(() => String)
        referralToken: string | null;
    
        @property(map(() => String, () => Boolean, {shape: MapShape.Object}))
        permissions: Map<string, boolean>;
    }
  4. Create a Decoverto instance const decoverto = new Decoverto()

  5. Convert some data

    // Convert raw data using the default JSON parser
    user = decoverto.type(User).rawToInstance(`{
        "createdAt": "2021-03-31T14:08:42.009Z",
        "givenName": "Mark",
        "referralToken": null,
        "permissions": {
            "canManageUsers": true,
            "canCreateProducts": true
        }
    }`);
    
    // Convert an object literal
    user = decoverto.type(User).plainToInstance({
        createdAt: new Date(),
        givenName: 'Mark',
        referralToken: null,
        permissions: {
            canManageUsers: true,
            canCreateProducts: true,
        }
    });

For more information, see links in the features list or the docs directory.

Examples/Playground

Decoverto has playgrounds where you can view examples and tinker with them. It allows you to see what is possible and experiment.