Skip to content

Instantly share code, notes, and snippets.

@jakobdamjensen
Last active August 29, 2015 14:07
Show Gist options
  • Save jakobdamjensen/6b483b3bb06c06fcc3e2 to your computer and use it in GitHub Desktop.
Save jakobdamjensen/6b483b3bb06c06fcc3e2 to your computer and use it in GitHub Desktop.
var knex = require('knex')({
client: 'pg',
debug: true,
connection: {
host: 'localhost',
//user: 'your_database_user',
//password: 'your_database_password',
database: 'ecare_dump_db',
charset: 'utf8'
}
}),
bookshelf = require('bookshelf')(knex),
Region = bookshelf.Model.extend({
tableName: 'regions',
published_documents: function () {
return this.belongsToMany(PublishedDocument).through(PublishedDocumentRegion, 'region_id', 'published_document_id');
}
}),
PublishedDocumentRegion = bookshelf.Model.extend({
tableName: 'published_document_regions'
}),
PublishedDocument = bookshelf.Model.extend({
tableName: 'published_documents',
regions: function () {
return this.belongsToMany(Region).through(PublishedDocumentRegion, 'published_document_id', 'region_id');
}
});
new PublishedDocument().regions().query({where: {'regions.id': 1}}).fetch({withRelated: 'regions'}).then(function (collection) {
console.log("length: ",collection.length);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment