json-api icon indicating copy to clipboard operation
json-api copied to clipboard

Convenience app method

Open shannon opened this issue 10 years ago • 1 comments

So as I was working on the Koa strategy I got to thinking and I was wondering if you would be open to creating an app method that looked something like this.

var api = new API.createApp({
  models: models,
  descriptions: descriptions,
  dbAdapter: 'Mongoose',
  httpStrategy: 'Express'
});

app.get("/:type(people|places)", api.middleware());
app.post("/:type(people|places)", api.middleware());
//....all the other routes....

app.get("/docs", api.docs.middleware());

Where models are mongoose models defined as in the example:

var models = {
  "Person": require('./models/person'),
  "Place": require('./models/place')
};

and descriptions are similar:

var descriptions = {
  "Person": require('./descriptions/person'),
  "Place": { //long form to show example description
    urlTemplates: {
      "self": "/places/{id}"
    }
  }
};

dbAdapters could still be overridden in the description. This way for a basic setup there's no need for the user to create a registry, adapter, 2 controllers, and 2 handlers. It would really just be a wrapper since everything to do it is already there. Even the ResourceTypeRegistry already takes typeDescriptions and descriptionDefaults could be used to for the dbAdapter.

shannon avatar Oct 24 '15 04:10 shannon

Yes! This is actually something I've wanted for quite a while—the current format is way too clunky!

The only syntax tweak I'd make, I think, is to have the user pass constructor functions as the options rather than strings, since the latter is better for type checkers (flow, etc) and IDEs. Something like:

var JsonApi = require("json-api");

var api = new JsonApi({
  models: models,
  descriptions: descriptions,
  dbAdapter: JsonApi.dbAdapters.mongoose,
  httpStrategy: JsonApi.httpStrategies.express
});

Otherwise, your structure looks great!

ethanresnick avatar Oct 24 '15 06:10 ethanresnick