Last active
October 29, 2018 10:38
-
-
Save fl0w/7c4d4675808b5f606e935472bae831aa to your computer and use it in GitHub Desktop.
Test modifiers
This file contains hidden or 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
'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