Skip to content

MarcosRava/model-prefect

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Model Prefect

A simple model presenter

WIP

Proposal

Instalation


npm install model-prefect

Simple usage

let ford = {
  fullName: "Ford Prefect",
  nickname: "Ix",
  species: "Betelgeusian"
};

const opts = {
  "map": {
    "fullName": "name"
  },
  "exclude" : ["nickname"]
};
let fordPresenter = new ModelPrefect(ford, opts);
// {"name": "Ford Prefect", "species": "Betelgeusian"}

Usage with Betelgeuse

import Betelgeuse, { Types } from 'betelgeuse';
import ModelPrefect from 'model-prefect';

ModelPrefect.goesToBetelgeuse(Betelgeuse); // Add `toPresenter` function to Betelgeuse instances

class Ford extends Betelgeuse {

  static schema = {
    fullName: {
      type: Types.string,
      minLength: 3
    },
    nickname: {
      type: Types.string,
      minLength: 3
    },
    species:  Types.string
  }

  static presenter = {
    "map": {
      "fullName": "name"
    }
    "exclude": ["nickname"]
  }
}

let ford = new Ford({
  fullName: "Ford Prefect",
  nickname: "Ix",
  species: "Betelgeusian"
});

ford.toPresenter();
// {"name": "Ford Prefect", "species": "Betelgeusian"}

Or

class FordPresenter extends ModelPrefect {

  static presenter = {
    "map": {
      "fullName": "name"
    }
    "exclude": ["nickname"]
  }

}

let fordPresenter = new FordPresenter(ford);
// {"name": "Ford Prefect", "species": "Betelgeusian"}

Schema

class FordPresenter extends ModelPrefect {

  static options = {
    "betelgeuse" : true, // convert validation schema
    "presenter": {
      "fullName": "name"
    }
    "exclude": ["nickname"]
  }

}

let fordPresenter = new FordPresenter(ford);
/*
{
  "name": "Ford Prefect",
  "species": "Betelgeusian",
  "_schema": {
    "name": {
      "type": "string",
      "minLength": 3
    },
    "species": {
      "type": "string"
    }
  }
}
*/
fordPresenter.isValid
// Function.isValid
fordPresenter.fields()
// {"name": "Ford Prefect", "species": "Betelgeusian"}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published