Marshaller
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.
Install
npm install marshaller
Example Entity
; ;console.loginstance;/* 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:
;; ;await connection.db'testing'.dropDatabase;; ; await database.saveSimpleModel, instance; ;;
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.
; ;;;