Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/flyfloor/em2
An isomorphic easy model manager
https://github.com/flyfloor/em2
Last synced: about 2 months ago
JSON representation
An isomorphic easy model manager
- Host: GitHub
- URL: https://github.com/flyfloor/em2
- Owner: flyfloor
- License: mit
- Created: 2016-05-05T08:51:18.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-05-11T14:30:16.000Z (over 7 years ago)
- Last Synced: 2024-10-28T23:03:08.114Z (2 months ago)
- Language: JavaScript
- Homepage:
- Size: 90.8 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## em2
this is a front end easy model manager
### Usage
#### create model
```
import em2 from 'em2';const User = new em2({
url: [api],
fields: [model fields, for cleaning params under POST, PUT, OPTIONS... methods]
}, {
pkey: [id => primary key, default is '_id'],
parseData: [function => global parseData handler, when data fetched, just after data to json object],
exception: [function => global error handler],
})
```fields can used three way:
```
fields: {
name: {
type: String,
default: '',
},
age: {
type: Number,
default: 0,
}
...
}
``````
fields: [
{
name,
type: String,
default: '',
},
{
name: 'age',
type: Number,
default: 0
}
...
]
```or simple way:
```
fields: ['name', 'age', ...]
```then use it:
#### model methods
```
//GET api?name=a&sex=b
User.find({name: 'a', sex: 'b'}).then(data => {
// after parseData if have parseData
}).catch(error => {
// after exception if have
})
``````
//GET api/:_id?name=a&sex=b
User.findOne(_id, params).then(succFunc).catch(errFunc)
``````
//POST api body: params
User.create(params)...
``````
//PUT api/:_id body:params
User.update({_id, name: 's', ...})
``````
//POST or PUT, whether params has model's pkey
User.save(params)
``````
//DELETE api?name=a&sex=b
User.destroy({_id, name: 'a',...})
```#### nested model
```
const Comment = new em2({
url: '/post/:post_id/comment'
})// GET /post/[post_id]/comment
Comment.find({post_id, ...})// GET /post/[post_id]/comment/[_id]
Comment.findOne({post_id, _id})
```#### also has default request
```
// request
User.request(['get', 'post', ...], api, params).then(data => {
// after parseData
}).catch(error => {
// after exception
})
```warning:
if you need model's `this`(like this.pkey, or this.fields), function **parseData** should not be write in arrow function. neither do function **exception**