Skip to content

Commit

Permalink
fix(src/serializers/datastore.ts): load cached entities with correct …
Browse files Browse the repository at this point in the history
…Datastore Key type (#265)

#243
  • Loading branch information
carnun authored Apr 6, 2022
1 parent d35f66a commit b06641b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,6 @@ notes.txt

!logo/logo.png
!logo/logo_small.png

# Redis
dump.rdb
26 changes: 26 additions & 0 deletions __tests__/integration/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ describe('Integration Tests (Cache)', () => {
return user.save().then((result) => {
addKey(result.entityKey);
return MyModel.get(result.entityKey.name!).then((e) => {
expect(ds.isKey(e.entityKey)).equal(true);
expect(e.email).equal('[email protected]');
});
});
Expand All @@ -99,6 +100,30 @@ describe('Integration Tests (Cache)', () => {
expect(responseMultiple[1].email).to.equal('[email protected]');
});

test('should load already cached entities with correct datastore entity keys', async () => {
const id1 = uniqueId();
const id2 = uniqueId();

const user1 = new MyModel({ email: '[email protected]' }, id1);
const user2 = new MyModel({ email: '[email protected]' }, id2);

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

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

const responseMultiple0 = await MyModel.list({ format: 'ENTITY', order: { property: 'email', descending: false } });

responseMultiple0.entities.forEach((entry) => {
expect(ds.isKey(entry?.entityKey)).to.equal(true);
});

const responseMultiple1 = await MyModel.list({ format: 'ENTITY', order: { property: 'email', descending: false } });

responseMultiple1.entities.forEach((entry) => {
expect(ds.isKey(entry?.entityKey)).to.equal(true);
});
});

test('should find one entity from the cache', async () => {
const id = uniqueId();

Expand All @@ -109,6 +134,7 @@ describe('Integration Tests (Cache)', () => {
addKey(result.entityKey);

const response = await MyModel.findOne({ email: '[email protected]' });
expect(ds.isKey(response?.entityKey)).equal(true);

expect(response!.email).to.eq('[email protected]');
expect(response!.entityKey.name).to.eq(id);
Expand Down
9 changes: 7 additions & 2 deletions src/serializers/datastore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,17 @@ const fromDatastore = <F extends 'JSON' | 'ENTITY' = 'JSON', R = F extends 'ENTI
Model: GstoreModel<any>,
options: { format?: F; readAll?: boolean; showKey?: boolean } = {},
): R => {
const getEntityKey = (): EntityKey => {
const keyData = entityData[Model.gstore.ds.KEY as any];
return Model.gstore.ds.isKey(keyData) ? (keyData as EntityKey) : Model.gstore.ds.key({ ...keyData });
};

const convertToJson = (): GenericObject => {
options.readAll = typeof options.readAll === 'undefined' ? false : options.readAll;

const { schema, gstore } = Model;
const { KEY } = gstore.ds;
const entityKey = entityData[KEY as any];
const entityKey = getEntityKey();
const data: { [key: string]: any } = {
id: idFromKey(entityKey),
};
Expand Down Expand Up @@ -117,7 +122,7 @@ const fromDatastore = <F extends 'JSON' | 'ENTITY' = 'JSON', R = F extends 'ENTI
};

const convertToEntity = (): GstoreEntity => {
const key: EntityKey = entityData[Model.gstore.ds.KEY as any];
const key = getEntityKey();
return new Model(entityData, undefined, undefined, undefined, key);
};

Expand Down

0 comments on commit b06641b

Please sign in to comment.