Skip to content

Instantly share code, notes, and snippets.

@doublemarked
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doublemarked/3da120228c2a51a8afe4 to your computer and use it in GitHub Desktop.
Save doublemarked/3da120228c2a51a8afe4 to your computer and use it in GitHub Desktop.
Demonstration of problems with hasManyThrough relations and the relation.findOne helper
// mocha hasManyThroughProblems.js
var app = require('loopback/test/fixtures/simple-integration-app/server/server.js');
var expect = require('chai').expect;
describe('relation check', function() {
before(function defineProductAndCategoryModels() {
var product = app.model(
'product',
{ properties: { id: 'string', name: 'string' }, dataSource: 'db' }
);
var category = app.model(
'category',
{ properties: { id: 'string', name: 'string' }, dataSource: 'db' }
);
var connector = app.model(
'connector',
{ properties: { id: 'string' }, dataSource: 'db' }
);
product.hasAndBelongsToMany(category, { through: connector });
category.hasMany(product, { through: connector });
});
var testCategory;
before(function initializeCategory(done) {
app.models.category.create({ name: 'my-category' }, function (err, instance) {
if (err) return done(err);
testCategory = instance;
done();
});
});
before(function initializeProduct(done) {
testCategory.products.create({ name: 'a-product' }, done);
});
it('should be possible to findOne on the relation and get back a product', function (done) {
testCategory.products.findOne(function (err, p) {
if (err) return done(err);
expect(p).to.be.ok();
expect(p).to.be.an.instanceof(app.models.product);
done();
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment