Skip to content

Instantly share code, notes, and snippets.

@PatrickLef
Created April 6, 2020 21:09
Show Gist options
  • Save PatrickLef/ce162c2533211ac2102ca7748f606d56 to your computer and use it in GitHub Desktop.
Save PatrickLef/ce162c2533211ac2102ca7748f606d56 to your computer and use it in GitHub Desktop.
/**
* 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
Model = require('objection').Model
const Knex = require('knex')
const chai = require('chai')
async function main() {
await createSchema()
///////////////////////////////////////////////////////////////
// Your reproduction
///////////////////////////////////////////////////////////////
const person = await Person.query()
.insert({
firstName: 'Jennifer',
lastName: '', // this causes a validation error
})
.onError((e) => {
// This runs and outputs error
console.dir(e)
return 'error'
})
console.dir(person)
chai.expect(person).to.equal('error')
}
///////////////////////////////////////////////////////////////
// Database
///////////////////////////////////////////////////////////////
const knex = Knex({
client: 'sqlite3',
useNullAsDefault: true,
debug: false,
connection: {
filename: ':memory:',
},
})
Model.knex(knex)
///////////////////////////////////////////////////////////////
// Models
///////////////////////////////////////////////////////////////
class Person extends Model {
static get tableName() {
return 'Person'
}
static get jsonSchema() {
return {
type: 'object',
required: ['firstName', 'lastName'],
properties: {
id: { type: 'integer' },
parentId: { type: ['integer', 'null'] },
firstName: { type: 'string', minLength: 1, maxLength: 255 },
lastName: { type: 'string', minLength: 1, maxLength: 255 },
},
}
}
}
///////////////////////////////////////////////////////////////
// Schema
///////////////////////////////////////////////////////////////
async function createSchema() {
await knex.schema.dropTableIfExists('Person')
await knex.schema.createTable('Person', (table) => {
table.increments('id').primary()
table.string('firstName')
table.string('lastName')
})
}
main()
.then(() => {
console.log('success')
return knex.destroy()
})
.catch((err) => {
console.error(err)
return knex.destroy()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment