From 792d3cfd423855c97a688b889fe0be5fb14218b2 Mon Sep 17 00:00:00 2001 From: Hendrik Schalekamp Date: Wed, 6 Apr 2022 20:58:29 +0200 Subject: [PATCH] fix(src/serializers/datastore.ts): load cached entities with correct Datastore Key type #243 --- .gitignore | 3 +++ __tests__/integration/cache.ts | 26 ++++++++++++++++++++++++++ src/serializers/datastore.ts | 9 +++++++-- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 67973d7a..671596d9 100644 --- a/.gitignore +++ b/.gitignore @@ -49,3 +49,6 @@ notes.txt !logo/logo.png !logo/logo_small.png + +# Redis +dump.rdb diff --git a/__tests__/integration/cache.ts b/__tests__/integration/cache.ts index 92cc5344..15b3fbe0 100644 --- a/__tests__/integration/cache.ts +++ b/__tests__/integration/cache.ts @@ -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('test@test.com'); }); }); @@ -99,6 +100,30 @@ describe('Integration Tests (Cache)', () => { expect(responseMultiple[1].email).to.equal('test2@test.com'); }); + test('should load already cached entities with correct datastore entity keys', async () => { + const id1 = uniqueId(); + const id2 = uniqueId(); + + const user1 = new MyModel({ email: 'test3@test.com' }, id1); + const user2 = new MyModel({ email: 'test4@test.com' }, 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(); @@ -109,6 +134,7 @@ describe('Integration Tests (Cache)', () => { addKey(result.entityKey); const response = await MyModel.findOne({ email: 'test2@test.com' }); + expect(ds.isKey(response?.entityKey)).equal(true); expect(response!.email).to.eq('test2@test.com'); expect(response!.entityKey.name).to.eq(id); diff --git a/src/serializers/datastore.ts b/src/serializers/datastore.ts index a7a763d3..0d074e05 100644 --- a/src/serializers/datastore.ts +++ b/src/serializers/datastore.ts @@ -59,12 +59,17 @@ const fromDatastore = , 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), }; @@ -117,7 +122,7 @@ const fromDatastore = { - const key: EntityKey = entityData[Model.gstore.ds.KEY as any]; + const key = getEntityKey(); return new Model(entityData, undefined, undefined, undefined, key); };