Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(src/serializers/datastore.ts): load cached entities with correct Datastore Key type #265

Merged
merged 1 commit into from
Apr 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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