Last active
April 18, 2020 20:21
-
-
Save joziahg/153d7c08ff0ab8054095e4a204fa23c7 to your computer and use it in GitHub Desktop.
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
/** | |
* This is a simple template for bug reproductions. It contains three models `Person`, `Animal` and `Movie`. | |
* They create a simple IMDB-style database. Try to add minimal modifications to this file to reproduce | |
* your bug. | |
* | |
* install: | |
* npm install objection knex sqlite3 chai | |
* | |
* run: | |
* node reproduction-template | |
*/ | |
let Model | |
try { | |
Model = require('./').Model | |
} catch (err) { | |
Model = require('objection').Model | |
} | |
const Knex = require('knex') | |
const chai = require('chai') | |
async function main () { | |
await createSchema() | |
/// //////////////////////////////////////////////////////////// | |
// Your reproduction | |
/// //////////////////////////////////////////////////////////// | |
await ProjectEvidenceCard.query().upsertGraph({ | |
id: '4c992c32-36b8-11e9-b210-d663bd873d93', | |
cardAction: 'Do a thing', | |
users: [ | |
{ | |
id: '09610ff0-36bf-11e9-b210-d663bd873d93', | |
name: 'A Person' | |
} | |
] | |
}, { relate: true, insertMissing: true }) | |
const person = await User.query() | |
.findOne({ name: 'A Person' }) | |
.eager('cards') | |
chai.expect(person.cards[0].cardAction).to.equal('Do a thing') | |
await knex.destroy() | |
} | |
/// //////////////////////////////////////////////////////////// | |
// Database | |
/// //////////////////////////////////////////////////////////// | |
const knex = Knex({ | |
client: 'sqlite3', | |
useNullAsDefault: true, | |
debug: true, | |
connection: { | |
filename: ':memory:' | |
} | |
}) | |
Model.knex(knex) | |
/// //////////////////////////////////////////////////////////// | |
// Models | |
/// //////////////////////////////////////////////////////////// | |
class ProjectEvidenceCard extends Model { | |
static get tableName () { | |
return 'projectEvidenceCards' | |
} | |
static get relationMappings () { | |
return { | |
users: { | |
relation: Model.ManyToManyRelation, | |
modelClass: User, | |
join: { | |
from: 'projectEvidenceCards.id', | |
through: { | |
from: 'projectEvidenceCardsUsersMap.cardId', | |
to: 'projectEvidenceCardsUsersMap.userId' | |
}, | |
to: 'users.id' | |
} | |
} | |
} | |
} | |
} | |
class User extends Model { | |
static get tableName () { | |
return 'users' | |
} | |
static get relationMappings () { | |
return { | |
cards: { | |
relation: Model.ManyToManyRelation, | |
modelClass: ProjectEvidenceCard, | |
join: { | |
from: 'users.id', | |
through: { | |
from: 'projectEvidenceCardsUsersMap.userId', | |
to: 'projectEvidenceCardsUsersMap.cardId' | |
}, | |
to: 'projectEvidenceCards.id' | |
} | |
} | |
} | |
} | |
} | |
/// //////////////////////////////////////////////////////////// | |
// Schema | |
/// //////////////////////////////////////////////////////////// | |
async function createSchema () { | |
await knex.schema | |
.dropTableIfExists('users') | |
.dropTableIfExists('projectEvidenceCards') | |
.dropTableIfExists('projectEvidenceCardsUsersMap') | |
await knex.schema | |
.createTable('users', table => { | |
table.uuid('id').notNullable().primary() | |
table.string('name').notNullable() | |
}) | |
.createTable('projectEvidenceCards', table => { | |
table.uuid('id').notNullable().primary() | |
table.string('cardAction') | |
}) | |
.createTable('projectEvidenceCardsUsersMap', table => { | |
table.uuid('id').notNullable().primary() | |
table.uuid('cardId').notNullable() | |
table.foreign('cardId').references('id').inTable('projectEvidenceCards') | |
table.uuid('userId').notNullable() | |
table.foreign('userId').references('id').inTable('users') | |
}) | |
} | |
main() | |
.then(() => console.log('success')) | |
.catch(console.error) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment