Permalink
Browse files

feat: findById

  • Loading branch information...
XadillaX committed Oct 8, 2016
1 parent fae2005 commit c04f23d716cc001f6ff0ce8c70a9f972b40cea3e
Showing with 123 additions and 0 deletions.
  1. +34 −0 lib/query.js
  2. +89 −0 test/query.js
@@ -193,6 +193,40 @@ class ToshihikoQuery {
});
}

findById(_id, callback, toJSON) {
const self = this;

let id = _id;
if(this.model.primaryKeys.length === 1 && typeof _id !== "object") {
id = {};
id[this.model.primaryKeys[0].name] = _id;
}

if(typeof id !== "object") {
return process.nextTick(function() {
return callback(new Error("you should pass a valid IDs object"));
});
}

if(this.cache) {
return this.cache.getData(this.toshihiko.database, this.model.name, id, function(err, data) {
// if err, fallback to no cache
if(err) data = [];

if(data.length !== 0) {
const yukari = new Yukari(self.model, "query");
yukari.fillRowFromSource(data[0], true);
return callback(undefined, toJSON ? yukari.toJSON() : yukari);
}

// fallback with no cache
return self.where(id).findOne(callback, toJSON);
});
}

return this.where(id).findOne(callback, toJSON);
}

findOne(callback, toJSON) {
this.find(callback, toJSON, { single: true });
}
@@ -334,6 +334,95 @@ describe("🐣 query", function() {
});
});

describe("👙 findById", function() {
it("should get with cache", function(done) {
const find = toshihiko.adapter.find;
toshihiko.adapter.find = function() {};

const model = toshihiko.define("model", [
{ name: "key1" },
{ name: "key2", primaryKey: true },
{ name: "key3", primaryKey: true }
], {
cache: {
getData: function(database, table, id, callback) {
database.should.equal("");
table.should.equal("model");
id.should.deepEqual({ key2: "1", key3: "2" });
callback(undefined, [ { key1: "3", key2: "1", key3: "2" } ]);
},
setData: function() {},
deleteData: function() {},
deleteKeys: function() {}
}
});
(new Query(model)).findById({ key2: "1", key3: "2" }, function(err, yukari) {
should.ifError(err);
yukari.should.be.instanceof(Yukari);
yukari.should.match({ key1: "3", key2: "1", key3: "2" });
toshihiko.adapter.find = find;
done();
});
});

it("should get without cache because of fallback", function(done) {
const find = toshihiko.adapter.find;
toshihiko.adapter.find = function(query, callback, options) {
options.single.should.equal(true);
options.noCache.should.equal(false);
query._fields.should.deepEqual([ "key1", "key2", "key3" ]);
query._where.should.deepEqual({ key2: "1", key3: "2" });
return callback(undefined, { key1: "3", key2: "1", key3: "2" });
};

const model = toshihiko.define("model", [
{ name: "key1" },
{ name: "key2", primaryKey: true },
{ name: "key3", primaryKey: true }
], {
cache: {
getData: function(database, table, id, callback) {
callback(new Error("err"));
},
setData: function() {},
deleteData: function() {},
deleteKeys: function() {}
}
});
(new Query(model)).findById({ key2: "1", key3: "2" }, function(err, yukari) {
should.ifError(err);
yukari.should.be.instanceof(Yukari);
yukari.should.match({ key1: "3", key2: "1", key3: "2" });
toshihiko.adapter.find = find;
done();
});
});

it("should get without cache", function(done) {
const find = toshihiko.adapter.find;
toshihiko.adapter.find = function(query, callback, options) {
options.single.should.equal(true);
options.noCache.should.equal(false);
query._fields.should.deepEqual([ "key1", "key2", "key3" ]);
query._where.should.deepEqual({ key2: "1", key3: "2" });
return callback(undefined, { key1: "3", key2: "1", key3: "2" });
};

const model = toshihiko.define("model", [
{ name: "key1" },
{ name: "key2", primaryKey: true },
{ name: "key3", primaryKey: true }
]);
(new Query(model)).findById({ key2: "1", key3: "2" }, function(err, yukari) {
should.ifError(err);
yukari.should.be.instanceof(Yukari);
yukari.should.match({ key1: "3", key2: "1", key3: "2" });
toshihiko.adapter.find = find;
done();
});
});
});

describe("👙 update", function() {
const query = new Query(model);

0 comments on commit c04f23d

Please sign in to comment.