Socket
Socket
Sign inDemoInstall

javascript-orm-mapper

Package Overview
Dependencies
103
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    javascript-orm-mapper

Javascript orm mapper


Version published
Weekly downloads
11
decreased by-57.69%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

Javascript orm mapper

Build Status

This library provides a simple way to map data from json to javascript objects that use multiple data types, including relational data types, for example: OneToMany

Installation

$ npm i javascript-orm-mapper

Tests

$ npm test

Defining Models

You can define models just like normal classes. There are no limits, you can define own methods, getters properties and whatever you want. All you have to do to make your class mappable is describing properties with type annotations

// Create database
const database = new Database()

// Post
@Entity({
    name: 'post',
    database: database
})
class Post extends Model {
    @Id()
    @String()
    id: string = ''

    @String()
    name: string = ''

    @OneToMany('comment')
    comments: Comment[] = []
}

// Comment
@Entity({
    name: 'comment',
    database: database
})
class Comment extends Model {
    @Id()
    @String()
    id: string = ''

    @String()
    content: string = ''

    @ManyToOne('post')
    post: Post = null
}

Mapping data to objects

let post = <Post>ModelMapper.persist({
    id: 1,
    name: 123,
    comments: [
        {
            id: 1,
            content: "Lorem ipsum",
            post: {
                id: 1,
                name: "New name"
            }
        }
    ]
}, Post)

// Result
// Post {
//   __orm_uid: "bf9929cb-f852-43a0-9260-2e3fb89833b7",
//   id: "1",
//   name: "New name",
//   comments: [
//     Comment { 
//        __orm_uid: "6599f446-fb0d-4194-abbd-659d40d5c9fb",
//        content: "Lorem ipsum",
//        post: Post {
//            __orm_uid: "bf9929cb-f852-43a0-9260-2e3fb89833b7",
//            id: "1",
//            name: "New name",
//            comments: [
//                [Circural]  
//            ]
//        }
//     }
//   ]
// }

Keywords

FAQs

Last updated on 07 Aug 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc