Skip to content

Commit

Permalink
Create transaction model (#79)
Browse files Browse the repository at this point in the history
As part of the work to implement a "add transaction to bill run" endpoint, this change creates a migration to create the transactions table in the database, creates a transaction model which has a relationship with the bill run model, and updates the bill run model to reflect this relationship.
  • Loading branch information
StuAA78 authored Dec 4, 2020
1 parent 85fa591 commit e95a1a2
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
8 changes: 8 additions & 0 deletions app/models/bill_run.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ class BillRunModel extends BaseModel {
from: 'bill_runs.regime_id',
to: 'regimes.id'
}
},
transactions: {
relation: Model.HasManyRelation,
modelClass: 'transaction.model',
join: {
from: 'bill_runs.id',
to: 'transactions.bill_run_id'
}
}
}
}
Expand Down
29 changes: 29 additions & 0 deletions app/models/transaction.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict'

/**
* @module TransactionModel
*/

const { Model } = require('objection')
const BaseModel = require('./base.model')

class TransactionModel extends BaseModel {
static get tableName () {
return 'transactions'
}

static get relationMappings () {
return {
billRun: {
relation: Model.BelongsToOneRelation,
modelClass: 'bill_run.model',
join: {
from: 'transactions.bill_run_id',
to: 'bill_runs.id'
}
}
}
}
}

module.exports = TransactionModel
34 changes: 34 additions & 0 deletions db/migrations/20201203144442_create_transactions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict'

const tableName = 'transactions'

exports.up = async function (knex) {
await knex
.schema
.createTable(tableName, table => {
// Primary Key
table.uuid('id').primary().defaultTo(knex.raw('gen_random_uuid()'))

// Data
table.uuid('bill_run_id').notNullable()
table.integer('charge_value').notNullable()
table.boolean('charge_credit').notNullable()

// Automatic timestamps
table.timestamps(false, true)
})

await knex.raw(`
CREATE TRIGGER update_timestamp
BEFORE UPDATE
ON ${tableName}
FOR EACH ROW
EXECUTE PROCEDURE update_timestamp();
`)
}

exports.down = async function (knex) {
return knex
.schema
.dropTableIfExists(tableName)
}

0 comments on commit e95a1a2

Please sign in to comment.