const KnormError = require('./KnormError');

class Transaction {
  constructor(callback) {
    if (typeof callback === 'function') {
      this.callback = callback;
    }

    const models = {};
    const transaction = this;
    const knorm = this.constructor.knorm;

    this.Field = knorm.Field;
    this.Model = class TransactionModel extends knorm.Model {};
    this.Query = this.Model.Query = class TransactionQuery extends knorm.Query {
      async query(sql) {
        return transaction.query(sql);
      }
    };

    this.Model.prototype.models = models;
    this.Query.prototype.models = models;

    Object.entries(this.models).forEach(([name, model]) => {
      models[name] = class extends model {};
      models[name].prototype.models = models;
      models[name].Query = class TransactionQuery extends model.Query {
        async query(sql) {
          return transaction.query(sql);
        }
      };
      models[name].Query.prototype.models = models;
      return models;
    });

    this.models = models;
  }

  async query() {
    throw new this.constructor.TransactionError(
      '`Transaction.prototype.query` is not implemented'
    );
  }

  async execute() {
    throw new this.constructor.TransactionError(
      '`Transaction.prototype.execute` is not implemented'
    );
  }

  async then() {
    const promise = this.execute();
    return promise.then.apply(promise, arguments);
  }

  async catch() {
    const promise = this.execute();
    return promise.catch.apply(promise, arguments);
  }
}

Transaction.TransactionError = class TransactionError extends KnormError {};

module.exports = Transaction;

@@ -304,91 +304,8 @@
 his.
-Model = class TransactionModel extends knorm.Model %7B%7D;%0A    this.Query = this.Model.
 Quer
@@ -432,17 +432,16 @@
 %0A    %7D;%0A
-%0A
     this
@@ -445,13 +445,13 @@
 his.
-Model
+Query
 .pro
@@ -470,24 +470,25 @@
 s = models;%0A
+%0A
     this.Que
@@ -476,37 +476,136 @@
 dels;%0A%0A    this.
-Query
+Model = class TransactionModel extends knorm.Model %7B%7D;%0A    this.Model.Query = this.Query;%0A    this.Model
 .prototype.model
@@ -727,54 +727,8 @@
 %7B%7D;%0A
-      models%5Bname%5D.prototype.models = models;%0A
     
@@ -877,24 +877,71 @@
   %7D%0A      %7D;
+%0A%0A      models%5Bname%5D.prototype.models = models;
 %0A      model
@@ -985,29 +985,8 @@
 ls;%0A
-      return models;%0A
     

remove dead code