Created
June 9, 2016 18:37
-
-
Save gabmontes/3f7723a36fc3bec622252fd470da15fe to your computer and use it in GitHub Desktop.
Test file to reproduce an issue with polymorphic relationships and soft deletes using bookshelf and bookshelf-paranoia
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
const config = { | |
client: 'pg', | |
connection: { | |
host : 'localhost', | |
port : 5432, | |
user : 'user', | |
password : 'pass', | |
database : 'test' | |
} | |
} | |
const waterfall = require('promise-waterfall') | |
const knex = require('knex')(config) | |
const bookshelf = require('bookshelf')(knex) | |
bookshelf.plugin(require('bookshelf-paranoia')) | |
function createTable(name, columnsBuilder) { | |
return bookshelf.knex.schema.createTable(name, columnsBuilder) | |
} | |
function dropTable(name) { | |
return bookshelf.knex.schema.dropTable(name) | |
} | |
waterfall([ | |
function () { | |
return Promise.all([ | |
dropTable('boys'), | |
dropTable('girls'), | |
dropTable('parents') | |
]).catch(function () {}) | |
}, | |
function () { | |
return Promise.all([ | |
createTable('boys', function (table) { | |
table.increments('id').primary(), | |
table.timestamp('deleted_at') | |
}), | |
createTable('girls', function (table) { | |
table.increments('id').primary() | |
table.timestamp('deleted_at') | |
}), | |
createTable('parents', function (table) { | |
table.increments('id').primary() | |
table.string('child_id') | |
table.enum('child_type', ['boys', 'girls']), | |
table.timestamp('deleted_at') | |
}) | |
]) | |
}, | |
function () { | |
// models | |
const Boy = bookshelf.Model.extend({ | |
tableName: 'boys', | |
softDelete: true | |
}) | |
const Girl = bookshelf.Model.extend({ | |
tableName: 'girls', | |
softDelete: true | |
}) | |
const Parent = bookshelf.Model.extend({ | |
tableName: 'parents', | |
child () { | |
return this.morphTo('child', Boy, Girl) | |
}, | |
softDelete: true | |
}) | |
// helpers | |
function createBoy() { | |
return Boy.forge().save().then(function (child) { | |
return child.id | |
}) | |
} | |
function createGirl() { | |
return Girl.forge().save().then(function (child) { | |
return child.id | |
}) | |
} | |
function createParent() { | |
return Parent.forge().save().then(function (parent) { | |
return parent.id | |
}) | |
} | |
function setChild(parentId, childId, type) { | |
return Parent.forge({ | |
id: parentId | |
}).save({ | |
child_id: childId, | |
child_type: type | |
}) | |
} | |
// set up test scenario | |
const tasks = waterfall([ | |
function () { | |
return createBoy() | |
}, | |
function (childId) { | |
return createParent().then(function (parentId) { | |
return setChild(parentId, childId, 'boys') | |
}) | |
}, | |
function () { | |
return createGirl() | |
}, | |
function (parentBoyId) { | |
return createParent().then(function (parentId) { | |
return setChild(parentId, parentBoyId, 'girls') | |
}) | |
} | |
]) | |
// run query | |
return tasks.then(function () { | |
return Parent.fetchAll({ | |
withRelated: ['child'], | |
debug: true | |
}).then(function (parents) { | |
console.log('Parents:', parents.serialize()) | |
}) | |
}) | |
} | |
]).then(function () { | |
console.log('Done') | |
process.exit(0) | |
}).catch(function (err) { | |
console.log('Err: %s', err) | |
process.exit(1) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment