Skip to content

Commit

Permalink
Fix(cache): Get multiple keys (#198)
Browse files Browse the repository at this point in the history
fix #187
  • Loading branch information
sebelga committed Oct 24, 2019
1 parent 43b1b1e commit 0ec188d
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 37 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"lodash.get": "^4.4.2",
"lodash.set": "^4.3.2",
"moment": "^2.24.0",
"nsql-cache": "^1.1.4",
"nsql-cache": "^1.1.5",
"nsql-cache-datastore": "^1.1.6",
"optional": "^0.1.4",
"promised-hooks": "^3.1.0",
Expand Down
8 changes: 7 additions & 1 deletion src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,13 @@ export const generateModel = <T extends object, M extends object>(
}
return dataloader.loadMany(keysArray).then((result: EntityData) => arrify(result));
}
return this.gstore.ds.get(keysArray).then(([result]: [any]) => arrify(result));

return this.gstore.ds.get(keys).then(([result]: [any]) => {
if (Array.isArray(keys)) {
return arrify(result);
}
return result;
});
};

if (this.__hasCache(options)) {
Expand Down
61 changes: 37 additions & 24 deletions test/integration/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@ const { expect } = chai;

const allKeys = [];

const gstore = new Gstore({
cache: {
stores: [{ store: redisStore }],
config: {
ttl: {
keys: 600,
queries: 600,
},
},
},
});

gstore.connect(ds);

const uniqueId = () => chance.string({ pool: 'abcdefghijklmnopqrstuvwxyz0123456789' });

const cleanUp = cb => {
ds.delete(allKeys).then(cb);
};
Expand All @@ -24,33 +40,11 @@ const addKey = key => {
};

describe('Integration Tests (Cache)', () => {
let gstore;
let schema;
let Schema;
const { Schema } = gstore;
let Model;

beforeEach(() => {
if (!gstore) {
gstore = new Gstore({
cache: {
stores: [
{
store: redisStore,
},
],
config: {
ttl: {
keys: 600,
queries: 600,
},
},
},
});
gstore.connect(ds);
}

({ Schema } = gstore);

gstore.models = {};
gstore.modelSchemas = {};

Expand All @@ -70,7 +64,7 @@ describe('Integration Tests (Cache)', () => {
});

it('should set KEY symbol on query result', () => {
const id = chance.string({ pool: 'abcdefghijklmnopqrstuvwxyz0123456789' });
const id = uniqueId();
const user = new Model({ email: 'test@test.com' }, id);
return user.save().then(entity => {
addKey(entity.entityKey);
Expand All @@ -79,4 +73,23 @@ describe('Integration Tests (Cache)', () => {
});
});
});

it('should get one or multiple entities fron the cache', async () => {
const id1 = uniqueId();
const id2 = uniqueId();

const user1 = new Model({ email: 'test1@test.com' }, id1);
const user2 = new Model({ email: 'test2@test.com' }, id2);

const result = await Promise.all([user1.save(), user2.save()]);

result.forEach(entity => addKey(entity.entityKey));

const responseSingle = await Model.get(result[0].entityKey.name);
const responseMultiple = await Model.get([result[0].entityKey.name, result[1].entityKey.name]);

expect(responseSingle.email).to.equal('test1@test.com');
expect(responseMultiple[0].email).to.equal('test1@test.com');
expect(responseMultiple[1].email).to.equal('test2@test.com');
});
});
14 changes: 7 additions & 7 deletions test/model-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ describe('Model', () => {
return GstoreModel.get(123).then(onEntity);

function onEntity(_entity) {
expect(ds.get.getCall(0).args[0][0].constructor.name).equal('Key');
expect(ds.get.getCall(0).args[0].constructor.name).equal('Key');
expect(_entity instanceof Entity.default).equal(true);
}
});
Expand Down Expand Up @@ -345,24 +345,24 @@ describe('Model', () => {

it('not converting string with mix of number and non number', () =>
GstoreModel.get('123:456').then(() => {
expect(ds.get.getCall(0).args[0][0].name).equal('123:456');
expect(ds.get.getCall(0).args[0].name).equal('123:456');
}));

it('passing an ancestor path array', () => {
const ancestors = ['Parent', 'keyname'];

return GstoreModel.get(123, ancestors).then(() => {
expect(ds.get.getCall(0).args[0][0].constructor.name).equal('Key');
expect(ds.get.getCall(0).args[0][0].parent.kind).equal(ancestors[0]);
expect(ds.get.getCall(0).args[0][0].parent.name).equal(ancestors[1]);
expect(ds.get.getCall(0).args[0].constructor.name).equal('Key');
expect(ds.get.getCall(0).args[0].parent.kind).equal(ancestors[0]);
expect(ds.get.getCall(0).args[0].parent.name).equal(ancestors[1]);
});
});

it('should allow a namespace', () => {
const namespace = 'com.mydomain-dev';

return GstoreModel.get(123, null, namespace).then(() => {
expect(ds.get.getCall(0).args[0][0].namespace).equal(namespace);
expect(ds.get.getCall(0).args[0].namespace).equal(namespace);
});
});

Expand Down Expand Up @@ -578,7 +578,7 @@ describe('Model', () => {
GstoreModel.get(123).then(response => {
assert.ok(ds.get.called);
const { args } = ds.get.getCall(0);
expect(args[0][0].id).equal(123);
expect(args[0].id).equal(123);
expect(response.entityData).include(entity);
}));

Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5006,10 +5006,10 @@ nsql-cache-datastore@^1.1.6:
dependencies:
arrify "1.0.1"

nsql-cache@^1.1.4:
version "1.1.4"
resolved "https://registry.yarnpkg.com/nsql-cache/-/nsql-cache-1.1.4.tgz#8f408ce478b2f9338659ba27143435c1371405b9"
integrity sha512-E8LxBzf/Q6URAlkZZ6HIVTUGeiWYApf315pMfbPAE9FWHBB9rUzksm5XvrGDEYz/swUsEn4xvkayCgVvKpeKwg==
nsql-cache@^1.1.5:
version "1.1.5"
resolved "https://registry.yarnpkg.com/nsql-cache/-/nsql-cache-1.1.5.tgz#10619d77fd711531a2e25e9992166797e40d39ec"
integrity sha512-QRJe7Z3fqUJc57NVTHsxRFXlCEIoLlb0FQMW5NOfWGMDq8Qcz4ampT2wnnvufT+T63uKgqvX4qwTAWMcQ3iuQw==
dependencies:
arrify "^1.0.1"
cache-manager "^2.9.0"
Expand Down

0 comments on commit 0ec188d

Please sign in to comment.