Closed robinduckett closed 7 years ago
Your tracker isn't returning any responses?
tracker.on('query', function(q, step) {
console.log(q);
});
You can inspect the query object and use step parameter to return the data it requires. If your not sure you can knex's events using a live unmocked db to get the information you need.
http://knexjs.org/#Interfaces-Events
Likely either query and query-response or you may be able to get all the information from query-response.
Cheers,
Jason
Hi Jason,
I have a more succinct example:
let driver = require('knex');
let mockDb = require('mock-knex');
let knex = driver({
dialect: 'sqlite3',
connection: {
filename: ':memory:'
},
createTable: true,
useNullAsDefault: true
});
Promise.all([
knex.schema.createTable('users', function (table) {
table.increments('id');
table.string('username');
}),
knex.schema.createTable('messages', function (table) {
table.increments('id').primary();
table.integer('user_id').references('users');
table.string('body');
}),
knex.schema.createTable('messages_tags', function (table) {
table.increments('id').primary();
table.integer('message_id').references('messages');
table.integer('tag_id').references('tags');
}),
knex.schema.createTable('tags', function (table) {
table.increments('id').primary();
table.string('name');
}),
knex.insert({
username: 'robin'
}).into('users'),
knex.insert({
user_id: 1,
body: 'Message Test'
}).into('messages'),
knex.insert({
name: 'uncategorized'
}).into('tags'),
knex.insert({
tag_id: 1,
message_id: 1
}).into('messages_tags')
]).then(function() {
console.log('mocking');
mockDb.mock(knex, 'knex@0.11');
let tracker = mockDb.getTracker();
tracker.on('query', function(q) {
console.log(q);
});
var bookshelf = require('bookshelf')(knex);
var User = bookshelf.Model.extend({
tableName: 'users',
posts: function() {
return this.hasMany(Posts);
}
});
var Posts = bookshelf.Model.extend({
tableName: 'messages',
tags: function() {
return this.belongsToMany(Tag);
}
});
var Tag = bookshelf.Model.extend({
tableName: 'tags'
});
User.where('id', 1).fetch({withRelated: ['posts.tags']}).then(function(user) {
console.log(JSON.stringify(user.related('posts').toJSON(), null, 2));
}).catch(function(err) {
console.error(err);
});
});
When running this code without the mock-knex code, it runs fine, and returns the correct data.
When running this code with the mock-knex code, it fails to function, and returns an error.
TypeError: Cannot read property 'related' of null
at .<anonymous> (/home/robin/devtesting/test.js:86:36)
at tryCatcher (/home/robin/devtesting/node_modules/bookshelf/node_modules/bluebird/js/main/util.js:26:23)
at Promise._settlePromiseFromHandler (/home/robin/devtesting/node_modules/bookshelf/node_modules/bluebird/js/main/promise.js:510:31)
at Promise._settlePromiseAt (/home/robin/devtesting/node_modules/bookshelf/node_modules/bluebird/js/main/promise.js:584:18)
at Async._drainQueue (/home/robin/devtesting/node_modules/bookshelf/node_modules/bluebird/js/main/async.js:128:12)
at Async._drainQueues (/home/robin/devtesting/node_modules/bookshelf/node_modules/bluebird/js/main/async.js:133:10)
at Immediate.Async.drainQueues [as _onImmediate] (/home/robin/devtesting/node_modules/bookshelf/node_modules/bluebird/js/main/async.js:15:14)
at tryOnImmediate (timers.js:543:15)
at processImmediate [as _immediateCallback] (timers.js:523:5)
I have checked this code with the latest develop branch of mock-knex and it seems to work, I will continue testing.
@robinduckett for your example above you will change your tracker to look like
tracker.on('query', (query, step) => {
let result;
switch (step) {
case 1:
result = [{
id : 1,
}];
break;
case 2:
result = [
{
id : 1,
user_id : 1,
message : 'this is a message',
},
{
id : 2,
user_id : 1,
message : 'this is another message',
},
{
id : 3,
user_id : 1,
message : 'this is a third message',
},
]
break;
case 3:
result = [];
break;
}
query.response(result);
});
The reason you were getting Cannot read property 'related' of null
is because you are not returning a response so it becomes null
instead of a model.
Hi Guys,
I have a more detailed example of the problem I'm getting with related models with Bookshelf
I'm really sorry if I'm just misunderstanding how I should be using mock-knex, but I'm wondering if the relationships are being mapped properly.