const Bundle = require('bono/bundle');
const parse = require('co-body');

class RestBundle extends Bundle {
  constructor ({ schema }) {
    super();

    this.schema = schema;

    this.get('/', this.index.bind(this));
    this.post('/', this.create.bind(this));
  }

  getCollection (ctx) {
    return ctx.norm.factory(this.schema);
  }

  async index (ctx) {
    const entries = await this.getCollection(ctx).all();
    return { entries };
  }

  async create (ctx) {
    const body = await parse.json(ctx);

    const model = this.getCollection(ctx).new();

    model.set(body);

    await model.save();

    ctx.status = 201;
    ctx.response.set('Location', `${ctx.originalUrl}/${model.id}`);

    return model.get();
  }
}

module.exports = RestBundle;

@@ -256,16 +256,156 @@
 (this));
+%0A%0A    this.get('/%7Bid%7D', this.read.bind(this));%0A    this.put('/%7Bid%7D', this.update.bind(this));%0A    this.delete('/%7Bid%7D', this.del.bind(this));
 %0A  %7D%0A%0A  
@@ -521,38 +521,41 @@
 = await 
-this.getCollection(ctx
+ctx.norm.find(this.schema
 ).all();
@@ -617,19 +617,20 @@
   const 
-bod
+entr
 y = awai
@@ -657,212 +657,624 @@
     
-const model = this.getCollection(ctx).new();%0A%0A    model.set(body);%0A%0A    await model.save();%0A%0A    ctx.status = 201;%0A    ctx.response.set('Location', %60$%7B
+await ctx.norm.find(this.schema).insert(entry).save();%0A%0A    ctx.status = 201;%0A    ctx.response.set('Location', %60$%7Bctx.originalUrl%7D/$%7Bentry.id%7D%60);%0A%0A    return %7B entry %7D;%0A  %7D%0A%0A  async read (ctx) %7B%0A    const entry = await ctx.norm.find(this.schema, ctx.parameters.id).single();%0A%0A    if (!entry) %7B%0A      ctx.status = 404;%0A      return;%0A    %7D%0A%0A    return %7B entry %7D;%0A  %7D%0A%0A  async update (ctx) %7B%0A    const entry = await parse.json(ctx);%0A%0A    await 
 ctx.
+n
 or
-iginalUrl%7D/$%7Bmodel.id%7D%60);%0A%0A    return model.get
+m.find(this.schema, ctx.parameters.id).set(entry).save();%0A%0A    return %7B entry %7D;%0A  %7D%0A%0A  async del (ctx) %7B%0A    await ctx.norm.find(this.schema, ctx.parameters.id).delete
 ();%0A

refactored to simple norm