{"id":22220532,"url":"https://github.com/drawbotics/entman","last_synced_at":"2025-07-27T15:33:03.231Z","repository":{"id":57225731,"uuid":"65897036","full_name":"Drawbotics/entman","owner":"Drawbotics","description":"A simple library to manage normalizr entities in a Redux store","archived":false,"fork":false,"pushed_at":"2017-05-30T09:34:51.000Z","size":1034,"stargazers_count":25,"open_issues_count":4,"forks_count":2,"subscribers_count":9,"default_branch":"dev","last_synced_at":"2024-10-03T01:19:03.135Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Drawbotics.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-08-17T10:01:25.000Z","updated_at":"2019-08-07T00:06:42.000Z","dependencies_parsed_at":"2022-08-24T11:00:27.141Z","dependency_job_id":null,"html_url":"https://github.com/Drawbotics/entman","commit_stats":null,"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Drawbotics%2Fentman","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Drawbotics%2Fentman/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Drawbotics%2Fentman/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Drawbotics%2Fentman/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Drawbotics","download_url":"https://codeload.github.com/Drawbotics/entman/tar.gz/refs/heads/dev","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":227817177,"owners_count":17824199,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-02T23:09:00.166Z","updated_at":"2024-12-02T23:09:00.826Z","avatar_url":"https://github.com/Drawbotics.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cimg src=\"https://raw.githubusercontent.com/Drawbotics/entman/79b74b1ebdfba5250a273fe9da7a3839d8c81f4e/entman-logo.png\" width=\"50%\"\u003e\n\n\n# Entman\n\nA library to help you manage your entities in a [redux](https://github.com/reactjs/redux)\nstore when using [normalizr](https://github.com/paularmstrong/normalizr). **Entman** takes care of\nretrieving them from the store and creating, deleting\nand updating the entities while keeping the relations between them in sync.\n\nThe idea is that everything that has a model in the *backend* should be an\nentity in the *frontend*. The management of entities is usually something very\nstraightforward but tedious, so you leave this work to **entman** and\nyou can focus on the rest.\n\n[![npm version](https://img.shields.io/npm/v/entman.svg?style=flat-square)](https://www.npmjs.com/package/entman)\n[![build status](https://img.shields.io/travis/Drawbotics/entman/master.svg?style=flat-square)](https://travis-ci.org/Drawbotics/entman)\n[![coveralls](https://img.shields.io/coveralls/Drawbotics/entman.svg?style=flat-square)](https://coveralls.io/github/Drawbotics/entman)\n\n## Install\n\nInstall it as a node module as usual with [npm](https://www.npmjs.org/) along its peer dependencies:\n\n```bash\n$ npm install -S entman redux normalizr\n```\n\nOr using [yarn](https://yarnpkg.com/):\n\n```bash\n$ yarn add entman redux normalizr\n```\n\n\n## Example\n\nA quick example to see **entman** in action:\n\n### schemas.js\n\nWe use schemas to define relationships between our entities. We can also define\nmethods that will be available in the entity and serve like some sort of computed property.\n\n```javascript\nimport { defineSchema, hasMany, generateSchemas } from 'entman';\n\nconst Group = defineSchema('Group', {\n  attributes: {\n    users: hasMany('User'),  // Use the name of another model to define relationships\n\n    getNumberOfUsers() {  // Define methods that interact with the entity instance\n      return this.users.length;\n    }\n  }\n});\n\nconst User = defineSchema('User', {\n  attributes: {\n    group: 'Group',\n  }\n});\n\n// Generate and export the schemas. Schemas will be exported as an object\n// with the name of every schema as the keys and the actual schemas as values.\nexport default generateSchemas([\n  Group,\n  User,\n])\n```\n\n\n### reducer.js\n\nConnect the entities reducer to the state.\n\n```javascript\nimport { combineReducers } from 'redux';\nimport { reducer as entities } from 'entman';\nimport schemas from './schemas';\n\nexport default combineReducers({\n  // Other reducers,\n  entities: entities(schemas, {\n    // An initial state can also be specified\n    Group: {\n      1: { id: 1 },\n    },\n  }),\n})\n```\n\n\n### store.js\n\nConnect the entman middleware to the store.\n\n```javascript\nimport { createStore, applyMiddleware } from 'redux';\nimport { middleware as entman } from 'entman';\nimport reducer from './reducer';\n\nexport default createStore(\n  store,\n  applyMiddleware(entman({ enableBatching: true })),\n);\n```\n\n\n### selectors.js\n\nCreate selectors that will retrieve the entities from the store. Selectors also\ntake care of populating relationships and adding the *getter* methods defined in the\nschema. It's recommended to wrap **entman** selectors intead of using them directly\nso they're abstracted from the rest of the system.\n\n```javascript\nimport { getEntity } from 'entman';\nimport schemas from './schemas';\n\nexport function getGroup(state, id) {\n  return getEntity(state, schemas.Group, id);\n}\n```\n\n\n### actions.js\n\nCreate some actions using the helpers from **entman**. The helpers will take an action and\nwrap it with entman functionality. This way, you can still react in your reducers to your\nown actions and the entity management is just a side effect that entman will take care of.\n\n```javascript\nimport {\n  createEntities,\n} from 'entman';\nimport schemas from './schemas';\n\nexport const CREATE_USER = 'CREATE_USER';\n\nexport function createUser(user) {\n  return createEntities(schemas.User, 'payload.user', {\n    type: CREATE_USER,  // CREATE_USER action will be dispatched alongside entman actions\n    payload: { user },\n  });\n}\n```\n\n\n### Group.jsx\n\nFinally, use your actions and selectors like you would normally do.\n\n```jsx\nimport React from 'react';\nimport { connect } from 'react-redux';\nimport { getGroup } from './selectors';\nimport { loadGroup, createUser } from './actions';\n\nclass Group extends React.Component {\n  constructor(props) {\n    super(props);\n    this._handleInput = this._handleInput.bind(this);\n    this._handleAddUser = this._handleAddUser.bind(this);\n  }\n\n  componentDidMount() {\n    const { loadGroup, params } = this.props;\n    loadGroup(params.groupId);\n  }\n\n  render() {\n    const { group } = this.props;\n    return (\n      \u003cdiv\u003e\n        \u003ch1\u003e{group.name}\u003c/h1\u003e\n        \u003ch2\u003e{group.getNumberOfUsers()} members\u003c/h2\u003e\n        \u003cul\u003e\n          {group.users.map(u =\u003e (\n            \u003cli\u003e{u.name}\u003c/li\u003e\n          ))}\n        \u003c/ul\u003e\n        {this.state.showForm \u0026\u0026\n          this._renderUserForm()\n        }\n        { ! this.state.showForm \u0026\u0026\n          \u003cbutton type=\"button\" onClick={() =\u003e this.setState({ showForm: true })}\u003e\n            Add\n          \u003c/button\u003e\n        }\n      \u003c/div\u003e\n    );\n  }\n\n  _renderUserForm() {\n    return (\n      \u003cdiv\u003e\n        \u003cinput type=\"text\" onChange={this._handleInput} /\u003e\n        \u003cbutton type=\"button\" onClick={() =\u003e this.setState({ showForm: false })}\u003e\n          Cancel\n        \u003c/button\u003e\n        \u003cbutton type=\"button\" onClick={this._handleAddUser}\u003eSave\u003c/button\u003e\n      \u003c/div\u003e\n    );\n  }\n\n  _handleInput(e) {\n    this.setState({ name: e.target.value });\n  }\n\n  _handleAddUser(e) {\n    const { group, createUser } = this.props;\n    const { name } = this.state;\n    const user = { name, group: group.id };\n    createUser(user);\n  }\n}\n\nconst mapStateToProps = (state, ownProps) =\u003e ({\n  group: getGroup(state, ownProps.params.groupId),\n});\n\nconst mapDispatchToProps = {\n  loadGroup,\n  createUser,\n};\n\nexport default connect(mapStateToProps, mapDispatchToProps)(Group);\n```\n\n\n## API\n\nSee the [API Reference](docs/API.md)\n\n\n## LICENSE\n\nMIT. See [LICENSE](LICENSE) for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdrawbotics%2Fentman","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdrawbotics%2Fentman","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdrawbotics%2Fentman/lists"}