Skip to content

Instantly share code, notes, and snippets.

@fl0w
Last active October 29, 2018 10:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fl0w/7c4d4675808b5f606e935472bae831aa to your computer and use it in GitHub Desktop.
Save fl0w/7c4d4675808b5f606e935472bae831aa to your computer and use it in GitHub Desktop.
Test modifiers
'use strict'
const Knex = require('knex')
const objection = require('objection')
const knex = new Knex({
client: 'pg',
connection: { database: 'fl0w', user: 'fl0w' }
})
objection.Model.knex(knex)
const migrate = async () => {
await knex.schema.createTable('ModelB', t => {
t.increments('id')
t.timestamp('confirmedAt')
})
await knex.schema.createTable('ModelA', t => {
t.increments('id')
t.integer('bId').references('id').inTable('ModelB')
})
}
const teardown = async () => {
await knex.schema.dropTable('ModelA')
await knex.schema.dropTable('ModelB')
await knex.destroy()
}
class ModelA extends objection.Model {
static get tableName () {
return 'ModelA'
}
static get relationMappings () {
return {
b: {
relation: objection.Model.BelongsToOneRelation,
modelClass: ModelB,
join: {
from: 'ModelA.bId',
to: 'ModelB.id'
}
}
}
}
}
class ModelB extends objection.Model {
static get modifiers () {
return {
confirmed: builder => builder.whereNotNull('confirmedAt')
}
}
static get tableName () {
return 'ModelB'
}
}
async function main () {
await migrate()
await ModelA.fromJson({ id: 1, bId: 1 }).$loadRelated('b(confirmed)').debug()
await teardown()
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment