/** | |
* Term: schools.address.state | |
* | |
* Stores the state abbreviation for a school's location. | |
*/ | |
module.exports = { | |
table: 'school', // Entity table in DB | |
// `?state=tx` is supported (or would throw) here | |
filter: function(qb, value) { | |
// Compose query builder to only return schools in this state | |
qb.andWhere(this.get('column'), value.toUpperCase()); | |
}, | |
// Simple example how the UI was defined. | |
ui: { | |
type: 'radio', | |
query: function() { | |
// `this` has a dynamic context with services (e.g. `db`) | |
var qb = this.db('vars__HD2012__STABBR'); | |
return qb.select([ | |
db.raw('label'), | |
db.raw('code AS value'), | |
]); | |
} | |
}, | |
// How to fetch & store the data | |
warehouse: { | |
// Map the original column's value dynamically | |
// (terms are agnostic to exactly where they're stored) | |
query: function(qb) { | |
qb.column(qb.knex.raw('HD2012.STABBR as `' + this.get('tableColumn') + '`')); | |
}, | |
// Define the schema needed for the normalized value. | |
// Any changes here automatically became migrations (e.g. `.index()`) | |
schema: function(table) { | |
table.string(this.get('column'), 2).index(); | |
}, | |
// Transform the value (or combine multiple columns, like `${city}, ${state}`) | |
value: function(row) { | |
return row[this.get('tableColumn')]; | |
} | |
} | |
}; |