Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for alias to Legacy Models #423

Merged
merged 2 commits into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions app/models/legacy-base.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
* @module LegacyBaseModel
*/

const { QueryBuilder } = require('objection')
const path = require('path')

const BaseModel = require('./base.model.js')
const LegacyQueryBuilder = require('./legacy.query-builder.js')

class LegacyBaseModel extends BaseModel {
/**
Expand Down Expand Up @@ -36,10 +36,10 @@ class LegacyBaseModel extends BaseModel {
*
* See `schema()` for further details.
*
* @returns {Object} a custom Objection `QueryBuilder`
* @returns {module:LegacyQueryBuilder} a custom Objection `QueryBuilder`
*/
static get QueryBuilder () {
return SchemaQueryBuilder
return LegacyQueryBuilder
}

/**
Expand Down Expand Up @@ -223,11 +223,4 @@ class LegacyBaseModel extends BaseModel {
}
}

class SchemaQueryBuilder extends QueryBuilder {
constructor (modelClass) {
super(modelClass)
this.withSchema(modelClass.schema)
}
}

module.exports = LegacyBaseModel
31 changes: 31 additions & 0 deletions app/models/legacy.query-builder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict'

/**
* Wrapper around Objection.js QueryBuilder to add schema and alias support to Legacy Models
* @module LegacyBaseModel
*/

const { QueryBuilder } = require('objection')

class LegacyQueryBuilder extends QueryBuilder {
/**
* Wrapper around Objection.js QueryBuilder to add schema and alias support to Legacy Models
*
* @param {Object} modelClass The Objection legacy model to which this QueryBuilder will be applied
*/
constructor (modelClass) {
super(modelClass)

// ALL legacy models must implement a `schema` property. So, we always call this
this.withSchema(modelClass.schema)

// Only alias those that need to alias the table name to avoid errors, for example, needing to alias
// `charge_elements` as 'charge_references' so we can link them to `charge_purposes` but call purposes
// `ChargeElements`.
if (modelClass.alias) {
this.alias(modelClass.alias)
}
}
}

module.exports = LegacyQueryBuilder
66 changes: 66 additions & 0 deletions test/models/legacy.query-builder.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
'use strict'

'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

// Test helpers
const LegacyBaseModel = require('../../app/models/legacy-base.model.js')

// Thing under test
const LegacyQueryBuilder = require('../../app/models/legacy.query-builder.js')

describe('Legacy Base model', () => {
describe('when given a model class with only a schema', () => {
class SchemaLegacyModel extends LegacyBaseModel {
static get tableName () {
return 'schemaTable'
}

static get schema () {
return 'water'
}

static get QueryBuilder () {
return LegacyQueryBuilder
}
}

it('adds the schema name to the table in the queries it generates', () => {
const result = SchemaLegacyModel.query().toKnexQuery().toQuery()

expect(result).to.endWith('from "water"."schema_table"')
})
})

describe('when given a model class with an alias', () => {
class AliasLegacyModel extends LegacyBaseModel {
static get tableName () {
return 'aliasTable'
}

static get schema () {
return 'water'
}

static get alias () {
return 'chargeReferences'
}

static get QueryBuilder () {
return LegacyQueryBuilder
}
}

it('aliases the table in the queries it generates', () => {
const result = AliasLegacyModel.query().toKnexQuery().toQuery()

expect(result).to.endWith('from "water"."alias_table" as "charge_references"')
})
})
})