import _ from 'lodash';
import SchemaRoute from './schema_route';
import {GET, PUT, POST, DELETE} from 'node-bits';

// helpers
const defineHandler = (key, database, subscribers) => new SchemaRoute(key, database, subscribers);
const defineRoute = (verb, route, implementation) => ({verb, route, implementation});
const defineSubscribers = (subscribers, key) =>
  _.filter(subscribers, s => s.subscribe && s.subscribe(key));

const defineRoutes = (prefix, key, schema, database, subscribers) => {
  const applicableSubscribers = defineSubscribers(subscribers, key);
  const handler = defineHandler(key, database, applicableSubscribers);
  const route = `${prefix ? `/${prefix}` : ''}/${key}`;

  return [
    defineRoute(GET, route, handler),
    defineRoute(GET, `${route}/:id`, handler),
    defineRoute(POST, route, handler),
    defineRoute(POST, `${route}/:id`, handler),
    defineRoute(PUT, route, handler),
    defineRoute(PUT, `${route}/:id`, handler),
    defineRoute(DELETE, route, handler),
    defineRoute(DELETE, `${route}/:id`, handler),
  ];
};

// load route
export default config => {
  const subscribers = (config.subscribers || []).map(s => s.implementation);
  const routes = _.keys(config.database.models).map(key =>
    defineRoutes(config.prefix, key, config.database.models[key], config.database, subscribers)
  );

  return _.flattenDeep(routes);
};

@@ -456,16 +456,8 @@
 key,
- schema,
  dat
@@ -1263,37 +1263,8 @@
 key,
- config.database.models%5Bkey%5D,
  con

Remove extraneous function call