Options
All
  • Public
  • Public/Protected
  • All
Menu

Parseus

Parseus is a javascript library written in Typescript which allow marshall/unmarshall JSON into class instance. This library is able to run in NodeJS, Typescript or any JS platform. API Docs

Usage Javascript (ESNEXT)

class Person {
  @Field({ type: 'string', name: 'person_name' })
  name

  @Field({ readOnly: true, name: 'person_age', type: 'number' })
  age

  @Field({ name: 'person_last_name', type: 'string' })
  lastName

  @Field({ name: 'person_gender', default: 'M', type: 'string' })
  gender

  @Field({ name: 'person_created_at', type: 'date' })
  createdAt
}

const data = {
  person_name: 'Jhon',
  person_last_name: 'Smith',
  person_age: 25,
  person_created_at: '2018-01-01T12:00:00.000Z'
}
/**
 *  Returns an instance of Person like
 * {
 *   name:'Jhon',
 *   lastName: 'Smith',
 *   age: 25,
 *   gender: 'M',
 *   createdAt: Mon Jan 01 2018 08:00:00 GMT-0400 (Atlantic Standard Time) {}
 * }
 **/

const person = Parseus.decode(data).to(Person)
person.gender = 'F'
person.age = 18
person.name = 'Sara'

/**
 * The second parameter is optional, but if
 * the class instance has been mutated
 * we should pass the original class for references
 *
 * Returns an object with the next structure { [key:string]: any }
 * {
 *   "person_name": "Sara",
 *   "person_last_name": "Smith",
 *   "person_age": 18,
 *   "person_gender": F,
 *   "person_created_at": "2018-01-01T12:00:00.000Z"
 * }
 **/
const personMarshalled = Parseus.encode(person, Person)

Usage Typescript

class Person {
  @Field({ type: 'string', name: 'person_name' })
  name?: string

  @Field({ readOnly: true, name: 'person_age' })
  age?: number

  @Field({ name: 'person_last_name' })
  lastName?: string

  @Field({ name: 'person_gender', default: 'M' })
  gender?: string

  @Field({ name: 'person_created_at', type: 'date' })
  createdAt?: Date
}

const data = {
  person_name: 'Jhon',
  person_last_name: 'Smith',
  person_age: 25,
  person_created_at: '2018-01-01T12:00:00.000Z'
}
/**
 *  Returns an instance of Person like
 * {
 *   name:'Jhon',
 *   lastName: 'Smith',
 *   age: 25,
 *   gender: 'M',
 *   createdAt: Mon Jan 01 2018 08:00:00 GMT-0400 (Atlantic Standard Time) {}
 * }
 **/

const person = Parseus.decode(data).to(Person)
person.gender = 'F'
person.age = 18
person.name = 'Sara'

/**
 * The second parameter is optional, but if
 * the class instance has been mutated
 * we should pass the original class for references
 *
 * Returns an object with the next structure { [key:string]: any }
 * {
 *   "person_name": "Sara",
 *   "person_last_name": "Smith",
 *   "person_age": 18,
 *   "person_gender": F,
 *   "person_created_at": "2018-01-01T12:00:00.000Z"
 * }
 **/
const personMarshalled = Parseus.encode(person, Person)

Instalation

  1. Install the npm package:

    npm install parseus --save or using yarn yarn add @rijudev/parseus

  2. You need to install reflect-metadata shim:

    npm install reflect-metadata --save or using yarn yarn add reflect-metadata

    and import it somewhere in the global place of your app:

    import 'reflect-metadata'

API

FieldType

Parseus allow the next field type values:
  • string
  • number
  • decimal
  • boolean
  • unique
  • date
  • array
  • object

Field Options

Property Description Type Default
type Field type. Must be one of the values from the FieldType string 'string'
name Key name in source object. if this value is not provided it takes the model field's name wrapped string -
isVirtual Indicates if field's value is ignored when marshall Object boolean false
default Indicates the initial field's value any -
readOnly Indicates if field's value is read only (freeze) boolean false
fixed The scale for a decimal (exact numeric) field, which represents the number of digits to the right of the decimal point number 6
transformer Specifies a value transformer that is to be used to (un)marshall the current field when (un)marshall ITransformer -
factory Indicates the field's model class of target class -

ITransformer

Property Description Type Default
to Used to marshall data when writing to the new object Function -
from Used to unmarshall data when reading from object Function -

ITransformerParams

Property Description Type Default
key key name in source object string -
options Field type options field's IFieldOptions -
data complete mapped source object object -

Still in progress...

Index

Type aliases

FieldType

FieldType: "string" | "number" | "decimal" | "boolean" | "unique" | "date" | "array" | "object"

Field Types to encode/decode

ParseFunction

ParseFunction: object

Type declaration

Variables

Const DEFAULT_PRECISION

DEFAULT_PRECISION: 6 = 6

Const METADATA_DESING_TYPE

METADATA_DESING_TYPE: "design:type" = "design:type"

Const PARSEUS_META_KEY

PARSEUS_META_KEY: "design:parseus-field" = "design:parseus-field"

Const v1

v1: any = require('uuid/v1')

Functions

Field

  • Field decorator is used to mark a specific class property as a decoded object field. Only properties decorated with this decorator will be persisted after encode and decode

    Parameters

    Returns Function

cloneObject

  • cloneObject<T>(obj: T): T
  • Clone the target object if it's any array or an object. If the value is a primitive type it will returned as is.

    Type parameters

    • T: any

    Parameters

    • obj: T

    Returns T

defineMetadata

  • defineMetadata(metaKey: string, value: any, context: any): void

getFieldsFromModel

getMetadata

  • getMetadata(metaKey: string, context: any, propertyName?: undefined | string): any
  • Parameters

    • metaKey: string
    • context: any
    • Optional propertyName: undefined | string

    Returns any

getMetadataParse

getParseArray

getReflectType

  • getReflectType(context: any, propertyName?: undefined | string): any
  • Parameters

    • context: any
    • Optional propertyName: undefined | string

    Returns any

getType

mashallFactory

mergeMetadata

  • mergeMetadata(metaKey: string, value: any, context: any, propertyName: string): void
  • Parameters

    • metaKey: string
    • value: any
    • context: any
    • propertyName: string

    Returns void

parseFactory

setReadOnly

  • setReadOnly(context: any, propertyName: string): void
  • Converts the targetKey field into a readyonly field into context.

    Parameters

    • context: any
    • propertyName: string

    Returns void

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc