Skip to content

Commit

Permalink
Implement objection in water-abstraction-system (#34)
Browse files Browse the repository at this point in the history
https://eaflood.atlassian.net/browse/WATER-3829

Implement [Objection ORM](https://www.npmjs.com/package/objection) in `water-abstraction-system`.

The time for running raw Knex queries is ending. We’ll be creating `billing_batches` and `events` soon. So, we need our chosen ORM in place to do this.
  • Loading branch information
Jozzey authored Nov 23, 2022
1 parent b63ac60 commit 6894ea0
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 0 deletions.
40 changes: 40 additions & 0 deletions app/models/base.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict'

/**
* @module BaseModel
*/

const { Model } = require('objection')

const { db } = require('../../db/db.js')

// We only have to do this once in the app and then it will be set globally for Objection. As we are not using multiple
// databases we have no need to pass it into each query we build. And setting it here means all subclasses will inherit
// it. https://vincit.github.io/objection.js/api/model/static-methods.html#static-knex
Model.knex(db)

class BaseModel extends Model {
/**
* An objective property we override to tell it where to search for models for relationships
*
* When setting a relationship in a model we have to provide a reference to the related model. As we need to set the
* relationship on both sides this leads to
* {@link https://vincit.github.io/objection.js/guide/relations.html#require-loops|require-loops}. We can avoid this
* by having the model tell Objection where to search for models for relationships. In the relationship declaration we
* can then just use a string value
*
* ```
* // ...
* relation: Model.ManyToManyRelation,
modelClass: 'charge_version.model',
// ...
```
We don't want to do this in every model so set it in the `BaseModel` as Objection recommends.
*/
static get modelPaths () {
return [__dirname]
}
}

module.exports = BaseModel
30 changes: 30 additions & 0 deletions app/models/charge_version.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict'

/**
* @module ChargeVersionModel
*/

const { Model } = require('objection')

const BaseModel = require('./base.model.js')

class ChargeVersionModel extends BaseModel {
static get tableName () {
return 'water.chargeVersions'
}

static get relationMappings () {
return {
licences: {
relation: Model.BelongsToOneRelation,
modelClass: 'licence.model',
join: {
from: 'water.charge_versions.licence_id',
to: 'water.licences.licence_id'
}
}
}
}
}

module.exports = ChargeVersionModel
38 changes: 38 additions & 0 deletions app/models/licence.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict'

/**
* @module LicenceModel
*/

const { Model } = require('objection')

const BaseModel = require('./base.model.js')

class LicenceModel extends BaseModel {
static get tableName () {
return 'water.licences'
}

static get relationMappings () {
return {
chargeVersions: {
relation: Model.HasManyRelation,
modelClass: 'charge_version.model',
join: {
from: 'water.licences.licence_id',
to: 'water.charge_versions.licence_id'
}
},
regions: {
relation: Model.BelongsToOneRelation,
modelClass: 'region.model',
join: {
from: 'water.licences.region_id',
to: 'water.regions.region_id'
}
}
}
}
}

module.exports = LicenceModel
30 changes: 30 additions & 0 deletions app/models/region.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict'

/**
* @module RegionModel
*/

const { Model } = require('objection')

const BaseModel = require('./base.model.js')

class RegionModel extends BaseModel {
static get tableName () {
return 'water.regions'
}

static get relationMappings () {
return {
licences: {
relation: Model.BelongsToOneRelation,
modelClass: 'licence.model',
join: {
from: 'water.regions.region_id',
to: 'water.licences.region_id'
}
}
}
}
}

module.exports = RegionModel
19 changes: 19 additions & 0 deletions test/models/charge_version.model.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict'

// Test framework dependencies
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')

const { describe, it } = exports.lab = Lab.script()
const { expect } = Code

// Thing under test
const ChargeVersion = require('../../app/models/charge_version.model')

describe('ChargeVersion model', () => {
it('returns data', async () => {
const query = await ChargeVersion.query()

expect(query).to.exist()
})
})
19 changes: 19 additions & 0 deletions test/models/licence.model.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict'

// Test framework dependencies
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')

const { describe, it } = exports.lab = Lab.script()
const { expect } = Code

// Thing under test
const Licence = require('../../app/models/licence.model.js')

describe('Licence model', () => {
it('returns data', async () => {
const query = await Licence.query()

expect(query).to.exist()
})
})
19 changes: 19 additions & 0 deletions test/models/region.model.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict'

// Test framework dependencies
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')

const { describe, it } = exports.lab = Lab.script()
const { expect } = Code

// Thing under test
const Region = require('../../app/models/region.model')

describe('Region model', () => {
it('returns data', async () => {
const query = await Region.query()

expect(query).to.exist()
})
})

0 comments on commit 6894ea0

Please sign in to comment.