-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement objection in water-abstraction-system (#34)
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
Showing
7 changed files
with
195 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
}) |