Skip to content

Instantly share code, notes, and snippets.

@Morgul
Created July 21, 2014 23:27
Show Gist options
  • Save Morgul/6b9f743ef036cc6eb4dc to your computer and use it in GitHub Desktop.
Save Morgul/6b9f743ef036cc6eb4dc to your computer and use it in GitHub Desktop.
Thinky OrderBy issue
// ---------------------------------------------------------------------------------------------------------------------
// Testing OrderBy on a joined model
//
// @module orderby_test.js
// ---------------------------------------------------------------------------------------------------------------------
var util = require('util');
var thinky = require('thinky')({ db: 'orderby_test' });
var r = thinky.r;
// ---------------------------------------------------------------------------------------------------------------------
var Commit = thinky.createModel('Commit', {
message: String,
committed: { _type: Date, default: r.now() }
});
var Revision = thinky.createModel('Revision', {
title: String,
body: String,
tags: [String],
commit_id: String
});
Revision.belongsTo(Commit, "commit", "commit_id", "id");
// ---------------------------------------------------------------------------------------------------------------------
// Clear database
Commit.delete().run()
.then(function()
{
return Revision.delete().run();
})
.then(function()
{
// Populate some commits
var commit1 = new Commit({ message: "commit 1", committed: new Date(2013, 0, 1, 10, 12) });
var commit2 = new Commit({ message: "commit 2", committed: new Date(2014, 0, 1, 10, 12) });
var commit3 = new Commit({ message: "commit 3", committed: new Date(2014, 1, 8, 11, 32) });
var commit4 = new Commit({ message: "commit 4", committed: new Date(2014, 1, 8, 11, 14) });
var commit5 = new Commit({ message: "commit 5", committed: new Date(2014, 2, 2, 12, 24) });
return commit1.save()
.then(function()
{
return commit2.save();
})
.then(function()
{
return commit3.save();
})
.then(function()
{
return commit4.save();
})
.then(function()
{
return commit5.save();
})
.then(function()
{
// Populate some revisions
var rev1 = new Revision({ title: "Test Page", body: "Test body 1.", tags: ['test'], commit_id: commit1.id });
var rev2 = new Revision({ title: "Test Page", body: "Test body 2.", tags: ['test'], commit_id: commit2.id });
var rev3 = new Revision({ title: "Test Page", body: "Test body 3.", tags: ['test'], commit_id: commit3.id });
var rev4 = new Revision({ title: "Test Page", body: "Test body 4.", tags: ['test'], commit_id: commit4.id });
var rev5 = new Revision({ title: "Test Page", body: "Test body 5.", tags: ['test'], commit_id: commit5.id });
return rev1.save()
.then(function()
{
return rev2.save();
})
.then(function()
{
return rev3.save();
})
.then(function()
{
return rev4.save();
})
.then(function()
{
return rev5.save();
});
});
})
// Illustrate the issue
.then(function()
{
console.log('Should return in reverse order (ie: rev5, rev4, rev3, rev2, rev1):\n');
return Revision.orderBy(r.row('commit')('committed')).run().map(function(revision)
{
console.log(util.inspect(revision, { colors: true }));
return revision;
});
})
// Exit
.then(function()
{
process.exit();
});
// ---------------------------------------------------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment