Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@simon-weber
Created November 22, 2016 22:39
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 simon-weber/e9ac8147b5daaf1ca08fdae0ec55cad2 to your computer and use it in GitHub Desktop.
Save simon-weber/e9ac8147b5daaf1ca08fdae0ec55cad2 to your computer and use it in GitHub Desktop.
lovefield query/index/order bug example
document.write = function (s) {
// jsfiddle doesn't allow document.write.
document.body.insertAdjacentHTML("beforeend", s);
}
const schemaBuilder = lf.schema.create('schema', 1);
schemaBuilder.createTable('Item').
addColumn('id', lf.Type.INTEGER).
addColumn('num', lf.Type.STRING).
addPrimaryKey(['id']).
// Removing this index fixes the ordering.
addIndex('idxDesc', ['num'], false, lf.Order.ASC);
let db;
let item;
schemaBuilder.connect({storeType: lf.schema.DataStoreType.MEMORY}).then(function(_db) {
db = _db;
item = db.getSchema().table('Item');
const rows = [1, 2, 3].map(i => item.createRow({id: i, num: parseInt(i, 10)}));
return db.insertOrReplace().into(item).values(rows).exec();
}).then(function() {
// Show this working as expected without the or+match query.
return db.select().from(item).orderBy(item.num, lf.Order.ASC).exec();
}).then(function(results) {
document.write(`ordered as expected: ${JSON.stringify(results.map(i => i.num))}`);
}).then(function() {
// Show it breaking.
return db.select().from(item).where(
lf.op.or(
// When the index exists, these arguments determine the order of the results.
item.num.match('3'),
item.num.match('1'),
item.num.match('2')
))
.orderBy(item.num, lf.Order.ASC)
.exec();
}).then(function(results) {
document.write(`<br>ordered by args instead: ${JSON.stringify(results.map(i => i.num))}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment