Skip to content

Instantly share code, notes, and snippets.

@fl0w

fl0w/modifiers.js

Last active Oct 29, 2018
Embed
What would you like to do?
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
You can’t perform that action at this time.