Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/billykong/active-serializer
Rails active_model_serializer inspired nodejs object serializer
https://github.com/billykong/active-serializer
expressjs json nodejs-modules npm npm-package sequelize serializer
Last synced: about 1 month ago
JSON representation
Rails active_model_serializer inspired nodejs object serializer
- Host: GitHub
- URL: https://github.com/billykong/active-serializer
- Owner: billykong
- License: mit
- Created: 2017-09-10T14:58:29.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2022-12-06T17:23:09.000Z (about 2 years ago)
- Last Synced: 2024-10-30T22:26:16.496Z (3 months ago)
- Topics: expressjs, json, nodejs-modules, npm, npm-package, sequelize, serializer
- Language: JavaScript
- Size: 122 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![Build Status](https://travis-ci.org/billykong/active-serializer.svg?branch=master)](https://travis-ci.org/billykong/active-serializer)
[![Coverage Status](https://coveralls.io/repos/github/billykong/active-serializer/badge.svg?branch=master)](https://coveralls.io/github/billykong/active-serializer?branch=master)
[![npm version](https://img.shields.io/npm/v/active-serializer)](https://img.shields.io/npm/v/active-serializer)
[![Run on Repl.it](https://repl.it/badge/github/billykong/active-serializer)](https://repl.it/github/billykong/active-serializer)# active-serializer
Rails active_model_serializer inspired nodejs object serializer## Installation
`npm install active-serializer --save`
## Usage
```
const serialize = require('active-serializer');const object = {
id: 'some_id',
name: 'some_name',
unwanted: 'will not be shown',
image_url: async (options) => { return await 'some_url' }
};const attributes = ['id', 'name', 'image_url'];
const options = {}const result = await serialize(object, attributes, options);
console.log(result);
```
Output should be:
```
{
id: 'some_id',
name: 'some_name',
image_url: 'some_url'
}
```### Nested Serialization
```
const nestedObject = {
id: 'some_id',
name: 'some_name',
unwanted: 'will not be shown',
image_url: async (options) => { return await 'some_url' },
object2: {
id: 'some_other_id',
attr1: 'some_attr',
attr2: 'will not be shown'
}
};
const object2Serializer = async (object2, options) => {
const attributes2 = ['id', 'attr1'];
return await serialize(object2, attributes2, options);
};const attributes1 = ['id', 'name', 'image_url', 'object2'];
const options = {
'serializers': { 'object2': object2Serializer }
}
const result = await serialize(nestedObject, attributes1, options);
console.log(result);
```
Output should be:
```
{
id: 'some_id',
name: 'some_name',
image_url: 'some_url',
object2: {
id: 'some_other_id',
attr1: 'some_attr'
}
}
```## Tests
`npm test`
## Contributing
Please feel free and report issues or create pull request :]