0.1.0 • Published 8 years ago

data-adapter-js v0.1.0

Weekly downloads
6
License
MIT
Repository
github
Last release
8 years ago

data-adapter-js

You can use it to adapting data in according to a schema for Flux and Redux apps.

travis npm

Install

$ npm install data-adapter-js --save-dev

Require

import Adapter, { ArrayOf, InstanceOf, Model } from "data-adapter-js"

Run

$ npm start
$ npm test
$ npm run build
$ npm run lint

API

ArrayOf Parameters:

  1. Model
  2. array of data

InstanceOf Parameters:

  1. Model
  2. object

Model Parameters:

  1. pluralize name
  2. adapter function ( current, parrent )

Demo

import Adapter, { ArrayOf, InstanceOf, Model } from "data-adapter-js"
import data from './data'

const User = new Model('users' , (user) => ({
	id: user.id,
	username: user.name,
	comments : new ArrayOf(Comment, user.comments)
}))


const Comment = new Model('comments', (comment, user) => ({
	id: comment.id,
	userId: user.id,
	text: comment.content
}))

const Post = new Model('posts', (post) => ({
	id: post.id,
	user: new InstanceOf(User, post.author),
	text: post.content
}))

Adapter(data, User)

Data

before

[
	{
		id: 1,
		name: 'toto',
		comments : [
			{
				id: 31,
				postId: 1,
				content : 'lorem ipsum elms'
			},
			{
				id: 39,
				postId: 1,
				content : 'dare ipsum remu'
			},
		]
	},
	{
		id: 2,
		name: 'tata',
		comments : [ 
			{
				id: 34,
				postId: 4,
				content : 'lorem ipsum elms'
			},
			{
				id: 32,
				postId: 15,
				content : 'dare ipsum remu'
			},
		]
	}
]

After

{ 
	users: {
		1: {
			id: 1,
			username: 'toto',
			comments : [31, 39] 
		},
		2: {
			id: 2,
			username: 'tata',
			comments : [34, 32] 
		} 
	},
	comments: {
		31: { 
			id: 31,
			userId: 1,
			text: 'lorem ipsum elms' 
		},
		39: { 
			id: 39,
			userId: 1,
			text: 'dare ipsum remu' 
		},
		34: { 
			id: 34,
			serId: 2,
			text: 'lorem ipsum elms' 
		},
		32: { 
			id: 32,
			userId: 2,
			text: 'dare ipsum remu' 
		}
	}
}
0.1.0

8 years ago

0.0.9

8 years ago

0.0.8

8 years ago

0.0.7

8 years ago

0.0.6

8 years ago

0.0.5

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago