Created
June 12, 2016 04:30
-
-
Save ericclemmons/3fa247c544f024088861b29dc239aef7 to your computer and use it in GitHub Desktop.
Term for: schools.address.state (Stores the state abbreviation for a school's location)
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
/** | |
* 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')]; | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment