https://github.com/karolkrupa/javascript-orm-mapper
ORM mapping library. Especially for Rest API
https://github.com/karolkrupa/javascript-orm-mapper
api data data-mapper entity es6 javascript mapper model mongo mysql node nuxt orm relational rest typescript vue vuex
Last synced: about 1 month ago
JSON representation
ORM mapping library. Especially for Rest API
- Host: GitHub
- URL: https://github.com/karolkrupa/javascript-orm-mapper
- Owner: karolkrupa
- License: mit
- Created: 2020-07-28T20:30:41.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-06T13:18:31.000Z (about 3 years ago)
- Last Synced: 2025-03-17T02:07:29.761Z (11 months ago)
- Topics: api, data, data-mapper, entity, es6, javascript, mapper, model, mongo, mysql, node, nuxt, orm, relational, rest, typescript, vue, vuex
- Language: TypeScript
- Homepage:
- Size: 317 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Javascript orm mapper
[](https://travis-ci.org/karolkrupa/javascript-orm-mapper)
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
```bash
$ npm i javascript-orm-mapper
```
## Tests
```bash
$ 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
```typescript
// 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
```typescript
let 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]
// ]
// }
// }
// ]
// }
```