andywer / ideabox

Place to collect techy ideas and get feedback.
1 stars 1 forks source link

Frontend ORM for handling REST resources #2

Open andywer opened 8 years ago

andywer commented 8 years ago

I think it would be very useful to have something like an ORM for the frontend to fetch and manipulate REST resources. Especially if your backend goes an HATEOASish approach, since you want a tight coupling of resource and href (see https://www.npmjs.com/package/hal). There seems to be no suitable solution out there: https://www.google.de/search?q=redux%20hateoas

Idea:

////////////////
// Basic usage:

const User = createRestResourceType('user')

const user = await User.fetch('/href/to/user')
const users = await User.fetchAll('/href/to/all/users')

let newUser
// create new empty user instance, do not POST to server yet
newUser = User.new(userData)
// create new user instance, provide data, POST immediately
newUser = await User.create('/href/to/POST/to', userData)

user.update(userData)
user.destroy()

// API should be quite similar to existing JS ORMs like mongoose & sequelize
// Enumerating an instance's properties returns exactly the underlying REST object's properties

///////////////////
// Redux bindings:

bindActionCreators(User)
const reducers = reducersFor(User)

///////////////////////////////
// What the bindActionCreators() does:

User.on('fetchSuccess', (href, data) => (
  dispatch({
    // could use https://www.npmjs.com/package/action-names
    type: actionTypesFor(User).fetchSuccess,
    payload: { href, data: User.new(data) }
  })
))

// ...
andywer commented 8 years ago

@romansemko pointed out that Angular (1.x) has $resource which is pretty similar to that. But Angular-specific, of course... Angular 2 doesn't seem to have it yet: https://www.npmjs.com/package/angular2-resource

Would be important for it to be as framework-agnostic as possible, though.

romansemko commented 8 years ago

Have you checked: https://github.com/ngonzalvez/rest-facade

andywer commented 8 years ago

@romansemko Good hint, thanks! It could be a little more up-to-date (isomorphic-fetch instead of superagent) and a little less ID-centric, but seems like a valid option.

I've got to check if the enumerable properties of a resource instance are equal to the transferred REST object (I think this is pretty important for nice and stress-free development).

andywer commented 8 years ago

@romansemko Seems like https://github.com/ngonzalvez/rest-facade just returns the pure data, not instances with href-aware CRUD methods on the prototype :-/

romansemko commented 8 years ago

Hmm. That is as close as it gets. Might need to write own stuff after all...

andywer commented 8 years ago

Just for completeness:

This is closer to what I had in mind, but I think its API feels a little inconvenient: https://github.com/marmelab/restful.js

Also it is focused towards a pretty narrow API layout which might not work with every RESTful API. HATEOAS has not even been considered, I guess :-/

andywer commented 7 years ago

Looks like one of the closest solutions right now, build on the shoulders of GraphQL: https://github.com/stubailo/graphql-anywhere