Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support anonymous entities #40

Closed
raingerber opened this issue Dec 9, 2017 · 4 comments
Closed

Support anonymous entities #40

raingerber opened this issue Dec 9, 2017 · 4 comments

Comments

@raingerber
Copy link
Contributor

raingerber commented Dec 9, 2017

This feature introduces anonymous entities, which are just entities defined without names. Entities that do have names are referred to as declared entities.

API

DataPoint will expose a factory function for each registered entity type:

const dataPoint = require('data-point')

const factories = dataPoint.entityFactories

// {
//   entry: [Function: create],
//   request: [Function: create],
//   hash: [Function: create],
//   ...
// }

const requestEntity = factories.request({
  url: 'https://api.github.com/orgs/nodejs'
})

dataPoint.transform(requestEntity)

Example

This example shows the same transform with declared entities and anonymous entities:

declared:

dataPoint.addEntities({
  'request:getOrgInfo': {
    url: 'https://api.github.com/orgs/nodejs'
  },
  'hash:OrgInfo': {
    mapKeys: {
      reposUrl: '$repos_url',
      eventsUrl: '$events_url'
    }
  }
})

dataPoint.transform('request:getOrgInfo | hash:OrgInfo')

anonymous:

const { request, hash } = dataPoint.entityFactories

dataPoint.transform([
  request({
    url: 'https://api.github.com/orgs/nodejs'
  }),

  hash({
    mapKeys: {
      reposUrl: '$repos_url',
      eventsUrl: '$events_url'
    }
  })
])

These are main benefits I see from anonymous entities:

  • No pressure to think of good names for everything 😄
  • Currently, entities can reference any other entity, but anonymous entities are not declared in the "global" scope, so there's no question about where they're being used
  • More readable, because anonymous entities keep more code in the place where it's being used
  • On the other hand, it's easier to share entities between files. Example:
// data-hash.js

module.exports = hash({
  mapKeys: {
    reposUrl: '$repos_url',
    eventsUrl: '$events_url'
  }
}) 

// main.js

const dataHash = require('./data-hash')

dataPoint.transform([
  request({
    url: 'https://api.github.com/orgs/nodejs'
  }),
  dataHash
])

Any thoughts?

@paulmolluzzo
Copy link
Collaborator

This is a fantastic idea!

Sorry if this is obvious, but what would happen if an anonymous entity tried to refer to a declared entity?

Here's a rough example of the question:

// data-hash.js

module.exports = hash({
  value:  'request:getOrgInfo', // can refer to declared entity?
  mapKeys: {
    reposUrl: '$repos_url',
    eventsUrl: '$events_url'
  }
}) 

// main.js

const dataHash = require('./data-hash')

dataPoint.addEntities({
  'request:getOrgInfo': {
    url: 'https://api.github.com/orgs/nodejs'
  },
  'hash:OrgInfo': dataHash // would this pull the value from the declared entity?
})

@raingerber
Copy link
Contributor Author

Glad you like the idea! Your example should work out of the gate, because anonymous entities will support all the features that current entities have (and we can already use entities within other entities).

raingerber pushed a commit to raingerber/data-point that referenced this issue Dec 11, 2017
installs the object-hash library and exports an entityFactories object on datapoint instances

ViacomInc#40
raingerber pushed a commit to raingerber/data-point that referenced this issue Dec 12, 2017
@acatl
Copy link
Collaborator

acatl commented Dec 13, 2017

love this idea, my only concern is people abusing this feature and creating nested entities which could potentially end up in a crazy triangle of hell mess :)

I believe the feature adds a lot of value

things to keep in mind:

  • do not depend on datapoint instance, should be exposed as a static factory of DataPoint
  • think about usage of custom entities
  • parse at compile time, so errors happen there and not at run time

@acatl acatl added this to the v2.x milestone Dec 21, 2017
acatl pushed a commit that referenced this issue Jan 10, 2018
Refactors hash and collection entity's **assing** (formerly addKeys), *map**,  **find** and **filter** functionality to be used as standalone Reducers exposing them to DataPoint.helpers. 

close #123 #40
@acatl
Copy link
Collaborator

acatl commented Jan 10, 2018

superseded by #129

@acatl acatl closed this as completed Jan 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants